fermat.js

High-performance mathematics utilities, random number generation, Complex number, Vector, Matrix and geometry classes, unit conversion and expression parsing.

Explore Source on GitHub Download boost.min.js

Introduction

Currently, fermat.js can only use JavaScript's native numbers, and is restricted to their size and precision. In the future, we want to create BigNum and arbitrary precision number classes that work with all functions below.

Arithmetic

nearlyEquals

Boolean nearlyEquals(Number x, Number y, [Number tolerance])

sign

Integer sign(Number x)

square

Number square(Number x)

cube

Number cube(Number x)

numberFormat

String numberFormat(Number x)

toOrdinal

String toOrdinal(Number x)

digits

Integer[] digits(Integer n)

fractionalDigits

Integer[] fractionalDigits(Number n)

decimalPlaces

Integer decimalPlaces(Number n)

round

Number round(Number n, [Integer precision])

roundTo

Number roundTo(Number n, [Number increment])

roundTowardsZero

Integer roundTowardsZero(Number n)

toFraction

[Number numerator, Integer denominator] toFraction(Number n, [Number precision])

mod

Number mod(Number a, Number m)

log

Number log(Number x, [Number b])

Number Theory

gcd

Integer gcd(Integer ...n)

lcm

Integer lcm(Integer ...n)

isPrime

Boolean isPrime(Integer n)

primeFactorisation

Integer[] primeFactorisation(Integer n)

primeFactors

Integer[] primeFactors(Integer n)

listPrimes

Integer[] listPrimes([Integer n])

Combinatorics

factorial

Number factorial(Number x)

binomial

Integer binomial(Integer n, Integer k)

permutations

Array[] permutations(Array a)

subsets

Array[] subsets(Array a)

coinFlips

String coinFlips([Integer n])

Probability

shuffle

Array shuffle(Array a)

integer

Integer integer(Integer a, [Integer b])

intArray

Integer[] intArray(Integer n) Returns a random array of consecutive integers from 0 to n-1 (inclusive)

weighted

Integer intArray(Number[] weights) String intArray({String key: Number weight} weights)

smart

Integer smart(Integer n, String id)

bernoulli

Integer bernoulli([Number p])

binomial

Integer binomial([Integer n, Number p])

poisson

Number poisson([Number l])

uniform

Number uniform([Number a, Number b])

normal

Number normal([Number mean, Number variance])

exponential

Number exponential([Number l])

geometric

Number geometric([Number p])

cauchy

Number cauchy()

normalPDF

Number normalPDF(Number x, [Number mean, Number variance])

Statistics

mean

Number mean(Number[] data)

median

Number median(Number[] data)

mode

Any median(Any[] data)

variance

Number variance(Number[] data)

stdDev

Number stdDev(Number[] data)

covariance

Number covariance(Number[] d1, Number[] d2)

correlation

Number correlation(Number[] d1, Number[] d2)

rSquared

Number rSquared(Number[] source, Number[] regression)

linearRegression

Function linearRegression(Number[] x, Number[] y)

Complex

Constructor

Constructor

new Complex([Number re, Number im])

Properties

.magnitude

Number .magnitude

.phase

Number .phase

.conjugate

Complex .conjugate

Methods

.toString

String .toString()

Static Methods

Complex.sum

Complex Complex.sum(Complex a, Complex b)

Complex.difference

Complex Complex.difference(Complex a, Complex b)

Complex.product

Complex Complex.product(Complex a, Complex b)

Complex.quotient

Complex Complex.quotient(Complex a, Complex b)

Vector

Constructor

Vector

new Vector()

Properties

.total

Number .total

.average

Number .average

.norm

Number .norm

.first

Number .first

.last

Number .last

.min

Number .min

.max

Number .max

.range

[Number, Number] .range

Methods

.map

Vector .map(Function fn)

.shift

Vector .shift(Number x)

.scale

Vector .scale(Number x)

.normalise

Vector .normalise()

