Math.js is an extensive math library for JavaScript and Node.js featuring a flexible expression parser, symbolic computation, and support for numbers, big numbers, complex numbers, fractions, units, and matrices.
84
This document covers all trigonometric functions in Math.js, including basic trigonometric functions, inverse functions, hyperbolic functions, and their inverses. All functions support complex numbers, arrays, and matrices.
import {
// Basic trigonometric
sin, cos, tan, csc, sec, cot,
// Inverse trigonometric
asin, acos, atan, atan2, acsc, asec, acot,
// Hyperbolic
sinh, cosh, tanh, csch, sech, coth,
// Inverse hyperbolic
asinh, acosh, atanh, acsch, asech, acoth,
// Constants
pi, e
} from 'mathjs'All trigonometric functions expect angles in radians by default. Use unit() for degree input.
sin(x: MathType): MathType{ .api }
sin(0) // 0
sin(pi / 6) // 0.5
sin(pi / 2) // 1
sin(pi) // ~0 (floating point approximation)
// With degrees using units
sin(unit('30 deg')) // 0.5
sin(unit('90 deg')) // 1
// Complex numbers
sin(complex(0, 1)) // Complex(0, 1.175...) = i * sinh(1)
// Arrays and matrices (element-wise)
sin([0, pi/6, pi/4, pi/3, pi/2])
// [0, 0.5, 0.707..., 0.866..., 1]cos(x: MathType): MathType{ .api }
cos(0) // 1
cos(pi / 3) // 0.5
cos(pi / 2) // ~0
cos(pi) // -1
// With degrees
cos(unit('60 deg')) // 0.5
cos(unit('90 deg')) // ~0
// Complex numbers
cos(complex(1, 0)) // Complex(0.540..., 0)tan(x: MathType): MathType{ .api }
tan(0) // 0
tan(pi / 4) // 1
tan(pi / 3) // √3 ≈ 1.732
// Undefined at π/2, 3π/2, etc.
tan(pi / 2) // Infinity (or very large number)
// With degrees
tan(unit('45 deg')) // 1csc(x: MathType): MathType // 1 / sin(x)
sec(x: MathType): MathType // 1 / cos(x)
cot(x: MathType): MathType // cos(x) / sin(x){ .api }
csc(pi / 6) // 2 (since sin(π/6) = 0.5)
sec(pi / 3) // 2 (since cos(π/3) = 0.5)
cot(pi / 4) // 1 (since tan(π/4) = 1)
// Undefined where denominator is zero
csc(0) // Infinity (sin(0) = 0)
sec(pi / 2) // Infinity (cos(π/2) = 0)
cot(0) // Infinity (sin(0) = 0)Inverse functions return angles in radians. Results are in the principal value range.
asin(x: MathType): MathType{ .api }
asin(0) // 0
asin(0.5) // π/6 ≈ 0.524
asin(1) // π/2 ≈ 1.571
asin(-1) // -π/2 ≈ -1.571
// Domain: [-1, 1], Range: [-π/2, π/2]
asin(2) // Complex result: Complex(1.571..., -1.317...)
// Complex input
asin(complex(2, 0)) // Complex(1.571..., -1.317...)acos(x: MathType): MathType{ .api }
acos(1) // 0
acos(0.5) // π/3 ≈ 1.047
acos(0) // π/2 ≈ 1.571
acos(-1) // π ≈ 3.142
// Domain: [-1, 1], Range: [0, π]
acos(2) // Complex resultatan(x: MathType): MathType{ .api }
atan(0) // 0
atan(1) // π/4 ≈ 0.785
atan(Infinity) // π/2 ≈ 1.571
atan(-Infinity) // -π/2 ≈ -1.571
// Domain: (-∞, ∞), Range: (-π/2, π/2)
atan(complex(0, 1)) // Complex resultatan2(y: MathType, x: MathType): MathType{ .api }
// Returns angle from x-axis to point (x, y)
atan2(1, 1) // π/4 ≈ 0.785 (45 degrees)
atan2(1, 0) // π/2 (90 degrees)
atan2(0, 1) // 0 (0 degrees)
atan2(-1, -1) // -3π/4 ≈ -2.356 (-135 degrees)
// Handles all quadrants correctly
// Range: (-π, π]acsc(x: MathType): MathType // asin(1/x)
asec(x: MathType): MathType // acos(1/x)
acot(x: MathType): MathType // atan(1/x) or π/2 - atan(x){ .api }
acsc(2) // π/6 (since csc(π/6) = 2)
asec(2) // π/3 (since sec(π/3) = 2)
acot(1) // π/4 (since cot(π/4) = 1)
// Domain restrictions
acsc(0.5) // Error: |x| must be >= 1
asec(0.5) // Error: |x| must be >= 1Hyperbolic functions are analogs of trigonometric functions based on the hyperbola rather than the circle.
sinh(x: MathType): MathType // (e^x - e^(-x)) / 2{ .api }
sinh(0) // 0
sinh(1) // (e - 1/e) / 2 ≈ 1.175
sinh(-1) // -(e - 1/e) / 2 ≈ -1.175
// Relationship to trig functions
sinh(complex(0, pi/2)) // Complex(0, 1) = icosh(x: MathType): MathType // (e^x + e^(-x)) / 2{ .api }
cosh(0) // 1
cosh(1) // (e + 1/e) / 2 ≈ 1.543
cosh(-1) // (e + 1/e) / 2 ≈ 1.543 (even function)
// Always >= 1 for real numbers
cosh(complex(pi/2, 0)) // Complex(0, 0) ≈ 0 + 0itanh(x: MathType): MathType // sinh(x) / cosh(x){ .api }
tanh(0) // 0
tanh(Infinity) // 1
tanh(-Infinity) // -1
// Sigmoid-like function: Range (-1, 1)
tanh(1) // (e^2 - 1) / (e^2 + 1) ≈ 0.762csch(x: MathType): MathType // 1 / sinh(x)
sech(x: MathType): MathType // 1 / cosh(x)
coth(x: MathType): MathType // cosh(x) / sinh(x){ .api }
csch(1) // 1 / sinh(1) ≈ 0.851
sech(0) // 1 / cosh(0) = 1
coth(1) // cosh(1) / sinh(1) ≈ 1.313
// Undefined where denominator is zero
csch(0) // Infinity (sinh(0) = 0)
coth(0) // Infinity (sinh(0) = 0)asinh(x: MathType): MathType // ln(x + sqrt(x^2 + 1)){ .api }
asinh(0) // 0
asinh(1) // ln(1 + √2) ≈ 0.881
asinh(-1) // -ln(1 + √2) ≈ -0.881
// Domain: (-∞, ∞), Range: (-∞, ∞)
asinh(sinh(5)) // 5 (inverse property)acosh(x: MathType): MathType // ln(x + sqrt(x^2 - 1)){ .api }
acosh(1) // 0
acosh(2) // ln(2 + √3) ≈ 1.317
// Domain: [1, ∞), Range: [0, ∞)
acosh(0.5) // Complex result (outside domain)
acosh(cosh(3)) // 3 (inverse property)atanh(x: MathType): MathType // 0.5 * ln((1 + x) / (1 - x)){ .api }
atanh(0) // 0
atanh(0.5) // 0.5 * ln(3) ≈ 0.549
// Domain: (-1, 1), Range: (-∞, ∞)
atanh(1) // Infinity
atanh(-1) // -Infinity
atanh(2) // Complex result (outside domain)acsch(x: MathType): MathType // asinh(1/x)
asech(x: MathType): MathType // acosh(1/x)
acoth(x: MathType): MathType // atanh(1/x){ .api }
acsch(1) // asinh(1) ≈ 0.881
asech(0.5) // acosh(2) ≈ 1.317
acoth(2) // atanh(0.5) ≈ 0.549
// Domain restrictions apply based on underlying functions
acsch(0) // Infinity
asech(0) // Infinity
asech(2) // Complex result (1/x not in [0,1])All trigonometric functions support complex numbers using standard mathematical definitions:
// Euler's formula: e^(ix) = cos(x) + i*sin(x)
sin(complex(0, 1)) // i * sinh(1) ≈ Complex(0, 1.175)
cos(complex(0, 1)) // cosh(1) ≈ Complex(1.543, 0)
// Complex argument
const z = complex(1, 2) // 1 + 2i
sin(z) // Complex result
cos(z) // Complex result
// Inverse functions with complex results
asin(2) // Complex(π/2, -ln(2 + √3))
acos(2) // Complex(0, ln(2 + √3))Math.js can work with angular units:
import { unit, sin, cos, evaluate } from 'mathjs'
// Using degree units
sin(unit('30 deg')) // 0.5
cos(unit('60 deg')) // 0.5
tan(unit('45 deg')) // 1
// Converting between radians and degrees
const angle = unit('π/4 rad')
unit(angle, 'deg') // 45 deg
// In expressions
evaluate('sin(30 deg)') // 0.5
evaluate('cos(π/3 rad)') // 0.5All trigonometric functions work element-wise on arrays and matrices:
// Arrays
const angles = [0, pi/6, pi/4, pi/3, pi/2]
sin(angles) // [0, 0.5, 0.707..., 0.866..., 1]
cos(angles) // [1, 0.866..., 0.707..., 0.5, 0]
// Matrices
const angleMatrix = [[0, pi/4], [pi/3, pi/2]]
sin(angleMatrix) // [[0, 0.707...], [0.866..., 1]]
// Complex matrices
const complexMatrix = [[complex(1, 1), complex(0, 1)]]
sin(complexMatrix) // Element-wise complex sineMath.js respects mathematical identities (within floating-point precision):
// Pythagorean identity
const x = 0.7
pow(sin(x), 2) + pow(cos(x), 2) // ≈ 1
// Angle sum formulas (implemented internally)
sin(add(a, b)) // sin(a)cos(b) + cos(a)sin(b)
// Hyperbolic identities
const y = 1.5
pow(cosh(y), 2) - pow(sinh(y), 2) // ≈ 1
// Euler's formula
const theta = pi/3
exp(complex(0, theta)) // cos(theta) + i*sin(theta)// Faster: direct radian input
sin(1.047) // π/3 in radians
// Slower: unit conversion overhead
sin(unit('60 deg'))// Faster: single function call on array
sin([0, pi/4, pi/2, pi])
// Slower: multiple function calls
[0, pi/4, pi/2, pi].map(x => sin(x))// For repeated use
const sin30 = sin(pi/6) // 0.5
const cos60 = cos(pi/3) // 0.5import { pi, e, i } from 'mathjs'
// Key angles in radians
pi/6 // 30°
pi/4 // 45°
pi/3 // 60°
pi/2 // 90°
pi // 180°
2*pi // 360°
// Special values
sin(0) // 0
sin(pi/2) // 1
cos(0) // 1
cos(pi) // -1
// Complex unit circle
exp(multiply(i, pi)) // -1 (Euler's identity)import { ArgumentsError } from 'mathjs'
try {
// Some functions have domain restrictions
asin(2) // Returns complex number (not error)
acosh(0.5) // Returns complex number
// Unit compatibility
sin(unit('5 meter')) // Error: expecting angle unit
} catch (error) {
if (error instanceof ArgumentsError) {
console.log('Invalid arguments provided')
}
}All trigonometric functions work in the chain interface:
chain(pi/6)
.sin() // 0.5
.asin() // π/6 (back to original)
.multiply(180/pi) // Convert to degrees
.done() // 30// Cartesian to polar
function toPolar(x, y) {
const r = hypot(x, y)
const theta = atan2(y, x)
return { r, theta }
}
// Polar to Cartesian
function toCartesian(r, theta) {
const x = multiply(r, cos(theta))
const y = multiply(r, sin(theta))
return { x, y }
}// Sine wave generator
function sineWave(amplitude, frequency, phase, time) {
return multiply(amplitude, sin(add(multiply(frequency, time), phase)))
}
const wave = range(0, 2*pi, 0.1).map(t => sineWave(1, 1, 0, t))// Taylor series approximation (Math.js does this internally)
function sinApprox(x, terms = 10) {
let result = 0
for (let n = 0; n < terms; n++) {
const term = multiply(
pow(-1, n),
divide(pow(x, 2*n + 1), factorial(2*n + 1))
)
result = add(result, term)
}
return result
}Install with Tessl CLI
npx tessl i tessl/npm-mathjsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10