-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomial.js
More file actions
60 lines (57 loc) · 1.66 KB
/
Copy pathpolynomial.js
File metadata and controls
60 lines (57 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Polynomial calculus on coefficient arrays.
*
* A polynomial is represented as an array of coefficients where the value at
* index `i` is the coefficient of `x^i`. For example, `[4, 0, 2]` represents
* `4 + 0·x + 2·x²`.
*/
/**
* Derivative of a polynomial.
*
* Since `d/dx (Σ aᵢ·xⁱ) = Σ i·aᵢ·xⁱ⁻¹`, every coefficient moves down one power
* and is multiplied by its old power. A constant differentiates to the empty
* polynomial.
*
* Time / Space: O(n)
*
* @param {number[]} coeffs
* @returns {number[]} Coefficients of the derivative.
*/
export function derivative(coeffs) {
const result = [];
for (let power = 1; power < coeffs.length; power++) {
result.push(power * coeffs[power]);
}
return result;
}
/**
* Indefinite integral of a polynomial.
*
* Since `∫ (Σ aᵢ·xⁱ) dx = C + Σ aᵢ/(i+1)·xⁱ⁺¹`, every coefficient moves up one
* power and is divided by its new power. `C` is the constant of integration.
*
* Time / Space: O(n)
*
* @param {number[]} coeffs
* @param {number} [constant=0] - The constant of integration `C`.
* @returns {number[]} Coefficients of the integral.
*/
export function integral(coeffs, constant = 0) {
const result = [constant];
for (let power = 0; power < coeffs.length; power++) {
result.push(coeffs[power] / (power + 1));
}
return result;
}
/**
* Formats a coefficient array as a readable polynomial string.
* Example: `[4, 0, 2]` becomes `"4 + 0X^1 + 2X^2"`.
*
* @param {number[]} coeffs
* @returns {string}
*/
export function formatPolynomial(coeffs) {
return coeffs
.map((coeff, power) => (power === 0 ? `${coeff}` : `${coeff}X^${power}`))
.join(' + ');
}