.toString

String .toString()

Static Methods

Vector.equals

Boolean Vector.equals(Vector a, Vector b)

Vector.sum

Vector Vector.sum(Vector a, Vector b)

Vector.difference

Vector Vector.difference(Vector a, Vector b)

Vector.product

Vector Vector.product(Vector a, Vector b)

Vector.dot

Number Vector.dot(Vector a, Vector b)

Vector.cross

Vector Vector.cross(Vector a, Vector b)

Vector.cross2D

Number Vector.equals(Number[] a, Number[] b)

Matrix (alpha)

Constructors

Matrix

new Matrix()

Matrix.rotation

Matrix Matrix.rotation(Number angle)

Matrix.shear

Matrix Matrix.shear(Number angle)

Matrix.reflection

Matrix Matrix.reflection()

Matrix.projection

Matrix Matrix.rotation()

Properties

.isSquare

Boolean .isSquare

.row

Vector .row(Integer i)

.column

Vector .column(Integer j)

.transpose

Matrix .transpose

.inverse

Matrix .inverse

.determinant

Number .determinant

Methods

.scalarMultiply

Matrix .scalarMultiply(Number x)

Static Methods

Matrix.add

Matrix Matrix.add(Matrix a, Matrix b)

Matrix.product

Matrix Matrix.product((Matrix|Vector) a, (Matrix|Vector) b)

Geometry (alpha)

Shape

Properties

Methods

Point extends Shape

Point

new Point(Number x, Number y)

Properties

Methods

Point.fromPolar

Point.dot

Point.sum

Point.difference

Point.distance

Point.manhatten

Line extends Shape

Line

new Line(Point p1, Point p2)

Properties

Methods

Line.isParallel

Line.isPerpendicular

Line.angleBetween

Line.angleBisector

Bezier extends Shape

Bezier

new Bezier(Point p1, Point p2, [Point q1, Point q2])

Properties

Ellipse extends Shape

Ellipse

Circle extends Ellipse

Circle

new Circle([Point center, Number radius])

Properties

Rectangle extends Shape

Rectangle

new Rectangle([Point x, Point y, Number width, Number height])

Properties

Square extends Rectangle

Square

new Square([Point x, Point y, Number width])

Polygon extends Shape

Polygon

new Polygon(Point[] ...points)

Properties

Triangle extends Polygon

Triangle

new Triangle(Point[] ...points)

Properties

Utilities

angle

Number angle(Point a, Point b, Point c)

same

Boolean same(Shape x, Shape y)

intersect

Shape intersect(Shape x, Shape y)

convexHull

Polygon convexHull(Point[] ...points)

travellingSalesman

{ Integer[] path, Number length } travellingSalesman(Array distanceMatrix)

graphColouring

Integer[] graphColouring(Array adjacencyMatrix)

Cryptography

caesarCipher

String caesarCipher(String msg, Integer shift)

vigenereCipher

String vigenereCipher(String msg, String key)

letterFrequency

Number letterFrequency(String letter)

cipherLetterFreq

Integer[] cipherLetterFreq(String msg)

Numeric

bisect

Integer bisect(Function fn, [Integer precision, Number l, Number h]

more functions to come soon ...

Units

convert

Number convert(Number value, String fromUnit, [String toUnit])

define

String define(String unit)

Expression (alpha)

Constructor

Expression

new Expression(String str)

Methods

.simplify

Expression .simplify()

.evaluate

Any .evaluate([Object variables])

.toString

String .toString()

Bignum (alpha)

Constructor

Integer

new Integer((String|Integer) n, [Integer base])

Properties

.sqrt

.power

.mod

.factorial

Methods

.toString

.valueOf

.isUnit

.isZero

Static Methods

Integer.equals

Integer.lessOrEqual

Integer.greater

Integer.greaterOrEqual

Integer.add

Integer.subtract

Integer.multiply

Integer.divide

Integer.lcm

Integer.gcd