@numero-uno-jpn/rstats 0.10.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 (280) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.ja.md +13 -9
  3. package/README.md +13 -8
  4. package/dist/chunks/{chunk-QJJWGNJP.js → chunk-2X7JYQBI.js} +2 -2
  5. package/dist/chunks/{chunk-O6PT7IVU.js → chunk-36GVER3O.js} +2 -2
  6. package/dist/chunks/{chunk-EWC6I75B.js → chunk-3WUXRSM6.js} +1 -1
  7. package/dist/chunks/{chunk-4NM37MGA.js → chunk-6I6QKTFK.js} +2 -2
  8. package/dist/chunks/{chunk-2XKG3DX4.js → chunk-ETOEZTGL.js} +1 -1
  9. package/dist/chunks/{chunk-IT354SHV.js → chunk-FVPDVRJQ.js} +2 -2
  10. package/dist/chunks/{chunk-QULPY6YM.js → chunk-GG4BJMJW.js} +1 -1
  11. package/dist/chunks/{chunk-7HPVUVZB.js → chunk-HITD7CX6.js} +1 -1
  12. package/dist/chunks/{chunk-LIMZKONM.js → chunk-KK4GE65D.js} +2 -2
  13. package/dist/chunks/chunk-LAUYSGV4.js +3 -0
  14. package/dist/chunks/chunk-M6GDXKKS.js +2 -0
  15. package/dist/chunks/{chunk-QFULZNHL.js → chunk-PHHUHR7T.js} +1 -1
  16. package/dist/chunks/{chunk-ENL43FNO.js → chunk-WQPFDDIY.js} +2 -2
  17. package/dist/chunks/chunk-X2AWQSVS.js +2 -0
  18. package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-ZRJQD7VS.js} +2 -2
  19. package/dist/data/grouping.d.ts +100 -0
  20. package/dist/data/grouping.js +264 -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 +138 -0
  25. package/dist/descriptive/dataFrames.js +400 -0
  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 +42 -0
  31. package/dist/descriptive/missing.js +52 -0
  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 +105 -0
  37. package/dist/descriptive/pivot.js +347 -0
  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 +5 -1
  105. package/dist/entries/descriptive.js +4 -0
  106. package/dist/entries/numerics.d.ts +1 -1
  107. package/dist/entries/tests.js +1 -1
  108. package/dist/htest/agreement.d.ts +61 -23
  109. package/dist/htest/agreement.js +19 -9
  110. package/dist/htest/ansariTest.d.ts +19 -1
  111. package/dist/htest/ansariTest.js +5 -17
  112. package/dist/htest/aov.d.ts +43 -10
  113. package/dist/htest/aov.js +17 -3
  114. package/dist/htest/binomTest.d.ts +12 -2
  115. package/dist/htest/binomTest.js +2 -0
  116. package/dist/htest/brunnerMunzelTest.d.ts +9 -3
  117. package/dist/htest/brunnerMunzelTest.js +4 -2
  118. package/dist/htest/chisqTest.d.ts +19 -3
  119. package/dist/htest/cor.d.ts +31 -3
  120. package/dist/htest/cor.js +9 -27
  121. package/dist/htest/effectSize.d.ts +70 -12
  122. package/dist/htest/effectSize.js +40 -7
  123. package/dist/htest/fisherTest.d.ts +9 -1
  124. package/dist/htest/fisherTest.js +2 -0
  125. package/dist/htest/groups.d.ts +5 -1
  126. package/dist/htest/ksTest.d.ts +32 -2
  127. package/dist/htest/ksTest.js +5 -8
  128. package/dist/htest/leveneTest.d.ts +15 -4
  129. package/dist/htest/leveneTest.js +5 -2
  130. package/dist/htest/mantelhaenTest.d.ts +25 -3
  131. package/dist/htest/mantelhaenTest.js +15 -1
  132. package/dist/htest/mcnemarTest.d.ts +14 -4
  133. package/dist/htest/mcnemarTest.js +4 -2
  134. package/dist/htest/multipleComparisons.d.ts +173 -29
  135. package/dist/htest/multipleComparisons.js +123 -19
  136. package/dist/htest/normalityTests.d.ts +56 -9
  137. package/dist/htest/normalityTests.js +40 -6
  138. package/dist/htest/onewayTest.d.ts +45 -2
  139. package/dist/htest/onewayTest.js +15 -7
  140. package/dist/htest/pAdjust.d.ts +9 -1
  141. package/dist/htest/pAdjust.js +2 -0
  142. package/dist/htest/pairwise.d.ts +50 -5
  143. package/dist/htest/pairwise.js +25 -0
  144. package/dist/htest/poissonTest.d.ts +12 -2
  145. package/dist/htest/poissonTest.js +2 -0
  146. package/dist/htest/polycor.d.ts +29 -10
  147. package/dist/htest/power.d.ts +60 -6
  148. package/dist/htest/power.js +6 -0
  149. package/dist/htest/propTest.d.ts +13 -2
  150. package/dist/htest/propTest.js +2 -0
  151. package/dist/htest/pwr.d.ts +286 -36
  152. package/dist/htest/pwr.js +116 -15
  153. package/dist/htest/rankTests.d.ts +132 -7
  154. package/dist/htest/rankTests.js +30 -33
  155. package/dist/htest/tTest.d.ts +32 -2
  156. package/dist/htest/tTest.js +19 -24
  157. package/dist/htest/testFormula.d.ts +57 -0
  158. package/dist/htest/testFormula.js +171 -0
  159. package/dist/htest/trendTests.d.ts +29 -8
  160. package/dist/htest/trendTests.js +8 -4
  161. package/dist/htest/types.d.ts +14 -2
  162. package/dist/htest/varTest.d.ts +46 -6
  163. package/dist/htest/varTest.js +11 -21
  164. package/dist/htest/wilcoxTest.d.ts +23 -1
  165. package/dist/htest/wilcoxTest.js +9 -24
  166. package/dist/internal/dpq.d.ts +4 -0
  167. package/dist/internal/noncentral.d.ts +2 -0
  168. package/dist/internal/vectorize.d.ts +4 -0
  169. package/dist/linalg/basic.d.ts +39 -5
  170. package/dist/linalg/basic.js +15 -0
  171. package/dist/linalg/chol.d.ts +26 -5
  172. package/dist/linalg/chol.js +7 -0
  173. package/dist/linalg/eigen.d.ts +16 -7
  174. package/dist/linalg/eigen.js +6 -5
  175. package/dist/linalg/expm.d.ts +15 -5
  176. package/dist/linalg/expm.js +5 -3
  177. package/dist/linalg/kappa.d.ts +21 -1
  178. package/dist/linalg/kappa.js +16 -0
  179. package/dist/linalg/lu.d.ts +30 -6
  180. package/dist/linalg/lu.js +15 -3
  181. package/dist/linalg/matrix.d.ts +4 -0
  182. package/dist/linalg/norm.d.ts +16 -1
  183. package/dist/linalg/norm.js +9 -0
  184. package/dist/linalg/ops.d.ts +168 -36
  185. package/dist/linalg/ops.js +116 -20
  186. package/dist/linalg/qr.d.ts +87 -12
  187. package/dist/linalg/qr.js +31 -0
  188. package/dist/linalg/schur.d.ts +8 -2
  189. package/dist/linalg/schur.js +3 -1
  190. package/dist/linalg/solve.d.ts +42 -6
  191. package/dist/linalg/solve.js +22 -0
  192. package/dist/linalg/svd.d.ts +12 -2
  193. package/dist/linalg/svd.js +2 -0
  194. package/dist/linalg.js +2 -2
  195. package/dist/models/anovaSequential.d.ts +26 -4
  196. package/dist/models/anovaSequential.js +3 -1
  197. package/dist/models/aovEz.d.ts +29 -6
  198. package/dist/models/aovEz.js +5 -2
  199. package/dist/models/aovStrata.d.ts +12 -2
  200. package/dist/models/carAnova.d.ts +9 -3
  201. package/dist/models/carAnova.js +4 -2
  202. package/dist/models/coxph.d.ts +33 -7
  203. package/dist/models/coxph.js +8 -3
  204. package/dist/models/glm.d.ts +128 -21
  205. package/dist/models/glm.js +71 -14
  206. package/dist/models/glmAnova.d.ts +30 -5
  207. package/dist/models/glmAnova.js +15 -2
  208. package/dist/models/glmConfint.d.ts +16 -2
  209. package/dist/models/glmConfint.js +9 -1
  210. package/dist/models/glmFamily.d.ts +27 -5
  211. package/dist/models/lm.d.ts +75 -17
  212. package/dist/models/lm.js +22 -4
  213. package/dist/models/mauchly.d.ts +20 -5
  214. package/dist/models/mauchly.js +7 -3
  215. package/dist/models/modelFrame.d.ts +5 -1
  216. package/dist/models/modelMatrix.d.ts +18 -4
  217. package/dist/models/modelMatrix.js +3 -1
  218. package/dist/models/survival.d.ts +41 -9
  219. package/dist/models/survival.js +10 -4
  220. package/dist/numerics/deriv.d.ts +21 -6
  221. package/dist/numerics/deriv.js +16 -5
  222. package/dist/numerics/integrate.d.ts +37 -7
  223. package/dist/numerics/integrate.js +8 -2
  224. package/dist/numerics/interp.d.ts +63 -12
  225. package/dist/numerics/interp.js +30 -6
  226. package/dist/numerics/ode.d.ts +15 -5
  227. package/dist/numerics/ode.js +5 -3
  228. package/dist/numerics.js +2 -2
  229. package/dist/optim/optim.d.ts +32 -8
  230. package/dist/optim/optim.js +8 -4
  231. package/dist/random/beta.d.ts +8 -0
  232. package/dist/random/beta.js +8 -0
  233. package/dist/random/binomial.d.ts +16 -0
  234. package/dist/random/binomial.js +16 -0
  235. package/dist/random/continuous.d.ts +51 -0
  236. package/dist/random/continuous.js +51 -0
  237. package/dist/random/extra.d.ts +66 -6
  238. package/dist/random/extra.js +66 -6
  239. package/dist/random/gamma.d.ts +33 -0
  240. package/dist/random/gamma.js +33 -0
  241. package/dist/random/hyper.d.ts +8 -0
  242. package/dist/random/hyper.js +8 -0
  243. package/dist/random/poisson.d.ts +25 -0
  244. package/dist/random/poisson.js +25 -0
  245. package/dist/random/r2dtable.d.ts +2 -0
  246. package/dist/random/r2dtable.js +2 -0
  247. package/dist/random/rank.d.ts +16 -0
  248. package/dist/random/rank.js +16 -0
  249. package/dist/random/rng.d.ts +49 -6
  250. package/dist/random/rng.js +29 -2
  251. package/dist/random/sample.d.ts +10 -2
  252. package/dist/random/sample.js +5 -1
  253. package/dist/random/streams.d.ts +13 -0
  254. package/dist/random/streams.js +13 -0
  255. package/dist/rstats.js +14 -14
  256. package/dist/special/besselIK.d.ts +11 -3
  257. package/dist/special/besselIK.js +6 -2
  258. package/dist/special/besselJY.d.ts +6 -2
  259. package/dist/special/besselJY.js +6 -2
  260. package/dist/special/beta.d.ts +16 -2
  261. package/dist/special/beta.js +16 -2
  262. package/dist/special/choose.d.ts +13 -5
  263. package/dist/special/choose.js +13 -5
  264. package/dist/special/erf.d.ts +8 -3
  265. package/dist/special/erf.js +8 -3
  266. package/dist/special/extra.d.ts +30 -10
  267. package/dist/special/extra.js +25 -9
  268. package/dist/special/gamma.d.ts +18 -2
  269. package/dist/special/gamma.js +18 -2
  270. package/dist/special/log1p.d.ts +16 -7
  271. package/dist/special/log1p.js +16 -7
  272. package/dist/special/psigamma.d.ts +9 -3
  273. package/dist/special/psigamma.js +9 -3
  274. package/dist/special/rCompatible.d.ts +5 -1
  275. package/dist/special.js +2 -2
  276. package/dist/tests.js +12 -13
  277. package/llms.txt +14 -0
  278. package/package.json +1 -1
  279. package/dist/chunks/chunk-AECGWZHD.js +0 -2
  280. package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
