-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymmetricMatrix.ts
223 lines (200 loc) · 6.34 KB
/
SymmetricMatrix.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { SymmetricMatrixInterface } from "./MatrixInterfaces"
import { SquareMatrix } from "./SquareMatrix"
import { DiagonalMatrix } from "./DiagonalMatrix";
import { containsNaN } from "./MathVectorBasicOperations";
/**
* A symmetric matrix
*/
export class SymmetricMatrix implements SymmetricMatrixInterface {
private _shape: number[]
private data: number[]
/**
* Create a Symmetric Matrix
* @param size The number of rows or the number columns
* @param data The matrix data in a flat vector
*/
constructor(size: number, data?: Array<number>) {
this._shape = [size, size]
if (data) {
if (data.length !== size * (size + 1) / 2) {
throw new Error("Square matrix constructor expect the data to have (size * (size + 1) / 2) length")
}
this.data = data.slice()
} else {
this.data = []
const n = (size * (size + 1)) / 2
for (let i = 0; i < n; i += 1) {
this.data.push(0);
}
}
}
/**
* Returns the shape of the matrix : [number of rows, number of columns]
*/
get shape() {
return this._shape
}
/**
* Returns the corresponding index in the flat data vector.
* In this flat data vector the upper triangular matrix is store row-wise.
* @param row The row index
* @param column The column index
*/
private dataIndex(row: number, column: number) {
if (row <= column) {
return row * this.shape[1] - (row - 1) * row / 2 + column - row;
}
return column * this.shape[0] - (column - 1) * column / 2 + row - column;
}
/**
* Returns the value at a given row and column position
* @param row The row index
* @param column The column index
* @return Scalar
* @throws If an index is out of range
*/
get(row: number, column: number) {
this.checkRowRange(row);
this.checkColumnRange(column);
return this.data[this.dataIndex(row, column)];
}
/**
* Set a given value at a given row and column position
* @param row The row index
* @param column The column index
* @param value The new value
* @throws If an index is out of range
*/
set(row: number, column: number, value: number) {
this.checkRowRange(row);
this.checkColumnRange(column);
this.data[this.dataIndex(row, column)] = value;
}
/**
* Check that the index is inside appropriate range
* @param index The column or the row index
* @throws If an index is out of range
*/
checkRowRange(index: number) {
if (index < 0 || index >= this.shape[0]) {
throw new Error("SymmetricMatrix index is out of range");
}
}
/**
* Check that the index is inside appropriate range
* @param index The column or the row index
* @throws If an index is out of range
*/
checkColumnRange(index: number) {
if (index < 0 || index >= this.shape[1]) {
throw new Error("SymmetricMatrix index is out of range");
}
}
/**
* Compute the product v^t M v
* @param v Vector
* @return Scalar
*/
quadraticForm(v: Array<number>) {
let result = 0
for (let i = 1; i < this.shape[1]; i += 1) {
for (let j = 0; j < i; j += 1) {
result += this.get(i, j) * v[i] * v[j];
}
}
result *= 2;
for (let i = 0; i < this.shape[1]; i += 1) {
result += this.get(i, i) * Math.pow(v[i], 2);
}
return result;
}
/**
* Return a safe copy of this matrix
* */
clone() {
return new SymmetricMatrix(this.shape[0], this.data)
}
/**
* Increases the given element of the matrix by the value
* @param row The row index
* @param column The column index
* @param value The number to be added
* @throws If an index is out of range
*/
addAt(row: number, column: number, value: number) {
this.checkRowRange(row)
this.checkColumnRange(row)
this.data[this.dataIndex(row, column)] += value
}
/**
* Increases every diagonal element of the matrix by the value
* @param value The number to be added
*/
addValueOnDiagonalInPlace(value: number) {
const m = this.shape[0]
for (let i = 0; i < m; i += 1) {
this.data[this.dataIndex(i, i)] += value
}
}
/**
* Returns the new matrix: this.matrix + value * I
* @param value
* @returns SymmetricMatrix
*/
addValueOnDiagonal(value: number) {
let result = this.clone()
result.addValueOnDiagonalInPlace(value)
return result
}
/**
* Returns a SquareMatrix with the values of this matrix
*/
squareMatrix() {
const n = this.shape[0]
let result = new SquareMatrix(n);
for (let i = 0; i < n; i += 1) {
for (let j = 0; j < n; j += 1) {
result.set(i, j, this.get(i, j));
}
}
return result;
}
plusSymmetricMatrixMultipliedByValue(matrix: SymmetricMatrixInterface, value: number) {
if (this.shape[0] !== matrix.shape[0]) {
throw new Error("Adding two symmetric matrix with different shapes");
}
let result = this.clone()
const n = result.shape[0]
if (matrix instanceof DiagonalMatrix) {
for (let i = 0; i < n; i += 1) {
result.addAt(i, i, matrix.get(i, i) * value);
}
return result
} else {
for (let i = 0; i < n; i += 1) {
for (let j = 0; j <= i; j += 1) {
result.addAt(i, j, matrix.get(i, j) * value);
}
}
return result
}
}
multiplyByVector(v: number[]) {
if (this.shape[1] !== v.length) {
throw new Error("SymmetricMatrix multiply a vector of incorrect length");
}
let result = []
const n = this.shape[1]
for (let i = 0; i < n; i += 1) {
let temp = 0;
for (let j = 0; j < n; j += 1) {
temp += this.get(i, j) * v[j];
}
result.push(temp);
}
return result;
}
containsNaN() {
return containsNaN(this.data)
}
}