-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.js
42 lines (38 loc) · 925 Bytes
/
ls.js
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
const { multiply, subtract, transpose, inv, matrix, format } = require("mathjs");
const solve = (A, b) => {
const transposed = transpose(A);
const product = multiply(transposed, A);
const inverse = inv(product);
const pseudoInverse = multiply(inverse, transposed);
const x = multiply(pseudoInverse, b);
return x;
};
// const A = matrix([
// [4, 2, 1],
// [25, 5, 1],
// [49, 7, 1],
// [121, 11, 1],
// [196, 14, 1],
// [324, 18, 1],
// ]);
// const A = matrix([
// [4, 2, 1],
// [25, 5, 1],
// [49, 7, 1],
// [121, 11, 1],
// [196, 14, 1],
// [324, 18, 1],
// ]);
const A = matrix([
[-1,1,0],
[0,1,-1],
[1,1,1],
]);
//const b = matrix([5, 5, 8, 7, 9, 7]);
const b = matrix([3,8,25]);
const x = solve(A, b);
console.log(format(x, 5));
const residual = subtract(multiply(A,x),b)
const squares = residual.map(x => x*x)
console.log(format(residual,5))
console.log(format(squares,5))