@numero-uno-jpn/rstats 0.12.0 → 0.13.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 +56 -0
- package/README.ja.md +14 -14
- package/README.md +13 -13
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/dist/chunks/chunk-22SXZNR6.js +2 -0
- package/dist/chunks/chunk-2IAT6MT2.js +2 -0
- package/dist/chunks/chunk-3AKA4KEL.js +3 -0
- package/dist/chunks/{chunk-LEIH4U5X.js → chunk-4AQNVF3Z.js} +2 -2
- package/dist/chunks/chunk-4GNJEYXR.js +2 -0
- package/dist/chunks/chunk-AGRKZXLG.js +2 -0
- package/dist/chunks/{chunk-6BH65HGN.js → chunk-BJHBYEEU.js} +1 -1
- package/dist/chunks/chunk-BSOUJPDF.js +2 -0
- package/dist/chunks/{chunk-VQHEOBTM.js → chunk-CQNVEA22.js} +2 -2
- package/dist/chunks/{chunk-XJQKSR52.js → chunk-F44XUHWI.js} +1 -1
- package/dist/chunks/chunk-JGFUZKLJ.js +2 -0
- package/dist/chunks/{chunk-OX3SBG5X.js → chunk-MEMYFTYS.js} +1 -1
- package/dist/chunks/{chunk-EZ2KD5NA.js → chunk-N3FV6H3C.js} +2 -2
- package/dist/chunks/chunk-NRV3BH7I.js +2 -0
- package/dist/chunks/{chunk-UV2MZYDQ.js → chunk-RTWLZPPG.js} +2 -2
- package/dist/chunks/chunk-SLSDFIW2.js +2 -0
- package/dist/chunks/chunk-SVWU5T7C.js +2 -0
- package/dist/chunks/chunk-VG67L3JA.js +3 -0
- package/dist/chunks/chunk-VWGJYRE6.js +2 -0
- package/dist/data/grouping.js +11 -4
- package/dist/descriptive/basic.js +57 -49
- package/dist/descriptive/cov.js +7 -2
- package/dist/descriptive/factanal.d.ts +16 -0
- package/dist/descriptive/factanal.js +26 -0
- package/dist/descriptive/hist.d.ts +8 -2
- package/dist/descriptive/hist.js +10 -2
- package/dist/descriptive/quantile.d.ts +8 -3
- package/dist/descriptive/quantile.js +10 -6
- package/dist/descriptive/table.d.ts +7 -5
- package/dist/descriptive/table.js +39 -22
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +17 -0
- package/dist/distributions/binomial.js +3 -0
- package/dist/distributions/gamma.js +7 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +1 -1
- package/dist/htest/agreement.js +7 -2
- package/dist/htest/groups.js +7 -3
- package/dist/htest/mcnemarTest.js +6 -2
- package/dist/htest/pairwise.js +6 -3
- package/dist/htest/polycor.js +5 -3
- package/dist/htest/tTest.d.ts +3 -2
- package/dist/htest/tTest.js +6 -3
- package/dist/htest/trendTests.js +10 -5
- package/dist/internal/levels.d.ts +11 -0
- package/dist/internal/levels.js +48 -0
- package/dist/internal/summary.d.ts +19 -1
- package/dist/internal/summary.js +69 -8
- package/dist/linalg/balance.d.ts +21 -0
- package/dist/linalg/balance.js +191 -0
- package/dist/linalg/eigen.d.ts +1 -1
- package/dist/linalg/eigen.js +8 -2
- package/dist/linalg/expm.d.ts +1 -11
- package/dist/linalg/expm.js +2 -158
- package/dist/linalg/kappa.d.ts +6 -2
- package/dist/linalg/kappa.js +16 -7
- package/dist/linalg/lu.d.ts +13 -2
- package/dist/linalg/lu.js +17 -3
- package/dist/linalg/qr.d.ts +4 -1
- package/dist/linalg/qr.js +2 -2
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/aovEz.js +6 -4
- package/dist/models/coxph.js +5 -2
- package/dist/models/glm.d.ts +4 -1
- package/dist/models/glmFamily.d.ts +1 -1
- package/dist/models/glmFamily.js +24 -2
- package/dist/models/mauchly.js +2 -1
- package/dist/models/modelFrame.js +2 -1
- package/dist/models/survival.js +5 -1
- package/dist/numerics/integrate.d.ts +1 -1
- package/dist/numerics/integrate.js +13 -3
- package/dist/numerics/interp.d.ts +1 -1
- package/dist/numerics/interp.js +3 -0
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +10 -20
- package/dist/special.js +2 -2
- package/dist/tests.js +12 -12
- package/package.json +1 -1
- package/dist/chunks/chunk-3VMBBQLV.js +0 -3
- package/dist/chunks/chunk-4OKAZJJG.js +0 -3
- package/dist/chunks/chunk-ATC4F4BX.js +0 -2
- package/dist/chunks/chunk-EDH53DXN.js +0 -2
- package/dist/chunks/chunk-GSPOO2G3.js +0 -2
- package/dist/chunks/chunk-K7AZ65H7.js +0 -2
- package/dist/chunks/chunk-SGZQDJRR.js +0 -2
- package/dist/chunks/chunk-UH2N3ZQW.js +0 -2
- package/dist/chunks/chunk-WJQZZYOI.js +0 -2
- package/dist/chunks/chunk-WKEFHOFR.js +0 -2
- package/dist/chunks/chunk-WXC25OPJ.js +0 -2
package/dist/internal/summary.js
CHANGED
|
@@ -3,16 +3,72 @@
|
|
|
3
3
|
// R's mean and var accumulate sums in extended precision. Here sums are taken in double-double (src/special/dd.ts)
|
|
4
4
|
// and rounded to double at the end. The mean divides the first sum by n, then corrects it with the sum of deviations
|
|
5
5
|
// (so that rounding errors from adding double values do not remain).
|
|
6
|
+
//
|
|
7
|
+
// R's long double accumulator has a much wider exponent range than double, so a sum may pass the largest double on
|
|
8
|
+
// the way without overflowing; only the result is rounded to double (±Inf beyond the range). Here the double-double
|
|
9
|
+
// sum runs unscaled while it stays finite (so its results are unchanged), and once it would overflow it continues on
|
|
10
|
+
// the values scaled by a power of 2 (exact) and is scaled back at the end. A mean whose sum overflows a double is the
|
|
11
|
+
// mean of the scaled values, scaled back (R averages x[i] / n in that case, which gives the mean without overflow).
|
|
6
12
|
import { ddAdd, ddDivNum, ddMul, ddSub } from "../special/dd.js";
|
|
13
|
+
export function wideSum() {
|
|
14
|
+
return { s: [0, 0], shift: 0 };
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Power of 2 by which the sums of n finite values are scaled once they overflow: the scaled sum stays below 2^992,
|
|
18
|
+
* so that it can also be divided in double-double (whose splitting overflows above about 2^996). Values below
|
|
19
|
+
* 2^(shift - 1022) lose bits when scaled, which cannot matter next to a sum beyond the double range
|
|
20
|
+
*/
|
|
21
|
+
function overflowShift(n) {
|
|
22
|
+
return 32 + Math.ceil(Math.log2(Math.max(n, 1)));
|
|
23
|
+
}
|
|
24
|
+
/** Adds the finite value v to acc; n is the number of values the sum will have in all */
|
|
25
|
+
export function wideSumAdd(acc, v, n) {
|
|
26
|
+
if (acc.shift === 0) {
|
|
27
|
+
const t = ddAdd(acc.s, [v, 0]);
|
|
28
|
+
if (Number.isFinite(t[0]) && Number.isFinite(t[1])) {
|
|
29
|
+
acc.s = t;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
acc.shift = overflowShift(n);
|
|
33
|
+
const f = 2 ** -acc.shift;
|
|
34
|
+
acc.s = [acc.s[0] * f, acc.s[1] * f];
|
|
35
|
+
}
|
|
36
|
+
acc.s = ddAdd(acc.s, [v * 2 ** -acc.shift, 0]);
|
|
37
|
+
}
|
|
38
|
+
/** The value of the sum rounded to double (±Infinity beyond the range) */
|
|
39
|
+
export function wideSumValue(acc) {
|
|
40
|
+
return ldexp(acc.s[0] + acc.s[1], acc.shift);
|
|
41
|
+
}
|
|
42
|
+
/** v 2^k for an integer k, in two steps so that 2^k itself does not overflow or underflow */
|
|
43
|
+
export function ldexp(v, k) {
|
|
44
|
+
const a = Math.trunc(k / 2);
|
|
45
|
+
return v * 2 ** a * 2 ** (k - a);
|
|
46
|
+
}
|
|
47
|
+
/** Sum of values that are not all finite, as in double arithmetic (NaN propagates, Inf and -Inf give NaN) */
|
|
48
|
+
function nonFiniteSum(x) {
|
|
49
|
+
let s = 0;
|
|
50
|
+
for (const v of x)
|
|
51
|
+
if (!Number.isFinite(v))
|
|
52
|
+
s += v;
|
|
53
|
+
return s;
|
|
54
|
+
}
|
|
7
55
|
/** Mean as double-double. x contains no NaN and has length >= 1 */
|
|
8
56
|
export function meanDD(x) {
|
|
9
57
|
const n = x.length;
|
|
10
|
-
// With Inf present the double-double sum becomes NaN
|
|
58
|
+
// With Inf present the double-double sum becomes NaN; the mean is the (infinite or NaN) sum divided by n
|
|
11
59
|
if (!x.every(Number.isFinite))
|
|
12
|
-
return [x
|
|
13
|
-
|
|
60
|
+
return [nonFiniteSum(x) / n, 0];
|
|
61
|
+
const acc = wideSum();
|
|
14
62
|
for (const v of x)
|
|
15
|
-
|
|
63
|
+
wideSumAdd(acc, v, n);
|
|
64
|
+
if (acc.shift !== 0) {
|
|
65
|
+
// The sum overflows a double: the mean of the values scaled by a power of 2, scaled back
|
|
66
|
+
const k = overflowShift(n);
|
|
67
|
+
const f = 2 ** -k;
|
|
68
|
+
const m = meanDD(x.map((v) => v * f));
|
|
69
|
+
return [ldexp(m[0], k), ldexp(m[1], k)];
|
|
70
|
+
}
|
|
71
|
+
const s = acc.s;
|
|
16
72
|
const m = ddDivNum(s, n);
|
|
17
73
|
if (!Number.isFinite(m[0]))
|
|
18
74
|
return [s[0] / n, 0];
|
|
@@ -62,12 +118,17 @@ function scaleExponent(d) {
|
|
|
62
118
|
m = Math.max(m, Math.abs(v[0]));
|
|
63
119
|
return m === 0 || !Number.isFinite(m) ? 0 : Math.max(-1022, Math.floor(Math.log2(m)));
|
|
64
120
|
}
|
|
65
|
-
/**
|
|
121
|
+
/**
|
|
122
|
+
* Sum (accumulated in double-double without overflow, then rounded; ±Infinity beyond the double range). With
|
|
123
|
+
* infinite or NaN values, as in double arithmetic
|
|
124
|
+
*/
|
|
66
125
|
export function sum(x) {
|
|
67
|
-
|
|
126
|
+
if (!x.every(Number.isFinite))
|
|
127
|
+
return nonFiniteSum(x);
|
|
128
|
+
const acc = wideSum();
|
|
68
129
|
for (const v of x)
|
|
69
|
-
|
|
70
|
-
return
|
|
130
|
+
wideSumAdd(acc, v, x.length);
|
|
131
|
+
return wideSumValue(acc);
|
|
71
132
|
}
|
|
72
133
|
/**
|
|
73
134
|
* Ranks (ties get the average rank, R's rank(ties.method = "average")). x contains no NaN.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Mat } from "./matrix.js";
|
|
2
|
+
/**
|
|
3
|
+
* Balancing (dgebal) with job "P" (permute only), "S" (scale only) or "B" (permute, then scale rows and columns
|
|
4
|
+
* ilo..ihi); A is overwritten. Indices are 0-based: scale[i] holds the index of the row and column interchanged
|
|
5
|
+
* with i for i < ilo and i > ihi, and the scaling factor for ilo <= i <= ihi
|
|
6
|
+
*/
|
|
7
|
+
export declare function gebal(job: "P" | "S" | "B", A: Mat): {
|
|
8
|
+
ilo: number;
|
|
9
|
+
ihi: number;
|
|
10
|
+
scale: Float64Array;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Back transformation of right eigenvectors (dgebak with job "B", side "R"): V holds the eigenvectors of the
|
|
14
|
+
* balanced matrix as columns and is overwritten with those of the original matrix. Row i in ilo..ihi is multiplied
|
|
15
|
+
* by its scaling factor, then the interchanges are undone in reverse order
|
|
16
|
+
*/
|
|
17
|
+
export declare function gebak(bal: {
|
|
18
|
+
ilo: number;
|
|
19
|
+
ihi: number;
|
|
20
|
+
scale: Float64Array;
|
|
21
|
+
}, V: Mat): void;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// Balancing of a general real matrix: a port of LAPACK 3.12.0's SRC/dgebal.f and SRC/dgebak.f (modified BSD; see
|
|
2
|
+
// THIRD_PARTY_NOTICES.md). Used by expm (jobs "P" then "S", as two calls) and by the nonsymmetric eigen (job "B",
|
|
3
|
+
// as dgeev does).
|
|
4
|
+
// - dgebal's DNRM2 is computed with the classic scaled sum of squares rather than reference BLAS 3.12's algorithm
|
|
5
|
+
// (it only decides between powers of 2 in the scaling; the results may differ in rare borderline cases)
|
|
6
|
+
/** 2-norm of n values a[o], a[o + stride], ... (scaled sum of squares) */
|
|
7
|
+
function nrm2(a, o, count, stride) {
|
|
8
|
+
let sc = 0;
|
|
9
|
+
let ssq = 1;
|
|
10
|
+
for (let k = 0; k < count; k++) {
|
|
11
|
+
const v = a[o + k * stride];
|
|
12
|
+
if (v === 0)
|
|
13
|
+
continue;
|
|
14
|
+
const av = Math.abs(v);
|
|
15
|
+
if (sc < av) {
|
|
16
|
+
ssq = 1 + ssq * (sc / av) ** 2;
|
|
17
|
+
sc = av;
|
|
18
|
+
}
|
|
19
|
+
else
|
|
20
|
+
ssq += (av / sc) ** 2;
|
|
21
|
+
}
|
|
22
|
+
return sc * Math.sqrt(ssq);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Balancing (dgebal) with job "P" (permute only), "S" (scale only) or "B" (permute, then scale rows and columns
|
|
26
|
+
* ilo..ihi); A is overwritten. Indices are 0-based: scale[i] holds the index of the row and column interchanged
|
|
27
|
+
* with i for i < ilo and i > ihi, and the scaling factor for ilo <= i <= ihi
|
|
28
|
+
*/
|
|
29
|
+
export function gebal(job, A) {
|
|
30
|
+
const n = A.nrow;
|
|
31
|
+
const a = A.data;
|
|
32
|
+
const at = (i, j) => i + j * n;
|
|
33
|
+
const scale = new Float64Array(n);
|
|
34
|
+
const swapCols = (i, j, rows) => {
|
|
35
|
+
for (let r = 0; r < rows; r++)
|
|
36
|
+
[a[at(r, i)], a[at(r, j)]] = [a[at(r, j)], a[at(r, i)]];
|
|
37
|
+
};
|
|
38
|
+
const swapRows = (i, j, from) => {
|
|
39
|
+
for (let c = from; c < n; c++)
|
|
40
|
+
[a[at(i, c)], a[at(j, c)]] = [a[at(j, c)], a[at(i, c)]];
|
|
41
|
+
};
|
|
42
|
+
// Permutation to isolate eigenvalues if possible
|
|
43
|
+
let k = 0;
|
|
44
|
+
let l = n - 1;
|
|
45
|
+
if (job !== "S") {
|
|
46
|
+
// Search for rows isolating an eigenvalue and push them down (the loop bounds are fixed at entry, as in Fortran)
|
|
47
|
+
let noconv = true;
|
|
48
|
+
while (noconv) {
|
|
49
|
+
noconv = false;
|
|
50
|
+
for (let i = l; i >= 0; i--) {
|
|
51
|
+
let canswap = true;
|
|
52
|
+
for (let j = 0; j <= l; j++) {
|
|
53
|
+
if (i !== j && a[at(i, j)] !== 0) {
|
|
54
|
+
canswap = false;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (canswap) {
|
|
59
|
+
scale[l] = i;
|
|
60
|
+
if (i !== l) {
|
|
61
|
+
swapCols(i, l, l + 1);
|
|
62
|
+
swapRows(i, l, k);
|
|
63
|
+
}
|
|
64
|
+
noconv = true;
|
|
65
|
+
if (l === 0)
|
|
66
|
+
return { ilo: 0, ihi: 0, scale };
|
|
67
|
+
l--;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Search for columns isolating an eigenvalue and push them left
|
|
72
|
+
noconv = true;
|
|
73
|
+
while (noconv) {
|
|
74
|
+
noconv = false;
|
|
75
|
+
for (let j = k, last = l; j <= last; j++) {
|
|
76
|
+
let canswap = true;
|
|
77
|
+
for (let i = k; i <= l; i++) {
|
|
78
|
+
if (i !== j && a[at(i, j)] !== 0) {
|
|
79
|
+
canswap = false;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (canswap) {
|
|
84
|
+
scale[k] = j;
|
|
85
|
+
if (j !== k) {
|
|
86
|
+
swapCols(j, k, l + 1);
|
|
87
|
+
swapRows(j, k, k);
|
|
88
|
+
}
|
|
89
|
+
noconv = true;
|
|
90
|
+
k++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
for (let i = k; i <= l; i++)
|
|
96
|
+
scale[i] = 1;
|
|
97
|
+
if (job === "P")
|
|
98
|
+
return { ilo: k, ihi: l, scale };
|
|
99
|
+
// Balance the submatrix in rows k to l by powers of 2 (iterative loop for norm reduction)
|
|
100
|
+
const SCLFAC = 2;
|
|
101
|
+
const FACTOR = 0.95;
|
|
102
|
+
const sfmin1 = 2 ** -1022 / Number.EPSILON;
|
|
103
|
+
const sfmax1 = 1 / sfmin1;
|
|
104
|
+
const sfmin2 = sfmin1 * SCLFAC;
|
|
105
|
+
const sfmax2 = 1 / sfmin2;
|
|
106
|
+
let noconv = true;
|
|
107
|
+
while (noconv) {
|
|
108
|
+
noconv = false;
|
|
109
|
+
for (let i = k; i <= l; i++) {
|
|
110
|
+
let c = nrm2(a, at(k, i), l - k + 1, 1);
|
|
111
|
+
let r = nrm2(a, at(i, k), l - k + 1, n);
|
|
112
|
+
let ca = 0;
|
|
113
|
+
for (let q = 0; q <= l; q++)
|
|
114
|
+
ca = Math.max(ca, Math.abs(a[at(q, i)]));
|
|
115
|
+
let ra = 0;
|
|
116
|
+
for (let q = k; q < n; q++)
|
|
117
|
+
ra = Math.max(ra, Math.abs(a[at(i, q)]));
|
|
118
|
+
// Guard against zero c or r due to underflow
|
|
119
|
+
if (c === 0 || r === 0)
|
|
120
|
+
continue;
|
|
121
|
+
if (Number.isNaN(c + ca + r + ra))
|
|
122
|
+
throw new RangeError("the matrix contains NaN or infinite values");
|
|
123
|
+
let g = r / SCLFAC;
|
|
124
|
+
let f = 1;
|
|
125
|
+
const s = c + r;
|
|
126
|
+
while (c < g && Math.max(f, c, ca) < sfmax2 && Math.min(r, g, ra) > sfmin2) {
|
|
127
|
+
f *= SCLFAC;
|
|
128
|
+
c *= SCLFAC;
|
|
129
|
+
ca *= SCLFAC;
|
|
130
|
+
r /= SCLFAC;
|
|
131
|
+
g /= SCLFAC;
|
|
132
|
+
ra /= SCLFAC;
|
|
133
|
+
}
|
|
134
|
+
g = c / SCLFAC;
|
|
135
|
+
while (g >= r && Math.max(r, ra) < sfmax2 && Math.min(f, c, g, ca) > sfmin2) {
|
|
136
|
+
f /= SCLFAC;
|
|
137
|
+
c /= SCLFAC;
|
|
138
|
+
g /= SCLFAC;
|
|
139
|
+
ca /= SCLFAC;
|
|
140
|
+
r *= SCLFAC;
|
|
141
|
+
ra *= SCLFAC;
|
|
142
|
+
}
|
|
143
|
+
// Now balance
|
|
144
|
+
if (c + r >= FACTOR * s)
|
|
145
|
+
continue;
|
|
146
|
+
if (f < 1 && scale[i] < 1 && f * scale[i] <= sfmin1)
|
|
147
|
+
continue;
|
|
148
|
+
if (f > 1 && scale[i] > 1 && scale[i] >= sfmax1 / f)
|
|
149
|
+
continue;
|
|
150
|
+
g = 1 / f;
|
|
151
|
+
scale[i] = scale[i] * f;
|
|
152
|
+
noconv = true;
|
|
153
|
+
for (let q = k; q < n; q++)
|
|
154
|
+
a[at(i, q)] = a[at(i, q)] * g;
|
|
155
|
+
for (let q = 0; q <= l; q++)
|
|
156
|
+
a[at(q, i)] = a[at(q, i)] * f;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return { ilo: k, ihi: l, scale };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Back transformation of right eigenvectors (dgebak with job "B", side "R"): V holds the eigenvectors of the
|
|
163
|
+
* balanced matrix as columns and is overwritten with those of the original matrix. Row i in ilo..ihi is multiplied
|
|
164
|
+
* by its scaling factor, then the interchanges are undone in reverse order
|
|
165
|
+
*/
|
|
166
|
+
export function gebak(bal, V) {
|
|
167
|
+
const { ilo, ihi, scale } = bal;
|
|
168
|
+
const n = V.nrow;
|
|
169
|
+
const m = V.ncol;
|
|
170
|
+
const v = V.data;
|
|
171
|
+
if (n === 0 || m === 0)
|
|
172
|
+
return;
|
|
173
|
+
if (ilo !== ihi) {
|
|
174
|
+
for (let i = ilo; i <= ihi; i++) {
|
|
175
|
+
const s = scale[i];
|
|
176
|
+
for (let j = 0; j < m; j++)
|
|
177
|
+
v[i + j * n] = v[i + j * n] * s;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const swap = (i) => {
|
|
181
|
+
const k = scale[i];
|
|
182
|
+
if (k === i)
|
|
183
|
+
return;
|
|
184
|
+
for (let j = 0; j < m; j++)
|
|
185
|
+
[v[i + j * n], v[k + j * n]] = [v[k + j * n], v[i + j * n]];
|
|
186
|
+
};
|
|
187
|
+
for (let i = ilo - 1; i >= 0; i--)
|
|
188
|
+
swap(i);
|
|
189
|
+
for (let i = ihi + 1; i < n; i++)
|
|
190
|
+
swap(i);
|
|
191
|
+
}
|
package/dist/linalg/eigen.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export interface EigenResult {
|
|
|
35
35
|
* Eigenvalues and eigenvectors of a square matrix. Equivalent to R's `eigen()`.
|
|
36
36
|
*
|
|
37
37
|
* Symmetric matrices use tridiagonalization and the implicit QL method; other matrices use Hessenberg reduction
|
|
38
|
-
* and the Francis double-shift QR method
|
|
38
|
+
* and the Francis double-shift QR method, after balancing the matrix as LAPACK's `dgebal` does. R uses LAPACK, so eigenvalues agree up to rounding, but
|
|
39
39
|
* eigenvector signs may differ from R (both are correct); for repeated eigenvalues the basis may differ.
|
|
40
40
|
* Complex eigenvalues and eigenvectors are returned as separate real and imaginary parts (`valuesIm`,
|
|
41
41
|
* `vectorsIm`). Each complex eigenvector is scaled to unit length with its largest component real and positive.
|
package/dist/linalg/eigen.js
CHANGED
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
// then compute eigenvectors by back substitution (same procedure as EISPACK's orthes and hqr2).
|
|
6
6
|
// The code is modelled on JAMA's EigenvalueDecomposition. EISPACK and JAMA are in the public domain.
|
|
7
7
|
// R uses LAPACK's dsyevr and dgeev, so eigenvalues agree to rounding error, while eigenvectors may differ in sign
|
|
8
|
-
// (phase for complex ones, orientation for repeated eigenvalues).
|
|
8
|
+
// (phase for complex ones, orientation for repeated eigenvalues).
|
|
9
|
+
// - Nonsymmetric matrices are balanced first as dgeev does (dgebal with job "B": permutations isolating eigenvalues,
|
|
10
|
+
// then scaling of the remaining rows and columns by powers of 2; src/linalg/balance.ts), and the eigenvectors are
|
|
11
|
+
// transformed back (dgebak) before they are normalised. Symmetric matrices are not balanced (nor are they by dsyevr)
|
|
9
12
|
// Argument checks and result shapes follow R's documentation (?eigen) and observed output.
|
|
10
13
|
// - If symmetric is omitted, the matrix is treated as symmetric when sum|A - t(A)| / sum|A| <= 100 ε, as R's isSymmetric
|
|
11
14
|
// - For symmetric matrices only the lower triangle is used
|
|
@@ -14,12 +17,13 @@
|
|
|
14
17
|
// (LAPACK's dgeev only makes that component real; signs may differ from R)
|
|
15
18
|
// - If a nonsymmetric matrix has all real eigenvalues, valuesIm and vectorsIm are omitted (R also returns real results)
|
|
16
19
|
// - NaN or Inf is an error
|
|
20
|
+
import { gebak, gebal } from "./balance.js";
|
|
17
21
|
import { fromMat, newMat, requireSquare, toMat } from "./matrix.js";
|
|
18
22
|
/**
|
|
19
23
|
* Eigenvalues and eigenvectors of a square matrix. Equivalent to R's `eigen()`.
|
|
20
24
|
*
|
|
21
25
|
* Symmetric matrices use tridiagonalization and the implicit QL method; other matrices use Hessenberg reduction
|
|
22
|
-
* and the Francis double-shift QR method
|
|
26
|
+
* and the Francis double-shift QR method, after balancing the matrix as LAPACK's `dgebal` does. R uses LAPACK, so eigenvalues agree up to rounding, but
|
|
23
27
|
* eigenvector signs may differ from R (both are correct); for repeated eigenvalues the basis may differ.
|
|
24
28
|
* Complex eigenvalues and eigenvectors are returned as separate real and imaginary parts (`valuesIm`,
|
|
25
29
|
* `vectorsIm`). Each complex eigenvector is scaled to unit length with its largest component real and positive.
|
|
@@ -277,6 +281,7 @@ function generalEigen(A, wantVectors) {
|
|
|
277
281
|
const V = newMat(n, n);
|
|
278
282
|
const d = new Float64Array(n);
|
|
279
283
|
const e = new Float64Array(n);
|
|
284
|
+
const bal = gebal("B", H);
|
|
280
285
|
orthes(H, V);
|
|
281
286
|
hqr2(H, V, d, e);
|
|
282
287
|
// Sort by decreasing absolute value (ties keep original order; conjugate pairs already have the positive imaginary part first)
|
|
@@ -288,6 +293,7 @@ function generalEigen(A, wantVectors) {
|
|
|
288
293
|
out.valuesIm = order.map((i) => e[i]);
|
|
289
294
|
if (!wantVectors)
|
|
290
295
|
return out;
|
|
296
|
+
gebak(bal, V);
|
|
291
297
|
// Eigenvectors: real eigenvalues use columns of V; a complex pair (j, j+1) gives V[:, j] ± i V[:, j+1]
|
|
292
298
|
const re = newMat(n, n);
|
|
293
299
|
const im = newMat(n, n);
|
package/dist/linalg/expm.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type MatrixInput } from "./matrix.js";
|
|
2
2
|
/**
|
|
3
3
|
* Methods of {@link expm}.
|
|
4
4
|
*
|
|
@@ -46,13 +46,3 @@ export interface ExpmOptions {
|
|
|
46
46
|
* @category Linear algebra
|
|
47
47
|
*/
|
|
48
48
|
export declare function expm(x: MatrixInput, options?: ExpmOptions): number[][];
|
|
49
|
-
/**
|
|
50
|
-
* Balancing (dgebal) with job "P" (permute only) or "S" (scale only); A is overwritten. Indices are 0-based:
|
|
51
|
-
* scale[i] holds the index of the row and column interchanged with i for i < ilo and i > ihi, and the scaling factor
|
|
52
|
-
* for ilo <= i <= ihi
|
|
53
|
-
*/
|
|
54
|
-
export declare function gebal(job: "P" | "S", A: Mat): {
|
|
55
|
-
ilo: number;
|
|
56
|
-
ihi: number;
|
|
57
|
-
scale: Float64Array;
|
|
58
|
-
};
|
package/dist/linalg/expm.js
CHANGED
|
@@ -4,14 +4,12 @@
|
|
|
4
4
|
// Sources: the specification oracle/matfn-behaviour.md; the scaling and squaring method with Padé approximants of
|
|
5
5
|
// N. J. Higham, "The scaling and squaring method for the matrix exponential revisited", SIAM J. Matrix Anal. Appl.
|
|
6
6
|
// 26 (2005) 1179-1193 (degrees 3, 5, 7, 9 and 13, the bounds on the 1-norm and the Padé coefficients);
|
|
7
|
-
// the balancing is a port of LAPACK 3.12.0's SRC/dgebal.f
|
|
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
9
|
// the result, as two separate calls; "Higham08" does not balance
|
|
10
10
|
// - The Padé degree is chosen from the 1-norm of the (balanced) matrix; above 2.1, degree 13 with scaling by 2^-s
|
|
11
11
|
// - X = (V - U)^-1 (V + U) is solved by LU decomposition as solve (which throws if V - U is computationally
|
|
12
12
|
// singular), then squared s times, and the balancing is undone
|
|
13
|
-
// - dgebal's DNRM2 is computed with the classic scaled sum of squares rather than reference BLAS 3.12's algorithm
|
|
14
|
-
// (it only decides between powers of 2 in the scaling; the results may differ in rare borderline cases)
|
|
15
13
|
// - "Pade", "Taylor", "PadeO", "TaylorO" (Shapira and Firth's methods, as the specification describes them): scaling
|
|
16
14
|
// by N = 2^p with p from sqrt(1-norm x infinity-norm), a Taylor polynomial or a diagonal Padé approximant of degree
|
|
17
15
|
// order (default 8) by Horner's rule, the Padé quotient by the conjugate gradient squared method column by column,
|
|
@@ -20,6 +18,7 @@
|
|
|
20
18
|
// - A 1 x 1 matrix gives exp of its entry, a 0 x 0 matrix gives a 0 x 0 matrix
|
|
21
19
|
import { fromMat, newMat, requireSquare, toMat } from "./matrix.js";
|
|
22
20
|
import { infNorm, oneNorm } from "./norm.js";
|
|
21
|
+
import { gebal } from "./balance.js";
|
|
23
22
|
import { solve } from "./solve.js";
|
|
24
23
|
const METHODS = ["Higham08.b", "Higham08", "Pade", "Taylor", "PadeO", "TaylorO"];
|
|
25
24
|
/**
|
|
@@ -317,158 +316,3 @@ function comb(terms) {
|
|
|
317
316
|
axpy(Y, terms[t][0], terms[t][1]);
|
|
318
317
|
return Y;
|
|
319
318
|
}
|
|
320
|
-
/** 2-norm of n values a[o], a[o + stride], ... (scaled sum of squares) */
|
|
321
|
-
function nrm2(a, o, count, stride) {
|
|
322
|
-
let sc = 0;
|
|
323
|
-
let ssq = 1;
|
|
324
|
-
for (let k = 0; k < count; k++) {
|
|
325
|
-
const v = a[o + k * stride];
|
|
326
|
-
if (v === 0)
|
|
327
|
-
continue;
|
|
328
|
-
const av = Math.abs(v);
|
|
329
|
-
if (sc < av) {
|
|
330
|
-
ssq = 1 + ssq * (sc / av) ** 2;
|
|
331
|
-
sc = av;
|
|
332
|
-
}
|
|
333
|
-
else
|
|
334
|
-
ssq += (av / sc) ** 2;
|
|
335
|
-
}
|
|
336
|
-
return sc * Math.sqrt(ssq);
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Balancing (dgebal) with job "P" (permute only) or "S" (scale only); A is overwritten. Indices are 0-based:
|
|
340
|
-
* scale[i] holds the index of the row and column interchanged with i for i < ilo and i > ihi, and the scaling factor
|
|
341
|
-
* for ilo <= i <= ihi
|
|
342
|
-
*/
|
|
343
|
-
export function gebal(job, A) {
|
|
344
|
-
const n = A.nrow;
|
|
345
|
-
const a = A.data;
|
|
346
|
-
const at = (i, j) => i + j * n;
|
|
347
|
-
const scale = new Float64Array(n);
|
|
348
|
-
const swapCols = (i, j, rows) => {
|
|
349
|
-
for (let r = 0; r < rows; r++)
|
|
350
|
-
[a[at(r, i)], a[at(r, j)]] = [a[at(r, j)], a[at(r, i)]];
|
|
351
|
-
};
|
|
352
|
-
const swapRows = (i, j, from) => {
|
|
353
|
-
for (let c = from; c < n; c++)
|
|
354
|
-
[a[at(i, c)], a[at(j, c)]] = [a[at(j, c)], a[at(i, c)]];
|
|
355
|
-
};
|
|
356
|
-
// Permutation to isolate eigenvalues if possible
|
|
357
|
-
let k = 0;
|
|
358
|
-
let l = n - 1;
|
|
359
|
-
if (job === "P") {
|
|
360
|
-
// Search for rows isolating an eigenvalue and push them down (the loop bounds are fixed at entry, as in Fortran)
|
|
361
|
-
let noconv = true;
|
|
362
|
-
while (noconv) {
|
|
363
|
-
noconv = false;
|
|
364
|
-
for (let i = l; i >= 0; i--) {
|
|
365
|
-
let canswap = true;
|
|
366
|
-
for (let j = 0; j <= l; j++) {
|
|
367
|
-
if (i !== j && a[at(i, j)] !== 0) {
|
|
368
|
-
canswap = false;
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
if (canswap) {
|
|
373
|
-
scale[l] = i;
|
|
374
|
-
if (i !== l) {
|
|
375
|
-
swapCols(i, l, l + 1);
|
|
376
|
-
swapRows(i, l, k);
|
|
377
|
-
}
|
|
378
|
-
noconv = true;
|
|
379
|
-
if (l === 0)
|
|
380
|
-
return { ilo: 0, ihi: 0, scale };
|
|
381
|
-
l--;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
// Search for columns isolating an eigenvalue and push them left
|
|
386
|
-
noconv = true;
|
|
387
|
-
while (noconv) {
|
|
388
|
-
noconv = false;
|
|
389
|
-
for (let j = k, last = l; j <= last; j++) {
|
|
390
|
-
let canswap = true;
|
|
391
|
-
for (let i = k; i <= l; i++) {
|
|
392
|
-
if (i !== j && a[at(i, j)] !== 0) {
|
|
393
|
-
canswap = false;
|
|
394
|
-
break;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
if (canswap) {
|
|
398
|
-
scale[k] = j;
|
|
399
|
-
if (j !== k) {
|
|
400
|
-
swapCols(j, k, l + 1);
|
|
401
|
-
swapRows(j, k, k);
|
|
402
|
-
}
|
|
403
|
-
noconv = true;
|
|
404
|
-
k++;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
for (let i = k; i <= l; i++)
|
|
410
|
-
scale[i] = 1;
|
|
411
|
-
if (job === "P")
|
|
412
|
-
return { ilo: k, ihi: l, scale };
|
|
413
|
-
// Balance the submatrix in rows k to l by powers of 2 (iterative loop for norm reduction)
|
|
414
|
-
const SCLFAC = 2;
|
|
415
|
-
const FACTOR = 0.95;
|
|
416
|
-
const sfmin1 = 2 ** -1022 / Number.EPSILON;
|
|
417
|
-
const sfmax1 = 1 / sfmin1;
|
|
418
|
-
const sfmin2 = sfmin1 * SCLFAC;
|
|
419
|
-
const sfmax2 = 1 / sfmin2;
|
|
420
|
-
let noconv = true;
|
|
421
|
-
while (noconv) {
|
|
422
|
-
noconv = false;
|
|
423
|
-
for (let i = k; i <= l; i++) {
|
|
424
|
-
let c = nrm2(a, at(k, i), l - k + 1, 1);
|
|
425
|
-
let r = nrm2(a, at(i, k), l - k + 1, n);
|
|
426
|
-
let ca = 0;
|
|
427
|
-
for (let q = 0; q <= l; q++)
|
|
428
|
-
ca = Math.max(ca, Math.abs(a[at(q, i)]));
|
|
429
|
-
let ra = 0;
|
|
430
|
-
for (let q = k; q < n; q++)
|
|
431
|
-
ra = Math.max(ra, Math.abs(a[at(i, q)]));
|
|
432
|
-
// Guard against zero c or r due to underflow
|
|
433
|
-
if (c === 0 || r === 0)
|
|
434
|
-
continue;
|
|
435
|
-
if (Number.isNaN(c + ca + r + ra))
|
|
436
|
-
throw new RangeError("the matrix contains NaN or infinite values");
|
|
437
|
-
let g = r / SCLFAC;
|
|
438
|
-
let f = 1;
|
|
439
|
-
const s = c + r;
|
|
440
|
-
while (c < g && Math.max(f, c, ca) < sfmax2 && Math.min(r, g, ra) > sfmin2) {
|
|
441
|
-
f *= SCLFAC;
|
|
442
|
-
c *= SCLFAC;
|
|
443
|
-
ca *= SCLFAC;
|
|
444
|
-
r /= SCLFAC;
|
|
445
|
-
g /= SCLFAC;
|
|
446
|
-
ra /= SCLFAC;
|
|
447
|
-
}
|
|
448
|
-
g = c / SCLFAC;
|
|
449
|
-
while (g >= r && Math.max(r, ra) < sfmax2 && Math.min(f, c, g, ca) > sfmin2) {
|
|
450
|
-
f /= SCLFAC;
|
|
451
|
-
c /= SCLFAC;
|
|
452
|
-
g /= SCLFAC;
|
|
453
|
-
ca /= SCLFAC;
|
|
454
|
-
r *= SCLFAC;
|
|
455
|
-
ra *= SCLFAC;
|
|
456
|
-
}
|
|
457
|
-
// Now balance
|
|
458
|
-
if (c + r >= FACTOR * s)
|
|
459
|
-
continue;
|
|
460
|
-
if (f < 1 && scale[i] < 1 && f * scale[i] <= sfmin1)
|
|
461
|
-
continue;
|
|
462
|
-
if (f > 1 && scale[i] > 1 && scale[i] >= sfmax1 / f)
|
|
463
|
-
continue;
|
|
464
|
-
g = 1 / f;
|
|
465
|
-
scale[i] = scale[i] * f;
|
|
466
|
-
noconv = true;
|
|
467
|
-
for (let q = k; q < n; q++)
|
|
468
|
-
a[at(i, q)] = a[at(i, q)] * g;
|
|
469
|
-
for (let q = 0; q <= l; q++)
|
|
470
|
-
a[at(q, i)] = a[at(q, i)] * f;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
return { ilo: k, ihi: l, scale };
|
|
474
|
-
}
|
package/dist/linalg/kappa.d.ts
CHANGED
|
@@ -9,8 +9,12 @@ export interface KappaOptions {
|
|
|
9
9
|
/** Compute the condition number exactly instead of estimating it. Default: `false`. */
|
|
10
10
|
exact?: boolean;
|
|
11
11
|
/**
|
|
12
|
-
* The norm. Default: `"2"` when `exact` is `true`, otherwise `"1"`. With `exact:
|
|
13
|
-
*
|
|
12
|
+
* The norm. Default: `"2"` when `exact` is `true`, otherwise `"1"`. With `exact: true`, `"2"` uses the singular
|
|
13
|
+
* values and other norms use `norm(z) * norm(solve(z))`. With `exact: false`, as in R: the estimates through a
|
|
14
|
+
* triangular factor (`method: "qr"`, or `"direct"` with `triangular: true`) use the infinity norm only for `"I"`
|
|
15
|
+
* written exactly so and the 1-norm for any other norm (R warns for norms other than `"1"`, `"O"` and `"I"`, and
|
|
16
|
+
* for `"2"`); `method: "direct"` without `triangular` accepts `"1"`, `"O"` and `"I"` in either case and throws for
|
|
17
|
+
* any other.
|
|
14
18
|
*/
|
|
15
19
|
norm?: NormType;
|
|
16
20
|
/**
|
package/dist/linalg/kappa.js
CHANGED
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
// - For dtrco with norm: "I", the same-side triangle of the transposed matrix is used, matching R's output
|
|
12
12
|
// (this looks at the opposite triangle of the original matrix; for R from a QR decomposition, only the diagonal)
|
|
13
13
|
// - triangular only has an effect with method: "direct" (from R's output)
|
|
14
|
+
// - Norms of the estimates (R's documented and observed behaviour): through a triangular factor (method "qr", or
|
|
15
|
+
// "direct" with triangular), only the norm written exactly "I" gives the infinity-norm estimate and every other
|
|
16
|
+
// norm ("2", "F", "M", "E", lower-case "i", ...) the 1-norm estimate (R warns for "2" and for norms other than
|
|
17
|
+
// "1", "O" and "I"; the library does not report warnings). rcond (method "direct") accepts "1", "O" and "I" in
|
|
18
|
+
// either case and rejects any other with R's message
|
|
14
19
|
// ginv is the Moore-Penrose generalised inverse via the SVD; singular values at most largest singular value × tol are treated as 0.
|
|
15
20
|
import { fromMat, newMat, toMat, transpose } from "./matrix.js";
|
|
16
21
|
import { norm } from "./norm.js";
|
|
@@ -37,9 +42,9 @@ import { svd } from "./svd.js";
|
|
|
37
42
|
* @category Linear algebra
|
|
38
43
|
*/
|
|
39
44
|
export function kappa(z, options = {}) {
|
|
40
|
-
// The default norm is "2" if exact, "1" if estimated
|
|
41
|
-
const nrm = options.norm === undefined ? (options.exact ? "2" : "O") : normalize(options.norm);
|
|
42
45
|
if (options.exact) {
|
|
46
|
+
// The default norm is "2" if exact, "1" if estimated
|
|
47
|
+
const nrm = options.norm === undefined ? "2" : normalize(options.norm);
|
|
43
48
|
if (nrm === "2") {
|
|
44
49
|
const d = svd(z, { nu: 0, nv: 0 }).d;
|
|
45
50
|
const min = Math.min(...d);
|
|
@@ -47,17 +52,21 @@ export function kappa(z, options = {}) {
|
|
|
47
52
|
}
|
|
48
53
|
return norm(z, { type: nrm }) * norm(solve(z), { type: nrm });
|
|
49
54
|
}
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
// Through a triangular factor, only "I" itself is the infinity norm; any other norm becomes the 1-norm (as in R)
|
|
56
|
+
const triNorm = options.norm === "I" ? "I" : "O";
|
|
52
57
|
if ((options.method ?? "qr") === "direct") {
|
|
53
58
|
if (options.triangular)
|
|
54
|
-
return 1 / triangularEstimate(toMat(z), (options.uplo ?? "U") === "U",
|
|
55
|
-
|
|
59
|
+
return 1 / triangularEstimate(toMat(z), (options.uplo ?? "U") === "U", triNorm);
|
|
60
|
+
const t = options.norm ?? "O";
|
|
61
|
+
const u = t.toUpperCase();
|
|
62
|
+
if (u !== "1" && u !== "O" && u !== "I")
|
|
63
|
+
throw new RangeError(`argument type[1]='${t}' must be one of '1','O', or 'I'`);
|
|
64
|
+
return 1 / rcond(z, { norm: u === "I" ? "I" : "O" });
|
|
56
65
|
}
|
|
57
66
|
// If wide, transpose before the QR decomposition (from R's output)
|
|
58
67
|
const zm = toMat(z);
|
|
59
68
|
const zz = zm.nrow < zm.ncol ? fromMat(transpose(zm)) : z;
|
|
60
|
-
return 1 / triangularEstimate(toMat(qrR(qr(zz))), true,
|
|
69
|
+
return 1 / triangularEstimate(toMat(qrR(qr(zz))), true, triNorm);
|
|
61
70
|
}
|
|
62
71
|
/**
|
|
63
72
|
* Reciprocal condition number of a triangular matrix by LINPACK's dtrco. With norm: "I", matching R's output,
|