-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathadj.ts
47 lines (36 loc) · 1019 Bytes
/
adj.ts
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
import { alloc_tensor } from '../runtime/alloc';
import { cadr, U } from '../runtime/defs';
import { stop } from '../runtime/run';
import { cofactor } from './cofactor';
import { Eval } from './eval';
import { is_square_matrix } from './tensor';
/* adj =====================================================================
Tags
----
scripting, JS, internal, treenode, general concept
Parameters
----------
m
General description
-------------------
Returns the adjunct of matrix m. The inverse of m is equal to adj(m) divided by det(m).
*/
export function Eval_adj(p1: U) {
return adj(Eval(cadr(p1)));
}
export function adj(p1: U): U {
if (!is_square_matrix(p1)) {
stop('adj: square matrix expected');
}
const n = p1.tensor.dim[0];
const p2 = alloc_tensor(n * n);
p2.tensor.ndim = 2;
p2.tensor.dim[0] = n;
p2.tensor.dim[1] = n;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
p2.tensor.elem[n * j + i] = cofactor(p1, n, i, j);
}
} // transpose
return p2;
}