# Operations — MathJS 0.0.1 documentation

### Navigation

- index
- next |
- previous |
- MathJS 0.0.1 documentation »

# Operations

`add`(a1, a2[, result])

  Arguments:

  - a1 (Matrix) – first operand
  - a2 (Matrix) – second operand
  - result (Matrix) – result buffer

  Returns: | result

  Adds two buffers of the same size.

`subtract`(a1, a2[, result])

  Arguments:

  - a1 (Matrix) – first operand
  - a2 (Matrix) – second operand
  - result (Matrix) – result buffer

  Returns: | result

  Subtracts two buffers of the same size.

`scale`(a, s[, result])

  Arguments:

  - a (Matrix) – buffer
  - s (number) – scalar
  - result (Matrix) – result

  Returns: | result

  Scales a buffer by multiplying each component by a scalar.

`equal`(a1, a2[, e])

  Arguments:

  - a1 (Matrix) – first operand
  - a2 (Matrix) – second operand
  - e (number) – comparision tolerance, defaults to `10E-5`

  Returns: | true if operands are equal (within tolerance), false otherwise

  Compares two buffers component-wise for equality. Differences smaller than `e` are ignored.

`clone`(a[, result])

  Arguments:

  - a (Matrix) – buffer
  - result (Matrix) – result buffer

  Creates a component-wise copy of a buffer.

`clear`(a[, s])

  Arguments:

  - a (Matrix) – buffer
  - s (number) – scalar, defaults to 0

  Clears a buffer by writing a scalar into each component.

`clamp`(s, min, max)

  Arguments:

  - s (number) – scalar
  - min (number) – minimum value
  - max (number) – maximum value

  Returns: | `s` restricted inside range of `min` to `max`

`toRadians`(angle)

  Arguments:

  - angle (number) – angle in degrees

  Returns: | angle in radians

`toDegrees`(angle)

  Arguments:

  - angle (number) – angle in radians

  Returns: | angle in degrees

`toMathML`(a)

  Arguments:

  - a (Matrix) – buffer

  Converts a buffer to MathML representation.

`toString`(a)

  Arguments:

  - a (Matrix) – buffer

  Converts a buffer to string representation.

## Vector

`set`(v, values)

  Arguments:

  - v (Vector) – vector
  - values (Array) – array of values

  Returns: | `v`

`length`(v)

  Arguments:

  - v (Vector) – vector

  Returns: | length of `v`

`length2`(v)

  Arguments:

  - v (Vector) – vector

  Returns: | squared length of `v`

`dot`(v1, v2)

  Arguments:

  - v1 (Vector) – first vector
  - v2 (Vector) – second vector

  Returns: | inner product of `v1` and `v2`

`negate`(v[, result])

  Arguments:

  - v (Vector) – vector
  - result (Vector) – result

  Returns: | negation of `v`

`limit`(v, s[, result])

  Arguments:

  - v (Vector) – vector
  - s (number) – scalar limit

  Returns: | `v` resized to have length at most `s`

`normalize`(v[, result])

  Arguments:

  - v (Vector) – vector
  - result (Vector) – result

  Returns: | a unit vector in the direction of `v`

`distance`(v1, v2)

  Arguments:

  - v1 (Vector) – origin vector
  - v2 (Vector) – target vector

  Returns: | the distance from v1 to v2

`angle`(v1, v2)

  Arguments:

  - v1 (Vector) – first vector
  - v2 (Vector) – second vector

  Returns: | the angle betwen `v1` and `v2`

`lerp`(v1, v2, s[, result])

  Arguments:

  - v1 (Vector) – origin vector
  - v2 (Vector) – target vector
  - s (number) – interpolation scalar
  - result (Vector) – result

  Returns: | interpolation from `v1` to `v2` by s

`direction`(v1, v2[, result])

  Arguments:

  - v1 (Vector) – origin vector
  - v2 (Vector) – target vector
  - result (Vector) – result

  Returns: | unit vector pointing from `v1` to `v2`

`extract`(v, offset, length[, result])

  Arguments:

  - v (Vector) – vector
  - offset (number) – offset from the start ov `v`
  - length (number) – number of elements to extract
  - result (Vector) – result

  Returns: | `length` elements of `v`, starting from `offset`

`zero`(v)

  Arguments:

  - v (Vector) – vector

  Returns: | `v` with all components 0

