@numero-uno-jpn/rstats 0.11.0 → 0.11.1

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.
Files changed (269) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.ja.md +5 -4
  3. package/README.md +6 -4
  4. package/dist/chunks/{chunk-YSWGDOWK.js → chunk-2X7JYQBI.js} +2 -2
  5. package/dist/chunks/{chunk-F5EDJFEO.js → chunk-36GVER3O.js} +2 -2
  6. package/dist/chunks/{chunk-7GQMKH3Z.js → chunk-3WUXRSM6.js} +1 -1
  7. package/dist/chunks/{chunk-JWBO5WVN.js → chunk-6I6QKTFK.js} +2 -2
  8. package/dist/chunks/{chunk-EZWBRCJL.js → chunk-ETOEZTGL.js} +1 -1
  9. package/dist/chunks/{chunk-VJ4BDBMU.js → chunk-FVPDVRJQ.js} +2 -2
  10. package/dist/chunks/{chunk-EZAFN5CD.js → chunk-GG4BJMJW.js} +1 -1
  11. package/dist/chunks/{chunk-5WDJPHYY.js → chunk-HITD7CX6.js} +1 -1
  12. package/dist/chunks/{chunk-Y5PYRAYP.js → chunk-KK4GE65D.js} +2 -2
  13. package/dist/chunks/{chunk-AHHS6AC4.js → chunk-LAUYSGV4.js} +2 -2
  14. package/dist/chunks/{chunk-ALM3VHFP.js → chunk-M6GDXKKS.js} +1 -1
  15. package/dist/chunks/{chunk-C5XTWMGB.js → chunk-PHHUHR7T.js} +1 -1
  16. package/dist/chunks/{chunk-TOGXTCZO.js → chunk-WQPFDDIY.js} +2 -2
  17. package/dist/chunks/{chunk-CGBNKHNZ.js → chunk-X2AWQSVS.js} +2 -2
  18. package/dist/chunks/{chunk-Y5CNQ266.js → chunk-ZRJQD7VS.js} +2 -2
  19. package/dist/data/grouping.d.ts +33 -12
  20. package/dist/data/grouping.js +2 -0
  21. package/dist/descriptive/basic.d.ts +130 -17
  22. package/dist/descriptive/basic.js +115 -14
  23. package/dist/descriptive/cov.d.ts +22 -12
  24. package/dist/descriptive/dataFrames.d.ts +39 -11
  25. package/dist/descriptive/dataFrames.js +14 -6
  26. package/dist/descriptive/factanal.d.ts +26 -6
  27. package/dist/descriptive/factanal.js +7 -3
  28. package/dist/descriptive/hist.d.ts +27 -6
  29. package/dist/descriptive/hist.js +9 -3
  30. package/dist/descriptive/missing.d.ts +13 -6
  31. package/dist/descriptive/missing.js +4 -2
  32. package/dist/descriptive/moments.d.ts +91 -13
  33. package/dist/descriptive/moments.js +66 -9
  34. package/dist/descriptive/omega.d.ts +28 -6
  35. package/dist/descriptive/omega.js +16 -4
  36. package/dist/descriptive/pivot.d.ts +22 -6
  37. package/dist/descriptive/pivot.js +7 -3
  38. package/dist/descriptive/quantile.d.ts +33 -5
  39. package/dist/descriptive/quantile.js +20 -2
  40. package/dist/descriptive/rank.d.ts +12 -2
  41. package/dist/descriptive/rank.js +2 -0
  42. package/dist/descriptive/table.d.ts +55 -7
  43. package/dist/descriptive/table.js +38 -4
  44. package/dist/descriptive.js +2 -2
  45. package/dist/distributions/arcsine.d.ts +40 -5
  46. package/dist/distributions/arcsine.js +35 -4
  47. package/dist/distributions/beta.d.ts +12 -0
  48. package/dist/distributions/beta.js +12 -0
  49. package/dist/distributions/binomial.d.ts +18 -0
  50. package/dist/distributions/binomial.js +18 -0
  51. package/dist/distributions/bvnorm.d.ts +4 -1
  52. package/dist/distributions/bvnorm.js +4 -1
  53. package/dist/distributions/cauchy.d.ts +24 -0
  54. package/dist/distributions/cauchy.js +24 -0
  55. package/dist/distributions/chisq.d.ts +18 -0
  56. package/dist/distributions/chisq.js +18 -0
  57. package/dist/distributions/exponential.d.ts +29 -1
  58. package/dist/distributions/exponential.js +24 -0
  59. package/dist/distributions/f.d.ts +18 -0
  60. package/dist/distributions/f.js +18 -0
  61. package/dist/distributions/gamma.d.ts +20 -0
  62. package/dist/distributions/gamma.js +18 -0
  63. package/dist/distributions/geometric.d.ts +24 -0
  64. package/dist/distributions/geometric.js +24 -0
  65. package/dist/distributions/hypergeometric.d.ts +24 -0
  66. package/dist/distributions/hypergeometric.js +24 -0
  67. package/dist/distributions/invgamma.d.ts +32 -4
  68. package/dist/distributions/invgamma.js +27 -3
  69. package/dist/distributions/kumaraswamy.d.ts +31 -3
  70. package/dist/distributions/kumaraswamy.js +31 -3
  71. package/dist/distributions/laplace.d.ts +36 -4
  72. package/dist/distributions/laplace.js +31 -3
  73. package/dist/distributions/logistic.d.ts +29 -1
  74. package/dist/distributions/logistic.js +24 -0
  75. package/dist/distributions/lognormal.d.ts +29 -1
  76. package/dist/distributions/lognormal.js +24 -0
  77. package/dist/distributions/multinomial.d.ts +7 -1
  78. package/dist/distributions/multinomial.js +2 -0
  79. package/dist/distributions/negbinomial.d.ts +29 -1
  80. package/dist/distributions/negbinomial.js +24 -0
  81. package/dist/distributions/normal.d.ts +11 -1
  82. package/dist/distributions/normal.js +6 -0
  83. package/dist/distributions/pareto.d.ts +36 -4
  84. package/dist/distributions/pareto.js +31 -3
  85. package/dist/distributions/poisson.d.ts +18 -0
  86. package/dist/distributions/poisson.js +18 -0
  87. package/dist/distributions/signrank.d.ts +24 -0
  88. package/dist/distributions/signrank.js +24 -0
  89. package/dist/distributions/summary.d.ts +20 -2
  90. package/dist/distributions/summary.js +4 -0
  91. package/dist/distributions/t.d.ts +18 -0
  92. package/dist/distributions/t.js +18 -0
  93. package/dist/distributions/triangular.d.ts +36 -4
  94. package/dist/distributions/triangular.js +31 -3
  95. package/dist/distributions/tukey.d.ts +15 -1
  96. package/dist/distributions/tukey.js +10 -0
  97. package/dist/distributions/uniform.d.ts +29 -1
  98. package/dist/distributions/uniform.js +24 -0
  99. package/dist/distributions/weibull.d.ts +29 -1
  100. package/dist/distributions/weibull.js +24 -0
  101. package/dist/distributions/wilcox.d.ts +24 -0
  102. package/dist/distributions/wilcox.js +24 -0
  103. package/dist/distributions.js +2 -2
  104. package/dist/entries/descriptive.d.ts +1 -1
  105. package/dist/entries/numerics.d.ts +1 -1
  106. package/dist/entries/tests.js +1 -1
  107. package/dist/htest/agreement.d.ts +61 -23
  108. package/dist/htest/agreement.js +19 -9
  109. package/dist/htest/ansariTest.d.ts +8 -2
  110. package/dist/htest/aov.d.ts +43 -10
  111. package/dist/htest/aov.js +17 -3
  112. package/dist/htest/binomTest.d.ts +12 -2
  113. package/dist/htest/binomTest.js +2 -0
  114. package/dist/htest/brunnerMunzelTest.d.ts +9 -3
  115. package/dist/htest/brunnerMunzelTest.js +4 -2
  116. package/dist/htest/chisqTest.d.ts +19 -3
  117. package/dist/htest/cor.d.ts +20 -4
  118. package/dist/htest/effectSize.d.ts +70 -12
  119. package/dist/htest/effectSize.js +40 -7
  120. package/dist/htest/fisherTest.d.ts +9 -1
  121. package/dist/htest/fisherTest.js +2 -0
  122. package/dist/htest/groups.d.ts +5 -1
  123. package/dist/htest/ksTest.d.ts +20 -3
  124. package/dist/htest/leveneTest.d.ts +15 -4
  125. package/dist/htest/leveneTest.js +5 -2
  126. package/dist/htest/mantelhaenTest.d.ts +25 -3
  127. package/dist/htest/mantelhaenTest.js +15 -1
  128. package/dist/htest/mcnemarTest.d.ts +14 -4
  129. package/dist/htest/mcnemarTest.js +4 -2
  130. package/dist/htest/multipleComparisons.d.ts +173 -29
  131. package/dist/htest/multipleComparisons.js +123 -19
  132. package/dist/htest/normalityTests.d.ts +56 -9
  133. package/dist/htest/normalityTests.js +40 -6
  134. package/dist/htest/onewayTest.d.ts +32 -3
  135. package/dist/htest/onewayTest.js +10 -0
  136. package/dist/htest/pAdjust.d.ts +9 -1
  137. package/dist/htest/pAdjust.js +2 -0
  138. package/dist/htest/pairwise.d.ts +50 -5
  139. package/dist/htest/pairwise.js +25 -0
  140. package/dist/htest/poissonTest.d.ts +12 -2
  141. package/dist/htest/poissonTest.js +2 -0
  142. package/dist/htest/polycor.d.ts +29 -10
  143. package/dist/htest/power.d.ts +60 -6
  144. package/dist/htest/power.js +6 -0
  145. package/dist/htest/propTest.d.ts +13 -2
  146. package/dist/htest/propTest.js +2 -0
  147. package/dist/htest/pwr.d.ts +286 -36
  148. package/dist/htest/pwr.js +116 -15
  149. package/dist/htest/rankTests.d.ts +80 -12
  150. package/dist/htest/rankTests.js +11 -2
  151. package/dist/htest/tTest.d.ts +13 -3
  152. package/dist/htest/trendTests.d.ts +29 -8
  153. package/dist/htest/trendTests.js +8 -4
  154. package/dist/htest/types.d.ts +14 -2
  155. package/dist/htest/varTest.d.ts +25 -8
  156. package/dist/htest/wilcoxTest.d.ts +10 -2
  157. package/dist/internal/dpq.d.ts +4 -0
  158. package/dist/internal/noncentral.d.ts +2 -0
  159. package/dist/internal/vectorize.d.ts +4 -0
  160. package/dist/linalg/basic.d.ts +39 -5
  161. package/dist/linalg/basic.js +15 -0
  162. package/dist/linalg/chol.d.ts +26 -5
  163. package/dist/linalg/chol.js +7 -0
  164. package/dist/linalg/eigen.d.ts +16 -7
  165. package/dist/linalg/eigen.js +6 -5
  166. package/dist/linalg/expm.d.ts +15 -5
  167. package/dist/linalg/expm.js +5 -3
  168. package/dist/linalg/kappa.d.ts +21 -1
  169. package/dist/linalg/kappa.js +16 -0
  170. package/dist/linalg/lu.d.ts +30 -6
  171. package/dist/linalg/lu.js +15 -3
  172. package/dist/linalg/matrix.d.ts +4 -0
  173. package/dist/linalg/norm.d.ts +16 -1
  174. package/dist/linalg/norm.js +9 -0
  175. package/dist/linalg/ops.d.ts +156 -35
  176. package/dist/linalg/ops.js +96 -15
  177. package/dist/linalg/qr.d.ts +87 -12
  178. package/dist/linalg/qr.js +31 -0
  179. package/dist/linalg/schur.d.ts +8 -2
  180. package/dist/linalg/schur.js +3 -1
  181. package/dist/linalg/solve.d.ts +42 -6
  182. package/dist/linalg/solve.js +22 -0
  183. package/dist/linalg/svd.d.ts +12 -2
  184. package/dist/linalg/svd.js +2 -0
  185. package/dist/linalg.js +2 -2
  186. package/dist/models/anovaSequential.d.ts +26 -4
  187. package/dist/models/anovaSequential.js +3 -1
  188. package/dist/models/aovEz.d.ts +29 -6
  189. package/dist/models/aovEz.js +5 -2
  190. package/dist/models/aovStrata.d.ts +12 -2
  191. package/dist/models/carAnova.d.ts +9 -3
  192. package/dist/models/carAnova.js +4 -2
  193. package/dist/models/coxph.d.ts +33 -7
  194. package/dist/models/coxph.js +8 -3
  195. package/dist/models/glm.d.ts +128 -21
  196. package/dist/models/glm.js +71 -14
  197. package/dist/models/glmAnova.d.ts +30 -5
  198. package/dist/models/glmAnova.js +15 -2
  199. package/dist/models/glmConfint.d.ts +16 -2
  200. package/dist/models/glmConfint.js +9 -1
  201. package/dist/models/glmFamily.d.ts +27 -5
  202. package/dist/models/lm.d.ts +75 -17
  203. package/dist/models/lm.js +22 -4
  204. package/dist/models/mauchly.d.ts +20 -5
  205. package/dist/models/mauchly.js +7 -3
  206. package/dist/models/modelFrame.d.ts +5 -1
  207. package/dist/models/modelMatrix.d.ts +18 -4
  208. package/dist/models/modelMatrix.js +3 -1
  209. package/dist/models/survival.d.ts +41 -9
  210. package/dist/models/survival.js +10 -4
  211. package/dist/numerics/deriv.d.ts +21 -6
  212. package/dist/numerics/deriv.js +16 -5
  213. package/dist/numerics/integrate.d.ts +37 -7
  214. package/dist/numerics/integrate.js +8 -2
  215. package/dist/numerics/interp.d.ts +63 -12
  216. package/dist/numerics/interp.js +30 -6
  217. package/dist/numerics/ode.d.ts +15 -5
  218. package/dist/numerics/ode.js +5 -3
  219. package/dist/numerics.js +2 -2
  220. package/dist/optim/optim.d.ts +32 -8
  221. package/dist/optim/optim.js +8 -4
  222. package/dist/random/beta.d.ts +8 -0
  223. package/dist/random/beta.js +8 -0
  224. package/dist/random/binomial.d.ts +16 -0
  225. package/dist/random/binomial.js +16 -0
  226. package/dist/random/continuous.d.ts +51 -0
  227. package/dist/random/continuous.js +51 -0
  228. package/dist/random/extra.d.ts +66 -6
  229. package/dist/random/extra.js +66 -6
  230. package/dist/random/gamma.d.ts +33 -0
  231. package/dist/random/gamma.js +33 -0
  232. package/dist/random/hyper.d.ts +8 -0
  233. package/dist/random/hyper.js +8 -0
  234. package/dist/random/poisson.d.ts +25 -0
  235. package/dist/random/poisson.js +25 -0
  236. package/dist/random/r2dtable.d.ts +2 -0
  237. package/dist/random/r2dtable.js +2 -0
  238. package/dist/random/rank.d.ts +16 -0
  239. package/dist/random/rank.js +16 -0
  240. package/dist/random/rng.d.ts +49 -6
  241. package/dist/random/rng.js +29 -2
  242. package/dist/random/sample.d.ts +10 -2
  243. package/dist/random/sample.js +5 -1
  244. package/dist/random/streams.d.ts +13 -0
  245. package/dist/random/streams.js +13 -0
  246. package/dist/rstats.js +1 -1
  247. package/dist/special/besselIK.d.ts +11 -3
  248. package/dist/special/besselIK.js +6 -2
  249. package/dist/special/besselJY.d.ts +6 -2
  250. package/dist/special/besselJY.js +6 -2
  251. package/dist/special/beta.d.ts +16 -2
  252. package/dist/special/beta.js +16 -2
  253. package/dist/special/choose.d.ts +13 -5
  254. package/dist/special/choose.js +13 -5
  255. package/dist/special/erf.d.ts +8 -3
  256. package/dist/special/erf.js +8 -3
  257. package/dist/special/extra.d.ts +30 -10
  258. package/dist/special/extra.js +25 -9
  259. package/dist/special/gamma.d.ts +18 -2
  260. package/dist/special/gamma.js +18 -2
  261. package/dist/special/log1p.d.ts +16 -7
  262. package/dist/special/log1p.js +16 -7
  263. package/dist/special/psigamma.d.ts +9 -3
  264. package/dist/special/psigamma.js +9 -3
  265. package/dist/special/rCompatible.d.ts +5 -1
  266. package/dist/special.js +2 -2
  267. package/dist/tests.js +2 -2
  268. package/llms.txt +2 -0
  269. package/package.json +1 -1
