fermat.js

Powerful mathematics, statistics and geometry library, containing everything from number theory to random numbers, expression parsing and linear algebra classes.

Arithmetic

nearlyEquals(x, y, t) : boolean

Checks if two numbers are nearly equals.

Kind: global function

Param Type Description
x number
y number
t number The allowed tolerance

isBetween(x, a, b, t) : boolean

Checks if a number x is between two numbers a and b.

Kind: global function

Param Type
x number
a number
b number
t number

sign(x, t) : number

Returns the sign of a number x, as +1, 0 or –1.

Kind: global function

Param Type
x number
t number

numberFormat(x) : string

Adds thousand separators to a number.

Kind: global function

Param Type
x number

toOrdinal(x) : string

Converts a number to an ordinal.

Kind: global function

Param Type
x number

toWord(n) : string

Spells a number as an English word.

Kind: global function

Param Type
n number

digits(n) : Array.<number>

Returns the digits of a number n.

Kind: global function

Param Type
n number

fractionalDigits(n) : Array.<number>

Returns the decimal digits of a number n.

Kind: global function

Param Type
n number

decimalPlaces(n) : number

Returns the number of decimal places in a number n.

Kind: global function

Param Type
n number

round(n, precision) : number

Rounds a number n to precision decimal places.

Kind: global function

Param Type
n number
precision number

roundTo(n, increment) : number

Round a number n to the nearest multiple of increment.

Kind: global function

Param Type
n number
increment number

toFraction(decimal, precision) : Array.<number>

Returns an [numerator, denominator] array that approximated a decimal to precision. See http://en.wikipedia.org/wiki/Continued_fraction

Kind: global function

Param Type
decimal number
precision number

mod(a, m) : number

Calculates a mod m. The JS implementation of the % operator returns the symmetric modulo. Both are identical if a >= 0 and m >= 0 but the results differ if a or m < 0.

Kind: global function

Param Type
a number
m number

log(x, b) : number

Calculates the logarithm of x with base b.

Kind: global function

Param Type
x number
b number

Combinatorics

factorial(x) : number

Calculates the factorial of a number x.

Kind: global function

Param Type
x number

binomial(n, k) : number

Calculates the binomial coefficient nCk of two numbers n and k.

Kind: global function

Param Type
n number
k number

permutations(arr) : Array

Returns an array of all possible permutations of an input array arr. In this implementation, we always have permutations(arr)[0] == arr.

Kind: global function

Param Type
arr Array

subsets(arr, length) : Array

Returns an array of all possible subsets (of given length) of an input array arr.

Kind: global function

Param Type
arr Array
length number

coinFlips(n) : string

Returns a string of n coin flips like 'HTTHTHTTHTT'.

Kind: global function

Param Type
n number

Complex

Complex

Complex number class.

Kind: global class


complex.magnitude : number

Kind: instance property of Complex


complex.phase : number

Kind: instance property of Complex


complex.conjugate : Complex

Kind: instance property of Complex


complex.toString() : string

Kind: instance method of Complex


Complex.sum(c1, c2) : Complex

Calculates the sum of two complex numbers c1 and c2.

Kind: static method of Complex

Param Type
c1 Complex
c2 Complex

Complex.difference(c1, c2) : Complex

Calculates the difference of two complex numbers c1 and c2.

Kind: static method of Complex

Param Type
c1 Complex
c2 Complex

Complex.product(c1, c2) : Complex

Calculates the product of two complex numbers c1 and c2.

Kind: static method of Complex

Param Type
c1 Complex
c2 Complex

Complex.quotient(c1, c2) : Complex

Calculates the sum of two quotient numbers c1 and c2.

Kind: static method of Complex

Param Type
c1 Complex
c2 Complex

Cryptography

caesarCipher(msg, shift) : string

Enciphers a string by shifting every letter by a certain offset through the alphabet.

Kind: global function

Param Type
msg string
shift number

vigenereCipher(msg, key) : string

Enciphers a string using a Vigenere cipher with a give key.

Kind: global function

Param Type
msg string
key string

letterFrequency(letter) : number

Returns the relative frequency of a given letter in the English language.

Kind: global function

Param Type
letter string

cipherLetterFreq(cipher) : Array.<number>

Counts how often every letter occurs in a given cipher.

Kind: global function

