Github url


by Jam3

a cheat-sheet for mathematical notation in code form

11.1K Stars 770 Forks Last release: Not found MIT License 135 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


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.


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.




  • [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






 symbols being used for _definition_.<sup><a href="">1</a></sup>

For example, the following defines _x_ to be another name for 2_kj_.


<!-- x := 2kj -->

In JavaScript, we might use


 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


 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:


<!-- x = 2kj -->

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:


<!-- \left(\sqrt{x}\right)^2 = x -->

In programming we use a


 function, like so: 

var x = 9; console.log(Math.sqrt(x)); //=> 3

Complex numbers are expressions of the form ![complex](;&plus;&space;ib), where ![a]( is the real part and ![b]( is the imaginary part. The imaginary number ![i]( is defined as:

![imaginary]( \<!-- i=\sqrt{-1} --\>

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:


<!-- 5 \cdot 4 = 5 \times 4 -->

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:


<!-- 3kj -->

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](<sup><a href="">2</a></sup>


<!-- 3\mathbf{k}\circ\mathbf{j} -->

In other instances, the author might explicitly define a different notation, such as a circled dot

 or a filled circle 

.<sup><a href="">3</a></sup>

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]





 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 section.
#### dot product

The dot symbol


 can be used to denote the [_dot product_]( of two vectors. Sometimes this is called the _scalar product_ since it evaluates to a scalar.


<!-- \mathbf{k}\cdot \mathbf{j} -->

It is a very common feature of linear algebra, and with a 3D vector it might look like this:

var k = [0, 1, 0] var j = [1, 0, 0] var d = dot(k, j) //=> 0

The result


 tells us our vectors are perpendicular. Here is a 


 function for 3-component vectors:

function dot(a, b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] }

#### cross product

The cross symbol


 can be used to denote the [_cross product_]( of two vectors.


<!-- \mathbf{k}\times \mathbf{j} -->

In code, it would look like this:

var k = [0, 1, 0] var j = [1, 0, 0] var result = cross(k, j) //=> [0, 0, -1]

Here, we get

[0, 0, -1]

, which is perpendicular to both **k** and **j**.




function cross(a, b) { var ax = a[0], ay = a[1], az = a[2], bx = b[0], by = b[1], bz = b[2] var rx = ay * bz - az * by var ry = az * bx - ax * bz var rz = ax * by - ay * bx return [rx, ry, rz] }

For other implementations of vector multiplication, cross product, and dot product:

- [gl-vec3](
- [gl-vec2](
- [vectors]( - includes n-dimensional

## sigma

The big Greek


 (Sigma) is for [Summation]( In other words: summing up some numbers.


<!-- \sum_{i=1}^{100}i -->



 says to start at 


 and end at the number above the Sigma, 


. These are the lower and upper bounds, respectively. The _i_ to the right of the "E" tells us what we are summing. In code:

var sum = 0 for (var i = 1; i <= 100; i++) { sum += i }

The result of





**Tip:** With whole numbers, this particular pattern can be optimized to the following:

var n = 100 // upper bound var sum = (n * (n + 1)) / 2

Here is another example where the _i_, or the "what to sum," is different:


<!-- \sum_{i=1}^{100}(2i+1) -->

In code:

var sum = 0 for (var i = 1; i <= 100; i++) { sum += (2 * i + 1) }

The result of





The notation can be nested, which is much like nesting a


 loop. You should evaluate the right-most sigma first, unless the author has enclosed them in parentheses to alter the order. However, in the following case, since we are dealing with finite sums, the order does not matter.


<!-- \sum_{i=1}^{2}\sum_{j=4}^{6}(3ij) -->

In code:

var sum = 0 for (var i = 1; i <= 2; i++) { for (var j = 4; j <= 6; j++) { sum += (3 * i * j) } }



 will be 


## capital Pi

The capital Pi or "Big Pi" is very similar to [Sigma](, except we are using multiplication to find the product of a sequence of values.

Take the following:


<!-- \prod_{i=1}^{6}i -->

In code, it might look like this:

var value = 1 for (var i = 1; i <= 6; i++) { value *= i }



 will evaluate to 


## pipes

Pipe symbols, known as _bars_, can mean different things depending on the context. Below are three common uses: [absolute value](, [Euclidean norm](, and [determinant](

These three features all describe the _length_ of an object.

#### absolute value


<!-- \left | x \right | -->

For a number _x_,


 means the absolute value of _x_. In code:

var x = -5 var result = Math.abs(x) // => 5

#### Euclidean norm


<!-- \left \| \mathbf{v} \right \| -->

For a vector **v**,


 is the [Euclidean norm]( of **v**. It is also referred to as the "magnitude" or "length" of a vector.

Often this is represented by double-bars to avoid ambiguity with the _absolute value_ notation, but sometimes you may see it with single bars:


<!-- \left | \mathbf{v} \right | -->

Here is an example using an array

[x, y, z]

 to represent a 3D vector.

var v = [0, 4, -3] length(v) //=> 5




function length (vec) { var x = vec[0] var y = vec[1] var z = vec[2] return Math.sqrt(x * x + y * y + z * z) }

Other implementations:

- [magnitude]( - n-dimensional
- [gl-vec2/length]( - 2D vector
- [gl-vec3/length]( - 3D vector

#### determinant


<!-- \left |\mathbf{A} \right | -->

For a matrix **A**,


 means the [determinant]( of matrix **A**.

Here is an example computing the determinant of a 2x2 matrix, represented by a flat array in column-major format.

var determinant = require('gl-mat2/determinant') var matrix = [1, 0, 0, 1] var det = determinant(matrix) //=> 1


- [gl-mat4/determinant]( - also see [gl-mat3]( and [gl-mat2](
- [ndarray-determinant](
- [glsl-determinant](
- [robust-determinant](
- [robust-determinant-2]( and [robust-determinant-3](, specifically for 2x2 and 3x3 matrices, respectively

## hat

In geometry, the "hat" symbol above a character is used to represent a [unit vector]( For example, here is the unit vector of **a**:


<!-- \hat{\mathbf{a}} -->

In Cartesian space, a unit vector is typically length 1. That means each part of the vector will be in the range of -1.0 to 1.0. Here we _normalize_ a 3D vector into a unit vector:

var a = [0, 4, -3] normalize(a) //=> [0, 0.8, -0.6]

Here is the


 function, operating on 3D vectors:

function normalize(vec) { var x = vec[0] var y = vec[1] var z = vec[2] var squaredLength = x * x + y * y + z * z if (squaredLength > 0) { var length = Math.sqrt(squaredLength) vec[0] = x / length vec[1] = y / length vec[2] = z / length } return vec }

Other implementations:

- [gl-vec3/normalize]( and [gl-vec2/normalize](
- [vectors/normalize-nd]( (n-dimensional)

## element

In set theory, the "element of" symbol


 can be used to describe whether something is an element of a _set_. For example:


<!-- A=\left \{3,9,14}{ \right \}, 3 \in A -->

Here we have a set of numbers _A_

{ 3, 9, 14 }

 and we are saying 


 is an "element of" that set. 

A simple implementation in ES5 might look like this:

var A = [3, 9, 14] A.indexOf(3) >= 0 //=> true

However, it would be more accurate to use a


 which only holds unique values. This is a feature of ES6.

var A = new Set([3, 9, 14]) A.has(3) //=> true

The backwards

 is the same, but the order changes:


<!-- A=\left \{3,9,14}{ \right \}, A \ni 3 -->

You can also use the "not an element of" symbols


 like so:


<!-- A=\left \{3,9,14}{ \right \}, 6 \notin A -->
## common number sets

You may see some some large [Blackboard]( letters among equations. Often, these are used to describe sets.

For example, we might describe _k_ to be an [element of]( the set



<!-- k \in \mathbb{R} -->

Listed below are a few common sets and their symbols.


 real numbers

The large

 describes the set of _real numbers_. These include integers, as well as rational and irrational numbers.

JavaScript treats floats and integers as the same type, so the following would be a simple test of our _k_ ∈ ℝ example:

function isReal (k) { return typeof k === 'number' && isFinite(k); }

_Note:_ Real numbers are also _finite_, as in, _not infinite._


 rational numbers

Rational numbers are real numbers that can be expressed as a fraction, or _ratio_ (like

). Rational numbers cannot have zero as a denominator.

This also means that all integers are rational numbers, since the denominator can be expressed as 1.

An irrational number, on the other hand, is one that cannot be expressed as a ratio, like π (PI).



An integer, i.e. a real number that has no fractional part. These can be positive or negative.

A simple test in JavaScript might look like this:

function isInteger (n) { return typeof n === 'number' && n % 1 === 0 }


 natural numbers

A natural number, a positive and non-negative integer. Depending on the context and field of study, the set may or may not include zero, so it could look like either of these:

{ 0, 1, 2, 3, ... } { 1, 2, 3, 4, ... }

The former is more common in computer science, for example:

function isNaturalNumber (n) { return isInteger(n) && n >= 0 }


 complex numbers

A complex number is a combination of a real and imaginary number, viewed as a co-ordinate in the 2D plane. For more info, see [A Visual, Intuitive Guide to Imaginary Numbers](

## function

[Functions]( are fundamental features of mathematics, and the concept is fairly easy to translate into code.

A function relates an input to an output value. For example, the following is a function:


<!-- x^{2} -->

We can give this function a _name_. Commonly, we use


 to describe a function, but it could be named 


 or anything else.


<!-- f\left (x \right ) = x^{2} -->

In code, we might name it


 and write it like this:

function square (x) { return Math.pow(x, 2) }

Sometimes a function is not named, and instead the output is written.


<!-- y = x^{2} -->

In the above example, _x_ is the input, the relationship is _squaring_, and _y_ is the output.

Functions can also have multiple parameters, like in a programming language. These are known as _arguments_ in mathematics, and the number of arguments a function takes is known as the _arity_ of the function.


<!-- f(x,y) = \sqrt{x^2 + y^2} -->

In code:

function length (x, y) { return Math.sqrt(x * x + y * y) }

### piecewise function

Some functions will use different relationships depending on the input value, _x_.

The following function _ƒ_ chooses between two "sub functions" depending on the input value.


<!-- f(x)= 
    \frac{x^2-x}{x},& \text{if } x\geq 1\\
    0, & \text{otherwise}
\end{cases} -->

This is very similar to




 in code. The right-side conditions are often written as **"for x \< 0"** or **"if x = 0"**. If the condition is true, the function to the left is used.

In piecewise functions, **"otherwise"** and **"elsewhere"** are analogous to the


 statement in code.

function f (x) { if (x >= 1) { return (Math.pow(x, 2) - x) / x } else { return 0 } }

### common functions

There are some function names that are ubiquitous in mathematics. For a programmer, these might be analogous to functions "built-in" to the language (like


 in JavaScript).

One such example is the _sgn_ function. This is the _signum_ or _sign_ function. Let's use [piecewise function]( notation to describe it:


<!-- sgn(x) := 
    -1& \text{if } x < 0\\
    0, & \text{if } {x = 0}\\
    1, & \text{if } x > 0\\
\end{cases} -->

In code, it might look like this:

function sgn (x) { if (x < 0) return -1 if (x > 0) return 1 return 0 }

See [signum]( for this function as a module.

Other examples of such functions: _sin_, _cos_, _tan_.

### function notation

In some literature, functions may be defined with more explicit notation. For example, let's go back to the


 function we mentioned earlier:


<!-- f\left (x \right ) = x^{2} -->

It might also be written in the following form:


<!-- f : x \mapsto x^2 -->

The arrow here with a tail typically means "maps to," as in _x maps to x<sup>2</sup>_.

Sometimes, when it isn't obvious, the notation will also describe the _domain_ and _codomain_ of the function. A more formal definition of _ƒ_ might be written as:


<!-- \begin{align*}
f :&\mathbb{R} \rightarrow \mathbb{R}\\
&x \mapsto x^2 

A function's _domain_ and _codomain_ is a bit like its _input_ and _output_ types, respectively. Here's another example, using our earlier _sgn_ function, which outputs an integer:


<!-- sgn : \mathbb{R} \rightarrow \mathbb{Z} -->

The arrow here (without a tail) is used to map one _set_ to another.

In JavaScript and other dynamically typed languages, you might use documentation and/or runtime checks to explain and validate a function's input/output. Example:

/** * Squares a number. * @param {Number} a real number * @return {Number} a real number */ function square (a) { if (typeof a !== 'number') { throw new TypeError('expected a number') } return Math.pow(a, 2) }

Some tools like [flowtype]( attempt to bring static typing into JavaScript.

Other languages, like Java, allow for true method overloading based on the static types of a function's input/output. This is closer to mathematics: two functions are not the same if they use a different _domain_.

## prime

The prime symbol (

) is often used in variable names to describe things which are similar, without giving it a different name altogether. It can describe the "next value" after some transformation.

For example, if we take a 2D point _(x, y)_ and rotate it, you might name the result _(x′, y′)_. Or, the _transpose_ of matrix **M** might be named **M′**.

In code, we typically just assign the variable a more descriptive name, like



For a mathematical [function](, the prime symbol often describes the _derivative_ of that function. Derivatives will be explained in a future section. Let's take our earlier function:


<!-- f\left (x \right ) = x^{2} -->

Its derivative could be written with a prime



<!-- f'(x) = 2x -->

In code:

function f (x) { return Math.pow(x, 2) } function fPrime (x) { return 2 * x }

Multiple prime symbols can be used to describe the second derivative _ƒ′′_ and third derivative _ƒ′′′_. After this, authors typically express higher orders with roman numerals _ƒ_<sup>IV</sup> or superscript numbers _ƒ_<sup>(n)</sup>.

## floor & ceiling

The special brackets




 represent the _floor_ and _ceil_ functions, respectively.


<!-- floor(x) = \lfloor x \rfloor -->


<!-- ceil(x) = \lceil x \rceil -->

In code:

Math.floor(x) Math.ceil(x)

When the two symbols are mixed


, it typically represents a function that rounds to the nearest integer:


<!-- round(x) = \lfloor x \rceil -->

In code:


## arrows

Arrows are often used in [function notation]( Here are a few other areas you might see them.

#### material implication

Arrows like


 are sometimes used in logic for _material implication._ That is, if A is true, then B is also true.


<!-- A \Rightarrow B -->

Interpreting this as code might look like this:

if (A === true) { console.assert(B === true) }

The arrows can go in either direction

, or both 

. When _A ⇒ B_ and _B ⇒ A_, they are said to be equivalent:


<!-- A \Leftrightarrow B -->
#### equality

In math, the




 are typically used in the same way we use them in code: _less than_, _greater than_, _less than or equal to_ and _greater than or equal to_, respectively.

50 > 2 === true 2 < 10 === true 3 <= 4 === true 4 >= 4 === true

On rare occasions you might see a slash through these symbols, to describe _not_. As in, _k_ is "not greater than" _j_.


<!-- k \ngtr j -->



 are sometimes used to represent _significant_ inequality. That is, _k_ is an [order of magnitude]( larger than _j_.


<!-- k \gg j -->

In mathematics, _order of magnitude_ is rather specific; it is not just a "really big difference." A simple example of the above:

orderOfMagnitude(k) > orderOfMagnitude(j)

And below is our


 function, using [Math.trunc]( (ES6).

function log10(n) { // logarithm in base 10 return Math.log(n) / Math.LN10 } function orderOfMagnitude (n) { return Math.trunc(log10(n)) }

<sup><em>Note:</em> This is not numerically robust.</sup>

See [math-trunc]( for a ponyfill in ES5.

#### conjunction & disjunction

Another use of arrows in logic is conjunction

 and disjunction 

. They are analogous to a programmer's 




 operators, respectively.

The following shows conjunction

, the logical 




<!-- k > 2 \land k < 4 \Leftrightarrow k = 3 -->

In JavaScript, we use


. Assuming _k_ is a natural number, the logic implies that _k_ is 3:

if (k > 2 && k < 4) { console.assert(k === 3) }

Since both sides are equivalent

, it also implies the following:

if (k === 3) { console.assert(k > 2 && k < 4) }

The down arrow

 is logical disjunction, like the OR operator.


<!-- A \lor B -->

In code:

A || B

## logical negation

Occasionally, the






 symbols are used to represent logical 


. For example, _¬A_ is only true if A is false.

Here is a simple example using the _not_ symbol:


<!-- x \neq y \Leftrightarrow \lnot(x = y) -->

An example of how we might interpret this in code:

if (x !== y) { console.assert(!(x === y)) }

_Note:_ The tilde


 has many different meanings depending on context. For example, _row equivalence_ (matrix theory) or _same order of magnitude_ (discussed in [equality](
## intervals

Sometimes a function deals with real numbers restricted to some range of values, such a constraint can be represented using an _interval_

For example we can represent the numbers between zero and one including/not including zero and/or one as:

- Not including zero or one: ![interval-opened-left-opened-right](
<!-- (0, 1) -->
- Including zero or but not one: ![interval-closed-left-opened-right](
<!-- [0, 1) -->
- Not including zero but including one: ![interval-opened-left-closed-right](
<!-- (0, 1] -->
- Including zero and one: ![interval-closed-left-closed-right](
<!-- [0, 1] -->

For example we to indicate that a point


 is in the unit cube in 3D we say:


<!-- x \in [0, 1]^3 -->

In code we can represent an interval using a two element 1d array:

var nextafter = require('nextafter') var a = [nextafter(0, Infinity), nextafter(1, -Infinity)] // open interval var b = [nextafter(0, Infinity), 1] // interval closed on the left var c = [0, nextafter(1, -Infinity)] // interval closed on the right var d = [0, 1] // closed interval

Intervals are used in conjunction with set operations:

- _intersection_ e.g. ![interval-intersection](
<!-- [3, 5) \cap [4, 6] = [4, 5) -->
- _union_ e.g. ![interval-union](
<!-- [3, 5) \cup [4, 6] = [3, 6] -->
- _difference_ e.g. ![interval-difference-1]( and ![interval-difference-2](
<!-- [3, 5) - [4, 6] = [3, 4) --><!-- [4, 6] - [3, 5) = [5, 6] -->

In code:

var Interval = require('interval-arithmetic') var nextafter = require('nextafter') var a = Interval(3, nextafter(5, -Infinity)) var b = Interval(4, 6) Interval.intersection(a, b) // {lo: 4, hi: 4.999999999999999} Interval.union(a, b) // {lo: 3, hi: 6} Interval.difference(a, b) // {lo: 3, hi: 3.9999999999999996} Interval.difference(b, a) // {lo: 5, hi: 6}




Like this guide? Suggest some more features or send us a Pull Request!


For details on how to contribute, see


MIT, see for details.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.