@numero-uno-jpn/rstats 0.12.0 → 0.14.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 +76 -0
- package/README.ja.md +26 -17
- package/README.md +25 -16
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/dist/chunks/{chunk-VQHEOBTM.js → chunk-6JRESWVF.js} +2 -2
- package/dist/chunks/chunk-7CPM3M6Q.js +2 -0
- package/dist/chunks/chunk-7PSGKKOP.js +2 -0
- package/dist/chunks/{chunk-LEIH4U5X.js → chunk-7T74KLE5.js} +2 -2
- package/dist/chunks/{chunk-XJQKSR52.js → chunk-A76XBA3D.js} +1 -1
- package/dist/chunks/chunk-ACE64PMM.js +3 -0
- package/dist/chunks/chunk-DCGBYO44.js +2 -0
- package/dist/chunks/chunk-DWNGNTS6.js +2 -0
- package/dist/chunks/chunk-FBDZ6B2G.js +2 -0
- package/dist/chunks/chunk-IBJ7RI44.js +2 -0
- package/dist/chunks/chunk-IGSR4GIF.js +2 -0
- package/dist/chunks/{chunk-6BH65HGN.js → chunk-NTETWLP3.js} +1 -1
- package/dist/chunks/chunk-Q4JF4QHS.js +2 -0
- package/dist/chunks/chunk-RZIWVCHO.js +2 -0
- package/dist/chunks/chunk-SD7ML5PE.js +2 -0
- package/dist/chunks/chunk-SZ357HRC.js +3 -0
- package/dist/chunks/chunk-WM33ADZV.js +2 -0
- package/dist/chunks/{chunk-OX3SBG5X.js → chunk-XCAMWVNO.js} +1 -1
- package/dist/chunks/chunk-YQLEXH2Y.js +3 -0
- package/dist/chunks/chunk-YTV3RFCA.js +2 -0
- package/dist/chunks/chunk-ZUESNU7W.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/mixed.d.ts +3 -1
- package/dist/entries/mixed.js +4 -1
- 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/fround.d.ts +12 -0
- package/dist/internal/fround.js +89 -0
- 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 +4 -3
- 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/lmer.d.ts +37 -4
- package/dist/models/lmer.js +18 -1
- package/dist/models/lmerAnova.d.ts +138 -0
- package/dist/models/lmerAnova.js +134 -0
- package/dist/models/lmerHypotheses.d.ts +105 -0
- package/dist/models/lmerHypotheses.js +303 -0
- package/dist/models/lmerTest.d.ts +233 -0
- package/dist/models/lmerTest.js +306 -0
- package/dist/models/mauchly.js +2 -1
- package/dist/models/mixedDerivatives.d.ts +27 -0
- package/dist/models/mixedDerivatives.js +342 -0
- package/dist/models/mixedDeviance.d.ts +112 -0
- package/dist/models/mixedDeviance.js +216 -86
- package/dist/models/mixedStructure.d.ts +5 -3
- package/dist/models/modelFrame.js +2 -1
- package/dist/models/modelMatrix.d.ts +10 -3
- package/dist/models/modelMatrix.js +23 -2
- 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 +11 -12
- package/llms.txt +6 -1
- 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-EZ2KD5NA.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-UV2MZYDQ.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
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// R's round(x, digits) and zapsmall(x).
|
|
2
|
+
//
|
|
3
|
+
// Written for rStats.js (no ported code) from the description in oracle/lmertest-behaviour.md ("Rounding") and R's
|
|
4
|
+
// documentation of round (?Round: the candidates on either side of x at the given number of decimal digits, the one
|
|
5
|
+
// closer to x chosen, a tie to the even last digit; digits = 0 is rounding to the nearest integer, ties to even).
|
|
6
|
+
const LOG10_2 = Math.log10(2);
|
|
7
|
+
/** The binary exponent of |x| (finite, nonzero): the exponent of its leading bit, as C's logb */
|
|
8
|
+
function binaryExponent(x) {
|
|
9
|
+
const view = new DataView(new ArrayBuffer(8));
|
|
10
|
+
view.setFloat64(0, x);
|
|
11
|
+
const biased = (view.getUint16(0) >> 4) & 0x7ff;
|
|
12
|
+
if (biased > 0)
|
|
13
|
+
return biased - 1023;
|
|
14
|
+
// subnormal: the position of the leading bit of the significand
|
|
15
|
+
const hi = view.getUint32(0) & 0xfffff;
|
|
16
|
+
const lo = view.getUint32(4);
|
|
17
|
+
const bits = hi !== 0 ? 32 + (31 - Math.clz32(hi)) : 31 - Math.clz32(lo);
|
|
18
|
+
return bits - 1074;
|
|
19
|
+
}
|
|
20
|
+
/** 10^k for an integer k >= 0, by repeated multiplication in double arithmetic */
|
|
21
|
+
function powerOf10(k) {
|
|
22
|
+
let p = 1;
|
|
23
|
+
for (let i = 0; i < k; i++)
|
|
24
|
+
p *= 10;
|
|
25
|
+
return p;
|
|
26
|
+
}
|
|
27
|
+
/** The nearest integer, ties to even (C's nearbyint in the default rounding mode); the sign of x is kept */
|
|
28
|
+
function roundHalfEven(x) {
|
|
29
|
+
const f = Math.floor(x);
|
|
30
|
+
const d = x - f;
|
|
31
|
+
const r = d < 0.5 ? f : d > 0.5 ? f + 1 : f % 2 === 0 ? f : f + 1;
|
|
32
|
+
return r === 0 ? (x < 0 ? -0 : 0) : r;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* R's `round(x, digits)`: x rounded to `digits` decimal places (negative: to a power of ten), choosing between the
|
|
36
|
+
* two neighbouring candidates the one closer to x in double arithmetic, a tie to the even one. Values that already
|
|
37
|
+
* have at most about 15 significant digits at that position are returned unchanged.
|
|
38
|
+
*/
|
|
39
|
+
export function roundR(x, digits = 0) {
|
|
40
|
+
if (Number.isNaN(x) || Number.isNaN(digits))
|
|
41
|
+
return NaN;
|
|
42
|
+
if (!Number.isFinite(x) || x === 0 || digits > 323)
|
|
43
|
+
return x;
|
|
44
|
+
if (digits < -308)
|
|
45
|
+
return 0;
|
|
46
|
+
if (digits === 0)
|
|
47
|
+
return roundHalfEven(x);
|
|
48
|
+
const dig = Math.floor(digits + 0.5);
|
|
49
|
+
const ax = Math.abs(x);
|
|
50
|
+
const l = LOG10_2 * (0.5 + binaryExponent(ax));
|
|
51
|
+
if (l + dig > 15)
|
|
52
|
+
return x;
|
|
53
|
+
let xd;
|
|
54
|
+
let xu;
|
|
55
|
+
let i10;
|
|
56
|
+
if (dig <= 308) {
|
|
57
|
+
const p = dig >= 0 ? powerOf10(dig) : 1 / powerOf10(-dig);
|
|
58
|
+
const x10 = ax * p;
|
|
59
|
+
i10 = Math.floor(x10);
|
|
60
|
+
xd = i10 / p;
|
|
61
|
+
xu = Math.ceil(x10) / p;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const p308 = powerOf10(308);
|
|
65
|
+
const pRest = powerOf10(dig - 308);
|
|
66
|
+
const x10 = ax * p308 * pRest;
|
|
67
|
+
i10 = Math.floor(x10);
|
|
68
|
+
xd = i10 / p308 / pRest;
|
|
69
|
+
xu = Math.ceil(x10) / p308 / pRest;
|
|
70
|
+
}
|
|
71
|
+
const du = xu - ax;
|
|
72
|
+
const dd = ax - xd;
|
|
73
|
+
const r = du < dd || (du === dd && i10 % 2 === 1) ? xu : xd;
|
|
74
|
+
return x < 0 ? -r : r;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* R's `zapsmall(x, digits = 7)` of the entries of a matrix (array of rows) taken together: every entry rounded by
|
|
78
|
+
* {@link roundR} to `max(0, digits - log10(mx))` decimal places, mx the largest absolute entry (NaN left out), or
|
|
79
|
+
* to `digits` places when mx is 0.
|
|
80
|
+
*/
|
|
81
|
+
export function zapsmall(m, digits = 7) {
|
|
82
|
+
let mx = 0;
|
|
83
|
+
for (const row of m)
|
|
84
|
+
for (const v of row)
|
|
85
|
+
if (Math.abs(v) > mx)
|
|
86
|
+
mx = Math.abs(v);
|
|
87
|
+
const d = mx > 0 ? Math.max(0, digits - Math.log10(mx)) : digits;
|
|
88
|
+
return m.map((row) => row.map((v) => roundR(v, d)));
|
|
89
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The labels with every number replaced by the value of its level (see the top of this file): numbers with the same
|
|
3
|
+
* 15-digit text become the same number, the smallest of them; -0 becomes 0. NaN and labels that are not numbers are
|
|
4
|
+
* unchanged
|
|
5
|
+
*/
|
|
6
|
+
export declare function numberLevels<T>(labels: ArrayLike<T>): T[];
|
|
7
|
+
/**
|
|
8
|
+
* The text of a level as R writes it: a number as as.character does (15 significant digits, so 1e15 is "1e+15" and
|
|
9
|
+
* 1/3 is "0.333333333333333"; Inf is "Inf"), anything else with String
|
|
10
|
+
*/
|
|
11
|
+
export declare function levelText(v: unknown): string;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Numbers as the levels of a factor, the way R's factor() makes them: the sorted distinct values are written as
|
|
2
|
+
// as.character writes them (15 significant digits, src/htest/core/simulate.ts's formatR), and the values with the
|
|
3
|
+
// same text are one level. So 0.1 + 0.2 and 0.3 are one level ("0.3"), and so are 1e15 and 1e15 + 1 ("1e+15").
|
|
4
|
+
// Written for rStats.js (no ported code) from oracle/report-0.11.1-behaviour.md.
|
|
5
|
+
import { formatR } from "../htest/core/simulate.js";
|
|
6
|
+
/**
|
|
7
|
+
* For each number, the value of its level: the smallest value with the same text (R takes the level's text from
|
|
8
|
+
* the smallest, since the values are sorted first). -0 is 0; NaN is returned as is
|
|
9
|
+
*/
|
|
10
|
+
function levelValueMap(values) {
|
|
11
|
+
const distinct = [...new Set(Array.from(values, (v) => (v === 0 ? 0 : v)))].filter((v) => !Number.isNaN(v)).sort((a, b) => a - b);
|
|
12
|
+
const out = new Map();
|
|
13
|
+
// Rounding to 15 digits keeps the order, so the values with the same text are next to each other
|
|
14
|
+
let text;
|
|
15
|
+
let level = NaN;
|
|
16
|
+
for (const v of distinct) {
|
|
17
|
+
const t = formatR(v);
|
|
18
|
+
if (t !== text) {
|
|
19
|
+
text = t;
|
|
20
|
+
level = v;
|
|
21
|
+
}
|
|
22
|
+
out.set(v, level);
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The labels with every number replaced by the value of its level (see the top of this file): numbers with the same
|
|
28
|
+
* 15-digit text become the same number, the smallest of them; -0 becomes 0. NaN and labels that are not numbers are
|
|
29
|
+
* unchanged
|
|
30
|
+
*/
|
|
31
|
+
export function numberLevels(labels) {
|
|
32
|
+
const xs = Array.from(labels);
|
|
33
|
+
const map = levelValueMap(xs.filter((v) => typeof v === "number"));
|
|
34
|
+
return xs.map((v) => (typeof v === "number" && !Number.isNaN(v) ? map.get(v) : v));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The text of a level as R writes it: a number as as.character does (15 significant digits, so 1e15 is "1e+15" and
|
|
38
|
+
* 1/3 is "0.333333333333333"; Inf is "Inf"), anything else with String
|
|
39
|
+
*/
|
|
40
|
+
export function levelText(v) {
|
|
41
|
+
if (typeof v !== "number")
|
|
42
|
+
return String(v);
|
|
43
|
+
if (v === Infinity)
|
|
44
|
+
return "Inf";
|
|
45
|
+
if (v === -Infinity)
|
|
46
|
+
return "-Inf";
|
|
47
|
+
return formatR(v);
|
|
48
|
+
}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { type DD } from "../special/dd.js";
|
|
2
|
+
/**
|
|
3
|
+
* Running sum of finite values in double-double that does not overflow: its value is (s[0] + s[1]) 2^shift. shift
|
|
4
|
+
* is 0 until an addition would overflow; from then on the values are added scaled by 2^-shift
|
|
5
|
+
*/
|
|
6
|
+
export interface WideSum {
|
|
7
|
+
s: DD;
|
|
8
|
+
shift: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function wideSum(): WideSum;
|
|
11
|
+
/** Adds the finite value v to acc; n is the number of values the sum will have in all */
|
|
12
|
+
export declare function wideSumAdd(acc: WideSum, v: number, n: number): void;
|
|
13
|
+
/** The value of the sum rounded to double (±Infinity beyond the range) */
|
|
14
|
+
export declare function wideSumValue(acc: WideSum): number;
|
|
15
|
+
/** v 2^k for an integer k, in two steps so that 2^k itself does not overflow or underflow */
|
|
16
|
+
export declare function ldexp(v: number, k: number): number;
|
|
2
17
|
/** Mean as double-double. x contains no NaN and has length >= 1 */
|
|
3
18
|
export declare function meanDD(x: readonly number[]): DD;
|
|
4
19
|
export declare function mean(x: readonly number[]): number;
|
|
@@ -6,7 +21,10 @@ export declare function mean(x: readonly number[]): number;
|
|
|
6
21
|
export declare function variance(x: readonly number[]): number;
|
|
7
22
|
/** Unbiased covariance. x and y have the same length and contain no NaN */
|
|
8
23
|
export declare function covariance(x: readonly number[], y: readonly number[]): number;
|
|
9
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Sum (accumulated in double-double without overflow, then rounded; ±Infinity beyond the double range). With
|
|
26
|
+
* infinite or NaN values, as in double arithmetic
|
|
27
|
+
*/
|
|
10
28
|
export declare function sum(x: readonly number[]): number;
|
|
11
29
|
/**
|
|
12
30
|
* Ranks (ties get the average rank, R's rank(ties.method = "average")). x contains no NaN.
|
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
|
-
};
|