@@ -1,74 +1,205 @@
1
1
  import type { Sample } from "../htest/types.js";
2
- /** A vector (plain array) or a matrix (array of rows). */
2
+ import type { DataFrame } from "../models/modelFrame.js";
3
+ /**
4
+ * A vector (plain array) or a matrix (array of rows).
5
+ *
6
+ * @category Linear algebra
7
+ */
3
8
  export type Operand = number | Sample | readonly (readonly number[])[];
4
- type Matrix = number[][];
5
9
  /**
6
- * Matrix product. Equivalent to R's `x %*% y`: a vector is taken as a row or a column so that the product is
7
- * 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).
8
12
  *
9
13
  * @example
10
14
  * ```ts
11
15
  * matMul([[1, 2], [3, 4]], [[5], [6]]); // [[17], [39]]
16
+ * matMul([1, 2, 3], [4, 5, 6]); // [[32]]
17
+ * ```
18
+ *
19
+ * @category Linear algebra
20
+ */
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]
12
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
13
46
  */
14
- export declare function matMul(x: Operand, y: Operand): Matrix;
15
- /** Element-wise sum with R's recycling (R's `x + y`). */
16
- export declare function add(x: Operand, y: Operand): number[] | Matrix;
17
- /** Element-wise difference with R's recycling (R's `x - y`). */
18
- export declare function subtract(x: Operand, y: Operand): number[] | Matrix;
19
- /** Element-wise product with R's recycling (R's `x * y`); the matrix product is {@link matMul}. */
20
- export declare function multiply(x: Operand, y: Operand): number[] | Matrix;
21
- /** Element-wise quotient with R's recycling (R's `x / y`). */
22
- export declare function divide(x: Operand, y: Operand): number[] | Matrix;
23
- /**
24
- * Outer product of two vectors: the matrix of `fun(x[i], y[j])`. Equivalent to R's `outer()`; `fun` is `"*"`
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 `"*"`
25
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
26
103
  */