Param Type
cipher str

Expression

Expression

Parsed mathematical expression class.

Kind: global class


expression.simplified : Expression

Kind: instance property of Expression


expression.functions : Array.<string>

Kind: instance property of Expression


expression.variables : Array.<string>

Kind: instance property of Expression


expression.toString() : string

Kind: instance method of Expression


expression.same(other) : boolean

Checks if two expressions are exactly the same.

Kind: instance method of Expression

Param Type
other Expression

expression.equals(other) : boolean

Checks if two expressions are mathematically equivalent.

Kind: instance method of Expression

Param Type
other Expression

expression.evaluate(vars) : number

Evaluates this expression with a given set of variable values.

Kind: instance method of Expression
Throws:

  • ExprError
Param
vars

expression.numEquals(other) : boolean

Numerically checks if two expressions are mathematically equal.

Kind: instance method of Expression

Param Type
other Expression

expression.substitute(vars) : Expression

Substitutes certain variables in an expression.

Kind: instance method of Expression

Param Type
vars Object

Geometry

Point

A single point class defined by two coordinates x and y.

Kind: global class


point.x : number

Kind: instance property of Point


point.y : number

Kind: instance property of Point


point.normal : Point

Kind: instance property of Point


point.length : number

Kind: instance property of Point


point.inverse : Point

Kind: instance property of Point


point.flip : Point

Kind: instance property of Point


point.array : Array.<number>

Kind: instance property of Point


point.distanceFromLine(l) : number

Calculates the shortest distance between this point and a line l.

Kind: instance method of Point

Param Type
l Line

point.clamp(xMin, xMax, yMin, yMax) : Point

Clamps this point between given x and y values.

Kind: instance method of Point

Param Type
xMin number
xMax number
yMin number
yMax number

point.transform(m) : Point

Transforms this point using a 2x3 matrix m.

Kind: instance method of Point

Param Type
m Array.<Array.<number>>

point.rotate(angle, c) : Point

Rotates this point by a given angle (in radians) around c.

Kind: instance method of Point

Param Type
angle number
c Point

point.reflect(l) : Point

Reflects this point across a line l.

Kind: instance method of Point

Param Type
l Line

Point.average(...points) : Point

Calculates the average of multiple points.

Kind: static method of Point

Param Type
...points Point

Point.dot(p1, p2) : number

Calculates the dot product of two points p1 and p2.

Kind: static method of Point

Param Type
p1 Point
p2 Point

Point.distance(p1, p2) : number

Returns the Euclidean distance between two points p1 and p2.

Kind: static method of Point

Param Type
p1 Point
p2 Point

Point.manhattan(p1, p2) : number

Returns the Manhattan distance between two points p1 and p2.

Kind: static method of Point

Param Type
p1 Point
p2 Point

Point.interpolate(p1, p2, t) : Point

Interpolates two points p1 and p2 by a factor of t.

Kind: static method of Point

Param Type Default
p1 Point
p2 Point
t number 0.5

Point.equals(p1, p2) : boolean

Checks if two points p1 and p2 are equal.

Kind: static method of Point

Param Type
p1 Point
p2 Point

Angle

A 2-dimensional angle class, defined by three points.

Kind: global class


angle.rad : number

The size, in radians, of this angle.

Kind: instance property of Angle


angle.deg : number

The size, in degrees, of this angle.

Kind: instance property of Angle


angle.size : number

The smaller size of this angle, in radians, between 0 and Pi.

Kind: instance property of Angle


angle.isRight : boolean

Checks if this angle is right-angled.

Kind: instance property of Angle


angle.bisector : Line

The bisector of this angle.

Kind: instance property of Angle


Line

An infinite straight line that goes through two points.

Kind: global class


line.p1 : Point

Kind: instance property of Line


line.p2 : Point

Kind: instance property of Line


line.midpoint : Point

The midpoint of this line.

Kind: instance property of Line


line.perpBisector : Line

The perpendicular bisector of this line.

Kind: instance property of Line


line.parallel(p) : Line

Finds the line parallel to this one, going though point p.

Kind: instance method of Line

Param Type
p Point

line.perpendicular(p) : Line

Finds the line perpendicular to this one, going though point p.

Kind: instance method of Line

Param Type
p Point

line.contains(p) : boolean

