@numero-uno-jpn/rstats 0.15.3 → 0.17.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 +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +24 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/linalg/ops.js
CHANGED
|
@@ -6,6 +6,16 @@
|
|
|
6
6
|
// vector, which R treats as a row or a column as the operation needs. Element-wise operations recycle a shorter
|
|
7
7
|
// vector over a matrix in column-major order (R's storage order), as R does.
|
|
8
8
|
import { cbindFrames, rbindFrames } from "../descriptive/dataFrames.js";
|
|
9
|
+
import { maxOf } from "../internal/extremes.js";
|
|
10
|
+
import { optionalArgs } from "../internal/optionalArgs.js";
|
|
11
|
+
import { checkDot, checkedDot } from "./dot.js";
|
|
12
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
13
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
14
|
+
import { optionsIn } from "../internal/optionalArgs.js";
|
|
15
|
+
const OUTER_KEYS = ["fun"];
|
|
16
|
+
const DIAG_KEYS = ["nrow", "ncol"];
|
|
17
|
+
const MATRIX_KEYS = ["nrow", "ncol", "byrow"];
|
|
18
|
+
const SEQ_KEYS = ["by", "lengthOut"];
|
|
9
19
|
function isMatrix(x) {
|
|
10
20
|
return typeof x !== "number" && x.length > 0 && Array.isArray(x[0]);
|
|
11
21
|
}
|
|
@@ -70,14 +80,29 @@ export function matMul(x, y) {
|
|
|
70
80
|
if (a.length === 0 || b.length !== n)
|
|
71
81
|
throw new RangeError("non-conformable arguments");
|
|
72
82
|
checkRows(a);
|
|
73
|
-
|
|
83
|
+
// an inner dimension of 0 (an n x 0 matrix times an empty vector) gives n x 1 zeros, as R (b[0] was read)
|
|
84
|
+
const p = b.length > 0 ? checkRows(b) : 1;
|
|
85
|
+
// the smallest nonzero |entry| of each column of b and row of a: tiny sums checked for underflow only (#395)
|
|
86
|
+
const minB = new Array(p).fill(Infinity);
|
|
87
|
+
for (const r of b)
|
|
88
|
+
for (let j = 0; j < p; j++) {
|
|
89
|
+
const v = Math.abs(r[j]);
|
|
90
|
+
if (v !== 0 && v < minB[j])
|
|
91
|
+
minB[j] = v;
|
|
92
|
+
}
|
|
74
93
|
return a.map((row) => {
|
|
94
|
+
let minRow = Infinity;
|
|
95
|
+
for (const t of row) {
|
|
96
|
+
const v = Math.abs(t);
|
|
97
|
+
if (v !== 0 && v < minRow)
|
|
98
|
+
minRow = v;
|
|
99
|
+
}
|
|
75
100
|
const out = new Array(p).fill(0);
|
|
76
101
|
for (let j = 0; j < p; j++) {
|
|
77
102
|
let s = 0;
|
|
78
103
|
for (let k = 0; k < n; k++)
|
|
79
104
|
s += row[k] * b[k][j];
|
|
80
|
-
out[j] = s;
|
|
105
|
+
out[j] = checkDot(s, minRow * minB[j]) ? checkedDot(row, b.map((r) => r[j]), s) : s;
|
|
81
106
|
}
|
|
82
107
|
return out;
|
|
83
108
|
});
|
|
@@ -178,19 +203,9 @@ const OUTER_OPS = {
|
|
|
178
203
|
"/": (a, b) => a / b,
|
|
179
204
|
"^": (a, b) => a ** b,
|
|
180
205
|
};
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```ts
|
|
187
|
-
* outer([1, 2], [1, 2, 3]); // [[1, 2, 3], [2, 4, 6]]
|
|
188
|
-
* outer([1, 2], [1, 2, 3], "+"); // [[2, 3, 4], [3, 4, 5]]
|
|
189
|
-
* ```
|
|
190
|
-
*
|
|
191
|
-
* @category Linear algebra
|
|
192
|
-
*/
|
|
193
|
-
export function outer(x, y, fun = "*") {
|
|
206
|
+
export function outer(x, y, ...args) {
|
|
207
|
+
checkOptions(optionsIn(args), OUTER_KEYS, "outer"); // generated: option-keys.mjs
|
|
208
|
+
const fun = optionalArgs(["fun"], args).fun ?? "*";
|
|
194
209
|
const f = typeof fun === "function" ? fun : OUTER_OPS[fun];
|
|
195
210
|
if (f === undefined)
|
|
196
211
|
throw new RangeError(`invalid fun "${String(fun)}"`);
|
|
@@ -230,6 +245,7 @@ export function t(x) {
|
|
|
230
245
|
* @category Linear algebra
|
|
231
246
|
*/
|
|
232
247
|
export function diag(x, options = {}) {
|
|
248
|
+
checkOptions(options, DIAG_KEYS, "diag"); // generated: option-keys.mjs
|
|
233
249
|
if (isMatrix(x)) {
|
|
234
250
|
const nc = checkRows(x);
|
|
235
251
|
return Array.from({ length: Math.min(x.length, nc) }, (_, i) => x[i][i]);
|
|
@@ -245,20 +261,26 @@ export function diag(x, options = {}) {
|
|
|
245
261
|
}
|
|
246
262
|
else {
|
|
247
263
|
values = v;
|
|
248
|
-
nrow = options.nrow ?? v.length;
|
|
264
|
+
nrow = dimArg(options.nrow, "nrow") ?? v.length;
|
|
249
265
|
}
|
|
250
|
-
const ncol = options.ncol ?? nrow;
|
|
266
|
+
const ncol = dimArg(options.ncol, "ncol") ?? nrow;
|
|
267
|
+
// as R: no values for a diagonal of positive length (i % 0 put undefined on it)
|
|
268
|
+
if (values.length === 0 && Math.min(nrow, ncol) > 0)
|
|
269
|
+
throw new RangeError("'x' must have positive length");
|
|
251
270
|
const out = Array.from({ length: nrow }, () => new Array(ncol).fill(0));
|
|
252
271
|
for (let i = 0; i < Math.min(nrow, ncol); i++)
|
|
253
272
|
out[i][i] = values[i % values.length];
|
|
254
273
|
return nrow === 0 ? [] : out;
|
|
255
274
|
}
|
|
256
275
|
/** Columns of an operand for binding: a matrix's columns, or a vector (scalar) as one column of `n` values */
|
|
257
|
-
function bindParts(
|
|
276
|
+
function bindParts(all, byColumn) {
|
|
277
|
+
// as R, vectors of length 0 are left out when anything else is given (recycling them put undefined in the result)
|
|
278
|
+
const given = (a) => isMatrix(a) || asVector(a).length > 0;
|
|
279
|
+
const args = all.some(given) ? all.filter(given) : all;
|
|
258
280
|
const parts = args.map((a) => (isMatrix(a) ? (byColumn ? t(a) : a.map((r) => [...r])) : [asVector(a)]));
|
|
259
281
|
// the common length: the matrices' rows (cbind) or columns (rbind), else the longest vector
|
|
260
282
|
const mats = args.filter(isMatrix);
|
|
261
|
-
const len = mats.length > 0 ? (byColumn ? mats[0].length : checkRows(mats[0])) :
|
|
283
|
+
const len = mats.length > 0 ? (byColumn ? mats[0].length : checkRows(mats[0])) : maxOf(parts.map((p) => p[0].length));
|
|
262
284
|
mats.forEach((m, k) => {
|
|
263
285
|
const l = byColumn ? m.length : checkRows(m);
|
|
264
286
|
if (l !== len) {
|
|
@@ -288,6 +310,14 @@ export function rbind(...args) {
|
|
|
288
310
|
const frames = framesOf(args);
|
|
289
311
|
return frames === undefined ? bindParts(args, false) : rbindFrames(frames);
|
|
290
312
|
}
|
|
313
|
+
/** A dimension argument truncated to an integer as R's as.integer does; a negative or non-finite one is an error */
|
|
314
|
+
function dimArg(d, name) {
|
|
315
|
+
if (d === undefined)
|
|
316
|
+
return undefined;
|
|
317
|
+
if (!Number.isFinite(d) || d <= -1)
|
|
318
|
+
throw new RangeError(`invalid '${name}' value`);
|
|
319
|
+
return Math.trunc(d) + 0;
|
|
320
|
+
}
|
|
291
321
|
/**
|
|
292
322
|
* Build a matrix from data, recycled to fill it. Equivalent to R's `matrix()`. Filled by columns unless `byrow`.
|
|
293
323
|
*
|
|
@@ -300,9 +330,11 @@ export function rbind(...args) {
|
|
|
300
330
|
* @category Linear algebra
|
|
301
331
|
*/
|
|
302
332
|
export function matrix(data, options = {}) {
|
|
333
|
+
checkOptions(options, MATRIX_KEYS, "matrix"); // generated: option-keys.mjs
|
|
303
334
|
const v = asVector(data);
|
|
304
335
|
const len = v.length;
|
|
305
|
-
let
|
|
336
|
+
let nrow = dimArg(options.nrow, "nrow");
|
|
337
|
+
let ncol = dimArg(options.ncol, "ncol");
|
|
306
338
|
if (nrow === undefined && ncol === undefined) {
|
|
307
339
|
nrow = len;
|
|
308
340
|
ncol = 1;
|
|
@@ -353,6 +385,7 @@ export function kronecker(x, y) {
|
|
|
353
385
|
* @category Linear algebra
|
|
354
386
|
*/
|
|
355
387
|
export function seq(from, to, options = {}) {
|
|
388
|
+
checkOptions(options, SEQ_KEYS, "seq"); // generated: option-keys.mjs
|
|
356
389
|
const { by, lengthOut } = options;
|
|
357
390
|
if (to === undefined && by === undefined && lengthOut === undefined && from !== undefined) {
|
|
358
391
|
if (!Number.isFinite(from))
|
|
@@ -368,24 +401,32 @@ export function seq(from, to, options = {}) {
|
|
|
368
401
|
return colon(from ?? 1, to ?? 1);
|
|
369
402
|
const a = from ?? 1;
|
|
370
403
|
const b = to ?? 1;
|
|
371
|
-
|
|
404
|
+
// Finite ends whose difference overflows: in half coordinates, doubled (exact scalings, so the values round as
|
|
405
|
+
// they would without the overflow; seq(-1e308, 1e308, { by: 1e308 }) was an error, R gives -1e308, 0, 1e308)
|
|
406
|
+
const half = !Number.isFinite(b - a);
|
|
407
|
+
const f = half ? 0.5 : 1;
|
|
408
|
+
const del = b * f - a * f;
|
|
372
409
|
if (del === 0 && b === 0)
|
|
373
410
|
return [b];
|
|
374
|
-
const n = del / by;
|
|
411
|
+
const n = del / (by * f);
|
|
375
412
|
if (!Number.isFinite(n)) {
|
|
376
413
|
if (by === 0 && del === 0)
|
|
377
414
|
return [a];
|
|
378
415
|
throw new RangeError("invalid '(to - from)/by'");
|
|
379
416
|
}
|
|
380
|
-
|
|
417
|
+
// an infinite `by` makes n a signed zero, which passes n < 0: its direction is checked against `to - from` (#274)
|
|
418
|
+
if (n < 0 || (!Number.isFinite(by) && del !== 0 && del > 0 !== by > 0)) {
|
|
381
419
|
throw new RangeError("wrong sign in 'by' argument");
|
|
382
|
-
|
|
420
|
+
}
|
|
421
|
+
if (Math.abs(del) / Math.max(Math.abs(b * f), Math.abs(a * f)) < 100 * Number.EPSILON)
|
|
383
422
|
return [a];
|
|
384
423
|
const k = Math.trunc(n + 1e-10);
|
|
424
|
+
const at = half ? (i) => 2 * (a / 2 + i * (by / 2)) : (i) => a + i * by;
|
|
385
425
|
// the last value may overshoot `to` by rounding: hold it at `to`
|
|
386
|
-
return Array.from({ length: k + 1 }, (_, i) => (by > 0 ? Math.min(
|
|
426
|
+
return Array.from({ length: k + 1 }, (_, i) => (i === 0 ? a : by > 0 ? Math.min(at(i), b) : Math.max(at(i), b)));
|
|
387
427
|
}
|
|
388
|
-
|
|
428
|
+
// rounded up first, as R: a length in (-1, 0) is 0 (#98)
|
|
429
|
+
if (!Number.isFinite(lengthOut) || lengthOut <= -1)
|
|
389
430
|
throw new RangeError("'length.out' must be a non-negative number");
|
|
390
431
|
const len = Math.ceil(lengthOut);
|
|
391
432
|
if (len === 0)
|
|
@@ -397,6 +438,13 @@ export function seq(from, to, options = {}) {
|
|
|
397
438
|
return [a, b].slice(0, len);
|
|
398
439
|
if (a === b)
|
|
399
440
|
return new Array(len).fill(a);
|
|
441
|
+
if (!Number.isFinite(b - a)) {
|
|
442
|
+
// finite ends whose difference overflows: in half coordinates, doubled (as seqLength in src/internal/seq.ts;
|
|
443
|
+
// the interior values were NaN)
|
|
444
|
+
const h = a / 2;
|
|
445
|
+
const halfStep = (b / 2 - h) / (len - 1);
|
|
446
|
+
return Array.from({ length: len }, (_, i) => (i === 0 ? a : i === len - 1 ? b : 2 * (h + i * halfStep)));
|
|
447
|
+
}
|
|
400
448
|
const step = (b - a) / (len - 1);
|
|
401
449
|
return Array.from({ length: len }, (_, i) => (i === 0 ? a : i === len - 1 ? b : a + i * step));
|
|
402
450
|
}
|
package/dist/linalg/qr.d.ts
CHANGED
|
@@ -304,7 +304,8 @@ export declare function qrR(q: QR, options?: {
|
|
|
304
304
|
*
|
|
305
305
|
* @param q A QR decomposition from {@link qr}.
|
|
306
306
|
* @param options `complete`: whether the default `ncol` is the number of rows (`true`) or `min(nrow, ncol)` (`false`, the default).
|
|
307
|
-
* `ncol`: the number of columns to return
|
|
307
|
+
* `ncol`: the number of columns to return, from 0 to `max(nrow, ncol)`; columns beyond the original ones are taken
|
|
308
|
+
* from `Q` (as in R). A larger `ncol` is an error (R pads zero columns).
|
|
308
309
|
* @returns The reconstructed matrix as an array of rows.
|
|
309
310
|
*
|
|
310
311
|
* @example
|
package/dist/linalg/qr.js
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
// - Columns are processed left to right. Before processing a column, if its remaining norm is below its original norm × tol
|
|
8
8
|
// (always, for a column whose original norm is 0), it is moved to the right end and the rest shift left. rank drops by the number moved
|
|
9
9
|
// - Columns moved to the right end are still transformed when their turn comes (only the column at the last row is left unprocessed)
|
|
10
|
-
// - Remaining norms are updated after each transformation by the LINPACK method, and recomputed when cancellation occurs
|
|
10
|
+
// - Remaining norms are updated after each transformation by the LINPACK method, and recomputed when cancellation occurs:
|
|
11
|
+
// when 1 - (|x|/norm)^2 is below 1e-6, as R's output shows (the rule from issue #57; LINPACK's own test missed the
|
|
12
|
+
// cancellation near the rank tolerance and dropped an independent column)
|
|
11
13
|
// - rank = min(number of columns - number moved, number of rows). The public pivot is 0-based to suit JS (internally 1-based)
|
|
12
14
|
// lapack: true is the same column-pivoted decomposition as the LAPACK 3.12.0 reference SRC/dgeqp3.f
|
|
13
15
|
// (dlaqp2.f, dlarfg.f, dlarf.f). rank is always min(rows, cols) (as in R; rank is not determined).
|
|
@@ -15,7 +17,16 @@
|
|
|
15
17
|
//
|
|
16
18
|
// Difference from R: with lapack: true, results for matrices containing NaN or Inf are not necessarily the same as R
|
|
17
19
|
// (R does not raise an error either and returns meaningless values).
|
|
18
|
-
import {
|
|
20
|
+
import { ldexp } from "../internal/summary.js";
|
|
21
|
+
import { decompositionExponent, fromMat, isMatrixInput, newMat, rangeExponent, toMat, toMatOrColumn } from "./matrix.js";
|
|
22
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
23
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
24
|
+
const QR_KEYS = ["tol", "lapack"];
|
|
25
|
+
const QR_Q_KEYS = ["complete", "dvec"];
|
|
26
|
+
const QR_R_KEYS = ["complete"];
|
|
27
|
+
const QR_X_KEYS = ["complete", "ncol"];
|
|
28
|
+
const QR_FITTED_KEYS = ["k"];
|
|
29
|
+
const QR_SOLVE_KEYS = ["tol"];
|
|
19
30
|
/**
|
|
20
31
|
* QR decomposition. Equivalent to R's `qr()`.
|
|
21
32
|
*
|
|
@@ -41,9 +52,26 @@ import { fromMat, isMatrixInput, newMat, toMat, toMatOrColumn } from "./matrix.j
|
|
|
41
52
|
* @category Linear algebra
|
|
42
53
|
*/
|
|
43
54
|
export function qr(x, options = {}) {
|
|
55
|
+
checkOptions(options, QR_KEYS, "qr"); // generated: option-keys.mjs
|
|
44
56
|
const a = toMat(x);
|
|
45
57
|
if (options.lapack) {
|
|
46
|
-
const
|
|
58
|
+
const a0 = a.data.slice();
|
|
59
|
+
let { tau, pivot } = geqp3(a);
|
|
60
|
+
// Where the decomposition of a finite matrix overflowed (entries near 1e308), it is made again of the matrix divided
|
|
61
|
+
// by a power of 2 (as in svd; the whole matrix, since the pivoting compares the columns' norms), and R scaled back;
|
|
62
|
+
// the Householder vectors and tau have no units (#130). A finite result is kept as it is
|
|
63
|
+
if (a0.every(Number.isFinite) && !(a.data.every(Number.isFinite) && tau.every(Number.isFinite))) {
|
|
64
|
+
const k = decompositionExponent(a0);
|
|
65
|
+
if (k !== 0) {
|
|
66
|
+
const m = a.nrow;
|
|
67
|
+
for (let i = 0; i < a0.length; i++)
|
|
68
|
+
a.data[i] = ldexp(a0[i], -k);
|
|
69
|
+
({ tau, pivot } = geqp3(a));
|
|
70
|
+
for (let j = 0; j < a.ncol; j++)
|
|
71
|
+
for (let i = 0; i <= Math.min(j, m - 1); i++)
|
|
72
|
+
a.data[i + j * m] = ldexp(a.data[i + j * m], k);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
47
75
|
// One Householder scalar per reflection: min(nrow, ncol) elements, as in R
|
|
48
76
|
const qraux = Array.from(tau);
|
|
49
77
|
return { qr: fromMat(a), rank: Math.min(a.nrow, a.ncol), qraux, pivot: pivot.map((v) => v - 1), useLapack: true };
|
|
@@ -51,7 +79,38 @@ export function qr(x, options = {}) {
|
|
|
51
79
|
for (const v of a.data)
|
|
52
80
|
if (!Number.isFinite(v))
|
|
53
81
|
throw new RangeError("the matrix contains NaN or infinite values");
|
|
54
|
-
const
|
|
82
|
+
const tol = options.tol ?? 1e-7;
|
|
83
|
+
// a negative tolerance has no meaning (R accepts it, and no column is then ever dependent); Infinity is an error, as
|
|
84
|
+
// R's "NA/NaN/Inf in foreign function call" (it took every column as dependent, rank 0)
|
|
85
|
+
if (!(tol >= 0 && tol < Infinity))
|
|
86
|
+
throw new RangeError("'tol' must be a non-negative finite number");
|
|
87
|
+
const a0 = a.data.slice();
|
|
88
|
+
let { qraux, pivot, rank, transformed } = linpackQR(a, tol);
|
|
89
|
+
// Where the decomposition overflowed (sums of products of entries near 1e308: qr(matrix(1e308, 2, 2)) had rank 2 and
|
|
90
|
+
// -Inf in R, as in R, and lm on such a predictor gave NaN coefficients), it is made again with each column outside
|
|
91
|
+
// [2^-256, 2^256] divided by the power of 2 of its largest entry (exact). LINPACK's tests are relative to each column,
|
|
92
|
+
// so the pivoting and the rank are those of the exact decomposition; the upper triangle (R) and the norms kept in
|
|
93
|
+
// qraux for columns not transformed are scaled back, the Householder vectors and the other qraux have no units (#130).
|
|
94
|
+
// A finite result is kept as it is
|
|
95
|
+
if (!(a.data.every(Number.isFinite) && qraux.every(Number.isFinite))) {
|
|
96
|
+
const n = a.nrow;
|
|
97
|
+
const e = Array.from({ length: a.ncol }, (_, j) => rangeExponent(a0.subarray(j * n, (j + 1) * n)));
|
|
98
|
+
if (e.some((v) => v !== 0)) {
|
|
99
|
+
for (let j = 0; j < a.ncol; j++)
|
|
100
|
+
for (let i = 0; i < n; i++)
|
|
101
|
+
a.data[i + j * n] = ldexp(a0[i + j * n], -e[j]);
|
|
102
|
+
({ qraux, pivot, rank, transformed } = linpackQR(a, tol));
|
|
103
|
+
for (let j = 0; j < a.ncol; j++) {
|
|
104
|
+
const ej = e[pivot[j] - 1];
|
|
105
|
+
if (ej === 0)
|
|
106
|
+
continue;
|
|
107
|
+
for (let i = 0; i <= Math.min(j, n - 1); i++)
|
|
108
|
+
a.data[i + j * n] = ldexp(a.data[i + j * n], ej);
|
|
109
|
+
if (!transformed[j])
|
|
110
|
+
qraux[j] = ldexp(qraux[j], ej);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
55
114
|
return { qr: fromMat(a), rank, qraux: Array.from(qraux), pivot: pivot.map((v) => v - 1), useLapack: false };
|
|
56
115
|
}
|
|
57
116
|
/** 2-norm (divide by the largest absolute value before squaring and summing, to avoid overflow and underflow) */
|
|
@@ -74,12 +133,11 @@ function linpackQR(A, tol) {
|
|
|
74
133
|
const p = A.ncol;
|
|
75
134
|
const a = A.data;
|
|
76
135
|
const qraux = new Float64Array(p);
|
|
77
|
-
const work = new Float64Array(p); // norms used for updating (replaced when recomputed)
|
|
78
136
|
const orig = new Float64Array(p); // original norms (used for the test; 1 if 0)
|
|
137
|
+
const transformed = new Uint8Array(p); // 1 where qraux holds a Householder vector's leading element (not a norm)
|
|
79
138
|
const pivot = Array.from({ length: p }, (_, j) => j + 1);
|
|
80
139
|
for (let j = 0; j < p; j++) {
|
|
81
140
|
qraux[j] = nrm2(a, j * n, n);
|
|
82
|
-
work[j] = qraux[j];
|
|
83
141
|
orig[j] = qraux[j] === 0 ? 1 : qraux[j];
|
|
84
142
|
}
|
|
85
143
|
const col = new Float64Array(n);
|
|
@@ -89,15 +147,13 @@ function linpackQR(A, tol) {
|
|
|
89
147
|
// Move columns whose norm has become small to the right end (shifting the rest left)
|
|
90
148
|
while (l < k && qraux[l] < orig[l] * tol) {
|
|
91
149
|
col.set(a.subarray(l * n, (l + 1) * n));
|
|
92
|
-
const [q0,
|
|
150
|
+
const [q0, o0, p0] = [qraux[l], orig[l], pivot[l]];
|
|
93
151
|
a.copyWithin(l * n, (l + 1) * n, p * n);
|
|
94
152
|
a.set(col, (p - 1) * n);
|
|
95
153
|
qraux.copyWithin(l, l + 1);
|
|
96
|
-
work.copyWithin(l, l + 1);
|
|
97
154
|
orig.copyWithin(l, l + 1);
|
|
98
155
|
pivot.copyWithin(l, l + 1);
|
|
99
156
|
qraux[p - 1] = q0;
|
|
100
|
-
work[p - 1] = w0;
|
|
101
157
|
orig[p - 1] = o0;
|
|
102
158
|
pivot[p - 1] = p0;
|
|
103
159
|
k--;
|
|
@@ -125,22 +181,17 @@ function linpackQR(A, tol) {
|
|
|
125
181
|
a[cj + i] += t * a[cl + i];
|
|
126
182
|
if (qraux[j] === 0)
|
|
127
183
|
continue;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const t2 = tt;
|
|
131
|
-
tt = 1 + 0.05 * tt * (qraux[j] / work[j]) ** 2;
|
|
132
|
-
if (tt !== 1) {
|
|
133
|
-
qraux[j] *= Math.sqrt(t2);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
184
|
+
const tt = Math.max(1 - (Math.abs(a[cj + l]) / qraux[j]) ** 2, 0);
|
|
185
|
+
if (tt < 1e-6)
|
|
136
186
|
qraux[j] = nrm2(a, cj + l + 1, n - l - 1);
|
|
137
|
-
|
|
138
|
-
|
|
187
|
+
else
|
|
188
|
+
qraux[j] *= Math.sqrt(tt);
|
|
139
189
|
}
|
|
140
190
|
qraux[l] = a[cl + l];
|
|
191
|
+
transformed[l] = 1;
|
|
141
192
|
a[cl + l] = -nrmxl;
|
|
142
193
|
}
|
|
143
|
-
return { qraux, pivot, rank: Math.min(k, n) };
|
|
194
|
+
return { qraux, pivot, rank: Math.min(k, n), transformed };
|
|
144
195
|
}
|
|
145
196
|
/** Column-pivoted Householder QR (dlaqp2, the unblocked version of dgeqp3). Returns tau and pivot */
|
|
146
197
|
function geqp3(A) {
|
|
@@ -387,6 +438,7 @@ export function qrResid(q, y) {
|
|
|
387
438
|
return residOrFitted(q, y, false);
|
|
388
439
|
}
|
|
389
440
|
export function qrFitted(q, y, options = {}) {
|
|
441
|
+
checkOptions(options, QR_FITTED_KEYS, "qrFitted"); // generated: option-keys.mjs
|
|
390
442
|
return residOrFitted(q, y, true, options.k);
|
|
391
443
|
}
|
|
392
444
|
/**
|
|
@@ -404,6 +456,7 @@ export function qrFitted(q, y, options = {}) {
|
|
|
404
456
|
* @category Linear algebra
|
|
405
457
|
*/
|
|
406
458
|
export function qrQ(q, options = {}) {
|
|
459
|
+
checkOptions(options, QR_Q_KEYS, "qrQ"); // generated: option-keys.mjs
|
|
407
460
|
const d = decomp(q);
|
|
408
461
|
const n = d.a.nrow;
|
|
409
462
|
const nc = options.complete ? n : Math.min(n, d.a.ncol);
|
|
@@ -433,6 +486,7 @@ export function qrQ(q, options = {}) {
|
|
|
433
486
|
* @category Linear algebra
|
|
434
487
|
*/
|
|
435
488
|
export function qrR(q, options = {}) {
|
|
489
|
+
checkOptions(options, QR_R_KEYS, "qrR"); // generated: option-keys.mjs
|
|
436
490
|
const a = toMat(q.qr);
|
|
437
491
|
const nr = options.complete ? a.nrow : Math.min(a.nrow, a.ncol);
|
|
438
492
|
const r = newMat(nr, a.ncol);
|
|
@@ -449,7 +503,8 @@ export function qrR(q, options = {}) {
|
|
|
449
503
|
*
|
|
450
504
|
* @param q A QR decomposition from {@link qr}.
|
|
451
505
|
* @param options `complete`: whether the default `ncol` is the number of rows (`true`) or `min(nrow, ncol)` (`false`, the default).
|
|
452
|
-
* `ncol`: the number of columns to return
|
|
506
|
+
* `ncol`: the number of columns to return, from 0 to `max(nrow, ncol)`; columns beyond the original ones are taken
|
|
507
|
+
* from `Q` (as in R). A larger `ncol` is an error (R pads zero columns).
|
|
453
508
|
* @returns The reconstructed matrix as an array of rows.
|
|
454
509
|
*
|
|
455
510
|
* @example
|
|
@@ -460,6 +515,7 @@ export function qrR(q, options = {}) {
|
|
|
460
515
|
* @category Linear algebra
|
|
461
516
|
*/
|
|
462
517
|
export function qrX(q, options = {}) {
|
|
518
|
+
checkOptions(options, QR_X_KEYS, "qrX"); // generated: option-keys.mjs
|
|
463
519
|
const d = decomp(q);
|
|
464
520
|
const n = d.a.nrow;
|
|
465
521
|
const p = d.a.ncol;
|
|
@@ -467,6 +523,9 @@ export function qrX(q, options = {}) {
|
|
|
467
523
|
const ncol = options.ncol ?? (complete ? n : Math.min(n, p));
|
|
468
524
|
if (!Number.isInteger(ncol) || ncol < 0)
|
|
469
525
|
throw new RangeError(`invalid ncol (got ${ncol})`);
|
|
526
|
+
// Columns beyond max(n, p) are neither columns of x nor of an orthogonal completion (R pads them with zeros)
|
|
527
|
+
if (ncol > Math.max(n, p))
|
|
528
|
+
throw new RangeError(`ncol must be at most max(nrow, ncol of x) = ${Math.max(n, p)} (got ${ncol})`);
|
|
470
529
|
const pivoted = d.pivot.some((v, j) => v !== j + 1);
|
|
471
530
|
if (pivoted && ncol < p)
|
|
472
531
|
throw new RangeError("ncol must be at least the number of columns because the decomposition is pivoted");
|
|
@@ -511,15 +570,38 @@ export function qrX(q, options = {}) {
|
|
|
511
570
|
* @category Linear algebra
|
|
512
571
|
*/
|
|
513
572
|
export function qrSolve(a, b, options = {}) {
|
|
573
|
+
checkOptions(options, QR_SOLVE_KEYS, "qrSolve"); // generated: option-keys.mjs
|
|
514
574
|
const q = Array.isArray(a) ? qr(a, { tol: options.tol ?? 1e-7 }) : a;
|
|
515
575
|
const n = q.qr.length;
|
|
516
576
|
const p = n === 0 ? 0 : q.qr[0].length;
|
|
517
577
|
if (q.rank !== Math.min(n, p))
|
|
518
578
|
throw new Error("singular matrix in qrSolve");
|
|
579
|
+
// a LAPACK decomposition has no rank (always min(n, p)): with column pivoting the diagonal of R decreases, and a
|
|
580
|
+
// matrix whose |R_kk| is at most tol |R_11| is singular too (R returns a meaningless solution: (8, -2.5) for
|
|
581
|
+
// [[1, 2], [2, 4]] x = (3, 6))
|
|
582
|
+
if (q.useLapack) {
|
|
583
|
+
const tol = options.tol ?? 1e-7;
|
|
584
|
+
const r11 = Math.abs(q.qr[0]?.[0] ?? 0);
|
|
585
|
+
for (let k = 0; k < Math.min(n, p); k++)
|
|
586
|
+
if (!(Math.abs(q.qr[k][k]) > tol * r11))
|
|
587
|
+
throw new Error("singular matrix in qrSolve");
|
|
588
|
+
}
|
|
519
589
|
if (b === undefined && n !== p)
|
|
520
590
|
throw new TypeError("only square matrices can be inverted");
|
|
521
591
|
const y = b ?? Array.from({ length: n }, (_, i) => Array.from({ length: n }, (_, j) => (i === j ? 1 : 0)));
|
|
522
592
|
const res = qrCoef(q, y);
|
|
523
|
-
|
|
524
|
-
|
|
593
|
+
// The coefficients of the columns the decomposition leaves out (pivot[min(n, p)..], those of a wide matrix) are 0;
|
|
594
|
+
// a NaN from the solve itself (a NaN in the response) stays (R's res[is.na(res)] <- 0 makes it 0: qr.solve of the
|
|
595
|
+
// identity with b = (NaN, 1) is (0, 0))
|
|
596
|
+
const unused = new Set(q.pivot.slice(Math.min(n, p)));
|
|
597
|
+
if (isMatrixInput(y)) {
|
|
598
|
+
const m = res;
|
|
599
|
+
for (const j of unused)
|
|
600
|
+
m[j].fill(0);
|
|
601
|
+
return m;
|
|
602
|
+
}
|
|
603
|
+
const v = res;
|
|
604
|
+
for (const j of unused)
|
|
605
|
+
v[j] = 0;
|
|
606
|
+
return v;
|
|
525
607
|
}
|
package/dist/linalg/schur.js
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
// (no balancing, a different QR iteration)
|
|
13
13
|
// - NaN or Inf is an error, as in eigen
|
|
14
14
|
import { orthes, hqr2 } from "./eigen.js";
|
|
15
|
-
import { fromMat, newMat, requireSquare, toMat } from "./matrix.js";
|
|
15
|
+
import { decompositionExponent, fromMat, newMat, requireSquare, toMat } from "./matrix.js";
|
|
16
|
+
import { ldexp } from "../internal/summary.js";
|
|
16
17
|
/**
|
|
17
18
|
* Real Schur decomposition of a square matrix. Equivalent to R's `Matrix::Schur(x)`.
|
|
18
19
|
*
|
|
@@ -41,7 +42,11 @@ export function schur(x) {
|
|
|
41
42
|
for (const v of A.data)
|
|
42
43
|
if (!Number.isFinite(v))
|
|
43
44
|
throw new RangeError("the matrix contains NaN or infinite values");
|
|
44
|
-
|
|
45
|
+
// scaled by a power of 2 as in eigen and svd (exact): T and the eigenvalues are scaled back, Q has no units. With
|
|
46
|
+
// entries near 1e308, sums in the reduction overflowed: schur([[1e308, 0], [1e308, 1e308]]) lost T's off-diagonal
|
|
47
|
+
// entry, and a 3 x 3 matrix did not converge
|
|
48
|
+
const k = decompositionExponent(A.data);
|
|
49
|
+
const H = { nrow: n, ncol: n, data: k === 0 ? A.data.slice() : A.data.map((v) => ldexp(v, -k)) };
|
|
45
50
|
const V = newMat(n, n);
|
|
46
51
|
const d = new Float64Array(n);
|
|
47
52
|
const e = new Float64Array(n);
|
|
@@ -99,7 +104,10 @@ export function schur(x) {
|
|
|
99
104
|
valuesIm[j + 1] = s.rt2i;
|
|
100
105
|
j++;
|
|
101
106
|
}
|
|
102
|
-
|
|
107
|
+
if (k === 0)
|
|
108
|
+
return { T: fromMat(H), Q: fromMat(V), valuesRe, valuesIm };
|
|
109
|
+
const back = (x) => ldexp(x, k);
|
|
110
|
+
return { T: fromMat(H).map((r) => r.map(back)), Q: fromMat(V), valuesRe: valuesRe.map(back), valuesIm: valuesIm.map(back) };
|
|
103
111
|
}
|
|
104
112
|
const sign = (a, b) => (b >= 0 && !Object.is(b, -0) ? Math.abs(a) : -Math.abs(a));
|
|
105
113
|
/**
|
package/dist/linalg/solve.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export declare function getrf(A: Mat): {
|
|
|
9
9
|
* and with B^T x when kase = 2
|
|
10
10
|
*/
|
|
11
11
|
export declare function lacn2(n: number, apply: (kase: 1 | 2, x: Float64Array) => void): number;
|
|
12
|
+
/** A copy of a finite matrix divided by a power of 2 (exact; see decompositionExponent) when its largest entry is outside [2^-256, 2^256], else undefined */
|
|
13
|
+
export declare function scaledCopy(A: Mat): Mat | undefined;
|
|
12
14
|
/** Estimate the reciprocal condition number from the LU decomposition (dgecon). anorm is the norm of the original matrix */
|
|
13
15
|
export declare function gecon(LU: Mat, anorm: number, oneNormKind: boolean): number;
|
|
14
16
|
/** Estimate of the reciprocal condition number of a triangular matrix (dtrcon, non-unit diagonal) */
|
|
@@ -135,7 +137,10 @@ export interface RcondOptions {
|
|
|
135
137
|
norm?: "O" | "1" | "I";
|
|
136
138
|
/** Treat `x` as triangular and use only its triangle given by `uplo`. Default: `false`. */
|
|
137
139
|
triangular?: boolean;
|
|
138
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* With `triangular: true`, use the upper (`"U"`) or lower (`"L"`) triangle; any other value is an error for a
|
|
142
|
+
* square matrix (a non-square matrix uses R of its QR decomposition, as in R). Default: `"U"`.
|
|
143
|
+
*/
|
|
139
144
|
uplo?: "U" | "L";
|
|
140
145
|
}
|
|
141
146
|
/**
|