@thi.ng/math
Version:
Assorted common math functions & utilities
49 lines • 1.39 kB
TypeScript
/**
* Computes polynomial for `x` and given `coeffs` (in order of increasing
* exponents).
*
* @remarks
* See {@link polynomialRegression} for computing coefficients.
*
* The number of given coefficients defines the degree (+1) of the polynomial,
* i.e. a cubic function will require 4 coeffs, with the y-intercept being the
* first coeff.
*
* @example
* ```ts tangle:../export/polynomial.ts
* import { polynomial } from "@thi.ng/math";
*
* const coeffs = [-5, -4, 3, 2];
*
* for(let x = -2; x <= 2; x += 0.5) {
* console.log(`f(${x}) = ${polynomial(x, coeffs)}`);
* }
* // f(-2) = -1
* // f(-1.5) = 1
* // f(-1) = 0
* // f(-0.5) = -2.5
* // f(0) = -5
* // f(0.5) = -6
* // f(1) = -4
* // f(1.5) = 2.5
* // f(2) = 15
* ```
*
* @param x
* @param coeffs
*/
export declare const polynomial: (x: number, coeffs: number[]) => number;
/**
* Computes the coefficients of a polynomial regression for the given `samples`
* (each a [x,y] tuple). The `degree` param defines the degree of the polynomial
* and the number of returned coefficients (+1).
*
* @remarks
* The resulting coeffs can be then used with {@link polynomial} to evaluate the
* curve (i.e. used to make predictions).
*
* @param samples
* @param degree
*/
export declare const polynomialRegression: (samples: number[][], degree: number) => number[];
//# sourceMappingURL=polynomial.d.ts.map