Checks if a point p lies on this line.

Kind: instance method of Line

Param Type
p Point

line.project(p) : Point

Projects a point p onto this line.

Kind: instance method of Line

Param Type
p Point

Line.equals(l1, l2) : boolean

Checks if two lines l1 and l2 are equal.

Kind: static method of Line

Param Type
l1 Line
l2 Line

Segment

A finite line segment defined by its two endpoints.

Kind: global class


Segment.equals(l1, l2, [oriented]) : boolean

Checks if two line segments l1 and l2 are equal.

Kind: static method of Segment

Param Type Default Description
l1 Line
l2 Line
[oriented] boolean false Make true of the orientation matters.

Segment.intersect(l1, l2) : Point

Finds the intersection of two line segments l1 and l2 (or null).

Kind: static method of Segment

Param Type
l1 Line
l2 Line

Circle

A circle with a given center and radius.

Kind: global class


circle.c : Point

Kind: instance property of Circle


circle.r : number

Kind: instance property of Circle


circle.circumference : number

The length of the circumference of this circle.

Kind: instance property of Circle


circle.area : number

The area of this circle.

Kind: instance property of Circle


Arc

An arc segment of a circle, with given center, start point and angle.

Kind: global class


Polygon

A polygon defined by its vertex points.

Kind: global class


polygon.points : Array.<Point>

Kind: instance property of Polygon


polygon.circumference : number

Kind: instance property of Polygon


polygon.signedArea : number

The (signed) area of this polygon. The result is positive if the vertices are ordered clockwise, and negative otherwise.

Kind: instance property of Polygon


polygon.area : number

Kind: instance property of Polygon


polygon.centroid : Point

Kind: instance property of Polygon


polygon.edges : Array.<Segment>

Kind: instance property of Polygon


polygon.oriented : Polygon

The oriented version of this polygon (vertices in clockwise order).

Kind: instance property of Polygon


polygon.contains(p) : boolean

Checks if a point p lies inside this polygon.

Kind: instance method of Polygon

Param Type
p Point

polygon.intersect(polygon) : Polygon

The intersection of this and another polygon, calculated using the Weiler–Atherton clipping algorithm

Kind: instance method of Polygon

Param Type
polygon Polygon

Polygon.collision(p1, p2) : boolean

Checks if two polygons p1 and p2 collide.

Kind: static method of Polygon

Param Type
p1 Polygon
p2 Polygon

Polyline

A polyline defined by its vertex points.

Kind: global class


polyline.edges : Array.<Segment>

Kind: instance property of Polyline


Triangle

A triangle defined by its three vertices.

Kind: global class


triangle.circumcircle : Circle

Kind: instance property of Triangle


triangle.incircle : Circle

Kind: instance property of Triangle


triangle.orthocenter : Point

Kind: instance property of Triangle


Rectangle

A rectangle, defined by its top left vertex, width and height.

Kind: global class


rectangle.p : Point

Kind: instance property of Rectangle


rectangle.w : number

Kind: instance property of Rectangle


rectangle.h : number

Kind: instance property of Rectangle


rectangle.center : Point

Kind: instance property of Rectangle


rectangle.circumference : number

Kind: instance property of Rectangle


rectangle.area : number

Kind: instance property of Rectangle


rectangle.edges : Array.<Segment>

Kind: instance property of Rectangle


rectangle.polygon : Polygon

A polygon class representing this rectangle.

Kind: instance property of Rectangle


Square

A square, defined by its top left vertex and size.

Kind: global class


intersections(...elements) : *

Returns the intersection of two or more geometry objects.

Kind: global function

Param Type
...elements *

Matrix

matrix


matrix.identity(n) : Array.<Array.<number>>

Returns the identity matrix of size n.

Kind: static method of matrix

Param Type
n number

matrix.fill(x, y, value) : Array.<Array.<number>>

Fills a matrix of size x, y with a given value.

Kind: static method of matrix

Param Type
x number
y number
value number

matrix.sum(...matrices) : Array.<Array.<number>>

Calculates the sum of multiple matrices.

Kind: static method of matrix

Param Type
...matrices Array.<Array.<number>>

matrix.scalarProduct(M, v) : Array.<Array.<number>>

Multiplies a matrix M by a scalar v.

Kind: static method of matrix

