@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/expm.js
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// 26 (2005) 1179-1193 (degrees 3, 5, 7, 9 and 13, the bounds on the 1-norm and the Padé coefficients);
|
|
7
7
|
// the balancing is src/linalg/balance.ts (a port of LAPACK 3.12.0's SRC/dgebal.f, modified BSD).
|
|
8
8
|
// - "Higham08.b" (default) balances first: dgebal with job "P" (permutation), then dgebal with job "S" (scaling) on
|
|
9
|
-
// the result, as two separate calls; "Higham08" does not balance
|
|
9
|
+
// the result, as two separate calls; "Higham08" does not balance, except with job "S" where the scaling by 2^-s
|
|
10
|
+
// below would take a nonzero entry below the normal range (rStats.js; R loses the entry)
|
|
10
11
|
// - The Padé degree is chosen from the 1-norm of the (balanced) matrix; above 2.1, degree 13 with scaling by 2^-s
|
|
11
12
|
// - X = (V - U)^-1 (V + U) is solved by LU decomposition as solve (which throws if V - U is computationally
|
|
12
13
|
// singular), then squared s times, and the balancing is undone
|
|
@@ -18,8 +19,12 @@
|
|
|
18
19
|
// - A 1 x 1 matrix gives exp of its entry, a 0 x 0 matrix gives a 0 x 0 matrix
|
|
19
20
|
import { fromMat, newMat, requireSquare, toMat } from "./matrix.js";
|
|
20
21
|
import { infNorm, oneNorm } from "./norm.js";
|
|
22
|
+
import { ldexp } from "../internal/summary.js";
|
|
21
23
|
import { gebal } from "./balance.js";
|
|
22
24
|
import { solve } from "./solve.js";
|
|
25
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
26
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
27
|
+
const EXPM_KEYS = ["method", "order"];
|
|
23
28
|
const METHODS = ["Higham08.b", "Higham08", "Pade", "Taylor", "PadeO", "TaylorO"];
|
|
24
29
|
/**
|
|
25
30
|
* Matrix exponential. Equivalent to `expm::expm(x, method, order)` in R. The methods are `"Higham08.b"` (the
|
|
@@ -43,19 +48,34 @@ const METHODS = ["Higham08.b", "Higham08", "Pade", "Taylor", "PadeO", "TaylorO"]
|
|
|
43
48
|
* @category Linear algebra
|
|
44
49
|
*/
|
|
45
50
|
export function expm(x, options = {}) {
|
|
51
|
+
checkOptions(options, EXPM_KEYS, "expm"); // generated: option-keys.mjs
|
|
46
52
|
const method = options.method ?? "Higham08.b";
|
|
47
53
|
if (!METHODS.includes(method))
|
|
48
54
|
throw new RangeError(`unknown method: ${String(method)}`);
|
|
49
55
|
const order = options.order ?? 8;
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
// order is used by these four methods only (the Higham08 methods ignore it, as in R)
|
|
57
|
+
const usesOrder = method === "Pade" || method === "PadeO" || method === "Taylor" || method === "TaylorO";
|
|
58
|
+
// (up to R's integer range, beyond which R's expm fails too: from 2^53 on, the loops j = order, ..., 1 did not end)
|
|
59
|
+
if (usesOrder && !(Number.isInteger(order) && order >= 1 && order <= 2147483647)) {
|
|
60
|
+
throw new RangeError("order must be a positive integer at most 2147483647");
|
|
61
|
+
}
|
|
52
62
|
const A = toMat(x);
|
|
53
63
|
requireSquare(A);
|
|
54
64
|
const n = A.nrow;
|
|
55
65
|
if (n === 0)
|
|
56
66
|
return [];
|
|
67
|
+
// exp of the one entry, NaN and infinite ones included (as R)
|
|
57
68
|
if (n === 1)
|
|
58
69
|
return [[Math.exp(A.data[0])]];
|
|
70
|
+
// the loops make order matrix products of n^3 multiply-adds (plus a fixed cost per step): at most 2^31 in all,
|
|
71
|
+
// about 7 to 9 s on the development machine (order 29826161 at n = 2, 17171 at n = 50); the default 8 is far below
|
|
72
|
+
if (usesOrder && order * (n ** 3 + 64) > 2 ** 31) {
|
|
73
|
+
throw new RangeError(`order = ${order} is too large for a ${n} x ${n} matrix: order * (n^3 + 64) must be at most 2^31`);
|
|
74
|
+
}
|
|
75
|
+
// (the Padé and Taylor methods returned NaN silently; the Higham08 methods stopped later with other messages)
|
|
76
|
+
for (const v of A.data)
|
|
77
|
+
if (!Number.isFinite(v))
|
|
78
|
+
throw new RangeError("the matrix contains NaN or infinite values");
|
|
59
79
|
if (method === "Taylor" || method === "TaylorO")
|
|
60
80
|
return fromMat(taylorScaled(A, order));
|
|
61
81
|
if (method === "Pade" || method === "PadeO")
|
|
@@ -66,6 +86,13 @@ export function expm(x, options = {}) {
|
|
|
66
86
|
perm = gebal("P", A);
|
|
67
87
|
scaling = gebal("S", A);
|
|
68
88
|
}
|
|
89
|
+
else if (lostInSquaringScale(A)) {
|
|
90
|
+
// "Higham08" does not balance, but where A / 2^s would lose a nonzero entry below the normal range (the coupling
|
|
91
|
+
// 1e-308 of [[0, 1e308], [1e-308, 0]] became 0, and the result that of a nilpotent matrix, R too), the
|
|
92
|
+
// exponential is taken of the matrix balanced by dgebal's job "S" (a diagonal similarity by powers of 2) and
|
|
93
|
+
// transformed back, as "Higham08.b" does
|
|
94
|
+
scaling = gebal("S", A);
|
|
95
|
+
}
|
|
69
96
|
const X = pade(A);
|
|
70
97
|
const x_ = X.data;
|
|
71
98
|
const at = (i, j) => i + j * n;
|
|
@@ -104,6 +131,28 @@ const PADE13 = [
|
|
|
104
131
|
64764752532480000, 32382376266240000, 7771770303897600, 1187353796428800, 129060195264000, 10559470521600,
|
|
105
132
|
670442572800, 33522128640, 1323241920, 40840800, 960960, 16380, 182, 1,
|
|
106
133
|
];
|
|
134
|
+
/** log2 of the 1-norm of A (given as nu), from A divided by a power of 2 when nu overflows */
|
|
135
|
+
function log2OneNorm(A, nu) {
|
|
136
|
+
if (nu < Infinity)
|
|
137
|
+
return Math.log2(nu);
|
|
138
|
+
let mx = 0;
|
|
139
|
+
for (const v of A.data)
|
|
140
|
+
mx = Math.max(mx, Math.abs(v));
|
|
141
|
+
const k = Math.floor(Math.log2(mx));
|
|
142
|
+
return Math.log2(oneNorm({ ...A, data: A.data.map((v) => ldexp(v, -k)) })) + k;
|
|
143
|
+
}
|
|
144
|
+
/** The exponent s of the scaling A / 2^s before the Padé approximant of degree 13 (nu, the 1-norm of A, above 2.1) */
|
|
145
|
+
function squaringExponent(A, nu) {
|
|
146
|
+
return Math.max(0, Math.ceil(log2OneNorm(A, nu) - Math.log2(5.4)));
|
|
147
|
+
}
|
|
148
|
+
/** Whether the scaling A / 2^s of pade takes a nonzero entry of A below the normal range (2^-1022) */
|
|
149
|
+
function lostInSquaringScale(A) {
|
|
150
|
+
const nu = oneNorm(A);
|
|
151
|
+
if (nu <= 2.1)
|
|
152
|
+
return false;
|
|
153
|
+
const s = squaringExponent(A, nu);
|
|
154
|
+
return s > 0 && A.data.some((v) => v !== 0 && Math.abs(ldexp(v, -s)) < 2 ** -1022);
|
|
155
|
+
}
|
|
107
156
|
/** Scaling and squaring with a Padé approximant: exp(A) for the (balanced) matrix A */
|
|
108
157
|
function pade(A) {
|
|
109
158
|
const n = A.nrow;
|
|
@@ -130,8 +179,11 @@ function pade(A) {
|
|
|
130
179
|
V = v;
|
|
131
180
|
}
|
|
132
181
|
else {
|
|
133
|
-
s
|
|
134
|
-
|
|
182
|
+
// the norm's log from the matrix divided by a power of 2 when the column sums overflow (s was then Infinity, and
|
|
183
|
+
// the squaring below never ended), and A / 2^s formed in two steps (1 / 2^s is 0 from s = 1024 on, which made
|
|
184
|
+
// B = 0 and gave the identity instead of exp(A))
|
|
185
|
+
s = squaringExponent(A, nu);
|
|
186
|
+
const B = { ...A, data: A.data.map((v) => ldexp(v, -s)) };
|
|
135
187
|
const B2 = mul(B, B);
|
|
136
188
|
const B4 = mul(B2, B2);
|
|
137
189
|
const B6 = mul(B2, B4);
|
|
@@ -154,24 +206,45 @@ function pade(A) {
|
|
|
154
206
|
}
|
|
155
207
|
let X = toMat(solve(fromMat(vmu), fromMat(vpu)));
|
|
156
208
|
for (let k = 0; k < s; k++)
|
|
157
|
-
X =
|
|
209
|
+
X = mulSquaring(X, X);
|
|
158
210
|
return X;
|
|
159
211
|
}
|
|
160
212
|
/**
|
|
161
213
|
* Exponent p of the scaling by N = 2^p (Shapira and Firth): with nu = sqrt(|A|_1 |A|_inf), L >= 1 the smallest with
|
|
162
|
-
* 2^L >= nu, p = L + 4
|
|
214
|
+
* 2^L >= nu, p = L + 4. Where A / 2^p would take a nonzero entry below the normal range (2^-1022), the floor L >= 1
|
|
215
|
+
* is dropped (p >= 0 only), keeping 2^L >= nu, that is |A / N| <= 2^-4 (rStats.js, #404; R loses the entry)
|
|
163
216
|
*/
|
|
164
217
|
function scalingExponent(A) {
|
|
165
|
-
const
|
|
218
|
+
const n1 = oneNorm(A);
|
|
219
|
+
const nInf = infNorm(A);
|
|
220
|
+
// Finite norms whose product overflows: take the square roots first
|
|
221
|
+
const nu = n1 * nInf < Infinity ? Math.sqrt(n1 * nInf) : Math.sqrt(n1) * Math.sqrt(nInf);
|
|
166
222
|
let L = 1;
|
|
167
223
|
while (2 ** L < nu)
|
|
168
224
|
L++;
|
|
169
|
-
|
|
225
|
+
const p = L + 4;
|
|
226
|
+
if (!A.data.some((v) => v !== 0 && Math.abs(ldexp(v, -p)) < 2 ** -1022))
|
|
227
|
+
return p;
|
|
228
|
+
// The product of the norms underflows: nu from the logs
|
|
229
|
+
if (nu === 0 || n1 * nInf < 2 ** -1000)
|
|
230
|
+
return Math.max(0, Math.ceil((Math.log2(n1) + Math.log2(nInf)) / 2) + 4);
|
|
231
|
+
let L2 = L;
|
|
232
|
+
while (L2 > -4 && 2 ** (L2 - 1) >= nu)
|
|
233
|
+
L2--;
|
|
234
|
+
return Math.max(0, L2 + 4);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* A and N = 2^p for the scaled methods. When 2^p would overflow, part of the scaling (an exact power of 2) is moved
|
|
238
|
+
* into A, so that A / N is unchanged; below that the values are the same as with A and 2^p.
|
|
239
|
+
*/
|
|
240
|
+
function scaledPair(A, p) {
|
|
241
|
+
const q = Math.max(0, p - 1000);
|
|
242
|
+
return { A: q === 0 ? A : scale(A, 2 ** -q), N: 2 ** (p - q) };
|
|
170
243
|
}
|
|
171
244
|
/** R <- R R, p times */
|
|
172
245
|
function squareRepeatedly(R, p) {
|
|
173
246
|
for (let k = 0; k < p; k++)
|
|
174
|
-
R =
|
|
247
|
+
R = mulSquaring(R, R);
|
|
175
248
|
return R;
|
|
176
249
|
}
|
|
177
250
|
/** M_ii <- M_ii + 1 */
|
|
@@ -183,10 +256,10 @@ function addIdentity(M) {
|
|
|
183
256
|
/** "Taylor": S = I, then S <- (S A) (1 / (j N)) + I for j = order ... 1, then squared p times */
|
|
184
257
|
function taylorScaled(A, order) {
|
|
185
258
|
const p = scalingExponent(A);
|
|
186
|
-
const N =
|
|
259
|
+
const { A: As, N } = scaledPair(A, p);
|
|
187
260
|
let S = identity(A.nrow);
|
|
188
261
|
for (let j = order; j >= 1; j--) {
|
|
189
|
-
S = scale(mul(S,
|
|
262
|
+
S = scale(mul(S, As), 1 / (j * N));
|
|
190
263
|
addIdentity(S);
|
|
191
264
|
}
|
|
192
265
|
return squareRepeatedly(S, p);
|
|
@@ -206,9 +279,9 @@ function padeHorner(A, order, N, sign) {
|
|
|
206
279
|
function padeScaled(A, order) {
|
|
207
280
|
const n = A.nrow;
|
|
208
281
|
const p = scalingExponent(A);
|
|
209
|
-
const N =
|
|
210
|
-
const numer = padeHorner(
|
|
211
|
-
const denom = padeHorner(
|
|
282
|
+
const { A: As, N } = scaledPair(A, p);
|
|
283
|
+
const numer = padeHorner(As, order, N, 1);
|
|
284
|
+
const denom = padeHorner(As, order, N, -1);
|
|
212
285
|
const R = newMat(n, n);
|
|
213
286
|
for (let i = 0; i < n; i++) {
|
|
214
287
|
R.data.set(conjugateGradientSquared(denom, numer.data.subarray(i * n, (i + 1) * n)), i * n);
|
|
@@ -285,6 +358,31 @@ function identity(n) {
|
|
|
285
358
|
return I;
|
|
286
359
|
}
|
|
287
360
|
/** Matrix product (inner products in increasing order of the summation index) */
|
|
361
|
+
/**
|
|
362
|
+
* A B for the squaring phase, where entries may have overflowed to ±Infinity: such an entry stands for a finite value
|
|
363
|
+
* too large to represent, so its product with an exact 0 is 0, not NaN (exp(diag(1e308, 0)) is diag(Inf, 1)). The same
|
|
364
|
+
* as mul for finite entries; NaN still propagates
|
|
365
|
+
*/
|
|
366
|
+
function mulSquaring(A, B) {
|
|
367
|
+
const n = A.nrow;
|
|
368
|
+
const p = A.ncol;
|
|
369
|
+
const q = B.ncol;
|
|
370
|
+
const C = newMat(n, q);
|
|
371
|
+
for (let j = 0; j < q; j++) {
|
|
372
|
+
for (let i = 0; i < n; i++) {
|
|
373
|
+
let s = 0;
|
|
374
|
+
for (let k = 0; k < p; k++) {
|
|
375
|
+
const a = A.data[i + k * n];
|
|
376
|
+
const b = B.data[k + j * p];
|
|
377
|
+
if ((a === 0 && Math.abs(b) === Infinity) || (b === 0 && Math.abs(a) === Infinity))
|
|
378
|
+
continue;
|
|
379
|
+
s += a * b;
|
|
380
|
+
}
|
|
381
|
+
C.data[i + j * n] = s;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return C;
|
|
385
|
+
}
|
|
288
386
|
function mul(A, B) {
|
|
289
387
|
const n = A.nrow;
|
|
290
388
|
const p = A.ncol;
|
package/dist/linalg/kappa.js
CHANGED
|
@@ -17,11 +17,17 @@
|
|
|
17
17
|
// "1", "O" and "I"; the library does not report warnings). rcond (method "direct") accepts "1", "O" and "I" in
|
|
18
18
|
// either case and rejects any other with R's message
|
|
19
19
|
// ginv is the Moore-Penrose generalised inverse via the SVD; singular values at most largest singular value × tol are treated as 0.
|
|
20
|
-
import { fromMat, newMat, toMat, transpose } from "./matrix.js";
|
|
20
|
+
import { fromMat, newMat, rangeExponent, toMat, transpose } from "./matrix.js";
|
|
21
21
|
import { norm } from "./norm.js";
|
|
22
22
|
import { qr, qrR } from "./qr.js";
|
|
23
23
|
import { rcond, solve } from "./solve.js";
|
|
24
|
-
import {
|
|
24
|
+
import { svdScaled } from "./svd.js";
|
|
25
|
+
import { ldexp } from "../internal/summary.js";
|
|
26
|
+
import { maxOf, minOf } from "../internal/extremes.js";
|
|
27
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
28
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
29
|
+
const KAPPA_KEYS = ["exact", "norm", "method", "triangular", "uplo"];
|
|
30
|
+
const GINV_KEYS = ["tol"];
|
|
25
31
|
/**
|
|
26
32
|
* Condition number of a matrix, estimated or exact. Equivalent to R's `kappa()`.
|
|
27
33
|
*
|
|
@@ -42,21 +48,50 @@ import { svd } from "./svd.js";
|
|
|
42
48
|
* @category Linear algebra
|
|
43
49
|
*/
|
|
44
50
|
export function kappa(z, options = {}) {
|
|
51
|
+
checkOptions(options, KAPPA_KEYS, "kappa"); // generated: option-keys.mjs
|
|
52
|
+
// scale-free: a finite matrix outside [2^-256, 2^256] is divided by a power of 2 first, as in rcond (the condition
|
|
53
|
+
// number does not change; for entries near 1e308 the norms overflowed: Infinity from the triangular estimate and the
|
|
54
|
+
// exact 1- and inf-norms, NaN through the QR). Matrices in range are used as given, and so is one whose smallest
|
|
55
|
+
// nonzero entry would leave the normal range (diag(1e-200, 1, 1e200): its condition number of 1e400 still stops
|
|
56
|
+
// solve, as in R, instead of the entry 1e-200 becoming 0)
|
|
57
|
+
const zm0 = toMat(z);
|
|
58
|
+
let zm = zm0;
|
|
59
|
+
if (zm0.data.every(Number.isFinite)) {
|
|
60
|
+
// (brought near 1, not only down to 2^256 as in the decompositions (decompositionExponent): LINPACK's triangular
|
|
61
|
+
// estimate depends on the scale, and kept its values for the matrices it already scaled)
|
|
62
|
+
const e = rangeExponent(zm0.data);
|
|
63
|
+
const scaled = e === 0 ? undefined : { ...zm0, data: zm0.data.map((v) => ldexp(v, -e)) };
|
|
64
|
+
if (scaled !== undefined && scaled.data.every((v, i) => v === 0 ? zm0.data[i] === 0 : Math.abs(v) >= 2 ** -1022))
|
|
65
|
+
zm = scaled;
|
|
66
|
+
}
|
|
67
|
+
const zs = zm === zm0 ? z : fromMat(zm);
|
|
45
68
|
if (options.exact) {
|
|
46
69
|
// The default norm is "2" if exact, "1" if estimated
|
|
47
70
|
const nrm = options.norm === undefined ? "2" : normalize(options.norm);
|
|
48
71
|
if (nrm === "2") {
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
72
|
+
// the ratio of the scaled singular values (those of a matrix around 1e308 can overflow: Inf / Inf)
|
|
73
|
+
const d = svdScaled(z, { nu: 0, nv: 0 }).d;
|
|
74
|
+
const min = minOf(d);
|
|
75
|
+
return min === 0 ? Infinity : maxOf(d) / min;
|
|
76
|
+
}
|
|
77
|
+
// an exactly singular matrix gives Inf, as the 2-norm (R stops with "exactly singular"); a computationally singular
|
|
78
|
+
// one still stops, as in R (its inverse has no accurate norm)
|
|
79
|
+
let inverse;
|
|
80
|
+
try {
|
|
81
|
+
inverse = solve(zs);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
if (e instanceof Error && e.message.startsWith("the system is exactly singular"))
|
|
85
|
+
return Infinity;
|
|
86
|
+
throw e;
|
|
52
87
|
}
|
|
53
|
-
return norm(
|
|
88
|
+
return norm(zs, { type: nrm }) * norm(inverse, { type: nrm });
|
|
54
89
|
}
|
|
55
90
|
// Through a triangular factor, only "I" itself is the infinity norm; any other norm becomes the 1-norm (as in R)
|
|
56
91
|
const triNorm = options.norm === "I" ? "I" : "O";
|
|
57
92
|
if ((options.method ?? "qr") === "direct") {
|
|
58
93
|
if (options.triangular)
|
|
59
|
-
return 1 / triangularEstimate(
|
|
94
|
+
return 1 / triangularEstimate(zm, (options.uplo ?? "U") === "U", triNorm);
|
|
60
95
|
const t = options.norm ?? "O";
|
|
61
96
|
const u = t.toUpperCase();
|
|
62
97
|
if (u !== "1" && u !== "O" && u !== "I")
|
|
@@ -64,8 +99,7 @@ export function kappa(z, options = {}) {
|
|
|
64
99
|
return 1 / rcond(z, { norm: u === "I" ? "I" : "O" });
|
|
65
100
|
}
|
|
66
101
|
// If wide, transpose before the QR decomposition (from R's output)
|
|
67
|
-
const
|
|
68
|
-
const zz = zm.nrow < zm.ncol ? fromMat(transpose(zm)) : z;
|
|
102
|
+
const zz = zm.nrow < zm.ncol ? fromMat(transpose(zm)) : zs;
|
|
69
103
|
return 1 / triangularEstimate(toMat(qrR(qr(zz))), true, triNorm);
|
|
70
104
|
}
|
|
71
105
|
/**
|
|
@@ -203,22 +237,42 @@ function asum(z) {
|
|
|
203
237
|
* @category Linear algebra
|
|
204
238
|
*/
|
|
205
239
|
export function ginv(x, options = {}) {
|
|
240
|
+
checkOptions(options, GINV_KEYS, "ginv"); // generated: option-keys.mjs
|
|
206
241
|
const tol = options.tol ?? Math.sqrt(Number.EPSILON);
|
|
207
242
|
const a = toMat(x);
|
|
208
|
-
|
|
243
|
+
// in the units of the scaled singular values d (x 2^-s), the result scaled by 2^-s at the end: singular values that
|
|
244
|
+
// overflow made the cut-off Inf and the result 0
|
|
245
|
+
const { d, u, v, s } = svdScaled(x);
|
|
209
246
|
const n = a.nrow;
|
|
210
247
|
const p = a.ncol;
|
|
211
248
|
const out = newMat(p, n);
|
|
212
249
|
const cut = Math.max(tol * (d[0] ?? 0), 0);
|
|
250
|
+
// a singular value whose reciprocal overflows (d below 2^-1024 in the scaled units) is inverted as 1/(d 2^600) and
|
|
251
|
+
// its terms are added in the original units, ldexp(…, 600 - s), after the others: ginv(diag(1e308, 1e-80), tol = 0)
|
|
252
|
+
// gave NaN and Inf (0 × Inf), for 1e80 (#127). The other terms are computed as before
|
|
253
|
+
const extra = [];
|
|
213
254
|
for (let k = 0; k < d.length; k++) {
|
|
214
255
|
if (!(d[k] > cut))
|
|
215
256
|
continue;
|
|
216
257
|
const inv = 1 / d[k];
|
|
258
|
+
if (!Number.isFinite(inv)) {
|
|
259
|
+
extra.push([k, 1 / ldexp(d[k], 600)]);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
217
262
|
for (let j = 0; j < n; j++) {
|
|
218
263
|
const ujk = u[j][k] * inv;
|
|
219
264
|
for (let i = 0; i < p; i++)
|
|
220
265
|
out.data[i + j * p] += v[i][k] * ujk;
|
|
221
266
|
}
|
|
222
267
|
}
|
|
268
|
+
if (s !== 0)
|
|
269
|
+
out.data = out.data.map((t) => ldexp(t, -s));
|
|
270
|
+
for (const [k, inv] of extra) {
|
|
271
|
+
for (let j = 0; j < n; j++) {
|
|
272
|
+
const ujk = u[j][k] * inv;
|
|
273
|
+
for (let i = 0; i < p; i++)
|
|
274
|
+
out.data[i + j * p] += ldexp(v[i][k] * ujk, 600 - s);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
223
277
|
return fromMat(out);
|
|
224
278
|
}
|
package/dist/linalg/lu.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export interface LuResult {
|
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* LU decomposition with partial (row) pivoting, `x = P L U`. Equivalent to R's `Matrix::lu()`. The factors are those
|
|
17
|
-
* of `Matrix::expand(Matrix::lu(x))` (LAPACK's dgetrf).
|
|
17
|
+
* of `Matrix::expand(Matrix::lu(x))` (LAPACK's dgetrf). Throws on `NaN` or infinite values (`Matrix::lu` returns factors
|
|
18
|
+
* with NaN or infinite values).
|
|
18
19
|
*
|
|
19
20
|
* @example
|
|
20
21
|
* ```ts
|
package/dist/linalg/lu.js
CHANGED
|
@@ -9,11 +9,18 @@
|
|
|
9
9
|
import { scale as scaleColumns } from "../descriptive/moments.js";
|
|
10
10
|
import { svd } from "./svd.js";
|
|
11
11
|
import { matMul } from "./ops.js";
|
|
12
|
+
import { maxOf } from "../internal/extremes.js";
|
|
13
|
+
import { ldexp } from "../internal/summary.js";
|
|
14
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
15
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
16
|
+
const PRCOMP_KEYS = ["center", "scale", "rank", "tol"];
|
|
17
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
12
18
|
/** Smallest normal double (LAPACK's dlamch("S")): below it, 1 / pivot would overflow. */
|
|
13
19
|
const SFMIN = 2.2250738585072014e-308;
|
|
14
20
|
/**
|
|
15
21
|
* LU decomposition with partial (row) pivoting, `x = P L U`. Equivalent to R's `Matrix::lu()`. The factors are those
|
|
16
|
-
* of `Matrix::expand(Matrix::lu(x))` (LAPACK's dgetrf).
|
|
22
|
+
* of `Matrix::expand(Matrix::lu(x))` (LAPACK's dgetrf). Throws on `NaN` or infinite values (`Matrix::lu` returns factors
|
|
23
|
+
* with NaN or infinite values).
|
|
17
24
|
*
|
|
18
25
|
* @example
|
|
19
26
|
* ```ts
|
|
@@ -29,6 +36,9 @@ export function lu(x) {
|
|
|
29
36
|
const a = x.map((r) => {
|
|
30
37
|
if (r.length !== n)
|
|
31
38
|
throw new TypeError("all rows of the matrix must have the same length");
|
|
39
|
+
// the factors of a matrix with NaN or an infinite value do not give back x = P L U: an error, as svd, qr and eigen (#343)
|
|
40
|
+
if (!r.every(Number.isFinite))
|
|
41
|
+
throw new RangeError("the matrix contains NaN or infinite values");
|
|
32
42
|
return Array.from(r);
|
|
33
43
|
});
|
|
34
44
|
const perm = Array.from({ length: m }, (_, i) => i);
|
|
@@ -81,6 +91,7 @@ export function lu(x) {
|
|
|
81
91
|
* @category Linear algebra
|
|
82
92
|
*/
|
|
83
93
|
export function prcomp(x, options = {}) {
|
|
94
|
+
checkOptions(options, PRCOMP_KEYS, "prcomp"); // generated: option-keys.mjs
|
|
84
95
|
const { rank, tol } = options;
|
|
85
96
|
if (rank !== undefined) {
|
|
86
97
|
if (!Number.isFinite(rank))
|
|
@@ -90,21 +101,45 @@ export function prcomp(x, options = {}) {
|
|
|
90
101
|
throw new RangeError("as.integer(rank.) > 0 is not TRUE");
|
|
91
102
|
}
|
|
92
103
|
const rows = x.map((r) => Array.from(r));
|
|
93
|
-
const
|
|
104
|
+
const center = options.center ?? true;
|
|
105
|
+
const scale = options.scale ?? false;
|
|
106
|
+
let s = scaleColumns(rows, { center, scale });
|
|
94
107
|
if (s.scale?.some((v) => v === 0))
|
|
95
108
|
throw new RangeError("cannot rescale a constant/zero column to unit variance");
|
|
96
|
-
const dec = svd(s.scaled, { nu: 0 });
|
|
97
109
|
const n = rows.length;
|
|
98
|
-
const
|
|
110
|
+
const finite = (m) => m.every((r) => r.every(Number.isFinite));
|
|
111
|
+
let dec = finite(rows) && finite(s.scaled) ? svd(s.scaled, { nu: 0 }) : undefined;
|
|
112
|
+
// 2^unit: the factor of sdev and the scores (the unit of the data, when the columns are not divided by a scale)
|
|
113
|
+
let unit = 0;
|
|
114
|
+
if (finite(rows) && (dec === undefined || dec.d.some((d) => d === Infinity))) {
|
|
115
|
+
// the deviations (1.5e308 from a mean of -5e307) or a singular value overflow while sdev may be in range (sdev
|
|
116
|
+
// 1.15e308 of c(1e308, -1e308, 1e308, -1e308) was Infinity, R too): all in the coordinates 2^-e (exact)
|
|
117
|
+
const e = Math.floor(Math.log2(maxOf(rows.flat().map(Math.abs))));
|
|
118
|
+
const down = (o) => (typeof o === "boolean" ? o : o.map((v) => ldexp(v, -e)));
|
|
119
|
+
const r = scaleColumns(rows.map((row) => row.map((v) => ldexp(v, -e))), { center: down(center), scale: down(scale) });
|
|
120
|
+
dec = svd(r.scaled, { nu: 0 });
|
|
121
|
+
if (scale === false)
|
|
122
|
+
unit = e;
|
|
123
|
+
s = { scaled: r.scaled };
|
|
124
|
+
if (r.center !== undefined)
|
|
125
|
+
s.center = r.center.map((v) => ldexp(v, e));
|
|
126
|
+
if (r.scale !== undefined)
|
|
127
|
+
s.scale = r.scale.map((v) => ldexp(v, e));
|
|
128
|
+
}
|
|
129
|
+
// a non-finite value in the data: an error from svd, as before
|
|
130
|
+
dec ??= svd(s.scaled, { nu: 0 });
|
|
131
|
+
const sdev = dec.d.map((d) => ldexp(d / Math.sqrt(Math.max(1, n - 1)), unit));
|
|
99
132
|
// Components kept: at most rank, and with tol only those whose sdev is greater than tol times the first
|
|
100
133
|
let k = rank === undefined ? sdev.length : Math.min(Math.trunc(rank), sdev.length);
|
|
134
|
+
if (Number.isNaN(tol))
|
|
135
|
+
throw new RangeError("'tol' must not be NaN");
|
|
101
136
|
if (tol !== undefined)
|
|
102
137
|
k = Math.min(k, sdev.filter((d) => d > sdev[0] * tol).length);
|
|
103
138
|
const rotation = dec.v.map((r) => r.slice(0, k));
|
|
104
139
|
const out = {
|
|
105
140
|
sdev,
|
|
106
141
|
rotation,
|
|
107
|
-
x: matMul(s.scaled, dec.v).map((r) => r.slice(0, k)),
|
|
142
|
+
x: matMul(s.scaled, dec.v).map((r) => r.slice(0, k).map((v) => ldexp(v, unit))),
|
|
108
143
|
};
|
|
109
144
|
if (s.center !== undefined)
|
|
110
145
|
out.center = s.center;
|
package/dist/linalg/matrix.d.ts
CHANGED
|
@@ -31,3 +31,17 @@ export declare function column(m: Mat, j: number): number[];
|
|
|
31
31
|
export declare function transpose(m: Mat): Mat;
|
|
32
32
|
export declare function hasNonFinite(m: Mat): boolean;
|
|
33
33
|
export declare function requireSquare(m: Mat, what?: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* 0 when the largest absolute value is within [2^-256, 2^256] (or all are 0); otherwise its binary exponent, the
|
|
36
|
+
* power of 2 to divide by to bring the values near 1
|
|
37
|
+
*/
|
|
38
|
+
export declare function rangeExponent(data: ArrayLike<number>): number;
|
|
39
|
+
/**
|
|
40
|
+
* The power of 2 to divide a matrix by before a decomposition (eigen, schur, svd, and the condition estimates of
|
|
41
|
+
* rcond, solve and kappa): 0 within [2^-256, 2^256]; a smaller matrix is brought up near 1, a larger one only down to
|
|
42
|
+
* [2^256, 2^257), the top of the range used as given (#127, #131, #132). Bringing it down to 1 put every entry below
|
|
43
|
+
* 2 of a matrix near 1e308 into the subnormal range: svd(diag(1e308, 1e-15)) gave 8.9e-16 and ginv's 1/d overflowed.
|
|
44
|
+
* Division by a power of 2 is exact, so a matrix whose result was finite and correct gives the same bits. eigen passes
|
|
45
|
+
* top = 459, LAPACK's dgeev bound (see eigen)
|
|
46
|
+
*/
|
|
47
|
+
export declare function decompositionExponent(data: ArrayLike<number>, top?: number): number;
|
package/dist/linalg/matrix.js
CHANGED
|
@@ -63,3 +63,27 @@ export function requireSquare(m, what = "the matrix") {
|
|
|
63
63
|
if (m.nrow !== m.ncol)
|
|
64
64
|
throw new TypeError(`${what} must be square (got ${m.nrow} x ${m.ncol})`);
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* 0 when the largest absolute value is within [2^-256, 2^256] (or all are 0); otherwise its binary exponent, the
|
|
68
|
+
* power of 2 to divide by to bring the values near 1
|
|
69
|
+
*/
|
|
70
|
+
export function rangeExponent(data) {
|
|
71
|
+
let mx = 0;
|
|
72
|
+
for (let i = 0; i < data.length; i++)
|
|
73
|
+
mx = Math.max(mx, Math.abs(data[i]));
|
|
74
|
+
if (mx === 0 || (mx >= 2 ** -256 && mx <= 2 ** 256))
|
|
75
|
+
return 0;
|
|
76
|
+
return Math.floor(Math.log2(mx));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The power of 2 to divide a matrix by before a decomposition (eigen, schur, svd, and the condition estimates of
|
|
80
|
+
* rcond, solve and kappa): 0 within [2^-256, 2^256]; a smaller matrix is brought up near 1, a larger one only down to
|
|
81
|
+
* [2^256, 2^257), the top of the range used as given (#127, #131, #132). Bringing it down to 1 put every entry below
|
|
82
|
+
* 2 of a matrix near 1e308 into the subnormal range: svd(diag(1e308, 1e-15)) gave 8.9e-16 and ginv's 1/d overflowed.
|
|
83
|
+
* Division by a power of 2 is exact, so a matrix whose result was finite and correct gives the same bits. eigen passes
|
|
84
|
+
* top = 459, LAPACK's dgeev bound (see eigen)
|
|
85
|
+
*/
|
|
86
|
+
export function decompositionExponent(data, top = 256) {
|
|
87
|
+
const k = rangeExponent(data);
|
|
88
|
+
return k > 0 ? Math.max(0, k - top) : k;
|
|
89
|
+
}
|
package/dist/linalg/norm.js
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
// "1" is the same as "O", "E" the same as "F").
|
|
8
8
|
import { toMat } from "./matrix.js";
|
|
9
9
|
import { svdValues } from "./svd.js";
|
|
10
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
11
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
12
|
+
const NORM_KEYS = ["type"];
|
|
10
13
|
/**
|
|
11
14
|
* Matrix norm. Equivalent to R's `norm()`.
|
|
12
15
|
*
|
|
@@ -26,6 +29,7 @@ import { svdValues } from "./svd.js";
|
|
|
26
29
|
* @category Linear algebra
|
|
27
30
|
*/
|
|
28
31
|
export function norm(x, options = {}) {
|
|
32
|
+
checkOptions(options, NORM_KEYS, "norm"); // generated: option-keys.mjs
|
|
29
33
|
const m = toMat(x);
|
|
30
34
|
const type = options.type ?? "O";
|
|
31
35
|
if (type.length !== 1)
|
package/dist/linalg/ops.d.ts
CHANGED
|
@@ -71,9 +71,25 @@ export declare function multiply(x: Operand, y: Operand): number[] | number[][];
|
|
|
71
71
|
* @category Linear algebra
|
|
72
72
|
*/
|
|
73
73
|
export declare function divide(x: Operand, y: Operand): number[] | number[][];
|
|
74
|
+
/**
|
|
75
|
+
* The operation of {@link outer}: `"*"`, `"+"`, `"-"`, `"/"`, `"^"` or a function of two numbers.
|
|
76
|
+
*
|
|
77
|
+
* @category Linear algebra
|
|
78
|
+
*/
|
|
79
|
+
export type OuterFun = "*" | "+" | "-" | "/" | "^" | ((a: number, b: number) => number);
|
|
80
|
+
/**
|
|
81
|
+
* Options for {@link outer}.
|
|
82
|
+
*
|
|
83
|
+
* @category Linear algebra
|
|
84
|
+
*/
|
|
85
|
+
export interface OuterOptions {
|
|
86
|
+
/** The operation. Default `"*"`. */
|
|
87
|
+
fun?: OuterFun;
|
|
88
|
+
}
|
|
74
89
|
/**
|
|
75
90
|
* Outer product of two vectors: the matrix of `fun(x[i], y[j])`. Equivalent to R's `outer()`. `fun` is `"*"`
|
|
76
|
-
* (default), `"+"`, `"-"`, `"/"`, `"^"` or a function of two numbers
|
|
91
|
+
* (default), `"+"`, `"-"`, `"/"`, `"^"` or a function of two numbers, given as the third argument or as the option
|
|
92
|
+
* `fun` (giving both is a RangeError).
|
|
77
93
|
*
|
|
78
94
|
* @example
|
|
79
95
|
* ```ts
|
|
@@ -83,7 +99,16 @@ export declare function divide(x: Operand, y: Operand): number[] | number[][];
|
|
|
83
99
|
*
|
|
84
100
|
* @category Linear algebra
|
|
85
101
|
*/
|
|
86
|
-
export declare function outer(x: Sample, y: Sample, fun?:
|
|
102
|
+
export declare function outer(x: Sample, y: Sample, fun?: OuterFun): number[][];
|
|
103
|
+
/**
|
|
104
|
+
* Outer product of two vectors, with the operation in the options: `outer(x, y, { fun: "+" })`.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* outer([1, 2], [1, 2, 3], { fun: "+" }); // [[2, 3, 4], [3, 4, 5]]
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function outer(x: Sample, y: Sample, options: OuterOptions): number[][];
|
|
87
112
|
/**
|
|
88
113
|
* Transpose. Equivalent to R's `t()`. A vector becomes a 1-row matrix.
|
|
89
114
|
*
|