-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.ts
48 lines (40 loc) · 1.23 KB
/
add.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
48
import { SparseMatrix, Term } from "./types";
import { DimensionError } from "./error";
/**
* Adds two sparse matrices. `O(A.terms + B.terms)`.
* @param A The first matrix.
* @param B The second matrix.
* @returns The sum of the two matrices.
*/
export function add(A: SparseMatrix, B: SparseMatrix): SparseMatrix {
if (A[0]?.col !== B[0]?.col || A[0]?.row !== B[0]?.row) {
throw new DimensionError(A, B);
}
const C: SparseMatrix = [{ row: A[0].row, col: A[0].col, value: 0 }];
let [i, j, k] = [1, 1, 1];
while (i <= A[0].value && j <= B[0].value) {
if (A[i].row < B[j].row) {
C[k++] = A[i++];
} else if (A[i].row > B[j].row) {
C[k++] = B[j++];
} else if (A[i].col < B[j].col) {
C[k++] = A[i++];
} else if (A[i].col > B[j].col) {
C[k++] = B[j++];
} else {
const term: Term = { row: A[i].row, col: A[i].col, value: A[i].value + B[j].value };
if (term.value) {
C[k++] = term;
}
i++, j++;
}
}
while (i <= A[0].value) {
C[k++] = A[i++];
}
while (j <= B[0].value) {
C[k++] = B[j++];
}
C[0].value = k - 1;
return C;
}