Param Type
M Array.<Array.<number>>
v number

matrix.product(...matrices) : Array.<Array.<number>>

Calculates the matrix product of multiple matrices.

Kind: static method of matrix

Param Type
...matrices Array.<Array.<number>>

matrix.transpose(M) :

Calculates the transpose of a matrix M.

Kind: static method of matrix
Returns: Array.<number[]>

Param Type
M Array.<Array.<number>>

matrix.determinant(M) :

Calculates the determinant of a matrix M.

Kind: static method of matrix
Returns: number

Param Type
M Array.<Array.<number>>

matrix.inverse(M) :

Calculates the inverse of a matrix M.

Kind: static method of matrix
Returns: Array.<number[]>

Param Type
M Array.<Array.<number>>

Number Theory

gcd(...numbers) : number

Calculates the greatest common divisor of multiple numbers.

Kind: global function

Param Type
...numbers number

lcm(...numbers) : number

Calculates the lowest common multiple of multiple numbers.

Kind: global function

Param Type
...numbers number

isPrime(n) : boolean

Checks if a number n is prime. Contains no dependencies, so that this function can easily be stringified and run in a web worker.

Kind: global function

Param Type
n number

primeFactorisation(n) : Array.<number>

Finds the prime factorisation of a number n.

Kind: global function

Param Type
n number

primeFactors(n) : Array.<number>

Finds all prime factors of a number n.

Kind: global function

Param Type
n number

listPrimes(n) : Array.<number>

Lists all prime numbers between 0 and n.

Kind: global function

Param Type
n number

generatePrime(d) : number

Generates a random prime number with d digits, where 2 <= d <= 16. Contains no dependencies, so that this function can easily be stringified and run in a web worker.

Kind: global function

Param Type
d number

goldbach(x) : Array.<number>

Tries to write a number x as the sum of two primes. Contains no dependencies, so that this function can easily be stringified and run in a web worker.

Kind: global function

Param Type
x number

Numeric

bisect(fn, precision, l, h) : number

Uses interval bisection to find a solution to fn(x) = 0. For example, bisect(function(x){ return Math.cos(x/2); }, 10) => Pi

Kind: global function

Param Type
fn function
precision number
l number
h number

integrate(fn, xMin, xMax, dx) : number

Riemann-integrates a function fn from xMin to xMax, with an interval size dx.

Kind: global function

Param Type
fn function
xMin number
xMax number
dx number

Random

random


random.shuffle(a) : Array

Randomly shuffles the elements in an array a.

Kind: static method of random

Param Type
a Array

random.integer(a, b) : number

Generates a random integer between 0 and a, or between a and b.

Kind: static method of random

Param Type
a number
b number

random.intArray(n) : Array.<number>

Generates an array of the integers from 0 to n in random order.

Kind: static method of random

Param Type
n number

random.weighted(obj) : *

Chooses a random value from weights [2, 5, 3] or { a: 2, b: 5, c: 3 }.

Kind: static method of random

Param Type
obj Object | Array

random.smart(n, id) : number

Returns a random number between 0 and n, but avoids returning the same number multiple times in a row.

Kind: static method of random

Param Type
n number
id string

random.bernoulli(p) : number

Generates a Bernoulli random variable.

Kind: static method of random

Param Type
p number

random.binomial(n, p) : number

Generates a Binomial random variable.

Kind: static method of random

Param Type
n number
p number

random.poisson(l) : number

Generates a Poisson random variable.

Kind: static method of random

Param Type
l number

random.uniform(a, b) : number

Generates a uniform random variable.

Kind: static method of random

Param Type
a number
b number

random.normal(m, v) : number

Generates a normal random variable with mean m and variance v.

Kind: static method of random

Param Type
m number
v number

random.exponential(l) : number

Generates an exponential random variable.

Kind: static method of random

Param Type
l number

random.geometric(p) : number

Generates a geometric random variable.

Kind: static method of random

Param Type
p number

random.cauchy() : number

Generates an Cauchy random variable.

Kind: static method of random


random.normalPDF(x, m, v) : number

Generates pdf(x) for the normal distribution with mean m and variance v.

Kind: static method of random

Param Type
x number
m number
v number

random.chiCDF(chi, deg) : number

The chi CDF function.

Kind: static method of random

Param Type
chi number
deg number

