0
# Mathematical Solvers
1
2
Equation solving functions for linear, quadratic, and cubic equations, plus numerical methods and derivative calculations.
3
4
## Capabilities
5
6
### Polynomial Equation Solvers
7
8
Functions for solving polynomial equations of various degrees.
9
10
```typescript { .api }
11
/**
12
* Computes solution for linear equation: ax + b = 0
13
* @param a - Coefficient of x
14
* @param b - Constant term
15
* @returns Solution x, or NaN if no solution exists
16
*/
17
function solveLinear(a: number, b: number): number;
18
19
/**
20
* Computes solutions for quadratic equation: ax² + bx + c = 0
21
* @param a - Coefficient of x²
22
* @param b - Coefficient of x
23
* @param c - Constant term
24
* @param eps - Epsilon for numerical precision (optional)
25
* @returns Array of real solutions (0, 1, or 2 solutions)
26
*/
27
function solveQuadratic(a: number, b: number, c: number, eps?: number): number[];
28
29
/**
30
* Computes solutions for cubic equation: ax³ + bx² + cx + d = 0
31
* @param a - Coefficient of x³
32
* @param b - Coefficient of x²
33
* @param c - Coefficient of x
34
* @param d - Constant term
35
* @param eps - Epsilon for numerical precision (optional)
36
* @returns Array of real solutions (1, 2, or 3 solutions)
37
*/
38
function solveCubic(a: number, b: number, c: number, d: number, eps?: number): number[];
39
```
40
41
**Usage Examples:**
42
43
```typescript
44
import { solveLinear, solveQuadratic, solveCubic } from "@thi.ng/math/solve";
45
46
// Linear equation: 2x + 4 = 0
47
const linearRoot = solveLinear(2, 4); // -2
48
49
// Quadratic equation: x² - 5x + 6 = 0
50
const quadRoots = solveQuadratic(1, -5, 6); // [2, 3]
51
52
// Quadratic with no real solutions: x² + 1 = 0
53
const noRealRoots = solveQuadratic(1, 0, 1); // []
54
55
// Cubic equation: x³ - 6x² + 11x - 6 = 0
56
const cubicRoots = solveCubic(1, -6, 11, -6); // [1, 2, 3]
57
```
58
59
### System Solvers
60
61
Functions for solving systems of linear equations.
62
63
```typescript { .api }
64
/**
65
* Solves tridiagonal system of linear equations using Thomas algorithm
66
* Solves: a[i]*x[i-1] + b[i]*x[i] + c[i]*x[i+1] = d[i]
67
* @param a - Lower diagonal coefficients (a[0] ignored)
68
* @param b - Main diagonal coefficients
69
* @param c - Upper diagonal coefficients (c[n-1] ignored)
70
* @param d - Right-hand side values
71
* @returns Solution vector x
72
*/
73
function solveTridiagonal(
74
a: NumericArray,
75
b: NumericArray,
76
c: NumericArray,
77
d: NumericArray
78
): NumericArray;
79
```
80
81
**Usage Examples:**
82
83
```typescript
84
import { solveTridiagonal } from "@thi.ng/math/solve";
85
86
// Solve tridiagonal system:
87
// 2x₀ + 1x₁ = 3
88
// 1x₀ + 3x₁ + 1x₂ = 4
89
// 1x₁ + 2x₂ = 1
90
91
const a = [0, 1, 1]; // Lower diagonal (first element ignored)
92
const b = [2, 3, 2]; // Main diagonal
93
const c = [1, 1, 0]; // Upper diagonal (last element ignored)
94
const d = [3, 4, 1]; // Right-hand side
95
96
const solution = solveTridiagonal(a, b, c, d); // Solution vector
97
```
98
99
### Numerical Methods
100
101
Functions for numerical analysis and derivative calculations.
102
103
```typescript { .api }
104
/**
105
* Produces derivative function of given single-argument function using finite differences
106
* @param f - Function to differentiate
107
* @param eps - Step size for finite difference (default: EPS)
108
* @returns Derivative function
109
*/
110
function derivative(f: (x: number) => number, eps?: number): (x: number) => number;
111
```
112
113
**Usage Examples:**
114
115
```typescript
116
import { derivative } from "@thi.ng/math/solve";
117
118
// Create derivative of a function
119
const f = (x: number) => x * x * x; // f(x) = x³
120
const df = derivative(f); // f'(x) ≈ 3x²
121
122
// Test derivative
123
const slope = df(2); // ≈ 12 (derivative of x³ at x=2 is 3*2² = 12)
124
125
// Derivative of sine function
126
const sin = Math.sin;
127
const cos_approx = derivative(sin); // Approximates cosine
128
const cosValue = cos_approx(Math.PI / 4); // ≈ 0.707 (cos(π/4))
129
130
// Find critical points (where derivative = 0)
131
function findCriticalPoints(f: (x: number) => number, start: number, end: number, steps: number): number[] {
132
const df = derivative(f);
133
const criticalPoints: number[] = [];
134
const stepSize = (end - start) / steps;
135
136
for (let i = 0; i < steps; i++) {
137
const x = start + i * stepSize;
138
if (Math.abs(df(x)) < 1e-6) {
139
criticalPoints.push(x);
140
}
141
}
142
143
return criticalPoints;
144
}
145
```
146
147
## Application Examples
148
149
```typescript
150
import { solveQuadratic, solveCubic, derivative } from "@thi.ng/math/solve";
151
152
// Physics: projectile motion
153
function projectileHitTime(height: number, velocity: number, gravity: number = 9.81): number[] {
154
// Solve: (1/2)gt² - vt + h = 0
155
return solveQuadratic(gravity / 2, -velocity, height);
156
}
157
158
// Graphics: ray-sphere intersection
159
function raySphereIntersection(
160
rayOrigin: number[], rayDir: number[],
161
sphereCenter: number[], sphereRadius: number
162
): number[] {
163
const dx = rayOrigin[0] - sphereCenter[0];
164
const dy = rayOrigin[1] - sphereCenter[1];
165
const dz = rayOrigin[2] - sphereCenter[2];
166
167
const a = rayDir[0] ** 2 + rayDir[1] ** 2 + rayDir[2] ** 2;
168
const b = 2 * (dx * rayDir[0] + dy * rayDir[1] + dz * rayDir[2]);
169
const c = dx ** 2 + dy ** 2 + dz ** 2 - sphereRadius ** 2;
170
171
return solveQuadratic(a, b, c);
172
}
173
174
// Optimization: find minimum/maximum using derivatives
175
function findMinimum(
176
f: (x: number) => number,
177
start: number,
178
end: number
179
): number {
180
const df = derivative(f);
181
const d2f = derivative(df);
182
183
// Newton's method for finding root of derivative
184
let x = (start + end) / 2;
185
for (let i = 0; i < 10; i++) {
186
const fx = df(x);
187
const dfx = d2f(x);
188
if (Math.abs(fx) < 1e-10) break;
189
x = x - fx / dfx;
190
}
191
192
return x;
193
}
194
```