27
- export declare function outer(x: Sample, y: Sample, fun?: "*" | "+" | "-" | "/" | "^" | ((a: number, b: number) => number)): Matrix;
28
- /** Transpose. Equivalent to R's `t()`: a vector becomes a 1-row matrix. */
29
- export declare function t(x: Operand): Matrix;
30
- /** Options for {@link diag}. */
31
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. */
32
106
  nrow?: number;
107
+ /** Number of columns of the matrix built from a vector or a number (R's `ncol`). Default: `nrow`. */
33
108
  ncol?: number;
34
109
  }
35
110
  /**
36
- * R's `diag()`, with its three meanings: the diagonal of a matrix (as a vector); the diagonal matrix of a vector of
37
- * 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
38
113
  * number n, the n x n identity matrix (with `nrow`/`ncol`, a matrix with that number on the diagonal).
39
114
  *
40
115
  * @example
41
116
  * ```ts
42
117
  * diag([[1, 2], [3, 4]]); // [1, 4]
43
118
  * diag([2, 3]); // [[2, 0], [0, 3]]
44
- * diag(3); // 3 x 3 identity
119
+ * diag(3); // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
120
+ * ```
121
+ *
122
+ * @category Linear algebra
123
+ */
124
+ export declare function diag(x: Operand, options?: DiagOptions): number[] | number[][];
125
+ /**
126
+ * Combine vectors and matrices as columns. Equivalent to R's `cbind()`. Vectors are recycled to the number of rows.
127
+ * Given data frames (objects of columns), puts their columns side by side, as R's `cbind()` of data frames: all must
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]]
45
134
  * ```
135
+ *
136
+ * @category Linear algebra
137
+ */
138
+ export declare function cbind(...args: Operand[]): number[][];
139
+ export declare function cbind(...frames: DataFrame[]): DataFrame;
140
+ /**
141
+ * Combine vectors and matrices as rows. Equivalent to R's `rbind()`. Given data frames (objects of columns), stacks
142
+ * their rows, as R's `rbind()` of data frames: all must have the same column names, matched by name (the result has
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
152
+ */
153
+ export declare function rbind(...args: Operand[]): number[][];
154
+ export declare function rbind(...frames: DataFrame[]): DataFrame;
155
+ /**
156
+ * Options for {@link matrix}.
157
+ *
158
+ * @category Linear algebra
46
159
  */