Regression

regression


regression.linear(data) : Array.<number>

Finds a linear regression that best approximates a set of data. The result will be an array [c, m], where y = m * x + c.

Kind: static method of regression

Param Type
data Array.<Array.<number>>

regression.linearThroughOrigin(data) : Array.<number>

Finds a linear regression going through the origin, that best approximates a set of data. The result will be an array [0, m], where y = m * x.

Kind: static method of regression

Param Type
data Array.<Array.<number>>

regression.exponential(data) : Array.<number>

Finds an exponential regression that best approximates a set of data. The result will be an array [a, b], where y = a * e^(bx).

Kind: static method of regression

Param Type
data Array.<Array.<number>>

regression.logarithmic(data) : Array.<number>

Finds a logarithmic regression that best approximates a set of data. The result will be an array [a, b], where y = a + b * log(x).

Kind: static method of regression

Param Type
data Array.<Array.<number>>

regression.power(data) : Array.<number>

Finds a power regression that best approximates a set of data. The result will be an array [a, b], where y = a * x^b.

Kind: static method of regression

Param Type
data Array.<Array.<number>>

regression.polynomial(data, order) : Array.<number>

Finds a polynomial regression of given order that best approximates a set of data. The result will be an array giving the coefficients of the resulting polynomial.

Kind: static method of regression

Param Type
data Array.<Array.<number>>
order number

regression.coefficient(data, fn) : number

Finds the regression coefficient of a given data set and regression function.

Kind: static method of regression

Param Type
data Array.<Array.<number>>
fn function

regression.find(data, threshold) : Object

Finds the most suitable

Kind: static method of regression

Param Type
data Array.<Array.<number>>
threshold number

Statistics

mean(values) : number

Calculates the mean of an array of numbers.

Kind: global function

Param Type
values Array.<number>

median(values) : number

Calculates the median of an array of numbers.

Kind: global function

Param Type
values Array.<number>

mode(values) : number

Calculates the mode of an array of numbers. Returns null if no mode exists, i.e. there are multiple values with the same largest count.

Kind: global function

Param Type
values Array.<number>

variance(values) : number

Calculates the variance of an array of numbers.

Kind: global function

Param Type
values Array.<number>

stdDev(values) : number

Calculates the standard deviation of an array of numbers.

Kind: global function

Param Type
values Array.<number>

covariance(aX, aY) : number

Calculates the covariance of the numbers in two arrays aX and aY.

Kind: global function

Param Type
aX Array.<number>
aY Array.<number>

correlation(aX, aY) : number

Calculates the correlation between the numbers in two arrays aX and aY.

Kind: global function

Param Type
aX Array.<number>
aY Array.<number>

Vector

Vector

A n-dimensional Vector class.

Kind: global class


vector.length : number

Kind: instance property of Vector


vector.magnitude : number

Kind: instance property of Vector


vector.normal : Vector

Kind: instance property of Vector


vector.scale(q) : Vector

Scales this vector by a factor q.

Kind: instance method of Vector

Param Type
q number

vector.map(fn) : Vector

Kind: instance method of Vector

Param Type
fn function

Vector.fill(n, value) : Vector

Creates an n-dimensional vector filled with value.

Kind: static method of Vector

Param Type
n number
value

Vector.sum(v1, v2) : Vector

Calculates the sum of two vectors v1 and v2.

Kind: static method of Vector

Param Type
v1 Vector
v2 Vector

Vector.difference(v1, v2) : Vector

Calculates the difference of two vectors v1 and v2.

Kind: static method of Vector

Param Type
v1 Vector
v2 Vector

Vector.product(v1, v2) : Vector

Calculates the element-wise product of two vectors v1 and v2.

Kind: static method of Vector

Param Type
v1 Vector
v2 Vector

Vector.dot(v1, v2) : number

Calculates the dot product of two vectors v1 and v2.

Kind: static method of Vector

Param Type
v1 Vector
v2 Vector

Vector.cross(v1, v2) : Vector

Finds the cross product of two 3-dimensional vectors v1 and v2.

Kind: static method of Vector

Param Type
v1 Vector
v2 Vector

Vector.equals(v1, v2) : boolean

Checks if two vectors are equal.

Kind: static method of Vector

Param Type
v1 Vector
v2 Vector