`zero`(d)

  Arguments:

  - d (number) – dimension

  Returns: | a new `Vector` with all components 0

`one`(v)

  Arguments:

  - v (Vector) – vector

  Returns: | `v` with all components 1

`one`(d)

  Arguments:

  - d (number) – dimension

  Returns: | a new `Vector` with all components 1

## Vector2

`transformPoint`(t, v[, result])

  Arguments:

  - t (Transform) – 2-dimensional transform
  - v (Vector) – vector
  - result (Vector) – result

  Returns: | `v` transformed by `t`

`transformDirection`(t, v[, result])

  Arguments:

  - t (Transform) – 2-dimensional transform
  - v (Vector) – vector
  - result (Vector) – result

  Returns: | `v` transformed by the linear part of `t`

`x`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 2-dimensional y unit vector (1, 0)

`y`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 2-dimensional y unit vector (0, 1)

`u`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 2-dimensional u unit vector (1, 0)

`v`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 2-dimensional v unit vector (0, 1)

## Vector3

`transformPoint`(t, v[, result])

  Arguments:

  - t (Transform) – 3-dimensional transform
  - v (Vector) – vector
  - result (Vector) – result

  Returns: | `v` transformed by `t`

`transformDirection`(t, v[, result])

  Arguments:

  - t (Transform) – 3-dimensional transform
  - v (Vector) – vector
  - result (Vector) – result

  Returns: | `v` transformed by the linear part of `t`

`cross`(v1, v2[, result])

  Arguments:

  - v1 (Vector) – first vector
  - v2 (Vector) – second vector
  - result (Vector) – result

  Returns: | outer product of `v1` and `v2`

`unproject`(v, view, projection, viewport[, result])

  Note

  Not yet implemented

  Arguments:

  - v (Vector) – screen space vector
  - view (Matrix) – view matrix
  - projection (Matrix) – projection matrix
  - viewport (Array) – viewport parameters, as `[x, y, width, height]`
  - result (Vector) – result

  Returns: | vector projected from screen space to object space

`x`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 3-dimensional y unit vector (1, 0, 0)

`y`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 3-dimensional y unit vector (0, 1, 0)

`z`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 3-dimensional z unit vector (0, 0, 1)

## Vector4

`x`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 4-dimensional y unit vector (1, 0, 0, 0)

`y`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 4-dimensional y unit vector (0, 1, 0, 0)

`z`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 4-dimensional z unit vector (0, 0, 1, 0)

`w`([result])

  Arguments:

  - result (Vector) – result

  Returns: | 4-dimensional w unit vector (0, 0, 0, 1)

## Quaternion

`inverse`(q[, result])

  Arguments:

  - q (Quaternion) – quaternion
  - result (Quaternion) – result

  Returns: | inverse of `q`

`conjugate`(q[, result])

  Arguments:

  - q (Quaternion) – quaternion
  - result (Quaternion) – result

  Returns: | conjugate of `q`

`identity`([result])

  Arguments:

  - result (Quaternion) – result

  Returns: | the identity quaternion (0, 0, 0, 1)

`slerp`(q1, q2, s[, result])

  Arguments:

  - q1 (Quaternion) – origin quaternion
  - q2 (Quaternion) – target quaternion
  - s (number) – interpolation scalar
  - result (Quaternion) – result

  Returns: | spherical linear interpolation between `q1` and `q2`

`rotation`(v1, v2[, result])

  Note

  Not yet implemented

  Arguments:

  - v1 (Vector) – origin vector
  - v2 (Vector) – target vector
  - result (Quaternion) – result

  Returns: | quaternion of rotation between two 3-dimensional vectors

`toAxisAngle`(q[, result])

  Arguments:

  - q (Quaternion) – quaternion
  - result (Vector) – result

  Returns: | axis-angle representated as a 3-dimensional vector

`fromAxisAngle`(v[, result])

  Arguments:

  - v (Vector) – vector
  - result (Quaternion) – result

  Returns: | quaternion computed from axis-angle as a 3-dimensional vector

## Matrix

`multiply`(m1, m2[, result])

  Arguments:

  - m1 (Matrix) – first matrix
  - m2 (Matrix) – second matrix
  - result (Matrix) – result

  Returns: | the matrix product of `m1` with `m2`