package/dist/linalg/lu.js CHANGED
@@ -12,13 +12,16 @@ import { matMul } from "./ops.js";
12
12
  /** Smallest normal double (LAPACK's dlamch("S")): below it, 1 / pivot would overflow. */
13
13
  const SFMIN = 2.2250738585072014e-308;
14
14
  /**
15
- * LU decomposition with partial (row) pivoting, `x = P L U`. Equivalent to `Matrix::expand(Matrix::lu(x))`
16
- * (LAPACK's dgetrf).
15
+ * 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).
17
17
  *
18
18
  * @example
19
19
  * ```ts
20
- * const { P, L, U } = lu([[1, 2], [3, 4]]); // P = [[0, 1], [1, 0]], L = [[1, 0], [1/3, 1]], U = [[3, 4], [0, 2/3]]
20
+ * lu([[1, 2], [3, 4]]);
21
+ * // { P: [[0, 1], [1, 0]], L: [[1, 0], [0.3333333333333333, 1]], U: [[3, 4], [0, 0.6666666666666667]] }
21
22
  * ```
23
+ *
24
+ * @category Linear algebra
22
25
  */
23
26
  export function lu(x) {
24
27
  const m = x.length;
@@ -66,6 +69,15 @@ export function lu(x) {
66
69
  /**
67
70
  * Principal components of the columns of `x` (observations as rows). Equivalent to R's `prcomp()`. The sign of each
68
71
  * component is arbitrary, as in R.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * const pc = prcomp([[1, 2], [2, 1], [3, 4], [4, 3]]);
76
+ * pc.sdev; // [1.632993161855452, 0.816496580927726]
77
+ * pc.x; // [[-1.4142, 0.7071], [-1.4142, -0.7071], [1.4142, 0.7071], [1.4142, -0.7071]] (signs may differ from R)
78
+ * ```
79
+ *
80
+ * @category Linear algebra
69
81
  */
70
82
  export function prcomp(x, options = {}) {
71
83
  const rows = x.map((r) => Array.from(r));
@@ -1,11 +1,15 @@
1
1
  /**
2
2
  * A matrix given as an array of rows. Each row may be a plain array or a `Float64Array`;
3
3
  * all rows must have the same length. A matrix with zero rows is treated as 0 x 0.
4
+ *
5
+ * @category Linear algebra
4
6
  */
5
7
  export type MatrixInput = readonly (readonly number[] | Float64Array)[];
6
8
  /**
7
9
  * A vector or a matrix (array of rows). Used for arguments that accept either, such as the
8
10
  * right-hand side of `backsolve`; a vector is treated as a one-column matrix, as in R.
11
+ *
12
+ * @category Linear algebra
9
13
  */
10
14
  export type VectorOrMatrix = readonly number[] | Float64Array | MatrixInput;
11
15
  /** Column-major matrix. Element (i, j) is data[i + j * nrow] */
@@ -2,9 +2,15 @@ import { type Mat, type MatrixInput } from "./matrix.js";
2
2
  /**
3
3
  * Matrix norm type for {@link norm}: `"O"`/`"1"` (one norm), `"I"` (infinity norm), `"F"`/`"E"` (Frobenius),
4
4
  * `"M"` (maximum modulus) or `"2"` (spectral norm). Case-insensitive, as in R.
5
+ *
6
+ * @category Linear algebra
5
7
  */
6
8
  export type NormType = "O" | "o" | "1" | "I" | "i" | "F" | "f" | "E" | "e" | "M" | "m" | "2";
7
- /** Options for {@link norm}. */
9
+ /**
10
+ * Options for {@link norm}.
11
+ *
12
+ * @category Linear algebra
13
+ */
8
14
  export interface NormOptions {
9
15
  /**
10
16
  * Which norm to compute (a single character, case-insensitive). Default: `"O"`.
@@ -24,6 +30,15 @@ export interface NormOptions {
24
30
  * @param x A matrix (array of rows).
25
31
  * @param options See {@link NormOptions}.
26
32
  * @returns The norm of `x`.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * norm([[1, -2], [3, 4]]); // 6 (one norm)
37
+ * norm([[1, -2], [3, 4]], { type: "I" }); // 7
38
+ * norm([[1, -2], [3, 4]], { type: "F" }); // 5.477225575051661
39
+ * ```
40
+ *
41
+ * @category Linear algebra
27
42
  */
28
43
  export declare function norm(x: MatrixInput, options?: NormOptions): number;
29
44
  export declare function oneNorm(m: Mat): number;
@@ -15,6 +15,15 @@ import { svdValues } from "./svd.js";
15
15
  * @param x A matrix (array of rows).
16
16
  * @param options See {@link NormOptions}.
17
17
  * @returns The norm of `x`.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * norm([[1, -2], [3, 4]]); // 6 (one norm)
22
+ * norm([[1, -2], [3, 4]], { type: "I" }); // 7
23
+ * norm([[1, -2], [3, 4]], { type: "F" }); // 5.477225575051661
24
+ * ```
25
+ *
26
+ * @category Linear algebra
18
27
  */
19
28
  export function norm(x, options = {}) {
20
29
  const m = toMat(x);
@@ -1,85 +1,205 @@
1
1
  import type { Sample } from "../htest/types.js";
2
2
  import type { DataFrame } from "../models/modelFrame.js";
3
- /** A vector (plain array) or a matrix (array of rows). */
3
+ /**
4
+ * A vector (plain array) or a matrix (array of rows).
5
+ *
6
+ * @category Linear algebra
7
+ */
4
8
  export type Operand = number | Sample | readonly (readonly number[])[];
5
- type Matrix = number[][];
6
9
  /**
7
- * Matrix product. Equivalent to R's `x %*% y`: a vector is taken as a row or a column so that the product is
8
- * defined (two vectors give their inner product as a 1 x 1 matrix).
10
+ * Matrix product. Equivalent to R's `%*%`. A vector is taken as a row or a column so that the product is defined
11
+ * (two vectors give their inner product as a 1 x 1 matrix).
9
12
  *
10
13
  * @example
11
14
  * ```ts
12
15
  * matMul([[1, 2], [3, 4]], [[5], [6]]); // [[17], [39]]
16
+ * matMul([1, 2, 3], [4, 5, 6]); // [[32]]
13
17
  * ```
18
+ *
19
+ * @category Linear algebra
14
20
  */
15
- export declare function matMul(x: Operand, y: Operand): Matrix;
16
- /** Element-wise sum with R's recycling (R's `x + y`). */
17
- export declare function add(x: Operand, y: Operand): number[] | Matrix;
18
- /** Element-wise difference with R's recycling (R's `x - y`). */
19
- export declare function subtract(x: Operand, y: Operand): number[] | Matrix;
20
- /** Element-wise product with R's recycling (R's `x * y`); the matrix product is {@link matMul}. */
21
- export declare function multiply(x: Operand, y: Operand): number[] | Matrix;
22
- /** Element-wise quotient with R's recycling (R's `x / y`). */
23
- export declare function divide(x: Operand, y: Operand): number[] | Matrix;
24
- /**
25
- * Outer product of two vectors: the matrix of `fun(x[i], y[j])`. Equivalent to R's `outer()`; `fun` is `"*"`
21
+ export declare function matMul(x: Operand, y: Operand): number[][];
22
+ /**
23
+ * Element-wise sum with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
24
+ * R's `+`.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * add([[1, 2], [3, 4]], [10, 20]); // [[11, 12], [23, 24]]
29
+ * add([1, 2, 3], 2); // [3, 4, 5]
30
+ * ```
31
+ *
32
+ * @category Linear algebra
33
+ */
34
+ export declare function add(x: Operand, y: Operand): number[] | number[][];
35
+ /**
36
+ * Element-wise difference with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
37
+ * R's `-`.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * subtract([[1, 2], [3, 4]], [10, 20]); // [[-9, -8], [-17, -16]]
42
+ * subtract([1, 2, 3], 2); // [-1, 0, 1]
43
+ * ```
44
+ *
45
+ * @category Linear algebra
46
+ */
47
+ export declare function subtract(x: Operand, y: Operand): number[] | number[][];
48
+ /**
49
+ * Element-wise product with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
50
+ * R's `*`. The matrix product is {@link matMul}.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * multiply([[1, 2], [3, 4]], [10, 20]); // [[10, 20], [60, 80]]
55
+ * multiply([1, 2, 3], 2); // [2, 4, 6]
56
+ * ```
57
+ *
58
+ * @category Linear algebra
59
+ */
60
+ export declare function multiply(x: Operand, y: Operand): number[] | number[][];
61
+ /**
62
+ * Element-wise quotient with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
63
+ * R's `/`.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * divide([[1, 2], [3, 4]], [10, 20]); // [[0.1, 0.2], [0.15, 0.2]]
68
+ * divide([1, 2, 3], 2); // [0.5, 1, 1.5]
69
+ * ```
70
+ *
71
+ * @category Linear algebra
72
+ */
73
+ export declare function divide(x: Operand, y: Operand): number[] | number[][];
74
+ /**
75
+ * Outer product of two vectors: the matrix of `fun(x[i], y[j])`. Equivalent to R's `outer()`. `fun` is `"*"`
26
76
  * (default), `"+"`, `"-"`, `"/"`, `"^"` or a function of two numbers.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * outer([1, 2], [1, 2, 3]); // [[1, 2, 3], [2, 4, 6]]
81
+ * outer([1, 2], [1, 2, 3], "+"); // [[2, 3, 4], [3, 4, 5]]
82
+ * ```
83
+ *
84
+ * @category Linear algebra
85
+ */
86
+ export declare function outer(x: Sample, y: Sample, fun?: "*" | "+" | "-" | "/" | "^" | ((a: number, b: number) => number)): number[][];
87
+ /**
88
+ * Transpose. Equivalent to R's `t()`. A vector becomes a 1-row matrix.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * t([[1, 2, 3], [4, 5, 6]]); // [[1, 4], [2, 5], [3, 6]]
93
+ * t([1, 2]); // [[1, 2]]
94
+ * ```
95
+ *
96
+ * @category Linear algebra
97
+ */
98
+ export declare function t(x: Operand): number[][];
99
+ /**
100
+ * Options for {@link diag}.
101
+ *
102
+ * @category Linear algebra
27
103
  */
28
- export declare function outer(x: Sample, y: Sample, fun?: "*" | "+" | "-" | "/" | "^" | ((a: number, b: number) => number)): Matrix;
29
- /** Transpose. Equivalent to R's `t()`: a vector becomes a 1-row matrix. */
30
- export declare function t(x: Operand): Matrix;
31
- /** Options for {@link diag}. */
32
104
  export interface DiagOptions {
105
+ /** Number of rows of the matrix built from a vector or a number (R's `nrow`). Default: the length of the vector. */
33
106
  nrow?: number;
107
+ /** Number of columns of the matrix built from a vector or a number (R's `ncol`). Default: `nrow`. */
34
108
  ncol?: number;
35
109
  }
36
110
  /**
37
- * R's `diag()`, with its three meanings: the diagonal of a matrix (as a vector); the diagonal matrix of a vector of
38
- * length 2 or more (its values recycled along the diagonal of an `nrow` x `ncol` matrix when given); and for a single
111
+ * Matrix diagonals, diagonal matrices and identity matrices. Equivalent to R's `diag()`. It has three meanings: the
112
+ * diagonal of a matrix (as a vector); the diagonal matrix of a vector of length 2 or more (its values recycled along the diagonal of an `nrow` x `ncol` matrix when given); and for a single
39
113
  * number n, the n x n identity matrix (with `nrow`/`ncol`, a matrix with that number on the diagonal).
40
114
  *
41
115
  * @example
42
116
  * ```ts
43
117
  * diag([[1, 2], [3, 4]]); // [1, 4]
44
118
  * diag([2, 3]); // [[2, 0], [0, 3]]
45
- * diag(3); // 3 x 3 identity
119
+ * diag(3); // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
46
120
  * ```
121
+ *
122
+ * @category Linear algebra
47
123
  */
48
- export declare function diag(x: Operand, options?: DiagOptions): number[] | Matrix;
124
+ export declare function diag(x: Operand, options?: DiagOptions): number[] | number[][];
49
125
  /**
50
- * Combine vectors and matrices as columns. Equivalent to R's `cbind()` (vectors recycled to the number of rows).
126
+ * Combine vectors and matrices as columns. Equivalent to R's `cbind()`. Vectors are recycled to the number of rows.
51
127
  * Given data frames (objects of columns), puts their columns side by side, as R's `cbind()` of data frames: all must
52
128
  * have the same number of rows, and a column name may occur only once.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * cbind([1, 2], [3, 4]); // [[1, 3], [2, 4]]
133
+ * cbind([[1, 2], [3, 4]], 0); // [[1, 2, 0], [3, 4, 0]]
134
+ * ```
135
+ *
136
+ * @category Linear algebra
53
137
  */
54
- export declare function cbind(...args: Operand[]): Matrix;
138
+ export declare function cbind(...args: Operand[]): number[][];
55
139
  export declare function cbind(...frames: DataFrame[]): DataFrame;
56
140
  /**
57
141
  * Combine vectors and matrices as rows. Equivalent to R's `rbind()`. Given data frames (objects of columns), stacks
58
142
  * their rows, as R's `rbind()` of data frames: all must have the same column names, matched by name (the result has
59
143
  * the first frame's column order).
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * rbind([1, 2], [3, 4]); // [[1, 2], [3, 4]]
148
+ * rbind([[1, 2], [3, 4]], [5, 6]); // [[1, 2], [3, 4], [5, 6]]
149
+ * ```
150
+ *
151
+ * @category Linear algebra
60
152
  */
61
- export declare function rbind(...args: Operand[]): Matrix;
153
+ export declare function rbind(...args: Operand[]): number[][];
62
154
  export declare function rbind(...frames: DataFrame[]): DataFrame;
63
- /** Options for {@link matrix}. */
155
+ /**
156
+ * Options for {@link matrix}.
157
+ *
158
+ * @category Linear algebra
159
+ */
64
160
  export interface MatrixOptions {
161
+ /**
162
+ * Number of rows (R's `nrow`). Default: the length of the data divided by `ncol` (rounded up), or the length of
163
+ * the data when `ncol` is not given either.
164
+ */
65
165
  nrow?: number;
166
+ /**
167
+ * Number of columns (R's `ncol`). Default: the length of the data divided by `nrow` (rounded up), or 1 when `nrow`
168
+ * is not given either.
169
+ */
66
170
  ncol?: number;
67
171
  /** Fill by rows instead of by columns (R's default). Default `false`. */
68
172
  byrow?: boolean;
69
173
  }
70
174
  /**
71
- * Build a matrix from data, recycled to fill it. Equivalent to R's `matrix()`: filled by columns unless `byrow`.
175
+ * Build a matrix from data, recycled to fill it. Equivalent to R's `matrix()`. Filled by columns unless `byrow`.
72
176
  *
73
177
  * @example
74
178
  * ```ts
75
179
  * matrix([1, 2, 3, 4, 5, 6], { nrow: 2 }); // [[1, 3, 5], [2, 4, 6]]
76
- * matrix(0, { nrow: 2, ncol: 3 }); // zeros
180
+ * matrix(0, { nrow: 2, ncol: 3 }); // [[0, 0, 0], [0, 0, 0]]
181
+ * ```
182
+ *
183
+ * @category Linear algebra
184
+ */
185
+ export declare function matrix(data: number | Sample, options?: MatrixOptions): number[][];
186
+ /**
187
+ * Kronecker product. Equivalent to R's `kronecker()`. Two vectors give a vector.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * kronecker([[1, 2], [3, 4]], [[1, 1]]); // [[1, 1, 2, 2], [3, 3, 4, 4]]
192
+ * kronecker([1, 2], [1, 10]); // [1, 10, 2, 20]
77
193
  * ```
194
+ *
195
+ * @category Linear algebra
196
+ */
197
+ export declare function kronecker(x: Operand, y: Operand): number[] | number[][];
198
+ /**
199
+ * Options for {@link seq}.
200
+ *
201
+ * @category Linear algebra
78
202
  */
79
- export declare function matrix(data: number | Sample, options?: MatrixOptions): Matrix;
80
- /** Kronecker product. Equivalent to R's `kronecker()` (two vectors give a vector). */
81
- export declare function kronecker(x: Operand, y: Operand): number[] | Matrix;
82
- /** Options for {@link seq}. */
83
203
  export interface SeqOptions {
84
204
  /** Increment. */
85
205
  by?: number;
@@ -87,7 +207,7 @@ export interface SeqOptions {
87
207
  lengthOut?: number;
88
208
  }
89
209
  /**
90
- * Regular sequences. Equivalent to R's `seq()`: `seq(from, to)` steps by ±1; `by` steps by the increment, up to
210
+ * Regular sequences. Equivalent to R's `seq()`. `seq(from, to)` steps by ±1; `by` steps by the increment, up to
91
211
  * `to`; `lengthOut` gives that many values, both ends exact. `from` or `to` may be omitted (`undefined`) when the
92
212
  * other two are given; `seq(n)` alone is `1, 2, ..., n`.
93
213
  *
@@ -96,6 +216,7 @@ export interface SeqOptions {
96
216
  * seq(0, 1, { by: 0.25 }); // [0, 0.25, 0.5, 0.75, 1]
97
217
  * seq(0, 1, { lengthOut: 3 }); // [0, 0.5, 1]
98
218
  * ```
219
+ *
220
+ * @category Linear algebra
99
221
  */
100
222
  export declare function seq(from?: number, to?: number, options?: SeqOptions): number[];
101
- export {};
@@ -19,13 +19,16 @@ function checkRows(x) {
19
19
  return nc;
20
20
  }
21
21
  /**
22
- * Matrix product. Equivalent to R's `x %*% y`: a vector is taken as a row or a column so that the product is
23
- * defined (two vectors give their inner product as a 1 x 1 matrix).
22
+ * Matrix product. Equivalent to R's `%*%`. A vector is taken as a row or a column so that the product is defined
23
+ * (two vectors give their inner product as a 1 x 1 matrix).
24
24
  *
25
25
  * @example
26
26
  * ```ts
27
27
  * matMul([[1, 2], [3, 4]], [[5], [6]]); // [[17], [39]]
28
+ * matMul([1, 2, 3], [4, 5, 6]); // [[32]]
28
29
  * ```
30
+ *
31
+ * @category Linear algebra
29
32
  */
30
33
  export function matMul(x, y) {
31
34
  let a;
@@ -108,19 +111,63 @@ function elementwise(x, y, f) {
108
111
  const n = Math.max(u.length, v.length);
109
112
  return Array.from({ length: n }, (_, i) => f(u[i % u.length], v[i % v.length]));
110
113
  }
111
- /** Element-wise sum with R's recycling (R's `x + y`). */
114
+ /**
115
+ * Element-wise sum with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
116
+ * R's `+`.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * add([[1, 2], [3, 4]], [10, 20]); // [[11, 12], [23, 24]]
121
+ * add([1, 2, 3], 2); // [3, 4, 5]
122
+ * ```
123
+ *
124
+ * @category Linear algebra
125
+ */
112
126
  export function add(x, y) {
113
127
  return elementwise(x, y, (a, b) => a + b);
114
128
  }
115
- /** Element-wise difference with R's recycling (R's `x - y`). */
129
+ /**
130
+ * Element-wise difference with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
131
+ * R's `-`.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * subtract([[1, 2], [3, 4]], [10, 20]); // [[-9, -8], [-17, -16]]
136
+ * subtract([1, 2, 3], 2); // [-1, 0, 1]
137
+ * ```
138
+ *
139
+ * @category Linear algebra
140
+ */
116
141
  export function subtract(x, y) {
117
142
  return elementwise(x, y, (a, b) => a - b);
118
143
  }
119
- /** Element-wise product with R's recycling (R's `x * y`); the matrix product is {@link matMul}. */
144
+ /**
145
+ * Element-wise product with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
146
+ * R's `*`. The matrix product is {@link matMul}.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * multiply([[1, 2], [3, 4]], [10, 20]); // [[10, 20], [60, 80]]
151
+ * multiply([1, 2, 3], 2); // [2, 4, 6]
152
+ * ```
153
+ *
154
+ * @category Linear algebra
155
+ */
120
156
  export function multiply(x, y) {
121
157
  return elementwise(x, y, (a, b) => a * b);
122
158
  }
123
- /** Element-wise quotient with R's recycling (R's `x / y`). */
159
+ /**
160
+ * Element-wise quotient with R's recycling (a shorter vector is recycled down the columns of a matrix). Equivalent to
161
+ * R's `/`.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * divide([[1, 2], [3, 4]], [10, 20]); // [[0.1, 0.2], [0.15, 0.2]]
166
+ * divide([1, 2, 3], 2); // [0.5, 1, 1.5]
167
+ * ```
168
+ *
169
+ * @category Linear algebra
170
+ */
124
171
  export function divide(x, y) {
125
172
  return elementwise(x, y, (a, b) => a / b);
126
173
  }
@@ -132,8 +179,16 @@ const OUTER_OPS = {
132
179
  "^": (a, b) => a ** b,
133
180
  };
134
181
  /**
135
- * Outer product of two vectors: the matrix of `fun(x[i], y[j])`. Equivalent to R's `outer()`; `fun` is `"*"`
182
+ * Outer product of two vectors: the matrix of `fun(x[i], y[j])`. Equivalent to R's `outer()`. `fun` is `"*"`
136
183
  * (default), `"+"`, `"-"`, `"/"`, `"^"` or a function of two numbers.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * outer([1, 2], [1, 2, 3]); // [[1, 2, 3], [2, 4, 6]]
188
+ * outer([1, 2], [1, 2, 3], "+"); // [[2, 3, 4], [3, 4, 5]]
189
+ * ```
190
+ *
191
+ * @category Linear algebra
137
192
  */
138
193
  export function outer(x, y, fun = "*") {
139
194
  const f = typeof fun === "function" ? fun : OUTER_OPS[fun];
@@ -143,7 +198,17 @@ export function outer(x, y, fun = "*") {
143
198
  const v = Array.from(y);
144
199
  return u.map((a) => v.map((b) => f(a, b)));
145
200
  }
146
- /** Transpose. Equivalent to R's `t()`: a vector becomes a 1-row matrix. */
201
+ /**
202
+ * Transpose. Equivalent to R's `t()`. A vector becomes a 1-row matrix.
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * t([[1, 2, 3], [4, 5, 6]]); // [[1, 4], [2, 5], [3, 6]]
207
+ * t([1, 2]); // [[1, 2]]
208
+ * ```
209
+ *
210
+ * @category Linear algebra
211
+ */
147
212
  export function t(x) {
148
213
  if (!isMatrix(x))
149
214
  return [asVector(x)];
@@ -151,16 +216,18 @@ export function t(x) {
151
216
  return Array.from({ length: nc }, (_, j) => x.map((r) => r[j]));
152
217
  }
153
218
  /**
154
- * R's `diag()`, with its three meanings: the diagonal of a matrix (as a vector); the diagonal matrix of a vector of
155
- * length 2 or more (its values recycled along the diagonal of an `nrow` x `ncol` matrix when given); and for a single
219
+ * Matrix diagonals, diagonal matrices and identity matrices. Equivalent to R's `diag()`. It has three meanings: the
220
+ * diagonal of a matrix (as a vector); the diagonal matrix of a vector of length 2 or more (its values recycled along the diagonal of an `nrow` x `ncol` matrix when given); and for a single
156
221
  * number n, the n x n identity matrix (with `nrow`/`ncol`, a matrix with that number on the diagonal).
157
222
  *
158
223
  * @example
159
224
  * ```ts
160
225
  * diag([[1, 2], [3, 4]]); // [1, 4]
161
226
  * diag([2, 3]); // [[2, 0], [0, 3]]
162
- * diag(3); // 3 x 3 identity
227
+ * diag(3); // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
163
228
  * ```
229
+ *
230
+ * @category Linear algebra
164
231
  */
165
232
  export function diag(x, options = {}) {
166
233
  if (isMatrix(x)) {
@@ -222,13 +289,15 @@ export function rbind(...args) {
222
289
  return frames === undefined ? bindParts(args, false) : rbindFrames(frames);
223
290
  }
224
291
  /**
225
- * Build a matrix from data, recycled to fill it. Equivalent to R's `matrix()`: filled by columns unless `byrow`.
292
+ * Build a matrix from data, recycled to fill it. Equivalent to R's `matrix()`. Filled by columns unless `byrow`.
226
293
  *
227
294
  * @example
228
295
  * ```ts
229
296
  * matrix([1, 2, 3, 4, 5, 6], { nrow: 2 }); // [[1, 3, 5], [2, 4, 6]]
230
- * matrix(0, { nrow: 2, ncol: 3 }); // zeros
297
+ * matrix(0, { nrow: 2, ncol: 3 }); // [[0, 0, 0], [0, 0, 0]]
231
298
  * ```
299
+ *
300
+ * @category Linear algebra
232
301
  */
233
302
  export function matrix(data, options = {}) {
234
303
  const v = asVector(data);
@@ -245,7 +314,17 @@ export function matrix(data, options = {}) {
245
314
  const byrow = options.byrow ?? false;
246
315
  return Array.from({ length: nrow }, (_, i) => Array.from({ length: ncol }, (_, j) => (len === 0 ? NaN : v[(byrow ? i * ncol + j : i + j * nrow) % len])));
247
316
  }
248
- /** Kronecker product. Equivalent to R's `kronecker()` (two vectors give a vector). */
317
+ /**
318
+ * Kronecker product. Equivalent to R's `kronecker()`. Two vectors give a vector.
319
+ *
320
+ * @example
321
+ * ```ts
322
+ * kronecker([[1, 2], [3, 4]], [[1, 1]]); // [[1, 1, 2, 2], [3, 3, 4, 4]]
323
+ * kronecker([1, 2], [1, 10]); // [1, 10, 2, 20]
324
+ * ```
325
+ *
326
+ * @category Linear algebra
327
+ */
249
328
  export function kronecker(x, y) {
250
329
  if (!isMatrix(x) && !isMatrix(y)) {
251
330
  const u = asVector(x);
@@ -261,7 +340,7 @@ export function kronecker(x, y) {
261
340
  return out;
262
341
  }
263
342
  /**
264
- * Regular sequences. Equivalent to R's `seq()`: `seq(from, to)` steps by ±1; `by` steps by the increment, up to
343
+ * Regular sequences. Equivalent to R's `seq()`. `seq(from, to)` steps by ±1; `by` steps by the increment, up to
265
344
  * `to`; `lengthOut` gives that many values, both ends exact. `from` or `to` may be omitted (`undefined`) when the
266
345
  * other two are given; `seq(n)` alone is `1, 2, ..., n`.
267
346
  *
@@ -270,6 +349,8 @@ export function kronecker(x, y) {
270
349
  * seq(0, 1, { by: 0.25 }); // [0, 0.25, 0.5, 0.75, 1]
271
350
  * seq(0, 1, { lengthOut: 3 }); // [0, 0.5, 1]
272
351
  * ```
352
+ *
353
+ * @category Linear algebra
273
354
  */
274
355
  export function seq(from, to, options = {}) {
275
356
  const { by, lengthOut } = options;