Coder Social home page Coder Social logo

math-as-code's Introduction

math-as-code

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 context and the field of study (linear algebra, set theory, etc). This guide may not cover all uses of a symbol, but PRs are welcome.

For a more complete list, refer to Wikipedia - List of Mathematical Symbols.

contents

variable naming 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 (i.e. a number)
  • x - bold lowercase letters for vectors (i.e. a 2D point)
  • A - bold uppercase letters for matrices (i.e. a 3D transformation)
  • θ - italic lowercase Greek letters for constants and special variables (i.e. 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 is a few common ones and an example of their use:

  • = is for equality (values are the same)
  • is for inequality (value are not the same)
  • is for approximately equal to (π ≈ 3.14159)

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
}

In mathematics, the := =: and = symbols are used for definition. The following defines x to be another name for 2kj.

equals1

Code:

var x = 2 * k * j

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:

dotcross1

In code we tend to use asterisk:

var result = 3 * 4

With adjacent letter variables, the multiplication sign is typically omitted.

dotcross2

If these variables represent scalars, the code would be:

var result = 3 * k * j

vector multiplication

To denote multiplication of one vector by another, or multiplication of a vector with a scalar, we 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:

dotcross3

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 a 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.

dotcross4

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 0 tells us our vectors are perpendicular. Our dot function:

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.

dotcross5

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.

Our cross function:

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:

sigma

The big Greek "E" (Sigma) is for Summation. In other words: summing up some numbers.

sigma

Here, i=1 says to start at 1 and end at the number above the Sigma, 100. 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 n = 100
var sum = 0
for (var i = 1; i <= n; i++) {
  sum += i
}

The result of sum is 5050.

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

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

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

sum2

In code:

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

The result of sum is 10200.

The notation can be nested, which is much like nesting a for loop. You should evaluate the right-most sigma first, unless the author has enclosed them in parentheses to alter the order.

sigma3

In code:

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

Here, sum will be 135.

capital Pi

The capital Pi or "Big Pi" is very similar to Sigma, except we are using multiplication to find the "products of sequences."

Take the following:

capitalPi

In code, it might look like this:

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

Where value will evaluate to 720.

pipes

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

absolute

pipes1

For a number x, | x | means the absolute of x. In code:

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

Euclidean norm

pipes4

For a vector v, ‖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 notation, but sometimes you may see it with single bars:

pipes2

Here is an example using an array [x, y, z] to represent a 3D vector.

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

The length function:

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:

determinant

pipes3

For a matrix A, |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-mat4/determinant')

var matrix = [ 1, 0, 0, 1 ]
var det = determinant(matrix)
//=> 1

Implementations:

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

In cartesian space, a unit vector is typically length 1, 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.6000000000000001 ]

Notice, since we are working with floating point values and not using numerically robust operations, we do not get a perfect -(3/5) value for the Z component.

Here is the normalize 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) {
    squaredLength = 1 / Math.sqrt(squaredLength)
    vec[0] = vec[0] * squaredLength
    vec[1] = vec[1] * squaredLength
    vec[2] = vec[2] * squaredLength
  }
  return vec
}

Other implementations:

element

In set theory, the "element of" symbol and can be used to describe whether something is an element of a set. For example:

element1

Here we have a set of numbers A [ 3, 9, 14 ] and we are saying 3 is an "element of" that set. In code:

var A = [ 3, 9, 14 ]

A.indexOf(3) >= 0
//=> true

The backwards is the same, but the order changes:

element2

You can also use the "not an element of" symbols and like so:

element3

more...

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

License

MIT, see LICENSE.md for details.

math-as-code's People

Contributors

mattdesl avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.