47
- export declare function diag(x: Operand, options?: DiagOptions): number[] | Matrix;
48
- /** Combine vectors and matrices as columns. Equivalent to R's `cbind()` (vectors recycled to the number of rows). */
49
- export declare function cbind(...args: Operand[]): Matrix;
50
- /** Combine vectors and matrices as rows. Equivalent to R's `rbind()`. */
51
- export declare function rbind(...args: Operand[]): Matrix;
52
- /** Options for {@link matrix}. */
53
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
+ */
54
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
+ */
55
170
  ncol?: number;
56
171
  /** Fill by rows instead of by columns (R's default). Default `false`. */
57
172
  byrow?: boolean;
58
173
  }
59
174
  /**
60
- * 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`.
61
176
  *
62
177
  * @example
63
178
  * ```ts
64
179
  * matrix([1, 2, 3, 4, 5, 6], { nrow: 2 }); // [[1, 3, 5], [2, 4, 6]]
65
- * matrix(0, { nrow: 2, ncol: 3 }); // zeros
180
+ * matrix(0, { nrow: 2, ncol: 3 }); // [[0, 0, 0], [0, 0, 0]]
66
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]
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
67
202
  */
68
- export declare function matrix(data: number | Sample, options?: MatrixOptions): Matrix;
69
- /** Kronecker product. Equivalent to R's `kronecker()` (two vectors give a vector). */
70
- export declare function kronecker(x: Operand, y: Operand): number[] | Matrix;
71
- /** Options for {@link seq}. */
72
203
  export interface SeqOptions {
73
204
  /** Increment. */
74
205
  by?: number;
@@ -76,7 +207,7 @@ export interface SeqOptions {
76
207
  lengthOut?: number;
77
208
  }
78
209
  /**
79
- * 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
80
211
  * `to`; `lengthOut` gives that many values, both ends exact. `from` or `to` may be omitted (`undefined`) when the
81
212
  * other two are given; `seq(n)` alone is `1, 2, ..., n`.
82
213
  *
@@ -85,6 +216,7 @@ export interface SeqOptions {
85
216
  * seq(0, 1, { by: 0.25 }); // [0, 0.25, 0.5, 0.75, 1]
86
217
  * seq(0, 1, { lengthOut: 3 }); // [0, 0.5, 1]
87
218
  * ```
219
+ *
220
+ * @category Linear algebra
88
221
  */
89
222
  export declare function seq(from?: number, to?: number, options?: SeqOptions): number[];
90
- export {};
@@ -1,9 +1,11 @@
1
1
  // Matrix arithmetic and construction: R's %*% (matMul), element-wise +, -, *, / with R's recycling (add, subtract,
2
- // multiply, divide), outer, t, diag, cbind, rbind, matrix, kronecker and seq.
2
+ // multiply, divide), outer, t, diag, cbind, rbind (also of data frames: src/descriptive/dataFrames.ts), matrix,
3
+ // kronecker and seq.
3
4
  //
4
5
  // Written for rStats.js (no ported code). Matrices are arrays of rows, as everywhere in rStats.js; a plain array is a
5
6
  // vector, which R treats as a row or a column as the operation needs. Element-wise operations recycle a shorter
6
7
  // vector over a matrix in column-major order (R's storage order), as R does.
8
+ import { cbindFrames, rbindFrames } from "../descriptive/dataFrames.js";
7
9
  function isMatrix(x) {
8
10
  return typeof x !== "number" && x.length > 0 && Array.isArray(x[0]);
9
11
  }
@@ -17,13 +19,16 @@ function checkRows(x) {
17
19
  return nc;
18
20
  }
19
21
  /**
20
- * Matrix product. Equivalent to R's `x %*% y`: a vector is taken as a row or a column so that the product is
21
- * 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).
22
24
  *
23
25
  * @example
24
26
  * ```ts
25
27
  * matMul([[1, 2], [3, 4]], [[5], [6]]); // [[17], [39]]
28
+ * matMul([1, 2, 3], [4, 5, 6]); // [[32]]
26
29
  * ```
30
+ *
31
+ * @category Linear algebra
27
32
  */
28
33
  export function matMul(x, y) {
29
34
  let a;
@@ -106,19 +111,63 @@ function elementwise(x, y, f) {
106
111
  const n = Math.max(u.length, v.length);
107
112
  return Array.from({ length: n }, (_, i) => f(u[i % u.length], v[i % v.length]));
108
113
  }
109
- /** 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
+ */
110
126
  export function add(x, y) {
111
127
  return elementwise(x, y, (a, b) => a + b);
112
128
  }
113
- /** 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
+ */
114
141
  export function subtract(x, y) {
115
142
  return elementwise(x, y, (a, b) => a - b);
116
143
  }
117
- /** 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
+ */
118
156
  export function multiply(x, y) {
119
157
  return elementwise(x, y, (a, b) => a * b);
120
158
  }
121
- /** 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
+ */
122
171
  export function divide(x, y) {
123
172
  return elementwise(x, y, (a, b) => a / b);
124
173
  }
@@ -130,8 +179,16 @@ const OUTER_OPS = {
130
179
  "^": (a, b) => a ** b,
131
180
  };
132
181
  /**
133
- * 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 `"*"`
134
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
135
192
  */
136
193
  export function outer(x, y, fun = "*") {
137
194
  const f = typeof fun === "function" ? fun : OUTER_OPS[fun];
@@ -141,7 +198,17 @@ export function outer(x, y, fun = "*") {
141
198
  const v = Array.from(y);
142
199
  return u.map((a) => v.map((b) => f(a, b)));
143
200
  }
144
- /** 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
+ */
145
212
  export function t(x) {
146
213
  if (!isMatrix(x))
147
214
  return [asVector(x)];
@@ -149,16 +216,18 @@ export function t(x) {
149
216
  return Array.from({ length: nc }, (_, j) => x.map((r) => r[j]));
150
217
  }
151
218
  /**
152
- * R's `diag()`, with its three meanings: the diagonal of a matrix (as a vector); the diagonal matrix of a vector of
153
- * 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
154
221
  * number n, the n x n identity matrix (with `nrow`/`ncol`, a matrix with that number on the diagonal).
155
222
  *
156
223
  * @example
157
224
  * ```ts
158
225
  * diag([[1, 2], [3, 4]]); // [1, 4]
159
226
  * diag([2, 3]); // [[2, 0], [0, 3]]
160
- * diag(3); // 3 x 3 identity
227
+ * diag(3); // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
161
228
  * ```
229
+ *
230
+ * @category Linear algebra
162
231
  */
163
232
  export function diag(x, options = {}) {
164
233
  if (isMatrix(x)) {
@@ -198,22 +267,37 @@ function bindParts(args, byColumn) {
198
267
  });
199
268
  return parts.flatMap((p) => p.map((v) => Array.from({ length: len }, (_, i) => v[i % v.length])));
200
269
  }
201
- /** Combine vectors and matrices as columns. Equivalent to R's `cbind()` (vectors recycled to the number of rows). */
270
+ /** A data frame (object of columns) rather than a number, vector or matrix */
271
+ function isDataFrame(x) {
272
+ return typeof x === "object" && !Array.isArray(x) && !ArrayBuffer.isView(x);
273
+ }
274
+ /** The data frames, if all arguments are data frames (and there is at least one); a mix is an error */
275
+ function framesOf(args) {
276
+ const frames = args.filter(isDataFrame);
277
+ if (frames.length === 0)
278
+ return undefined;
279
+ if (frames.length < args.length)
280
+ throw new TypeError("cannot combine data frames with vectors or matrices");
281
+ return frames;
282
+ }
202
283
  export function cbind(...args) {
203
- return t(bindParts(args, true));
284
+ const frames = framesOf(args);
285
+ return frames === undefined ? t(bindParts(args, true)) : cbindFrames(frames);
204
286
  }
205
- /** Combine vectors and matrices as rows. Equivalent to R's `rbind()`. */
206
287
  export function rbind(...args) {
207
- return bindParts(args, false);
288
+ const frames = framesOf(args);
289
+ return frames === undefined ? bindParts(args, false) : rbindFrames(frames);
208
290
  }
209
291
  /**
210
- * 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`.
211
293
  *
212
294
  * @example
213
295
  * ```ts
214
296
  * matrix([1, 2, 3, 4, 5, 6], { nrow: 2 }); // [[1, 3, 5], [2, 4, 6]]
215
- * matrix(0, { nrow: 2, ncol: 3 }); // zeros
297
+ * matrix(0, { nrow: 2, ncol: 3 }); // [[0, 0, 0], [0, 0, 0]]
216
298
  * ```
299
+ *
300
+ * @category Linear algebra
217
301
  */
218
302
  export function matrix(data, options = {}) {
219
303
  const v = asVector(data);
@@ -230,7 +314,17 @@ export function matrix(data, options = {}) {
230
314
  const byrow = options.byrow ?? false;
231
315
  return Array.from({ length: nrow }, (_, i) => Array.from({ length: ncol }, (_, j) => (len === 0 ? NaN : v[(byrow ? i * ncol + j : i + j * nrow) % len])));
232
316
  }
233
- /** 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
+ */
234
328
  export function kronecker(x, y) {
235
329
  if (!isMatrix(x) && !isMatrix(y)) {
236
330
  const u = asVector(x);
@@ -246,7 +340,7 @@ export function kronecker(x, y) {
246
340
  return out;
247
341
  }
248
342
  /**
249
- * 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
250
344
  * `to`; `lengthOut` gives that many values, both ends exact. `from` or `to` may be omitted (`undefined`) when the
251
345
  * other two are given; `seq(n)` alone is `1, 2, ..., n`.
252
346
  *
@@ -255,6 +349,8 @@ export function kronecker(x, y) {
255
349
  * seq(0, 1, { by: 0.25 }); // [0, 0.25, 0.5, 0.75, 1]
256
350
  * seq(0, 1, { lengthOut: 3 }); // [0, 0.5, 1]
257
351
  * ```
352
+ *
353
+ * @category Linear algebra
258
354
  */
259
355
  export function seq(from, to, options = {}) {
260
356
  const { by, lengthOut } = options;