An excerpt from the repository, fetched Nov 13, 2026.
Read the project’s introduction
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 < ≥ ≫
conjunction & disjunction ∧ ∨
logical negation ¬ ~ !
intervals
more...
variable name conventions
There are a variety of naming conventions depending on the context and field of study, and they are not always consistent. However, in some of the literature you may find variable names to follow a pattern like so:
s - italic lowercase letters for scalars (e.g. a number)
x - bold lowercase letters for vectors (e.g. a 2D point)
A - bold uppercase letters for matrices (e.g. a 3D transformation)
θ - italic lowercase Greek letters for constants and special variables (e.g. polar angle θ, theta)
This will also be the format of this guide.
equals symbols
There are a number of symbols resembling the equals sign =. Here are a few common examples:
= is for equality (values are the same)
≠ is for inequality (value are not the same)
≈ is for approximately equal to (π ≈ 3.14159)
:= is for definition (A is defined as B)
In JavaScript:
// equality
2 === 3
// inequality
2 !== 3
// approximately equal
almostEqual(Math.PI, 3.14159, 1e-5)
function almostEqual(a, b, epsilon) {
return Math.abs(a - b) <= epsilon
}
You might see the :=, =: and = symbols being used for definition.1
For example, the following defines x to be another name for 2kj.
In JavaScript, we might use var to define our variables and provide aliases:
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:
const f = (k, j) => 2 * k * j
The following, on the other hand, represents equality:
The above equation might be interpreted in code as an assertion:
console.assert(x === (2 * k * j))
square root and complex numbers
A square root operation is of the form:
In programming we use a sqrt function, like so:
var x = 9;
console.log(Math.sqrt(x));
//=> 3
Complex numbers are expressions of the form , where is the real part and is the imaginary part. The imaginary number is defined as:
.
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:
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(math.multiply(a, b).toString())
//=> '1 + 3i'
The library also supports evaluating a string expression, so the above could be re-written as:
console.log(math.eval('(3 - i) * i').toString())
//=> '1 + 3i'
Other implementations:
immutable-complex
complex-js
Numeric-js
dot & cross
The dot · and cross × symbols have different uses depending on context.
They might seem obvious, but it's important to understand the subtle differences before we continue into other sections.
scalar multiplication
Both symbols can represent simple multiplication of scalars. The following are equivalent:
In programming languages we tend to use asterisk for multiplication:
var result = 5 * 4
Often, the multiplication sign is only used to avoid ambiguity (e.g. between two numbers). Here, we can omit it entirely:
If these variables represent scalars, the code would be:
var result = 3 * k * j
vector multiplication
To denote multiplication of one vector with a scalar, or element-wise multiplication of a vector with another vector, we typically do not use the dot · or cross × symbols. These have different meanings in linear algebra, discussed shortly.
Let's take our earlier example but apply it to vectors. For element-wise vector multiplication, you might see an open dot ∘ to represent the Hadamard product.2
In other instances, the author might explicitly define a different notation, such as a circled dot ⊙ or a filled circle ●.3
Here is how it would look in code, using arrays [x, y] to represent the 2D vectors.
var s = 3
var k = [ 1, 2 ]
var j = [ 2, 3 ]
var tmp = multiply(k, j)
var result = multiplyScalar(tmp, s)
//=> [ 6, 18 ]
Our multiply and multiplyScalar functions look like this:
function multiply(a, b) {
return [ a[0] * b[0], a[1] * b[1] ]
}
function multiplyScalar(a, scalar) {
return [ a[0] * scalar, a[1] * scalar ]
}
Similarly, matrix multiplication typically does not use the dot · or cross symbol ×. Matrix multiplication will be covered in a later secti
Read the full README on GitHub