Experience-Monks /math-as-code
a cheat-sheet for mathematical notation in code form
★ 15,501⑂ 1,083👁 398⑃ 41● 48▤ 0
+2
Daily
15,501
stars
1,083
forks
398
watchers
41
prs
48
issues
0
releases
README
math-as-code
Chinese translation (中文版)
Python version (English)
This is a reference to ease developers into mathematical notation by showing comparisons with JavaScript code.
Motivation: Academic papers can be intimidating for self-taught game and graphics programmers. :)
This guide is not yet finished. If you see errors or want to contribute, please open a ticket or send a PR.
Note: For brevity, some code examples make use of npm packages. You can refer to their GitHub repos for implementation details.
foreword
Mathematical symbols can mean different things depending on the author, context and the field of study (linear algebra, set theory, etc). This guide may not cover all uses of a symbol. In some cases, real-world references (blog posts, publications, etc) will be cited to demonstrate how a symbol might appear in the wild.
For a more complete list, refer to Wikipedia - List of Mathematical Symbols.
For simplicity, many of the code examples here operate on floating point values and are not numerically robust. For more details on why this may be a problem, see Robust Arithmetic Notes by Mikola Lysenko.
contents
- variable name conventions
- equals = ≈ ≠ :=
- square root and complex numbers √ i
- dot & cross · × ∘
- scalar multiplication
- vector multiplication
- dot product
- cross product
- sigma Σ - summation
- capital Pi Π - products of sequences
- pipes ||
- absolute value
- Euclidean norm
- determinant
- hat â - unit vector
- "element of" ∈ ∉
- common number sets ℝ ℤ ℚ ℕ
- function ƒ
- piecewise function
- common functions
- function notation ↦ →
- prime ′
- floor & ceiling ⌊ ⌉
- arrows
- material implication ⇒ →
- equality [1]
For example, the following defines x to be another name for 2kj.
!equals1
In JavaScript, we might use var to define our variables and provide aliases:
js
var x = 2 k j
However, this is mutable, and only takes a snapshot of the values at that time. Some languages have pre-processor define statements, which are closer to a mathematical define.
A more accurate define in JavaScript (ES6) might look a bit like this:
js
const f = (k, j) = 2 k j
The following, on the other hand, represents equality:
!equals2
The above equation might be interpreted in code as an assertion:
js
console.assert(x === (2 k j))
square root and complex numbers
A square root operation is of the form:
!squareroot
In programming we use a sqrt function, like so:
js
var x = 9;
console.log(Math.sqrt(x));
//= 3
Complex numbers are expressions of the form !complex, where !a is the real part and !b is the imaginary part. The imaginary number !i is defined as:
!imaginary.
In JavaScript, there is no built-in functionality for complex numbers, but there are some libraries that support complex number arithmetic. For example, using mathjs:
js
var math = require('mathjs')
var a = math.complex(3, -1)
//= { re: 3, im: -1 }
var b = math.sqrt(-1)
//= { re: 0, im: 1 }
console.log(Top issues
#4Consider writing this as Jupyter notebooks instead of JavaScript31 comments · 2015-06-29#1More Features17 comments · 2015-06-29#7:heart:8 comments · 2015-06-30#12What is ∀?6 comments · 2015-06-30#10limit, derivatives5 comments · 2015-06-30#3hats also represent estimates5 comments · 2015-06-29#14"In mathematics, the := =: and = symbols are used for definition."4 comments · 2015-07-01#85Some Maths Illegible in GitHub Dark Mode2 comments · 2021-09-14