@ac-kit/math-linear 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/README.md +57 -4
- package/dist/_euclidean-norm.d.ts +18 -0
- package/dist/_euclidean-norm.d.ts.map +1 -0
- package/dist/_euclidean-norm.js +73 -0
- package/dist/_euclidean-norm.js.map +1 -0
- package/dist/_matrix-shape.d.ts +4 -0
- package/dist/_matrix-shape.d.ts.map +1 -0
- package/dist/_matrix-shape.js +7 -0
- package/dist/_matrix-shape.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/lu-decomposition.d.ts +142 -0
- package/dist/lu-decomposition.d.ts.map +1 -0
- package/dist/lu-decomposition.js +416 -0
- package/dist/lu-decomposition.js.map +1 -0
- package/dist/matrix.d.ts +136 -0
- package/dist/matrix.d.ts.map +1 -0
- package/dist/matrix.js +308 -0
- package/dist/matrix.js.map +1 -0
- package/dist/qr-decomposition.d.ts +283 -0
- package/dist/qr-decomposition.d.ts.map +1 -0
- package/dist/qr-decomposition.js +1071 -0
- package/dist/qr-decomposition.js.map +1 -0
- package/dist/singular-value-decomposition.d.ts +154 -0
- package/dist/singular-value-decomposition.d.ts.map +1 -0
- package/dist/singular-value-decomposition.js +474 -0
- package/dist/singular-value-decomposition.js.map +1 -0
- package/dist/symmetric-eigen-decomposition.d.ts +92 -0
- package/dist/symmetric-eigen-decomposition.d.ts.map +1 -0
- package/dist/symmetric-eigen-decomposition.js +289 -0
- package/dist/symmetric-eigen-decomposition.js.map +1 -0
- package/dist/vec2-array.d.ts +16 -0
- package/dist/vec2-array.d.ts.map +1 -1
- package/dist/vec2-array.js +42 -0
- package/dist/vec2-array.js.map +1 -1
- package/package.json +11 -10
package/dist/matrix.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { assertInteger, isCloseTo, viewsMayOverlap, } from "@ac-kit/core";
|
|
2
|
+
import { assertMatrixShape_ } from "./_matrix-shape.js";
|
|
3
|
+
function assertDimensions_(rows, columns) {
|
|
4
|
+
assertInteger(rows, "rows", 0);
|
|
5
|
+
assertInteger(columns, "columns", 0);
|
|
6
|
+
if (!Number.isSafeInteger(rows * columns)) {
|
|
7
|
+
throw new RangeError(`rows × columns must be a safe integer, got ${rows} × ${columns}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A `rows × columns` matrix of zeros, over a new `Float64Array`.
|
|
12
|
+
*
|
|
13
|
+
* @throws {RangeError} When `rows` or `columns` is not a non-negative safe
|
|
14
|
+
* integer, or their product is not a safe integer.
|
|
15
|
+
*/
|
|
16
|
+
export function matrixCreate(rows, columns) {
|
|
17
|
+
assertDimensions_(rows, columns);
|
|
18
|
+
return { data: new Float64Array(rows * columns), rows, columns };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A matrix over existing storage, without a copy: `data` must hold exactly
|
|
22
|
+
* `rows · columns` entries in column-major order (a `subarray` selects part of
|
|
23
|
+
* a larger buffer). Writes through the matrix change `data`, and the reverse.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* // [[1, 3], [2, 4]]: columns (1, 2) and (3, 4).
|
|
28
|
+
* const m = matrixView(Float64Array.of(1, 2, 3, 4), 2, 2);
|
|
29
|
+
* matrixGet(m, 0, 1); // 3
|
|
30
|
+
* ```;
|
|
31
|
+
*
|
|
32
|
+
* @throws {RangeError} When `rows` or `columns` is not a non-negative safe
|
|
33
|
+
* integer, or `data.length` is not their product.
|
|
34
|
+
*/
|
|
35
|
+
export function matrixView(data, rows, columns) {
|
|
36
|
+
assertDimensions_(rows, columns);
|
|
37
|
+
const matrix = { data, rows, columns };
|
|
38
|
+
assertMatrixShape_(matrix, "matrixView");
|
|
39
|
+
return matrix;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A matrix from its rows in reading order, copied: `matrixOfRows([[1, 2], [3,
|
|
43
|
+
* 4]])` has `1, 2` on its first row. An entry that is not a number is stored as
|
|
44
|
+
* `NaN`, as `…FromArray` does.
|
|
45
|
+
*
|
|
46
|
+
* @throws {RangeError} When the rows are not all of one length; an empty list
|
|
47
|
+
* gives a 0 × 0 matrix.
|
|
48
|
+
*/
|
|
49
|
+
export function matrixOfRows(rows) {
|
|
50
|
+
const rowCount = rows.length;
|
|
51
|
+
const columns = rowCount === 0 ? 0 : rows[0].length;
|
|
52
|
+
const matrix = matrixCreate(rowCount, columns);
|
|
53
|
+
const { data } = matrix;
|
|
54
|
+
for (let row = 0; row < rowCount; row++) {
|
|
55
|
+
const entries = rows[row];
|
|
56
|
+
if (entries.length !== columns) {
|
|
57
|
+
throw new RangeError(`matrixOfRows: row ${row} has ${entries.length} entries, row 0 has ${columns}`);
|
|
58
|
+
}
|
|
59
|
+
for (let column = 0; column < columns; column++) {
|
|
60
|
+
const value = entries[column];
|
|
61
|
+
data[rowCount * column + row] =
|
|
62
|
+
typeof value === "number" ? value : Number.NaN;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return matrix;
|
|
66
|
+
}
|
|
67
|
+
function assertIndex_(m, row, column, name) {
|
|
68
|
+
if (!Number.isInteger(row) ||
|
|
69
|
+
!Number.isInteger(column) ||
|
|
70
|
+
row < 0 ||
|
|
71
|
+
column < 0 ||
|
|
72
|
+
row >= m.rows ||
|
|
73
|
+
column >= m.columns) {
|
|
74
|
+
throw new RangeError(`${name}: (${row}, ${column}) is outside a ${m.rows} × ${m.columns} matrix`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The entry of row `row`, column `column`. Loops over many entries read `data`
|
|
79
|
+
* directly at `rows · column + row`.
|
|
80
|
+
*
|
|
81
|
+
* @throws {RangeError} When the index is not an integer inside the matrix.
|
|
82
|
+
*/
|
|
83
|
+
export function matrixGet(m, row, column) {
|
|
84
|
+
assertIndex_(m, row, column, "matrixGet");
|
|
85
|
+
return m.data[m.rows * column + row];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Stores `value` at row `row`, column `column`, and returns `m`.
|
|
89
|
+
*
|
|
90
|
+
* @throws {RangeError} When the index is not an integer inside the matrix.
|
|
91
|
+
*/
|
|
92
|
+
export function matrixSet(m, row, column, value) {
|
|
93
|
+
assertIndex_(m, row, column, "matrixSet");
|
|
94
|
+
m.data[m.rows * column + row] = value;
|
|
95
|
+
return m;
|
|
96
|
+
}
|
|
97
|
+
function assertSameShape_(out, m, name) {
|
|
98
|
+
assertMatrixShape_(out, name);
|
|
99
|
+
assertMatrixShape_(m, name);
|
|
100
|
+
if (out.rows !== m.rows || out.columns !== m.columns) {
|
|
101
|
+
throw new RangeError(`${name}: out is ${out.rows} × ${out.columns}, the input ${m.rows} × ${m.columns}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function assertDisjoint_(out, m, name) {
|
|
105
|
+
if (viewsMayOverlap(out.data, m.data)) {
|
|
106
|
+
throw new RangeError(`${name}: out must not share storage with an input`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Copies `m` into `out` (same shape) and returns `out`; `out` may be `m`.
|
|
111
|
+
*
|
|
112
|
+
* @throws {RangeError} When the shapes differ or a matrix breaks the
|
|
113
|
+
* {@link Matrix} invariant.
|
|
114
|
+
*/
|
|
115
|
+
export function matrixCopy(out, m) {
|
|
116
|
+
assertSameShape_(out, m, "matrixCopy");
|
|
117
|
+
out.data.set(m.data);
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Writes the identity into `out` — ones on the main diagonal, zeros elsewhere,
|
|
122
|
+
* for a rectangular matrix too — and returns it.
|
|
123
|
+
*
|
|
124
|
+
* @throws {RangeError} When `out` breaks the {@link Matrix} invariant.
|
|
125
|
+
*/
|
|
126
|
+
export function matrixIdentity(out) {
|
|
127
|
+
assertMatrixShape_(out, "matrixIdentity");
|
|
128
|
+
const { data, rows, columns } = out;
|
|
129
|
+
const diagonal = rows < columns ? rows : columns;
|
|
130
|
+
data.fill(0);
|
|
131
|
+
for (let index = 0; index < diagonal; index++) {
|
|
132
|
+
data[(rows + 1) * index] = 1;
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Writes `mᵀ` into `out` (`columns × rows`) and returns it.
|
|
138
|
+
*
|
|
139
|
+
* @throws {RangeError} When `out` is not `m.columns × m.rows`, shares storage
|
|
140
|
+
* with `m` (a transpose in place is not offered: it is only cheap for square
|
|
141
|
+
* matrices), or a matrix breaks the {@link Matrix} invariant.
|
|
142
|
+
*/
|
|
143
|
+
export function matrixTranspose(out, m) {
|
|
144
|
+
assertMatrixShape_(out, "matrixTranspose");
|
|
145
|
+
assertMatrixShape_(m, "matrixTranspose");
|
|
146
|
+
const { rows, columns } = m;
|
|
147
|
+
if (out.rows !== columns || out.columns !== rows) {
|
|
148
|
+
throw new RangeError(`matrixTranspose: out is ${out.rows} × ${out.columns}, needs ${columns} × ${rows}`);
|
|
149
|
+
}
|
|
150
|
+
assertDisjoint_(out, m, "matrixTranspose");
|
|
151
|
+
const source = m.data;
|
|
152
|
+
const target = out.data;
|
|
153
|
+
for (let column = 0; column < columns; column++) {
|
|
154
|
+
const offset = rows * column;
|
|
155
|
+
for (let row = 0; row < rows; row++) {
|
|
156
|
+
target[columns * row + column] = source[offset + row];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Writes the product `a · b` into `out` (`a.rows × b.columns`) and returns it.
|
|
163
|
+
* Plain arithmetic, summed in index order; O(a.rows · a.columns · b.columns).
|
|
164
|
+
*
|
|
165
|
+
* @throws {RangeError} When `a.columns !== b.rows`, `out` has the wrong shape
|
|
166
|
+
* or shares storage with `a` or `b`, or a matrix breaks the {@link Matrix}
|
|
167
|
+
* invariant.
|
|
168
|
+
*/
|
|
169
|
+
export function matrixMultiply(out, a, b) {
|
|
170
|
+
assertMatrixShape_(out, "matrixMultiply");
|
|
171
|
+
assertMatrixShape_(a, "matrixMultiply");
|
|
172
|
+
assertMatrixShape_(b, "matrixMultiply");
|
|
173
|
+
const rows = a.rows;
|
|
174
|
+
const inner = a.columns;
|
|
175
|
+
const columns = b.columns;
|
|
176
|
+
if (b.rows !== inner || out.rows !== rows || out.columns !== columns) {
|
|
177
|
+
throw new RangeError(`matrixMultiply: ${rows} × ${inner} times ${b.rows} × ${columns} into ${out.rows} × ${out.columns}`);
|
|
178
|
+
}
|
|
179
|
+
assertDisjoint_(out, a, "matrixMultiply");
|
|
180
|
+
assertDisjoint_(out, b, "matrixMultiply");
|
|
181
|
+
const left = a.data;
|
|
182
|
+
const right = b.data;
|
|
183
|
+
const target = out.data;
|
|
184
|
+
target.fill(0);
|
|
185
|
+
for (let column = 0; column < columns; column++) {
|
|
186
|
+
const targetOffset = rows * column;
|
|
187
|
+
for (let k = 0; k < inner; k++) {
|
|
188
|
+
const factor = right[inner * column + k];
|
|
189
|
+
const leftOffset = rows * k;
|
|
190
|
+
for (let row = 0; row < rows; row++) {
|
|
191
|
+
target[targetOffset + row] =
|
|
192
|
+
target[targetOffset + row] + left[leftOffset + row] * factor;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Writes `m · x` into `out` (`m.rows` numbers) and returns it. Plain
|
|
200
|
+
* arithmetic; O(rows · columns).
|
|
201
|
+
*
|
|
202
|
+
* @throws {RangeError} When `x` does not hold `m.columns` numbers, `out` does
|
|
203
|
+
* not hold `m.rows`, `out` shares storage with `x` or `m`, or `m` breaks the
|
|
204
|
+
* {@link Matrix} invariant.
|
|
205
|
+
*/
|
|
206
|
+
export function matrixMultiplyVector(out, m, x) {
|
|
207
|
+
assertMatrixShape_(m, "matrixMultiplyVector");
|
|
208
|
+
const { data, rows, columns } = m;
|
|
209
|
+
if (x.length !== columns || out.length !== rows) {
|
|
210
|
+
throw new RangeError(`matrixMultiplyVector: a ${rows} × ${columns} matrix needs x of ${columns} and out of ${rows}, got ${x.length} and ${out.length}`);
|
|
211
|
+
}
|
|
212
|
+
if (viewsMayOverlap(out, x) || viewsMayOverlap(out, data)) {
|
|
213
|
+
throw new RangeError("matrixMultiplyVector: out must not share storage with an input");
|
|
214
|
+
}
|
|
215
|
+
out.fill(0);
|
|
216
|
+
for (let column = 0; column < columns; column++) {
|
|
217
|
+
const factor = x[column];
|
|
218
|
+
const offset = rows * column;
|
|
219
|
+
for (let row = 0; row < rows; row++) {
|
|
220
|
+
out[row] = out[row] + data[offset + row] * factor;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Writes `mᵀ · x` into `out` (`m.columns` numbers) and returns it: one dot
|
|
227
|
+
* product per column, in index order. O(rows · columns).
|
|
228
|
+
*
|
|
229
|
+
* @throws {RangeError} When `x` does not hold `m.rows` numbers, `out` does not
|
|
230
|
+
* hold `m.columns`, `out` shares storage with `x` or `m`, or `m` breaks the
|
|
231
|
+
* {@link Matrix} invariant.
|
|
232
|
+
*/
|
|
233
|
+
export function matrixTransposeMultiplyVector(out, m, x) {
|
|
234
|
+
assertMatrixShape_(m, "matrixTransposeMultiplyVector");
|
|
235
|
+
const { data, rows, columns } = m;
|
|
236
|
+
if (x.length !== rows || out.length !== columns) {
|
|
237
|
+
throw new RangeError(`matrixTransposeMultiplyVector: a ${rows} × ${columns} matrix needs x of ${rows} and out of ${columns}, got ${x.length} and ${out.length}`);
|
|
238
|
+
}
|
|
239
|
+
if (viewsMayOverlap(out, x) || viewsMayOverlap(out, data)) {
|
|
240
|
+
throw new RangeError("matrixTransposeMultiplyVector: out must not share storage with an input");
|
|
241
|
+
}
|
|
242
|
+
for (let column = 0; column < columns; column++) {
|
|
243
|
+
const offset = rows * column;
|
|
244
|
+
let sum = 0;
|
|
245
|
+
for (let row = 0; row < rows; row++) {
|
|
246
|
+
sum += data[offset + row] * x[row];
|
|
247
|
+
}
|
|
248
|
+
out[column] = sum;
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Exact equality of shape and entries: `−0` equals `+0`, and `NaN` equals
|
|
254
|
+
* nothing. See {@link matrixIsClose} for a tolerant test.
|
|
255
|
+
*/
|
|
256
|
+
export function matrixEquals(a, b) {
|
|
257
|
+
if (a.rows !== b.rows || a.columns !== b.columns) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
const left = a.data;
|
|
261
|
+
const right = b.data;
|
|
262
|
+
const count = a.rows * a.columns;
|
|
263
|
+
if (left.length !== count || right.length !== count) {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
for (let index = 0; index < count; index++) {
|
|
267
|
+
if (left[index] !== right[index]) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Same shape and every pair of entries close by `@ac-kit/core`'s `isCloseTo`
|
|
275
|
+
* with `options` (none means exact). The tolerance is required: a matrix has no
|
|
276
|
+
* units of its own.
|
|
277
|
+
*/
|
|
278
|
+
export function matrixIsClose(a, b, options) {
|
|
279
|
+
if (a.rows !== b.rows || a.columns !== b.columns) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
const left = a.data;
|
|
283
|
+
const right = b.data;
|
|
284
|
+
const count = a.rows * a.columns;
|
|
285
|
+
if (left.length !== count || right.length !== count) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
for (let index = 0; index < count; index++) {
|
|
289
|
+
if (!isCloseTo(left[index], right[index], options)) {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Whether every entry is finite: the check to run on data of unknown origin
|
|
297
|
+
* (operations never check values).
|
|
298
|
+
*/
|
|
299
|
+
export function matrixIsFinite(m) {
|
|
300
|
+
const { data } = m;
|
|
301
|
+
for (let index = 0; index < data.length; index++) {
|
|
302
|
+
if (!Number.isFinite(data[index])) {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
//# sourceMappingURL=matrix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matrix.js","sourceRoot":"","sources":["../src/matrix.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,SAAS,EAET,eAAe,GACf,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AA6BxD,SAAS,iBAAiB,CAAC,IAAY,EAAE,OAAe;IACvD,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IAC/B,aAAa,CAAC,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;IACrC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,GAAG,OAAO,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,UAAU,CACnB,8CAA8C,IAAI,MAAM,OAAO,EAAE,CACjE,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,OAAe;IACzD,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjC,OAAO,EAAE,IAAI,EAAE,IAAI,YAAY,CAAC,IAAI,GAAG,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAClE,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CACzB,IAAkB,EA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IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;QAC5C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,KAAK,CAAE,EAAE,OAAO,CAAC,EAAE,CAAC;YACtD,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS;IACvC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;IAEnB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAClD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACnC,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import type { Matrix } from "./matrix.js";
|
|
2
|
+
/** How a {@link QrDecomposition} factorises a matrix. */
|
|
3
|
+
export type QrDecomposeOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Row weights, `rows` finite non-negative numbers: the decomposition is then
|
|
6
|
+
* that of `√W · A`, so {@link QrDecomposition.solve} minimises `Σ wᵢ (bᵢ −
|
|
7
|
+
* (A·x)ᵢ)²` — weighted least squares, the inner step of IRLS. A zero weight
|
|
8
|
+
* removes its row. `null` or absent: every weight `1`.
|
|
9
|
+
*/
|
|
10
|
+
readonly weights?: Float64Array | null;
|
|
11
|
+
/**
|
|
12
|
+
* The reciprocal condition below which the leading triangle stops growing: a
|
|
13
|
+
* pivot column joins `R₁₁` only while the estimated `σmin(R₁₁) / σmax(R₁₁)`
|
|
14
|
+
* stays above this (LAPACK `xGELSY`'s `rcond`). In `[0, 1)`; `0` drops only
|
|
15
|
+
* exactly dependent columns. Defaults to `max(rows, columns) · ε`, the
|
|
16
|
+
* threshold of NumPy's `lstsq` and MATLAB's `rank`. Statistical software
|
|
17
|
+
* often uses `1e-7` (R's `lm`), which also drops nearly collinear
|
|
18
|
+
* predictors.
|
|
19
|
+
*/
|
|
20
|
+
readonly rankTolerance?: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Which least-squares solution a rank-deficient {@link QrDecomposition} gives;
|
|
24
|
+
* the two agree at full column rank.
|
|
25
|
+
*
|
|
26
|
+
* - `"basic"`: the coefficients of the `rank` independent pivot columns, `0` for
|
|
27
|
+
* the others (the aliased columns of a regression design). The answer of R's
|
|
28
|
+
* `lm` with `NA` read as `0`, cheap, and stable under the drop.
|
|
29
|
+
* - `"minimum-norm"`: of all least-squares solutions of the leading `rank` pivot
|
|
30
|
+
* columns' span, the one of least Euclidean norm — the pseudo-inverse answer,
|
|
31
|
+
* by a complete orthogonal decomposition (LAPACK `xGELSY`).
|
|
32
|
+
*/
|
|
33
|
+
export type QrSolution = "basic" | "minimum-norm";
|
|
34
|
+
/** Options of {@link QrDecomposition.covariance}. */
|
|
35
|
+
export type QrCovarianceOptions = {
|
|
36
|
+
/**
|
|
37
|
+
* The factor of `(AᵀWA)⁻¹`: the residual variance `σ̂² = RSS / (n − rank)` of
|
|
38
|
+
* an ordinary or weighted regression, the dispersion of a GLM, `1` (the
|
|
39
|
+
* default) for the bare inverse. Finite and non-negative.
|
|
40
|
+
*/
|
|
41
|
+
readonly scale?: number;
|
|
42
|
+
/** The solution whose covariance is wanted; `"basic"` by default. */
|
|
43
|
+
readonly solution?: QrSolution;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* A Householder QR decomposition with column pivoting, `A·P = Q·R`, reused
|
|
47
|
+
* across matrices of one shape: the rank-revealing factorisation behind least
|
|
48
|
+
* squares, weighted least squares (IRLS), regression covariances and
|
|
49
|
+
* leverages.
|
|
50
|
+
*
|
|
51
|
+
* Construct one per shape (`rows × columns`, any of the two larger), then call
|
|
52
|
+
* {@link decompose} as often as needed: every method after the constructor
|
|
53
|
+
* allocates nothing. The factors are LAPACK's (`xGEQP3`, Businger and Golub
|
|
54
|
+
* 1965): `R` upper trapezoidal with `|R₀₀| ≥ |R₁₁| ≥ …` in practice, `Q` held
|
|
55
|
+
* as `min(rows, columns)` reflectors, `P` the pivot order.
|
|
56
|
+
*
|
|
57
|
+
* **Rank.** The leading triangle `R₁₁` grows pivot by pivot while its
|
|
58
|
+
* reciprocal condition, estimated incrementally (Bischof's ICE, as LAPACK
|
|
59
|
+
* `xGELSY`), stays above `rankTolerance`; {@link rank} is its size and
|
|
60
|
+
* {@link pivots}`[0…rank)` the independent columns, the rest aliased.
|
|
61
|
+
* {@link reciprocalCondition} is that estimate of `σmin(R₁₁) / σmax(R₁₁)`, the
|
|
62
|
+
* reciprocal 2-norm condition of the independent columns of `√W·A`: an upper
|
|
63
|
+
* bound on the true value, usually within a factor of ten. A least-squares
|
|
64
|
+
* answer loses about `log₁₀(1 / reciprocalCondition²)` digits when the residual
|
|
65
|
+
* is large, `log₁₀(1 / reciprocalCondition)` when it is small.
|
|
66
|
+
*
|
|
67
|
+
* **Values.** A non-finite entry makes {@link decompose} return `false`: the
|
|
68
|
+
* rank is then `0`, the condition `NaN`, and every method writes `NaN`. Any
|
|
69
|
+
* finite entries and weights factorise: norms are scaled, and a matrix whose
|
|
70
|
+
* largest weighted entry lies outside `[2⁻⁵⁰⁰, 2⁵⁰⁰]` is first brought near `1`
|
|
71
|
+
* by a power of two (exact), its results scaled back — only entries below
|
|
72
|
+
* `2⁻¹⁰²²` times the largest lose digits, as negligible as they are, and
|
|
73
|
+
* results that are themselves out of range (a covariance of `1e−400`) underflow
|
|
74
|
+
* or overflow. Backward stable: the computed factors are exact for `A + E` with
|
|
75
|
+
* `‖E‖ ≤ c · rows · columns · ε · ‖A‖` (Higham, _Accuracy and Stability_, §19).
|
|
76
|
+
* {@link solve} brings `√W·b` near `1` the same way, so any finite right-hand
|
|
77
|
+
* side is solved.
|
|
78
|
+
*
|
|
79
|
+
* **Complexity.** {@link decompose}: O(rows · columns · min(rows, columns))
|
|
80
|
+
* time. {@link solve}: O(rows · min(rows, columns) + rank²), and once per
|
|
81
|
+
* decomposition O(rank² · (columns − rank)) for the first minimum-norm call.
|
|
82
|
+
* {@link covariance}: O(rank³), O(rank · columns²) for the minimum-norm one.
|
|
83
|
+
* {@link leverages}: O(rows · rank · min(rows, columns)). Memory: O(rows ·
|
|
84
|
+
* columns + columns²) numbers, allocated by the constructor.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* // Fit y ≈ β₀ + β₁·t through (0, 1), (1, 3), (2, 5), (3, 7).
|
|
89
|
+
* const design = matrixOfRows([[1, 0], [1, 1], [1, 2], [1, 3]]);
|
|
90
|
+
* const qr = new QrDecomposition(4, 2);
|
|
91
|
+
* qr.decompose(design);
|
|
92
|
+
* const beta = qr.solve(new Float64Array(2), Float64Array.of(1, 3, 5, 7));
|
|
93
|
+
* // beta ≈ [1, 2], qr.rank === 2, qr.residualSumOfSquares ≈ 0
|
|
94
|
+
* ```;
|
|
95
|
+
*/
|
|
96
|
+
export declare class QrDecomposition {
|
|
97
|
+
/** Rows of the matrices this decomposition takes. */
|
|
98
|
+
readonly rows: number;
|
|
99
|
+
/** Columns of the matrices this decomposition takes. */
|
|
100
|
+
readonly columns: number;
|
|
101
|
+
/**
|
|
102
|
+
* The incremental estimate of `σmin(R₁₁) / σmax(R₁₁)` for the independent
|
|
103
|
+
* columns of `√W·A` (see the class): `1` for one column, `0` at rank `0`,
|
|
104
|
+
* `NaN` after a non-finite matrix. Read-only for callers (a field, not a
|
|
105
|
+
* getter, so reading it never boxes a number).
|
|
106
|
+
*/
|
|
107
|
+
reciprocalCondition: number;
|
|
108
|
+
/**
|
|
109
|
+
* `Σ wᵢ (bᵢ − (A·x)ᵢ)²` at the last {@link solve}'s answer, from the part of
|
|
110
|
+
* `Qᵀ·√W·b` outside the span of `R₁₁`, so it costs nothing extra; `0` before
|
|
111
|
+
* any solve. For the minimum-norm answer it neglects the dropped `R₂₂`, whose
|
|
112
|
+
* size `rankTolerance` bounds. Read-only for callers.
|
|
113
|
+
*/
|
|
114
|
+
residualSumOfSquares: number;
|
|
115
|
+
private readonly reflectors_;
|
|
116
|
+
private readonly factors_;
|
|
117
|
+
private readonly tau_;
|
|
118
|
+
private readonly pivots_;
|
|
119
|
+
private readonly norms_;
|
|
120
|
+
private readonly sqrtWeights_;
|
|
121
|
+
private readonly work_;
|
|
122
|
+
private readonly vector_;
|
|
123
|
+
private readonly estimates_;
|
|
124
|
+
private readonly trapezoid_;
|
|
125
|
+
private readonly trapezoidTau_;
|
|
126
|
+
private readonly square_;
|
|
127
|
+
/**
|
|
128
|
+
* `[residual sum of squares, reciprocal condition, ICE scratch × 6, rank
|
|
129
|
+
* tolerance, covariance scale]`: numbers passed between methods through an
|
|
130
|
+
* array, as a double crossing a call the engine does not inline is boxed.
|
|
131
|
+
*/
|
|
132
|
+
private readonly scalars_;
|
|
133
|
+
private rank_;
|
|
134
|
+
/** `A'` = `2^scaleExponent_ · √W·A` is what `factors_` decomposes. */
|
|
135
|
+
private scaleExponent_;
|
|
136
|
+
/** About `log₂ max √wᵢ`, `0` without weights: `solve` scales `√W·b` by it. */
|
|
137
|
+
private weightExponent_;
|
|
138
|
+
private valid_;
|
|
139
|
+
private weighted_;
|
|
140
|
+
private completed_;
|
|
141
|
+
/**
|
|
142
|
+
* Allocates the factors and every working array; until the first
|
|
143
|
+
* {@link decompose}, the decomposition is that of the zero matrix.
|
|
144
|
+
*
|
|
145
|
+
* @throws {RangeError} When `rows` or `columns` is not a non-negative safe
|
|
146
|
+
* integer, or `rows · columns` or `columns²` is not a safe integer.
|
|
147
|
+
*/
|
|
148
|
+
constructor(rows: number, columns: number);
|
|
149
|
+
/**
|
|
150
|
+
* The numerical rank: the size of the well-conditioned leading triangle
|
|
151
|
+
* `R₁₁`, at most `min(rows, columns)`; `0` for a zero or non-finite matrix.
|
|
152
|
+
*/
|
|
153
|
+
get rank(): number;
|
|
154
|
+
/**
|
|
155
|
+
* The column order `P`: `pivots[k]` is the column of `A` in position `k` of
|
|
156
|
+
* `R`. The first {@link rank} are the independent columns, the others aliased.
|
|
157
|
+
* The array is the decomposition's own: read it, never write it.
|
|
158
|
+
*/
|
|
159
|
+
get pivots(): Int32Array;
|
|
160
|
+
/**
|
|
161
|
+
* Factorises `√W · matrix` (`matrix` itself without weights) and decides its
|
|
162
|
+
* rank. `matrix` is only read; the previous decomposition is replaced.
|
|
163
|
+
*
|
|
164
|
+
* @returns `false`, with rank `0` and `NaN` from every method, when an entry
|
|
165
|
+
* of `matrix` is not finite; `true` otherwise.
|
|
166
|
+
* @throws {RangeError} When `matrix` is not `rows × columns` or breaks the
|
|
167
|
+
* {@link Matrix} invariant, `weights` does not hold `rows` finite
|
|
168
|
+
* non-negative numbers, or `rankTolerance` is not in `[0, 1)`. Nothing is
|
|
169
|
+
* changed then.
|
|
170
|
+
*/
|
|
171
|
+
decompose(matrix: Matrix, options?: QrDecomposeOptions): boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Solves the least-squares problem `min ‖√W·(b − A·x)‖` for the last
|
|
174
|
+
* decomposed `A`, and leaves its {@link residualSumOfSquares}.
|
|
175
|
+
*
|
|
176
|
+
* @param out - The solution `x`, `columns` numbers, in the columns' own
|
|
177
|
+
* order. May be `right` when the lengths agree.
|
|
178
|
+
* @param right - The right-hand side `b`, `rows` numbers (unweighted: the
|
|
179
|
+
* weights of {@link decompose} apply). Non-finite values propagate.
|
|
180
|
+
* @param solution - Which answer a rank-deficient matrix gives; see
|
|
181
|
+
* {@link QrSolution}. `"basic"` by default.
|
|
182
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
183
|
+
* @throws {RangeError} When the lengths are wrong or `solution` is not a
|
|
184
|
+
* {@link QrSolution}.
|
|
185
|
+
*/
|
|
186
|
+
solve(out: Float64Array, right: Float64Array, solution?: QrSolution): Float64Array;
|
|
187
|
+
/**
|
|
188
|
+
* Writes `scale · (AᵀWA)⁻¹` for the independent columns into `out` (`columns
|
|
189
|
+
* × columns`, the columns' own order): the covariance of the least-squares
|
|
190
|
+
* coefficients. `"basic"`: `scale · P·(R₁₁ᵀR₁₁)⁻¹·Pᵀ` on the independent
|
|
191
|
+
* columns and `NaN` on every row and column of an aliased one (their
|
|
192
|
+
* coefficient is not estimable). `"minimum-norm"`: `scale · (AᵀWA)⁺`, the
|
|
193
|
+
* pseudo-inverse of the rank-`rank` truncation, defined everywhere. They
|
|
194
|
+
* agree at full rank.
|
|
195
|
+
*
|
|
196
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
197
|
+
* @throws {RangeError} When `out` is not `columns × columns` or breaks the
|
|
198
|
+
* {@link Matrix} invariant, `scale` is not finite and non-negative, or
|
|
199
|
+
* `solution` is not a {@link QrSolution}.
|
|
200
|
+
*/
|
|
201
|
+
covariance(out: Matrix, options?: QrCovarianceOptions): Matrix;
|
|
202
|
+
/**
|
|
203
|
+
* Writes the leverages — the diagonal of the hat matrix
|
|
204
|
+
* `√W·A·(AᵀWA)⁻¹·AᵀW·√W` on the independent columns, `hᵢ = Σₖ₍ₖ₌₀…ᵣₐₙₖ₋₁₎
|
|
205
|
+
* Q[i, k]²` — into `out` (`rows` numbers), each in `[0, 1]` and summing to
|
|
206
|
+
* the rank; `0` for a row of weight zero. The input of studentised residuals,
|
|
207
|
+
* Cook's distance and robust-regression weight adjustments.
|
|
208
|
+
*
|
|
209
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
210
|
+
* @throws {RangeError} When `out` does not hold `rows` numbers.
|
|
211
|
+
*/
|
|
212
|
+
leverages(out: Float64Array): Float64Array;
|
|
213
|
+
/**
|
|
214
|
+
* Writes `Qᵀ · vector` into `out` (`rows` numbers each; `out` may be
|
|
215
|
+
* `vector`). `Q` is the full orthogonal factor of `√W·A`: weight `vector`
|
|
216
|
+
* first to stay in the weighted problem.
|
|
217
|
+
*
|
|
218
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
219
|
+
* @throws {RangeError} When a length is not `rows`.
|
|
220
|
+
*/
|
|
221
|
+
applyQTranspose(out: Float64Array, vector: Float64Array): Float64Array;
|
|
222
|
+
/**
|
|
223
|
+
* Writes `Q · vector` into `out` (`rows` numbers each; `out` may be
|
|
224
|
+
* `vector`), the inverse of {@link applyQTranspose}.
|
|
225
|
+
*
|
|
226
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
227
|
+
* @throws {RangeError} When a length is not `rows`.
|
|
228
|
+
*/
|
|
229
|
+
applyQ(out: Float64Array, vector: Float64Array): Float64Array;
|
|
230
|
+
/**
|
|
231
|
+
* Writes the thin orthogonal factor — the first `min(rows, columns)` columns
|
|
232
|
+
* of `Q` — into `out` (`rows × min(rows, columns)`).
|
|
233
|
+
*
|
|
234
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
235
|
+
* @throws {RangeError} When `out` has the wrong shape or breaks the
|
|
236
|
+
* {@link Matrix} invariant.
|
|
237
|
+
*/
|
|
238
|
+
q(out: Matrix): Matrix;
|
|
239
|
+
/**
|
|
240
|
+
* Writes the triangular factor `R` (`min(rows, columns) × columns`, upper
|
|
241
|
+
* trapezoidal, columns in pivot order) into `out`, zeros below the diagonal.
|
|
242
|
+
*
|
|
243
|
+
* @returns `out`; all `NaN` after a non-finite matrix.
|
|
244
|
+
* @throws {RangeError} When `out` has the wrong shape or breaks the
|
|
245
|
+
* {@link Matrix} invariant.
|
|
246
|
+
*/
|
|
247
|
+
r(out: Matrix): Matrix;
|
|
248
|
+
private prepareReflection_;
|
|
249
|
+
/** `xGEQP3`'s unblocked loop (`xLAQP2`) over `factors_`. */
|
|
250
|
+
private factorize_;
|
|
251
|
+
/**
|
|
252
|
+
* Swaps columns `step` and `pivot` of `factors_`, with their pivots and
|
|
253
|
+
* norms.
|
|
254
|
+
*/
|
|
255
|
+
private swapColumns_;
|
|
256
|
+
/** Incremental condition estimation over the pivoted diagonal (`xGELSY`). */
|
|
257
|
+
private estimateRank_;
|
|
258
|
+
/** `vector ← Qᵀ·vector`: the reflectors `H₀, H₁, …` in turn. */
|
|
259
|
+
private reflectTranspose_;
|
|
260
|
+
/** `vector ← H₀·H₁·…·H_last·vector`. */
|
|
261
|
+
private reflect_;
|
|
262
|
+
/**
|
|
263
|
+
* Column `column` of `Q` into `work_`: `Q·e_column`, where only the
|
|
264
|
+
* reflectors up to `column` act (the later ones are zero above their row).
|
|
265
|
+
*/
|
|
266
|
+
private basisColumn_;
|
|
267
|
+
/**
|
|
268
|
+
* The complete orthogonal decomposition `[R₁₁ R₁₂] = [T 0]·Z` of the leading
|
|
269
|
+
* `rank` rows (LAPACK `xTZRZF`), once per decomposition: `Z = G₀·G₁·…`, `Gᵢ`
|
|
270
|
+
* reflecting entry `i` against entries `rank…columns − 1` of row `i`.
|
|
271
|
+
*/
|
|
272
|
+
private complete_;
|
|
273
|
+
/** `y ← Zᵀ·y = G_{rank−1}·…·G₀·y` (each `Gᵢ` symmetric). */
|
|
274
|
+
private applyZTranspose_;
|
|
275
|
+
/**
|
|
276
|
+
* `square_ ← Zᵀ·square_·Z`, the symmetric `columns × columns` matrix
|
|
277
|
+
* conjugated by each `Gᵢ` in turn: with `p = τ·M·u` and `w = p −
|
|
278
|
+
* (τ/2)(uᵀp)·u`, `G·M·G = M − w·uᵀ − u·wᵀ` (LAPACK `xSYR2` form). `u` is `1`
|
|
279
|
+
* at `i` and the reflector's tail at `rank…columns − 1`.
|
|
280
|
+
*/
|
|
281
|
+
private conjugateByZ_;
|
|
282
|
+
}
|
|
283
|
+
//# sourceMappingURL=qr-decomposition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qr-decomposition.d.ts","sourceRoot":"","sources":["../src/qr-decomposition.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,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