• ARTICLE
  • STRING
  • CONVERTER
  • ENCRYPT
  • NETWORK
  • MORE
    CHART
    MATH
    COORDINATE
    IMAGE
    FILE
  • ARTICLE
    STRING
    CONVERTER
    ENCRYPT
    NETWORK
    MORE
    CHART
    MATH
    COORDINATE
    IMAGE
    FILE
logo Online Tools

math.js documentation

  1. Index
    1. Operators
    2. Precedence
    3. Functions
    4. Constants
    5. Others
  2. Operators
    Operator Name Syntax Associativity Example Result
    (, ) Grouping (x) None 2 * (3 + 4) 14
    [, ] Matrix, Index [...] None [[1,2],[3,4]] [[1,2],[3,4]]
    {, } Object {...} None {a: 1, b: 2} (This tool does not support this operator) {a: 1, b: 2}
    , Parameter separator x, y Left to right max(2, 1, 5) 5
    . Property accessor obj.prop Left to right obj={a: 12}; obj.a 12
    ; Statement separator x; y Left to right a=2; b=3; a*b [6]
    ; Row separator [x; y] Left to right [1,2;3,4] [[1,2],[3,4]]
    \n Statement separator x \n y Left to right a=2 \n b=3 \n a*b [2,3,6]
    + Add x + y Left to right 4 + 5 9
    + Unary plus +y Right to left +4 4
    - Subtract x - y Left to right 7 - 3 4
    - Unary minus -y Right to left -4 -4
    * Multiply x * y Left to right 2 * 3 6
    .* Element-wise multiply x .* y Left to right [1,2,3] .* [1,2,3] [1,4,9]
    / Divide x / y Left to right 6 / 2 3
    ./ Element-wise divide x ./ y Left to right [9,6,4] ./ [3,2,2] [3,3,2]
    % Percentage x% None 8% 0.08
    % Addition with Percentage x + y% Left to right 100 + 3% 103
    % Subtraction with Percentage x - y% Left to right 100 - 3% 97
    %, mod Modulus x % y Left to right 8 % 3 2
    ^ Power x ^ y Right to left 2 ^ 3 8
    .^ Element-wise power x .^ y Right to left [2,3] .^ [3,3] [8,27]
    ' Transpose y' Left to right [[1,2],[3,4]]' [[1,3],[2,4]]
    ! Factorial y! Left to right 5! 120
    & Bitwise and x & y Left to right 5 & 3 1
    ~ Bitwise not ~x Right to left ~2 -3
    | Bitwise or x | y Left to right 5 | 3 7
    ^| Bitwise xor x ^| y Left to right 5 ^| 2 7
    << Left shift x << y Left to right 4 << 1 8
    >> Right arithmetic shift x >> y Left to right 8 >> 1 4
    >>> Right logical shift x >>> y Left to right -8 >>> 1 2147483644
    and Logical and x and y Left to right true and false false
    not Logical not not y Right to left not true false
    or Logical or x or y Left to right true or false true
    xor Logical xor x xor y Left to right true xor true false
    = Assignment x = y Right to left a = 5 5
    ? : Conditional expression x ? y : z Right to left 15 > 100 ? 1 : -1 -1
    : Range x : y Right to left 1:4 [1,2,3,4]
    to, in Unit conversion x to y Left to right 2 inch to cm 5.08 cm
    == Equal x == y Left to right 2 == 4 - 2 true
    != Unequal x != y Left to right 2 != 3 true
    < Smaller x < y Left to right 2 < 3 true
    > Larger x > y Left to right 2 > 3 false
    <= Smallereq x <= y Left to right 4 <= 3 false
    >= Largereq x >= y Left to right 2 + 4 >= 6 true
  3. Precedence
    The operators have the following precedence, from highest to lowest:
    Operators Description
    (...)
    [...]
    {...}
    Grouping
    Matrix
    Object
    x(...)
    x[...]
    obj.prop
    :
    Function call
    Matrix index
    Property accessor
    Key/value separator
    ' Matrix transpose
    ! Factorial
    ^, .^ Exponentiation
    +, -, ~, not Unary plus, unary minus, bitwise not, logical not
    %, mod percentage, modulus
    See section below Implicit multiplication
    *, /, .*, ./ Multiply, divide
    +, - Add, subtract
    : Range
    to, in Unit conversion
    <<, >>, >>> Bitwise left shift, bitwise right arithmetic shift, bitwise right logical shift
    ==, !=, <, >, <=, >= Relational
    & Bitwise and (lazily evaluated)
    ^| Bitwise xor
    | Bitwise or (lazily evaluated)
    and Logical and (lazily evaluated)
    xor Logical xor
    or Logical or (lazily evaluated)
    ?, : Conditional expression
    = Assignment
    , Parameter and column separator
    ; Row separator
    \n, ; Statement separators
  4. Functions
    When using a function in an expression, omit math. and use the function name directly. For example, abs(3).
    Function Description
    math.abs(x) Calculate the absolute value of a number.
    math.add(x, y) Add two or more values, x + y.
    math.cbrt(x [, allRoots]) Calculate the cubic root of a value.
    math.ceil(x) Round a value towards plus infinity If x is complex, both real and imaginary part are rounded towards plus infinity.
    math.cube(x) Compute the cube of a value, x * x * x.
    math.divide(x, y) Divide two values, x / y.
    math.dotDivide(x, y) Divide two matrices element wise.
    math.dotMultiply(x, y) Multiply two matrices element wise.
    math.dotPow(x, y) Calculates the power of x to y element wise.
    math.exp(x) Calculate the exponential of a value.
    math.expm1(x) Calculate the value of subtracting 1 from the exponential value.
    math.fix(x) Round a value towards zero.
    math.floor(x) Round a value towards minus infinity.
    math.gcd(a, b) Calculate the greatest common divisor for two or more values or arrays.
    math.hypot(a, b, …) Calculate the hypotenuse of a list with values.
    math.invmod(a, b) Calculate the (modular) multiplicative inverse of a modulo b.
    math.lcm(a, b) Calculate the least common multiple for two or more values or arrays.
    math.log(x [, base]) Calculate the logarithm of a value.
    math.log10(x) Calculate the 10-base logarithm of a value.
    math.log1p(x) Calculate the logarithm of a value+1.
    math.log2(x) Calculate the 2-base of a value.
    math.mod(x, y) Calculates the modulus, the remainder of an integer division.
    math.multiply(x, y) Multiply two or more values, x * y.
    math.norm(x [, p]) Calculate the norm of a number, vector or matrix.
    math.nthRoot(a) Calculate the nth root of a value.
    math.nthRoots(x) Calculate the nth roots of a value.
    math.pow(x, y) Calculates the power of x to y, x ^ y.
    math.round(x [, n]) Round a value towards the nearest rounded value.
    math.sign(x) Compute the sign of a value.
    math.sqrt(x) Calculate the square root of a value.
    math.square(x) Compute the square of a value, x * x.
    math.subtract(x, y) Subtract two values, x - y.
    math.unaryMinus(x) Inverse the sign of a value, apply a unary minus operation.
    math.unaryPlus(x) Unary plus operation.
    math.xgcd(a, b) Calculate the extended greatest common divisor for two values.
    math.bitAnd(x, y) Bitwise AND two values, x & y.
    math.bitNot(x) Bitwise NOT value, ~x.
    math.bitOr(x, y) Bitwise OR two values, x | y.
    math.bitXor(x, y) Bitwise XOR two values, x ^ y.
    math.leftShift(x, y) Bitwise left logical shift of a value x by y number of bits, x << y.
    math.rightArithShift(x, y) Bitwise right arithmetic shift of a value x by y number of bits, x >> y.
    math.rightLogShift(x, y) Bitwise right logical shift of value x by y number of bits, x >>> y.
    math.arg(x) Compute the argument of a complex value.
    math.conj(x) Compute the complex conjugate of a complex value.
    math.im(x) Get the imaginary part of a complex number.
    math.re(x) Get the real part of a complex number.
    math.and(x, y) Logical and.
    math.not(x) Logical not.
    math.or(x, y) Logical or.
    math.xor(x, y) Logical xor.
    math.column(value, index) Return a column from a Matrix.
    math.concat(a, b, c, … [, dim]) Concatenate two or more matrices.
    math.count(x) Count the number of elements of a matrix, array or string.
    math.cross(x, y) Calculate the cross product for two vectors in three dimensional space.
    math.ctranspose(x) Transpose and complex conjugate a matrix.
    math.det(x) Calculate the determinant of a matrix.
    math.diag(X) Create a diagonal matrix or retrieve the diagonal of a matrix When x is a vector, a matrix with vector x on the diagonal will be returned.
    math.diff(arr) Create a new matrix or array of the difference between elements of the given array The optional dim parameter lets you specify the dimension to evaluate the difference of If no dimension parameter is passed it is assumed as dimension 0 Dimension is zero-based in javascript and one-based in the parser and can be a number or bignumber Arrays must be ‘rectangular’ meaning arrays like [1, 2] If something is passed as a matrix it will be returned as a matrix but other than that all matrices are converted to arrays.
    math.dot(x, y) Calculate the dot product of two vectors.
    math.eigs(x, [prec]) Compute eigenvalues and optionally eigenvectors of a square matrix.
    math.expm(x) Compute the matrix exponential, expm(A) = e^A.
    math.fft(arr) Calculate N-dimensional Fourier transform.
    math.filter(x, test) Filter the items in an array or one dimensional matrix.
    math.flatten(x) Flatten a multidimensional matrix into a single dimensional matrix.
    math.forEach(x, callback) Iterate over all elements of a matrix/array, and executes the given callback function.
    math.getMatrixDataType(x) Find the data type of all elements in a matrix or array, for example ‘number’ if all items are a number and ‘Complex’ if all values are complex numbers.
    math.identity(n) Create a 2-dimensional identity matrix with size m x n or n x n.
    math.ifft(arr) Calculate N-dimensional inverse Fourier transform.
    math.inv(x) Calculate the inverse of a square matrix.
    math.kron(x, y) Calculates the Kronecker product of 2 matrices or vectors.
    math.map(x, callback) Create a new matrix or array with the results of a callback function executed on each entry of a given matrix/array.
    math.mapSlices(A, dim, callback) Apply a function that maps an array to a scalar along a given axis of a matrix or array.
    math.matrixFromColumns(…arr) Create a dense matrix from vectors as individual columns.
    math.matrixFromFunction(size, fn) Create a matrix by evaluating a generating function at each index.
    math.matrixFromRows(…arr) Create a dense matrix from vectors as individual rows.
    math.ones(m, n, p, …) Create a matrix filled with ones.
    math.partitionSelect(x, k) Partition-based selection of an array or 1D matrix.
    math.pinv(x) Calculate the Moore–Penrose inverse of a matrix.
    math.range(start, end [, step]) Create an array from a range.
    math.reshape(x, sizes) Reshape a multi dimensional array to fit the specified dimensions.
    math.resize(x, size [, defaultValue]) Resize a matrix.
    math.rotate(w, theta) Rotate a vector of size 1x2 counter-clockwise by a given angle Rotate a vector of size 1x3 counter-clockwise by a given angle around the given axis.
    math.rotationMatrix(theta) Create a 2-dimensional counter-clockwise rotation matrix (2x2) for a given angle (expressed in radians).
    math.row(value, index) Return a row from a Matrix.
    math.size(x) Calculate the size of a matrix or scalar.
    math.sort(x) Sort the items in a matrix.
    math.sqrtm(A) Calculate the principal square root of a square matrix.
    math.squeeze(x) Squeeze a matrix, remove inner and outer singleton dimensions from a matrix.
    math.subset(x, index [, replacement]) Get or set a subset of a matrix or string.
    math.trace(x) Calculate the trace of a matrix: the sum of the elements on the main diagonal of a square matrix.
    math.transpose(x) Transpose a matrix.
    math.zeros(m, n, p, …) Create a matrix filled with zeros.
    math.combinations(n, k) Compute the number of ways of picking k unordered outcomes from n possibilities.
    math.combinationsWithRep(n, k) Compute the number of ways of picking k unordered outcomes from n possibilities, allowing individual outcomes to be repeated more than once.
    math.factorial(n) Compute the factorial of a value Factorial only supports an integer value as argument.
    math.gamma(n) Compute the gamma function of a value using Lanczos approximation for small values, and an extended Stirling approximation for large values.
    math.kldivergence(x, y) Calculate the Kullback-Leibler (KL) divergence between two distributions.
    math.lgamma(n) Logarithm of the gamma function for real, positive numbers and complex numbers, using Lanczos approximation for numbers and Stirling series for complex numbers.
    math.multinomial(a) Multinomial Coefficients compute the number of ways of picking a1, a2, .
    math.permutations(n [, k]) Compute the number of ways of obtaining an ordered subset of k elements from a set of n elements.
    math.pickRandom(array) Random pick one or more values from a one dimensional array.
    math.random([min, max]) Return a random number larger or equal to min and smaller than max using a uniform distribution.
    math.randomInt([min, max]) Return a random integer number larger or equal to min and smaller than max using a uniform distribution.
    math.compare(x, y) Compare two values.
    math.compareNatural(x, y) Compare two values of any type in a deterministic, natural way.
    math.compareText(x, y) Compare two strings lexically.
    math.deepEqual(x, y) Test element wise whether two matrices are equal.
    math.equal(x, y) Test whether two values are equal.
    math.equalText(x, y) Check equality of two strings.
    math.larger(x, y) Test whether value x is larger than y.
    math.largerEq(x, y) Test whether value x is larger or equal to y.
    math.smaller(x, y) Test whether value x is smaller than y.
    math.smallerEq(x, y) Test whether value x is smaller or equal to y.
    math.unequal(x, y) Test whether two values are unequal.
    math.corr(A, B) Compute the correlation coefficient of a two list with values, For matrices, the matrix correlation coefficient is calculated.
    math.cumsum(a, b, c, …) Compute the cumulative sum of a matrix or a list with values.
    math.mad(a, b, c, …) Compute the median absolute deviation of a matrix or a list with values.
    math.max(a, b, c, …) Compute the maximum value of a matrix or a list with values.
    math.mean(a, b, c, …) Compute the mean value of matrix or a list with values.
    math.median(a, b, c, …) Compute the median of a matrix or a list with values.
    math.min(a, b, c, …) Compute the minimum value of a matrix or a list of values.
    math.mode(a, b, c, …) Computes the mode of a set of numbers or a list with values(numbers or characters).
    math.prod(a, b, c, …) Compute the product of a matrix or a list with values.
    math.quantileSeq(A, prob[, sorted]) Compute the prob order quantile of a matrix or a list with values.
    math.std(a, b, c, …) Compute the standard deviation of a matrix or a list with values.
    math.sum(a, b, c, …) Compute the sum of a matrix or a list with values.
    math.variance(a, b, c, …) Compute the variance of a matrix or a list with values.
    math.bin(value) Format a number as binary.
    math.format(value [, precision]) Format a value of any type into a string.
    math.hex(value) Format a number as hexadecimal.
    math.oct(value) Format a number as octal.
    math.print(template, values [, precision]) Interpolate values into a string template.
    math.acos(x) Calculate the inverse cosine of a value.
    math.acosh(x) Calculate the hyperbolic arccos of a value, defined as acosh(x) = ln(sqrt(x^2 - 1) + x).
    math.acot(x) Calculate the inverse cotangent of a value, defined as acot(x) = atan(1/x).
    math.acoth(x) Calculate the inverse hyperbolic tangent of a value, defined as acoth(x) = atanh(1/x) = (ln((x+1)/x) + ln(x/(x-1))) / 2.
    math.acsc(x) Calculate the inverse cosecant of a value, defined as acsc(x) = asin(1/x).
    math.acsch(x) Calculate the inverse hyperbolic cosecant of a value, defined as acsch(x) = asinh(1/x) = ln(1/x + sqrt(1/x^2 + 1)).
    math.asec(x) Calculate the inverse secant of a value.
    math.asech(x) Calculate the hyperbolic arcsecant of a value, defined as asech(x) = acosh(1/x) = ln(sqrt(1/x^2 - 1) + 1/x).
    math.asin(x) Calculate the inverse sine of a value.
    math.asinh(x) Calculate the hyperbolic arcsine of a value, defined as asinh(x) = ln(x + sqrt(x^2 + 1)).
    math.atan(x) Calculate the inverse tangent of a value.
    math.atan2(y, x) Calculate the inverse tangent function with two arguments, y/x.
    math.atanh(x) Calculate the hyperbolic arctangent of a value, defined as atanh(x) = ln((1 + x)/(1 - x)) / 2.
    math.cos(x) Calculate the cosine of a value.
    math.cosh(x) Calculate the hyperbolic cosine of a value, defined as cosh(x) = 1/2 * (exp(x) + exp(-x)).
    math.cot(x) Calculate the cotangent of a value.
    math.coth(x) Calculate the hyperbolic cotangent of a value, defined as coth(x) = 1 / tanh(x).
    math.csc(x) Calculate the cosecant of a value, defined as csc(x) = 1/sin(x).
    math.csch(x) Calculate the hyperbolic cosecant of a value, defined as csch(x) = 1 / sinh(x).
    math.sec(x) Calculate the secant of a value, defined as sec(x) = 1/cos(x).
    math.sech(x) Calculate the hyperbolic secant of a value, defined as sech(x) = 1 / cosh(x).
    math.sin(x) Calculate the sine of a value.
    math.sinh(x) Calculate the hyperbolic sine of a value, defined as sinh(x) = 1/2 * (exp(x) - exp(-x)).
    math.tan(x) Calculate the tangent of a value.
    math.tanh(x) Calculate the hyperbolic tangent of a value, defined as tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1).
  5. Constants
    Constant Description Value
    e, E Euler’s number, the base of the natural logarithm. 2.718281828459045
    i Imaginary unit, defined as i * i = -1. A complex number is described as a + b * i, where a is the real part, and b is the imaginary part. sqrt(-1)
    Infinity Infinity, a number which is larger than the maximum number that can be handled by a floating point number. Infinity
    LN2 Returns the natural logarithm of 2. 0.6931471805599453
    LN10 Returns the natural logarithm of 10. 2.302585092994046
    LOG2E Returns the base-2 logarithm of E. 1.4426950408889634
    LOG10E Returns the base-10 logarithm of E. 0.4342944819032518
    NaN Not a number. NaN
    null Value null. null
    phi Phi is the golden ratio. Two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Phi is defined as (1 + sqrt(5)) / 2 1.618033988749895
    pi, PI The number pi is a mathematical constant that is the ratio of a circle's circumference to its diameter. 3.141592653589793
    SQRT1_2 Returns the square root of 1/2. 0.7071067811865476
    SQRT2 Returns the square root of 2. 1.4142135623730951
    tau Tau is the ratio constant of a circle's circumference to radius, equal to 2 * pi. 6.283185307179586
    undefined An undefined value. Preferably, use null to indicate undefined values. undefined
    version Returns the version number of math.js. For example 0.24.1
  6. For more documentation about expression syntax, please refer to the Syntax, Data Type, Function & Parameter Description & Example .