CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-thi-ng--math

Assorted common math functions & utilities for TypeScript/JavaScript applications

Pending
Overview
Eval results
Files

solvers.mddocs/

Mathematical Solvers

Equation solving functions for linear, quadratic, and cubic equations, plus numerical methods and derivative calculations.

Capabilities

Polynomial Equation Solvers

Functions for solving polynomial equations of various degrees.

/**
 * Computes solution for linear equation: ax + b = 0
 * @param a - Coefficient of x
 * @param b - Constant term
 * @returns Solution x, or NaN if no solution exists
 */
function solveLinear(a: number, b: number): number;

/**
 * Computes solutions for quadratic equation: ax² + bx + c = 0
 * @param a - Coefficient of x²
 * @param b - Coefficient of x
 * @param c - Constant term
 * @param eps - Epsilon for numerical precision (optional)
 * @returns Array of real solutions (0, 1, or 2 solutions)
 */
function solveQuadratic(a: number, b: number, c: number, eps?: number): number[];

/**
 * Computes solutions for cubic equation: ax³ + bx² + cx + d = 0
 * @param a - Coefficient of x³
 * @param b - Coefficient of x²
 * @param c - Coefficient of x
 * @param d - Constant term
 * @param eps - Epsilon for numerical precision (optional)
 * @returns Array of real solutions (1, 2, or 3 solutions)
 */
function solveCubic(a: number, b: number, c: number, d: number, eps?: number): number[];

Usage Examples:

import { solveLinear, solveQuadratic, solveCubic } from "@thi.ng/math/solve";

// Linear equation: 2x + 4 = 0
const linearRoot = solveLinear(2, 4); // -2

// Quadratic equation: x² - 5x + 6 = 0
const quadRoots = solveQuadratic(1, -5, 6); // [2, 3]

// Quadratic with no real solutions: x² + 1 = 0
const noRealRoots = solveQuadratic(1, 0, 1); // []

// Cubic equation: x³ - 6x² + 11x - 6 = 0
const cubicRoots = solveCubic(1, -6, 11, -6); // [1, 2, 3]

System Solvers

Functions for solving systems of linear equations.

/**
 * Solves tridiagonal system of linear equations using Thomas algorithm
 * Solves: a[i]*x[i-1] + b[i]*x[i] + c[i]*x[i+1] = d[i]
 * @param a - Lower diagonal coefficients (a[0] ignored)
 * @param b - Main diagonal coefficients
 * @param c - Upper diagonal coefficients (c[n-1] ignored)
 * @param d - Right-hand side values
 * @returns Solution vector x
 */
function solveTridiagonal(
  a: NumericArray, 
  b: NumericArray, 
  c: NumericArray, 
  d: NumericArray
): NumericArray;

Usage Examples:

import { solveTridiagonal } from "@thi.ng/math/solve";

// Solve tridiagonal system:
// 2x₀ + 1x₁ = 3
// 1x₀ + 3x₁ + 1x₂ = 4  
// 1x₁ + 2x₂ = 1

const a = [0, 1, 1];    // Lower diagonal (first element ignored)
const b = [2, 3, 2];    // Main diagonal
const c = [1, 1, 0];    // Upper diagonal (last element ignored)
const d = [3, 4, 1];    // Right-hand side

const solution = solveTridiagonal(a, b, c, d); // Solution vector

Numerical Methods

Functions for numerical analysis and derivative calculations.

/**
 * Produces derivative function of given single-argument function using finite differences
 * @param f - Function to differentiate
 * @param eps - Step size for finite difference (default: EPS)
 * @returns Derivative function
 */
function derivative(f: (x: number) => number, eps?: number): (x: number) => number;

Usage Examples:

import { derivative } from "@thi.ng/math/solve";

// Create derivative of a function
const f = (x: number) => x * x * x; // f(x) = x³
const df = derivative(f); // f'(x) ≈ 3x²

// Test derivative
const slope = df(2); // ≈ 12 (derivative of x³ at x=2 is 3*2² = 12)

// Derivative of sine function
const sin = Math.sin;
const cos_approx = derivative(sin); // Approximates cosine
const cosValue = cos_approx(Math.PI / 4); // ≈ 0.707 (cos(π/4))

// Find critical points (where derivative = 0)
function findCriticalPoints(f: (x: number) => number, start: number, end: number, steps: number): number[] {
  const df = derivative(f);
  const criticalPoints: number[] = [];
  const stepSize = (end - start) / steps;
  
  for (let i = 0; i < steps; i++) {
    const x = start + i * stepSize;
    if (Math.abs(df(x)) < 1e-6) {
      criticalPoints.push(x);
    }
  }
  
  return criticalPoints;
}

Application Examples

import { solveQuadratic, solveCubic, derivative } from "@thi.ng/math/solve";

// Physics: projectile motion
function projectileHitTime(height: number, velocity: number, gravity: number = 9.81): number[] {
  // Solve: (1/2)gt² - vt + h = 0
  return solveQuadratic(gravity / 2, -velocity, height);
}

// Graphics: ray-sphere intersection
function raySphereIntersection(
  rayOrigin: number[], rayDir: number[],
  sphereCenter: number[], sphereRadius: number
): number[] {
  const dx = rayOrigin[0] - sphereCenter[0];
  const dy = rayOrigin[1] - sphereCenter[1];
  const dz = rayOrigin[2] - sphereCenter[2];
  
  const a = rayDir[0] ** 2 + rayDir[1] ** 2 + rayDir[2] ** 2;
  const b = 2 * (dx * rayDir[0] + dy * rayDir[1] + dz * rayDir[2]);
  const c = dx ** 2 + dy ** 2 + dz ** 2 - sphereRadius ** 2;
  
  return solveQuadratic(a, b, c);
}

// Optimization: find minimum/maximum using derivatives
function findMinimum(
  f: (x: number) => number, 
  start: number, 
  end: number
): number {
  const df = derivative(f);
  const d2f = derivative(df);
  
  // Newton's method for finding root of derivative
  let x = (start + end) / 2;
  for (let i = 0; i < 10; i++) {
    const fx = df(x);
    const dfx = d2f(x);
    if (Math.abs(fx) < 1e-10) break;
    x = x - fx / dfx;
  }
  
  return x;
}

Install with Tessl CLI

npx tessl i tessl/npm-thi-ng--math

docs

angles.md

constants-types.md

easing.md

eqdelta.md

extrema.md

fit.md

index.md

integers.md

interpolation.md

intervals.md

libc.md

precision.md

solvers.md

utilities.md

tile.json