{"id":18024112,"url":"https://github.com/thomasnield/kotlin_math_cheatsheet","last_synced_at":"2025-04-04T18:27:54.930Z","repository":{"id":148076044,"uuid":"149684917","full_name":"thomasnield/kotlin_math_cheatsheet","owner":"thomasnield","description":"A cheatsheet for translation math expressions to Kotlin","archived":false,"fork":false,"pushed_at":"2019-05-06T15:12:32.000Z","size":84,"stargazers_count":84,"open_issues_count":0,"forks_count":10,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-10T03:43:08.087Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomasnield.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-20T23:59:40.000Z","updated_at":"2025-01-16T22:59:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"eb2d9066-f6d6-4204-9d71-fe576886bd8d","html_url":"https://github.com/thomasnield/kotlin_math_cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasnield%2Fkotlin_math_cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasnield%2Fkotlin_math_cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasnield%2Fkotlin_math_cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasnield%2Fkotlin_math_cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomasnield","download_url":"https://codeload.github.com/thomasnield/kotlin_math_cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247228321,"owners_count":20904846,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T07:11:56.301Z","updated_at":"2025-04-04T18:27:54.900Z","avatar_url":"https://github.com/thomasnield.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n![](http://i.imgur.com/v3FqiEA.png)\n\n# Math → Kotlin Cheatsheet\n\nThis is still very much a work-in-progress and I welcome any contributions. This cheat sheet was inspired by [Jam3's Math as Code project](https://github.com/Jam3/math-as-code). \n\nI am using this [online LaTeX equation editor](https://www.codecogs.com/eqnedit.php) to generate expressions, and if you want to learn MathJax notation [here is a great resource](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference).\n\nWhile you are here, please rally this [issue on Dokka](https://github.com/Kotlin/dokka/issues/245) to bring MathJax support to Kotlin Docs. \n\n\u003e Please note that these code examples may leave out some optimizations (e.g. using [collection operators instead of Sequences](https://winterbe.com/posts/2018/07/23/kotlin-sequence-tutorial/#sequences-vs-collections)) in order to reduce boilerplate and make concepts clear. \n\n# Understanding Math Expressions\n\nThink of mathematical notation as a form of pseudocode. It is often expressing an algorithm in declarative form. It is important to note however that these mathematical symbols existed well before computers, and therefore may not be expressed in a programming-friendly way (e.g. using 1-based indexing versus 0-based indexing). Like pseudocode, the author can take liberties in defining what symbols mean, and any symbol can mean different things depending on the context. \n\nAs a programmer, learning to interpret mathematical notation will help you navigate academic papers and blogs that apply mathematical concepts. This is especially the case when you dabble in machine learning, mathematical optimization, and data science. \n\n# Summation and Multiplication\n\nThis symbol \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;\\sum\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;\\sum\" title=\"\\sum\" /\u003e\u003c/a\u003e indicates you are summing a series of items. It is probably one of the most commonly used symbols in math to express iterative addition. \n\nSimilarly, a \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;\\prod\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;\\prod\" title=\"\\prod\" /\u003e\u003c/a\u003e is used to multiply a series of items. \n\n\n|Math Expression|Kotlin Code|\n|---|---|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{3}i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{3}i\" title=\"\\sum_{i=1}^{3}i\" /\u003e\u003c/a\u003e|`(1..3).sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{5}10i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{5}10i\" title=\"\\sum_{i=1}^{5}10i\" /\u003e\u003c/a\u003e|`(1..5).map { i -\u003e 10*i }.sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=0}^{n}i^2\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=0}^{n}i^2\" title=\"\\sum_{i=0}^{n}i^2\" /\u003e\u003c/a\u003e|`fun f(n: Int) = (0..n).map { it.pow(2) }.sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{100}3x^2\u0026space;\u0026plus;\u0026space;2i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{100}3x^2\u0026space;\u0026plus;\u0026space;2i\" title=\"\\sum_{i=1}^{100}3x^2 + 2i\" /\u003e\u003c/a\u003e|`fun f(x: Double) = (1..100).map { i -\u003e 3 * x.pow(2) + (2*i) }.sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=10\u0026space;\u0026plus;\u0026space;3\\sum_{i=0}^{n}i^2\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?10\u0026space;\u0026plus;\u0026space;3\\sum_{i=0}^{n}i^2\" title=\"10 + 3\\sum_{i=0}^{n}i^2\" /\u003e\u003c/a\u003e|        `fun f(n: Int) = 10 + 3*(0..n).map { it * it }.sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=0}^{n}\u0026space;x_i\u0026space;\u0026plus;\u0026space;i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=0}^{n}\u0026space;x_i\u0026space;\u0026plus;\u0026space;i\" title=\"\\sum_{i=0}^{n} x_i + i\" /\u003e\u003c/a\u003e|`fun f(allX: List\u003cInt\u003e) = allX.mapIndexed { i,x -\u003e x + i }.sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{10}\u0026space;\\sum_{j=4}^{20}\u0026space;2ij\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{4}\u0026space;\\sum_{j=4}^{20}\u0026space;2ij\" title=\"\\sum_{i=1}^{10} \\sum_{j=4}^{20} 2ij\" /\u003e\u003c/a\u003e|`(1..4).flatMap { i -\u003e (4..20).map { j -\u003e 2 *i * j } }.sum()`\u003cbr\u003e\u003cbr\u003e`sequence { for(i in 1..4) for (j in 4..20) yield(2 * i * j) }.sum()`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\prod_{i=1}^{n}\u0026space;i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\prod_{i=1}^{n}\u0026space;i\" title=\"\\\\prod_{i=1}^{n} i\" /\u003e\u003c/a\u003e|`(1..n).fold(1L, Long::times)`|\n\n### Nested Summation\n\nWhen you see more than one summation, this means they are nested summations. This is no different than summing with nested loops or flatmapped functional sequences. \n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{10}\u0026space;\\sum_{j=4}^{20}\u0026space;2ij\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{10}\u0026space;\\sum_{j=4}^{20}\u0026space;2ij\" title=\"\\sum_{i=1}^{10} \\sum_{j=4}^{20} 2ij\" /\u003e\u003c/a\u003e\n\n```kotlin\n(1..10).flatMap { i -\u003e (4..20).map { j -\u003e 2 *i * j } }.sum()\n```\n\n\n### Indexes and Iteration\n\nWhen approached with a mathematical expression, you need to consider it may not use 0-based indexing especially in the context of iterating elements. \n\nFor instance, here is an operation that is iterating `n` elements and summing. The iteration starts at index 1.\n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{n}\u0026space;x_i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{n}\u0026space;x_i\" title=\"\\sum_{i=1}^{n} x_i\" /\u003e\u003c/a\u003e\n\nWhen translating this to code, you should interpret this as iterating all the elements starting at index 0. It does not skip the first element or start at index 1. \n\n```kotlin \nfun f(elements: List\u003cInt\u003e) = elements.sum()\n```\n\nHowever, when you are not working with elements but rather an actual number sequence, you should interpret this literally. When we are iterating numbers 1 through 3, we really are iterating numbers 1 through 3. \n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\sum_{i=1}^{3}i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\sum_{i=1}^{3}i\" title=\"\\sum_{i=1}^{3}i\" /\u003e\u003c/a\u003e\n\n```kotlin\nval sum = (1..3).sum()\n```\nIn summary, beware of 0-index and 1-index conventions, and discern what the mathematical expression is trying to achieve before translating it into code. \n\n\n# Variables \n\nHopefully the concept of a variable (such as `x`) should be self-explanatory to a programmer. However, in mathematical notation it is common for subscripts and other \"decorators\" to distinctly describe several instances of that variable. For instance, we can use `old` and `new` `x` values to measure a rate of change.\n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=change\u0026space;=\u0026space;\\frac{x_{new}\u0026space;-\u0026space;x_{old}}{x_{old}}\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?change\u0026space;=\u0026space;\\frac{x_{new}\u0026space;-\u0026space;x_{old}}{x_{old}}\" title=\"change = \\frac{x_{new} - x_{old}}{x_{old}}\" /\u003e\u003c/a\u003e\n\nHere is a more iterative example. We can define 5 different variables all named \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x\" title=\"x\" /\u003e\u003c/a\u003e, but notate them as \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x_1,\u0026space;x_2,\u0026space;x_3,\u0026space;x_4,\u0026space;\\text{and}\u0026space;x_5\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x_1,\u0026space;x_2,\u0026space;x_3,\u0026space;x_4,\u0026space;\\text{and}\u0026space;x_5\" title=\"x_1, x_2, x_3, x_4, \\text{and} x_5\" /\u003e\u003c/a\u003e. Here is how we can notate an average operation.\n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=x_{average}\u0026space;=\u0026space;\\frac{x_1\u0026space;\u0026plus;\u0026space;x_2\u0026space;\u0026plus;\u0026space;x_3\u0026space;\u0026plus;\u0026space;x_4\u0026space;\u0026plus;\u0026space;x_5}{5}\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?x_{average}\u0026space;=\u0026space;\\frac{x_1\u0026space;\u0026plus;\u0026space;x_2\u0026space;\u0026plus;\u0026space;x_3\u0026space;\u0026plus;\u0026space;x_4\u0026space;\u0026plus;\u0026space;x_5}{5}\" title=\"x_{average} = \\frac{x_1 + x_2 + x_3 + x_4 + x_5}{5}\" /\u003e\u003c/a\u003e. \n\n```kotlin\nval xValues = listOf(23, 65, 45, 23, 66)\nval xAverage = allXs.sum() / 5 \n\n// or \n\nval xAverage = allXs.average()\n```\n\nMathematics and programming are similar in that they try to generalize. Instead of formulating an average for just `5` variables, we can notate it for any `n` number of variables. \n\n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=x_{average}\u0026space;=\u0026space;\\frac{x_1\u0026space;\u0026plus;\u0026space;x_2\u0026space;\u0026plus;\u0026space;...\u0026space;\u0026plus;\u0026space;x_n}{n}\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?x_{average}\u0026space;=\u0026space;\\frac{x_1\u0026space;\u0026plus;\u0026space;x_2\u0026space;\u0026plus;\u0026space;...\u0026space;\u0026plus;\u0026space;x_n}{n}\" title=\"x_{average} = \\frac{x_1 + x_2 + ... + x_n}{n}\"/\u003e\u003c/a\u003e\n\n\nTo maximize fanciness, we can go a step further and use a summation operator and just generalize each `x` value as \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x_i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x_i\" title=\"x_i\" /\u003e\u003c/a\u003e. Note that `n` is the number of variables/elements we are averaging. \n\n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=x_{average}\u0026space;=\u0026space;\\frac{1}{n}\\sum_{i=1}^{n}x_i\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?x_{average}\u0026space;=\u0026space;\\frac{1}{n}\\sum_{i=1}^{n}x_i\" title=\"x_{average} = \\frac{1}{n}\\sum_{i=1}^{n}x_i\" /\u003e\u003c/a\u003e\n\n### Decorators\n\nSometimes a variable can have \"decorators\" to further add meaning to a variable but with standardized conciseness.\n\n#### \"Hat\" Symbol\n\nThe hat symbol, such as \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;\\hat\u0026space;x\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;\\hat\u0026space;x\" title=\"\\hat x\" /\u003e\u003c/a\u003e, is often used to indicate a predicted value as opposed to an actual. This is often in the context of prediction and forecasting a number. You could simply express a predicted and actual value as \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x_{predicted}\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x_{predicted}\" title=\"x_{predicted}\" /\u003e\u003c/a\u003e and \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x_{actual}\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x_{actual}\" title=\"x_{actual}\" /\u003e\u003c/a\u003e, but it can be much more concise to use a `^` symbol. \n\nFor example, say we predict selling \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;\\hat\u0026space;x\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;\\hat\u0026space;x\" title=\"\\hat x\" /\u003e\u003c/a\u003e products, but in actuality we sell \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x\" title=\"x\" /\u003e\u003c/a\u003e. Our forecast error would be the difference between these two variables. \n\n\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\text{Forecast\u0026space;Error}\u0026space;=\u0026space;\\hat\u0026space;x\u0026space;-\u0026space;x\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\text{Forecast\u0026space;Error}\u0026space;=\u0026space;\\hat\u0026space;x\u0026space;-\u0026space;x\" title=\"\\text{Forecast Error} = \\hat x - x\" /\u003e\u003c/a\u003e\n\n### Greek Letters\n\nThe Greek alphabet can be used as variables (and sometimes operators) as well. There is little consistency on how they are used and for what. However, there are some common usages for certains symbols as documented in this table. \n\n| Uppercase | Lowercase | Name    | Common Usage |\n|-----------|-----------|---------|--------------|\n| Α         | α         | Alpha   |              |\n| Β         | β         | Beta    |              |\n| Γ         | γ         | Gamma   |              |\n| Δ         | δ         | Delta   |Measurement of change or difference, derivative... [Read More](https://en.wikipedia.org/wiki/Delta_(letter)#Upper_case)|\n| Ε         | ε         | Epsilon |              |\n| Ζ         | ζ         | Zeta    |              |\n| Η         | η         | Eta     |              |\n| Θ         | θ         | Theta   |Geometric angles, trigonometric variables... [Read More](https://en.wikipedia.org/wiki/Theta#Mathematics_and_science)|\n| Ι         | ι         | Iota    |              |\n| Κ         | κ         | Kappa   |              |\n| Λ         | λ         | Lambda  |              |\n| Μ         | μ         | Mu      |              |\n| Ν         | ν         | Nu      |              |\n| Ξ         | ξ         | Xi      |              |\n| Ο         | ο         | Omicron |              |\n| Π         | π         | Pi      |              |\n| Ρ         | ρ         | Rho     |              |\n| Σ         | σ/ς       | Sigma   |Summation, standard deviation... [Read More](https://en.wikipedia.org/wiki/Sigma#Science_and_mathematics)|\n| Τ         | τ         | Tau     |              |\n| Υ         | υ         | Upsilon |              |\n| Φ         | φ         | Phi     |              |\n| Χ         | χ         | Chi     |              |\n| Ψ         | ψ         | Psi     |              |\n| Ω         | ω         | Omega   |              |\n\n\n### Naming Variables in Kotlin \n\nTrying to express variables like \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x_1\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x_1\" title=\"x_1\" /\u003e\u003c/a\u003e and \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;x_{old}\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;x_{old}\" title=\"x_{old}\" /\u003e\u003c/a\u003e can be a little awkward in code. In Kotlin, you can choose to stick with conventional camelCase or break style guidelines by using underscores. \n\n```kotlin\nval x1 = 12\nval x_1 = 12\n\nval xOld = 13\nval x_old = 13\n```\nFor mathematical symbols like theta \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;\\theta\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;\\theta\" title=\"\\theta\" /\u003e\u003c/a\u003e, you can just name your variable `theta`. \n\n```kotlin \nimport kotlin.math.PI\nimport kotlin.math.sin \n\nval theta = PI / 2.0\nval result = sin(theta)\n```\n\nHopefully you will not run into this decision of naming variables often, as you may express variables as iterated elements in collections/sequences rather than give each one an explicit variable name. \n\n\n\n# Elements, Sets, and Ranges \n\n## Sets\n\n### Common Number Sets\n\n#### Real Numbers\n\n#### Rational Numbers\n\n#### Integers \n\n#### Natural Numbers \n\n## Ranges\n\n\n# Functions\n\n\n|Math Expression|Description|Kotlin Code|\n|---|---|---|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=e^x\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?e^x\" title=\"e^x\" /\u003e\u003c/a\u003e|Exponent of `e`|`exp(x)`|\n|\u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=ln(x)\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?ln(x)\" title=\"ln(x)\" /\u003e\u003c/a\u003e|Natural logarithm|`ln(x)`|\n\n\n\n# Linear Algebra\n\nLinear algebra is the building block of machine learning, computer graphics, linear programming, and many other mathematically-intense computer science disciplines. Linear algebra can seem pointless until you apply it to specific problems. For example, [a neural network](https://github.com/thomasnield/kotlin_simple_neural_network) is typically executed using dot products to multiply and sum weights and node values. \n\nLinear algebra takes the tediousness out of working with large multidimensional arrays of numbers and manipulating them with transformations. While you can certainly iterate these numbers yourself and perform whatever calculation you need, it can become extremely inefficient and cumbersome to do yourself. \n\nThere are great libraries that can do this for you. In the Python world you would typically use [NumPy](http://www.numpy.org/). In the JVM world, you can use [ND4J](http://nd4j.org/), [ojAlgo](https://github.com/optimatika/ojAlgo), or [JBlas](http://jblas.org/). The Kotlin multiplatform library [Koma](https://github.com/kyonifer/koma) will probably be your go-to if you intend on doing linear algebra in Kotlin. \n\nIf you want to learn more about Linear Algebra, [3Blue1Brown](https://www.youtube.com/watch?v=fNk_zzaMoSs\u0026list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab) has a great video series on linear algebra on YouTube. \n\n### Scalars\n\nA scalar is simply a single number (as opposed to an array of numbers which is called a vector). Declaring a scalar value in Kotlin is no different than declaring a numeric variable, such as an `Int` or `Double`.\n\nIn linear algebra, scalars are often used in the context of multiplication (hence the name _scalar_, because they _scale_ things). \n\n```kotlin \n// all these assignments are scalars\nval increaseRate = 1.04\n\nval oldSpeed = 60.0\n\nval newSpeed = oldSpeed * increaseRate\n```\n\n\n### Vectors\n\nIn strict programming terms, a vector is an array of numeric values often representing values of different variables. \n\n```kotlin\n// kotlin-stdlib\nval myVector = doubleArrayOf(1.0, 5.2, 2.4)\n\n//koma \nval myVector = rowVectorOf(1.0, 5.2, 2.4)\n```\n\nBy _variables_, we may be talking about _observations_. For instance, we can have a vector of temperatures for a given week.  \n\n```kotlin\n// kotlin-stdlib\nval lowTemperatures = intArrayOf(67, 62, 71, 73, 64, 66, 64)\nval highTemperatures = intArrayOf(76, 74, 77, 75, 76, 73, 72)\n\n// koma\nval lowTemperatures = rowVectorOf(67, 62, 71, 73, 64, 66, 64)\nval highTemperatures = rowVectorOf(76, 74, 77, 75, 76, 73, 72)\n```\n\nWe can also represent different attribributes of an item with a vector. For example, we can express a color in terms of three RGB values. \n\n```kotlin\n\n// kotlin-stdlib\nval pinkColor = intArrayOf(255, 175, 175)\n\n// koma\nval pinkColor = rowVectorOf(255, 175, 175)\n```\n\nWe can also express different attributes of a `Person`: \n\n```kotlin\n// height and weight of a person\n\n// kotlin-stdlib\nval personalAttributes = doubleArrayOf(71.5, 181.3)\n\n// koma\nval personalAttributes = rowVectorOf(71.5, 181.3)\n```\n\nSo why do we express things in terms of vectors rather than classes with properties? When it comes to heavy number-crunching (e.g. machine learning), raw numbers are far more efficient and standardized to compute with. Typically, vectors and matrices (covered next) are binded to a C library that can iteratively do math efficiently. \n\n#### Vector Math\n\nAdding/multipliying a vector with a scalar simply results in a vector with each value added/multiplied respectively by that scalar. \n\n```kotlin\nval populations = rowVectorOf(2310, 2387, 5732)\nval growthRate = 1.01\n\nval newPopulations = populations * growthRate\n\nprintln(newPopulations) // mat[ 2333.10,  2410.87,  5789.32 ]\n```\n\nIf the dimenstions match, you can sum each respective element together (kind of like zipping) between two vectors using a `+` operator: \n\n```kotlin\nval adultPopulations = rowVectorOf(2323, 5672, 2345)\nval childPopulations = rowVectorOf(1632, 1252, 1658)\n\nval totalPopulations = adultPopulations + childPopulations\n\nprintln(totalPopulations) // mat[ 3955.00,  6924.00,  4003.00 ]\n```\n\nHowever using a multiplication operator `*` with two vectors will not work like you probably expect. It does not zip and multiply respective elements together, but rather performs a dot product (which we will cover later). If we multiply two vectors of the same length, it throws an error:\n\n```kotlin \nval populations = rowVectorOf(2310, 2387, 5732)\nval growthRates = rowVectorOf(1.03, 1.06, 1.09)\n\nval newPopulations = populations * growthRates // ERROR! #columns != #rows\n\nprintln(newPopulations)\n```\n\nIf you want to zip and multipy each element respectively, use the `elementTimes()` operator. \n\n```kotlin \nval populations = rowVectorOf(2310, 2387, 5732)\nval growthRates = rowVectorOf(1.03, 1.06, 1.09)\n\nval newPopulations = populations.elementTimes(growthRates)\n\nprintln(newPopulations)\n```\n\n### Matrices\n\nTODO\n\n### Dot Products \n\n\n# Calculus\n\nCalculus is the study of _change_ in mathematics, studying rates of change and how a change in one variable affects another variable.\n\nRates of change and deriving functions that calculate rate of change is the core of calculus. It is useful in machine learning and optimization, so unsurprisingly calculus has been getting a lot of interest outside of academia. When you are trying to minimize error in your machine learning algorithm, you have to increase or decrease parameters to minimize the error. Rather than hopelessly trying every possible combination of parameters to find the lowest resulting error, it can be much more efficient to leverage rate of change in error. With that rate, you can determine if a given point is increasing/decreasing in error and use that as your compass. Then you know which direction to tune your parameters. \n\n [3Blue1Brown has a fun YouTube series on Calculus fundamentals](https://www.youtube.com/playlist?list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr).\n\n\n### Derivatives\n\nProgramming with math often models things as functions, such as \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;f(x)\u0026space;=\u0026space;3x\u0026space;\u0026plus;\u0026space;10\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;f(x)\u0026space;=\u0026space;3x\u0026space;\u0026plus;\u0026space;10\" title=\"f(x) = 3x + 10\" /\u003e\u003c/a\u003e. You can be trying to fit data to a linear function \u003ca href=\"https://www.codecogs.com/eqnedit.php?latex=\\inline\u0026space;y\u0026space;=\u0026space;mx\u0026space;\u0026plus;\u0026space;b\" target=\"_blank\"\u003e\u003cimg src=\"https://latex.codecogs.com/gif.latex?\\inline\u0026space;y\u0026space;=\u0026space;mx\u0026space;\u0026plus;\u0026space;b\" title=\"y = mx + b\" /\u003e\u003c/a\u003e, or minimizing a function measuring error in a neural network. \n\nHowever, when dealing with nonlinear functions (which represent a curvy line or plane, not a straight or flat one), we are often interested in finding the lowest or highest point for optimization reasons. Knowing the slope at a given point on that line or plane can help you follow the steepest direction and find that minimum or maximum. \n\n### Partial Derivatives\n\n### Integrals\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasnield%2Fkotlin_math_cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomasnield%2Fkotlin_math_cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasnield%2Fkotlin_math_cheatsheet/lists"}