`inverse`(m[, result])

  Note

  Valid for square matrices only

  Arguments:

  - m (Matrix) – square matrix
  - result (Matrix) – result

  Returns: | inverse of `m`

`transpose`(m[, result])

  Note

  Implemented for square matrices only

  Arguments:

  - m (Matrix) – matrix
  - result (Matrix) – result

  Returns: | transpose of `m`

`identity`(m)

  Note

  Valid for square matrices only

  Arguments:

  - m (Matrix) – square matrix

  Returns: | identity matrix

`identity`(d)

  Arguments:

  - d (number) – dimension

  Returns: | identity matrix of dimension `d`

`extract`(m, rowOffset, columnOffset, rows, columns[, result])

  Note

  Not yet implemented

  Arguments:

  - m (Matrix) – matrix
  - rowOffset (number) – starting row
  - columnOffset (number) – starting column
  - rows (number) – number of rows to extract
  - columns (number) – number of columns to extract
  - result (Matrix) – result

  Returns: | sub-matrix of `m`

## Matrix2

`multiply`(m1, m2[, result])

  Note

  Optimized

  Arguments:

  - m1 (Matrix) – first square matrix
  - m2 (Matrix) – second square matrix
  - result (Matrix) – result

  Returns: | the matrix product of `m1` with `m2`

`toAngle`(m)

  Arguments:

  - m (Matrix) – rotation matrix

  Returns: | angle in radians

`fromAngle`(angle[, result])

  Arguments:

  - angle (number) – angle in radians
  - result (Matrix) – result

  Returns: | 2-dimensional rotation matrix

## Matrix3

`multiply`(m1, m2[, result])

  Note

  Optimized

  Arguments:

  - m1 (Matrix) – first square matrix
  - m2 (Matrix) – second square matrix
  - result (Matrix) – result

  Returns: | the matrix product of `m1` with `m2`

`toQuaternion`(m[, result])

  Note

  Not yet implemented

  Arguments:

  - m (Matrix) – rotation matrix
  - result (Quaternion) – result

  Returns: | quaternion rotation

`fromQuaternion`(q[, result])

  Note

  Not yet implemented

  Arguments:

  - q (Quaternion) – quaternion
  - result (Matrix) – matrix

  Returns: | rotation matrix

`toAxisAngle`(m[, result])

  Note

  Not yet implemented

  Arguments:

  - m (Matrix) – matrix
  - result (Vector) – vector

  Returns: | axis-angle vector

`fromAxisAngle`(v[, result])

  Note

  Not yet implemented

  Arguments:

  - v (Vector) – axis-angle vector
  - result (Matrix) – result

  Returns: | rotation matrix

## Matrix4

`multiply`(m1, m2[, result])

  Note

  Optimized

  Arguments:

  - m1 (Matrix) – first square matrix
  - m2 (Matrix) – second square matrix
  - result (Matrix) – result

  Returns: | the matrix product of `m1` with `m2`

## Transform

`translate`(left, right[, result])

  Note

  One of the operands must be a Transform

  Arguments:

  - left – left operand
  - right – right operand
  - result (Transform) – result

  Returns: | transform with translation applied

  The translation should be expressed as a 3-dimensional vector.

`rotate`(left, right[, result])

  Note

  One of the operands must be a Transform

  Arguments:

  - left – left operand
  - right – right operand
  - result (Transform) – result

  Returns: | transform with rotation applied

  The rotation should be expressed as an angle or rotation matrix for 2 dimensions, or rotation matrix, axis-angle or quaternion for 3 dimensions.

`scale`(left, right[, result])

  Note

  One of the operands must be a Transform

  Arguments:

  - left – left operand
  - right – right operand
  - result (Transform) – result

  Returns: | transform with scaling applied

  The scaling should be expressed as a vector, with each component specifying the factor in the corresponding dimension.

`linear`(t[, result])

  Arguments:

  - t (Transform) – transform
  - result (Matrix) – result

  Returns: | linear part (rotation and scaling) of the affine transform

`svd`(t, translation, rotation, scaling)

  Arguments:

  - t (Transform) – transform
  - translation (Vector) – result vector to store translation
  - rotation (Quaternion) – result quaternion to store rotation
  - scaling (Vector) – result vector to store scaling

### Navigation

- index
- next |
- previous |
- MathJS 0.0.1 documentation »