@ac-kit/math-linear 0.2.1 → 0.4.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.
Files changed (178) hide show
  1. package/CHANGELOG.md +337 -0
  2. package/README.md +232 -16
  3. package/dist/_brand.d.ts +14 -0
  4. package/dist/_brand.d.ts.map +1 -0
  5. package/dist/_brand.js +2 -0
  6. package/dist/_brand.js.map +1 -0
  7. package/dist/_invert.d.ts +214 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +884 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +29 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +103 -0
  14. package/dist/_slerp.js.map +1 -1
  15. package/dist/_strided-range.d.ts +23 -0
  16. package/dist/_strided-range.d.ts.map +1 -0
  17. package/dist/_strided-range.js +75 -0
  18. package/dist/_strided-range.js.map +1 -0
  19. package/dist/_trs.d.ts +38 -0
  20. package/dist/_trs.d.ts.map +1 -0
  21. package/dist/_trs.js +132 -0
  22. package/dist/_trs.js.map +1 -0
  23. package/dist/affine2-array.d.ts +53 -0
  24. package/dist/affine2-array.d.ts.map +1 -0
  25. package/dist/affine2-array.js +107 -0
  26. package/dist/affine2-array.js.map +1 -0
  27. package/dist/affine2-componentwise.d.ts +28 -0
  28. package/dist/affine2-componentwise.d.ts.map +1 -0
  29. package/dist/affine2-componentwise.js +66 -0
  30. package/dist/affine2-componentwise.js.map +1 -0
  31. package/dist/affine2.d.ts +156 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +437 -0
  34. package/dist/affine2.js.map +1 -0
  35. package/dist/affine3-array.d.ts +53 -0
  36. package/dist/affine3-array.d.ts.map +1 -0
  37. package/dist/affine3-array.js +137 -0
  38. package/dist/affine3-array.js.map +1 -0
  39. package/dist/affine3-componentwise.d.ts +32 -0
  40. package/dist/affine3-componentwise.d.ts.map +1 -0
  41. package/dist/affine3-componentwise.js +100 -0
  42. package/dist/affine3-componentwise.js.map +1 -0
  43. package/dist/affine3.d.ts +205 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +792 -0
  46. package/dist/affine3.js.map +1 -0
  47. package/dist/checked/index.d.ts +35 -0
  48. package/dist/checked/index.d.ts.map +1 -0
  49. package/dist/checked/index.js +58 -0
  50. package/dist/checked/index.js.map +1 -0
  51. package/dist/euler.d.ts +65 -0
  52. package/dist/euler.d.ts.map +1 -0
  53. package/dist/euler.js +217 -0
  54. package/dist/euler.js.map +1 -0
  55. package/dist/index.d.ts +29 -0
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +29 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/kl-activations.d.ts.map +1 -1
  60. package/dist/kl-activations.js +4 -3
  61. package/dist/kl-activations.js.map +1 -1
  62. package/dist/mat2x2-array.d.ts +36 -0
  63. package/dist/mat2x2-array.d.ts.map +1 -0
  64. package/dist/mat2x2-array.js +71 -0
  65. package/dist/mat2x2-array.js.map +1 -0
  66. package/dist/mat2x2-componentwise.d.ts +86 -0
  67. package/dist/mat2x2-componentwise.d.ts.map +1 -0
  68. package/dist/mat2x2-componentwise.js +199 -0
  69. package/dist/mat2x2-componentwise.js.map +1 -0
  70. package/dist/mat2x2.d.ts +79 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +192 -93
  73. package/dist/mat2x2.js.map +1 -1
  74. package/dist/mat3x3-array.d.ts +36 -0
  75. package/dist/mat3x3-array.d.ts.map +1 -0
  76. package/dist/mat3x3-array.js +91 -0
  77. package/dist/mat3x3-array.js.map +1 -0
  78. package/dist/mat3x3-componentwise.d.ts +86 -0
  79. package/dist/mat3x3-componentwise.d.ts.map +1 -0
  80. package/dist/mat3x3-componentwise.js +299 -0
  81. package/dist/mat3x3-componentwise.js.map +1 -0
  82. package/dist/mat3x3.d.ts +153 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +470 -192
  85. package/dist/mat3x3.js.map +1 -1
  86. package/dist/mat4x4-array.d.ts +46 -0
  87. package/dist/mat4x4-array.d.ts.map +1 -0
  88. package/dist/mat4x4-array.js +133 -0
  89. package/dist/mat4x4-array.js.map +1 -0
  90. package/dist/mat4x4-componentwise.d.ts +87 -0
  91. package/dist/mat4x4-componentwise.d.ts.map +1 -0
  92. package/dist/mat4x4-componentwise.js +434 -0
  93. package/dist/mat4x4-componentwise.js.map +1 -0
  94. package/dist/mat4x4.d.ts +221 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +972 -334
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +113 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +418 -0
  101. package/dist/projection.js.map +1 -0
  102. package/dist/quaternion-array.d.ts +47 -0
  103. package/dist/quaternion-array.d.ts.map +1 -0
  104. package/dist/quaternion-array.js +86 -0
  105. package/dist/quaternion-array.js.map +1 -0
  106. package/dist/quaternion.d.ts +106 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +253 -182
  109. package/dist/quaternion.js.map +1 -1
  110. package/dist/transform.d.ts +220 -0
  111. package/dist/transform.d.ts.map +1 -0
  112. package/dist/transform.js +412 -0
  113. package/dist/transform.js.map +1 -0
  114. package/dist/tridiagonal.d.ts +52 -0
  115. package/dist/tridiagonal.d.ts.map +1 -0
  116. package/dist/tridiagonal.js +90 -0
  117. package/dist/tridiagonal.js.map +1 -0
  118. package/dist/upload.d.ts +55 -0
  119. package/dist/upload.d.ts.map +1 -0
  120. package/dist/upload.js +129 -0
  121. package/dist/upload.js.map +1 -0
  122. package/dist/vec2-array-componentwise.d.ts +207 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +472 -0
  125. package/dist/vec2-array-componentwise.js.map +1 -0
  126. package/dist/vec2-array.d.ts +53 -0
  127. package/dist/vec2-array.d.ts.map +1 -0
  128. package/dist/vec2-array.js +123 -0
  129. package/dist/vec2-array.js.map +1 -0
  130. package/dist/vec2-componentwise.d.ts +311 -0
  131. package/dist/vec2-componentwise.d.ts.map +1 -0
  132. package/dist/vec2-componentwise.js +542 -0
  133. package/dist/vec2-componentwise.js.map +1 -0
  134. package/dist/vec2.d.ts +97 -82
  135. package/dist/vec2.d.ts.map +1 -1
  136. package/dist/vec2.js +121 -216
  137. package/dist/vec2.js.map +1 -1
  138. package/dist/vec3-array-componentwise.d.ts +207 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +510 -0
  141. package/dist/vec3-array-componentwise.js.map +1 -0
  142. package/dist/vec3-array.d.ts +102 -0
  143. package/dist/vec3-array.d.ts.map +1 -0
  144. package/dist/vec3-array.js +299 -0
  145. package/dist/vec3-array.js.map +1 -0
  146. package/dist/vec3-componentwise.d.ts +310 -0
  147. package/dist/vec3-componentwise.d.ts.map +1 -0
  148. package/dist/vec3-componentwise.js +594 -0
  149. package/dist/vec3-componentwise.js.map +1 -0
  150. package/dist/vec3.d.ts +177 -87
  151. package/dist/vec3.d.ts.map +1 -1
  152. package/dist/vec3.js +391 -272
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +198 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +521 -0
  157. package/dist/vec4-array-componentwise.js.map +1 -0
  158. package/dist/vec4-array.d.ts +12 -0
  159. package/dist/vec4-array.d.ts.map +1 -0
  160. package/dist/vec4-array.js +43 -0
  161. package/dist/vec4-array.js.map +1 -0
  162. package/dist/vec4-componentwise.d.ts +299 -0
  163. package/dist/vec4-componentwise.d.ts.map +1 -0
  164. package/dist/vec4-componentwise.js +637 -0
  165. package/dist/vec4-componentwise.js.map +1 -0
  166. package/dist/vec4.d.ts +59 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +74 -249
  169. package/dist/vec4.js.map +1 -1
  170. package/dist/view-basis.d.ts +15 -0
  171. package/dist/view-basis.d.ts.map +1 -0
  172. package/dist/view-basis.js +29 -0
  173. package/dist/view-basis.js.map +1 -0
  174. package/dist/viewport.d.ts +114 -0
  175. package/dist/viewport.d.ts.map +1 -0
  176. package/dist/viewport.js +250 -0
  177. package/dist/viewport.js.map +1 -0
  178. package/package.json +19 -9
package/dist/mat3x3.js CHANGED
@@ -1,213 +1,491 @@
1
- import { isCloseTo } from "@ac-kit/core";
2
- import { diffOfProducts, dotPrecise } from "@ac-kit/math-scalar";
3
- export const MAT3X3_ZERO = [
4
- [0, 0, 0],
5
- [0, 0, 0],
6
- [0, 0, 0],
7
- ];
8
- export const MAT3X3_IDENTITY = [
9
- [1, 0, 0],
10
- [0, 1, 0],
11
- [0, 0, 1],
12
- ];
13
- export function mat3x3Add(a, b) {
14
- return [
15
- [a[0][0] + b[0][0], a[0][1] + b[0][1], a[0][2] + b[0][2]],
16
- [a[1][0] + b[1][0], a[1][1] + b[1][1], a[1][2] + b[1][2]],
17
- [a[2][0] + b[2][0], a[2][1] + b[2][1], a[2][2] + b[2][2]],
18
- ];
19
- }
20
- export function mat3x3Determinant(matrix) {
21
- const [[a, b, c], [d, e, f], [g, h, i]] = matrix;
22
- return dotPrecise([a, -b, c], [
23
- diffOfProducts(e, i, f, h),
24
- diffOfProducts(d, i, f, g),
25
- diffOfProducts(d, h, e, g),
26
- ]);
27
- }
28
- /** Exact element-wise equality. See {@link mat3x3IsClose} for a tolerant test. */
29
- export function mat3x3Equals(a, b) {
30
- return (a[0][0] === b[0][0] &&
31
- a[0][1] === b[0][1] &&
32
- a[0][2] === b[0][2] &&
33
- a[1][0] === b[1][0] &&
34
- a[1][1] === b[1][1] &&
35
- a[1][2] === b[1][2] &&
36
- a[2][0] === b[2][0] &&
37
- a[2][1] === b[2][1] &&
38
- a[2][2] === b[2][2]);
39
- }
40
- /**
41
- * Element-wise approximate equality.
42
- *
43
- * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
44
- * and never a basis for ordering — use {@link mat3x3Equals} there.
45
- */
46
- export function mat3x3IsClose(a, b, tolerance) {
47
- for (let row = 0; row < 3; row++) {
48
- for (let column = 0; column < 3; column++) {
49
- if (!isCloseTo(a[row][column], b[row][column], tolerance)) {
50
- return false;
51
- }
1
+ import { dotPrecise3 } from "@ac-kit/math-scalar";
2
+ import { BOUND_INPUT_, INVERSE_LIMIT_, invertRescaled_, isCofactorExpansionExact_, isWithinTolerance3_, ROUNDING_TOLERANCE_, UNIT_ROUNDOFF_, } from "./_invert.js";
3
+ /**
4
+ * Writes the product `a · b` into `out`: `b` is applied first. Not commutative.
5
+ * Plain arithmetic: each entry is within three roundings of the exact value
6
+ * relative to the sum of the absolute values of its terms, so it can lose every
7
+ * digit where the terms cancel (see {@link mat3x3MultiplyPrecise}). `out` may be
8
+ * `a` or `b`.
9
+ */
10
+ export function mat3x3Multiply(out, a, b) {
11
+ const a0 = a[0];
12
+ const a1 = a[1];
13
+ const a2 = a[2];
14
+ const a3 = a[3];
15
+ const a4 = a[4];
16
+ const a5 = a[5];
17
+ const a6 = a[6];
18
+ const a7 = a[7];
19
+ const a8 = a[8];
20
+ const b0 = b[0];
21
+ const b1 = b[1];
22
+ const b2 = b[2];
23
+ const b3 = b[3];
24
+ const b4 = b[4];
25
+ const b5 = b[5];
26
+ const b6 = b[6];
27
+ const b7 = b[7];
28
+ const b8 = b[8];
29
+ out[0] = a0 * b0 + a3 * b1 + a6 * b2;
30
+ out[1] = a1 * b0 + a4 * b1 + a7 * b2;
31
+ out[2] = a2 * b0 + a5 * b1 + a8 * b2;
32
+ out[3] = a0 * b3 + a3 * b4 + a6 * b5;
33
+ out[4] = a1 * b3 + a4 * b4 + a7 * b5;
34
+ out[5] = a2 * b3 + a5 * b4 + a8 * b5;
35
+ out[6] = a0 * b6 + a3 * b7 + a6 * b8;
36
+ out[7] = a1 * b6 + a4 * b7 + a7 * b8;
37
+ out[8] = a2 * b6 + a5 * b7 + a8 * b8;
38
+ return out;
39
+ }
40
+ /**
41
+ * {@link mat3x3Multiply} with each entry a compensated dot product, as accurate
42
+ * as one in doubled precision rounded once. `out` may be `a` or `b`.
43
+ */
44
+ export function mat3x3MultiplyPrecise(out, a, b) {
45
+ const a0 = a[0];
46
+ const a1 = a[1];
47
+ const a2 = a[2];
48
+ const a3 = a[3];
49
+ const a4 = a[4];
50
+ const a5 = a[5];
51
+ const a6 = a[6];
52
+ const a7 = a[7];
53
+ const a8 = a[8];
54
+ const b0 = b[0];
55
+ const b1 = b[1];
56
+ const b2 = b[2];
57
+ const b3 = b[3];
58
+ const b4 = b[4];
59
+ const b5 = b[5];
60
+ const b6 = b[6];
61
+ const b7 = b[7];
62
+ const b8 = b[8];
63
+ out[0] = dotPrecise3(a0, b0, a3, b1, a6, b2);
64
+ out[1] = dotPrecise3(a1, b0, a4, b1, a7, b2);
65
+ out[2] = dotPrecise3(a2, b0, a5, b1, a8, b2);
66
+ out[3] = dotPrecise3(a0, b3, a3, b4, a6, b5);
67
+ out[4] = dotPrecise3(a1, b3, a4, b4, a7, b5);
68
+ out[5] = dotPrecise3(a2, b3, a5, b4, a8, b5);
69
+ out[6] = dotPrecise3(a0, b6, a3, b7, a6, b8);
70
+ out[7] = dotPrecise3(a1, b6, a4, b7, a7, b8);
71
+ out[8] = dotPrecise3(a2, b6, a5, b7, a8, b8);
72
+ return out;
73
+ }
74
+ /**
75
+ * Writes `m · diag(v)` into `out`: column `c` of `m` multiplied by `v[c]`.
76
+ * `out` may be `m`.
77
+ */
78
+ export function mat3x3ScaleColumns(out, m, v) {
79
+ const v0 = v[0];
80
+ const v1 = v[1];
81
+ const v2 = v[2];
82
+ out[0] = m[0] * v0;
83
+ out[1] = m[1] * v0;
84
+ out[2] = m[2] * v0;
85
+ out[3] = m[3] * v1;
86
+ out[4] = m[4] * v1;
87
+ out[5] = m[5] * v1;
88
+ out[6] = m[6] * v2;
89
+ out[7] = m[7] * v2;
90
+ out[8] = m[8] * v2;
91
+ return out;
92
+ }
93
+ /**
94
+ * Writes `diag(v) · m` into `out`: row `r` of `m` multiplied by `v[r]`. `out`
95
+ * may be `m`.
96
+ */
97
+ export function mat3x3ScaleRows(out, m, v) {
98
+ const v0 = v[0];
99
+ const v1 = v[1];
100
+ const v2 = v[2];
101
+ out[0] = m[0] * v0;
102
+ out[1] = m[1] * v1;
103
+ out[2] = m[2] * v2;
104
+ out[3] = m[3] * v0;
105
+ out[4] = m[4] * v1;
106
+ out[5] = m[5] * v2;
107
+ out[6] = m[6] * v0;
108
+ out[7] = m[7] * v1;
109
+ out[8] = m[8] * v2;
110
+ return out;
111
+ }
112
+ /** Writes the transpose of `m` into `out`. `out` may be `m`. */
113
+ export function mat3x3Transpose(out, m) {
114
+ const m1 = m[1];
115
+ const m2 = m[2];
116
+ const m3 = m[3];
117
+ const m5 = m[5];
118
+ const m6 = m[6];
119
+ const m7 = m[7];
120
+ out[0] = m[0];
121
+ out[1] = m3;
122
+ out[2] = m6;
123
+ out[3] = m1;
124
+ out[4] = m[4];
125
+ out[5] = m7;
126
+ out[6] = m2;
127
+ out[7] = m5;
128
+ out[8] = m[8];
129
+ return out;
130
+ }
131
+ /**
132
+ * The determinant, by the rule of Sarrus-style cofactor expansion along the
133
+ * first column, in plain arithmetic: the signed volume scale of the
134
+ * transformation.
135
+ */
136
+ export function mat3x3Determinant(m) {
137
+ const m0 = m[0];
138
+ const m1 = m[1];
139
+ const m2 = m[2];
140
+ const m3 = m[3];
141
+ const m4 = m[4];
142
+ const m5 = m[5];
143
+ const m6 = m[6];
144
+ const m7 = m[7];
145
+ const m8 = m[8];
146
+ return (m0 * (m4 * m8 - m7 * m5) -
147
+ m3 * (m1 * m8 - m7 * m2) +
148
+ m6 * (m1 * m5 - m4 * m2));
149
+ }
150
+ /**
151
+ * Writes the inverse of `m` into `out` and returns `true`. When `m` has no
152
+ * inverse a binary64 number can hold (the determinant is zero, an entry is not
153
+ * finite, or an entry of the inverse overflows) `out` is filled with `NaN` and
154
+ * the result is `false`: `true` always comes with a finite `out`, never a stale
155
+ * or partial matrix, and nothing throws. A matrix of very large or very small
156
+ * entries, whose determinant or cofactors overflow or underflow, is inverted
157
+ * all the same (`diag(1e-100)` gives `diag(1e100)`, and `diag(1e160, 1e160,
158
+ * 1e-160)` gives `diag(1e-160, 1e-160, 1e160)`), by scaling its rows and
159
+ * columns by powers of two first. Plain arithmetic (the adjugate over the
160
+ * determinant); when the cofactors of the scaled matrix still cancel to a zero
161
+ * determinant (`[[1, 1, 0], [1, 1, 2^-500], [0, 2^-500, 1]]`, of determinant
162
+ * `-2^-1000`), or a forward error bound of the expansion (Higham, "Accuracy and
163
+ * Stability of Numerical Algorithms", §3.1) does not keep the determinant
164
+ * within `2^-41` of itself and the cofactors within `2^-41 / 3` of the largest
165
+ * of them (a matrix nearly singular, or whose cofactors cancel to a rounding
166
+ * residue) unless the expansion rounded nowhere, LU factorisation with partial
167
+ * pivoting (Golub and Van Loan, §3.4), backward stable, inverts it. An
168
+ * elimination without rounding proves the matrix singular or invertible; one
169
+ * that rounds is settled in exact arithmetic, as is an inverse whose
170
+ * scaled-back entries overflow or come within their error bound of the overflow
171
+ * threshold (`diag(2^-1024, 4237, 1)`), and a matrix whose scaling rounds an
172
+ * entry (one more than about `2^1074` below the largest of its column once its
173
+ * row is scaled, `2^-200` in `[[2^900, 2^900, 0], [2^900, 2^900, 2^-200], [0,
174
+ * 2^900, 2^900]]`). So `false` is never a rounding artefact: `m` is singular,
175
+ * an entry is not finite, or an entry of its inverse, rounded once, is beyond
176
+ * `Number.MAX_VALUE`. `true` comes with an inverse backward stable for `m` with
177
+ * its rows and columns so scaled (a normwise backward error of about `2^-40`),
178
+ * whose entries far below the largest of that scaled inverse may carry no
179
+ * correct digit, as in any elimination. Allocates nothing unless the exact
180
+ * settlement runs: for a matrix of condition number beyond about `2^52`, an
181
+ * inverse that overflows, or an entry the scaling rounds. `out` may be `m`.
182
+ */
183
+ export function mat3x3Invert(out, m) {
184
+ return (invert_(out, m, false) || invertRescaled_(out, m, 3, invertDividing_, false));
185
+ }
186
+ /** {@link invert_} that divides by a subnormal determinant, for any layout. */
187
+ function invertDividing_(out, m) {
188
+ return invert_(out, m, true);
189
+ }
190
+ /**
191
+ * The inverse by the adjugate over the determinant, written to `out` only when
192
+ * every entry is finite. The reciprocal of the determinant multiplies the
193
+ * adjugate; when it overflows (a subnormal determinant) the adjugate is divided
194
+ * by the determinant instead, if `divide`, and not otherwise.
195
+ */
196
+ function invert_(out, m, divide) {
197
+ const m0 = m[0];
198
+ const m1 = m[1];
199
+ const m2 = m[2];
200
+ const m3 = m[3];
201
+ const m4 = m[4];
202
+ const m5 = m[5];
203
+ const m6 = m[6];
204
+ const m7 = m[7];
205
+ const m8 = m[8];
206
+ const c0 = m4 * m8 - m7 * m5;
207
+ const c1 = m7 * m2 - m1 * m8;
208
+ const c2 = m1 * m5 - m4 * m2;
209
+ const c3 = m6 * m5 - m3 * m8;
210
+ const c4 = m0 * m8 - m6 * m2;
211
+ const c5 = m3 * m2 - m0 * m5;
212
+ const c6 = m3 * m7 - m6 * m4;
213
+ const c7 = m6 * m1 - m0 * m7;
214
+ const c8 = m0 * m4 - m3 * m1;
215
+ const determinant = m0 * c0 + m3 * c1 + m6 * c2;
216
+ if (determinant === 0 || !Number.isFinite(determinant)) {
217
+ return false;
218
+ }
219
+ // A forward error bound of the expansion (Higham, "Accuracy and Stability
220
+ // of Numerical Algorithms", §3.1): a minor `x·y − z·w` is within
221
+ // 3u·(|x·y| + |z·w|) + η of its value, the determinant within
222
+ // 7u·(Σ|m|·that bound) + (Σ|m| + 2)·2η. The inverse is accepted when the
223
+ // determinant is within 2^-41 of itself and the minors within 2^-41 / 3 of
224
+ // the largest of them, a normwise backward error of at most about 2^-40.
225
+ // The permanents are first bounded by products of the column sums of |m|,
226
+ // and the largest minor below by the determinant over the first column's
227
+ // sum (expanded along that column); the bound is computed exactly only
228
+ // when that is not enough.
229
+ const a0 = Math.abs(m0);
230
+ const a1 = Math.abs(m1);
231
+ const a2 = Math.abs(m2);
232
+ const a3 = Math.abs(m3);
233
+ const a4 = Math.abs(m4);
234
+ const a5 = Math.abs(m5);
235
+ const a6 = Math.abs(m6);
236
+ const a7 = Math.abs(m7);
237
+ const a8 = Math.abs(m8);
238
+ const magnitude = Math.abs(determinant);
239
+ const s1 = a0 + a1 + a2;
240
+ const s2 = a3 + a4 + a5;
241
+ const s3 = a6 + a7 + a8;
242
+ const s12 = s1 * s2;
243
+ if (!(7 * UNIT_ROUNDOFF_ * (s12 * s3) <= ROUNDING_TOLERANCE_ * magnitude &&
244
+ 9 * UNIT_ROUNDOFF_ * (s12 + (s1 + s2) * s3) * s1 <=
245
+ ROUNDING_TOLERANCE_ * magnitude &&
246
+ s1 + s2 + s3 <= 2 ** 200 &&
247
+ magnitude >= 2 ** -400)) {
248
+ BOUND_INPUT_[0] = magnitude;
249
+ if (!isWithinTolerance3_(m) && !isCofactorExpansionExact_(m, 3)) {
250
+ return false;
52
251
  }
53
252
  }
253
+ const scale = 1 / determinant;
254
+ const reciprocal = Number.isFinite(scale);
255
+ if (!reciprocal && !divide) {
256
+ return false;
257
+ }
258
+ const o0 = reciprocal ? c0 * scale : c0 / determinant;
259
+ const o1 = reciprocal ? c1 * scale : c1 / determinant;
260
+ const o2 = reciprocal ? c2 * scale : c2 / determinant;
261
+ const o3 = reciprocal ? c3 * scale : c3 / determinant;
262
+ const o4 = reciprocal ? c4 * scale : c4 / determinant;
263
+ const o5 = reciprocal ? c5 * scale : c5 / determinant;
264
+ const o6 = reciprocal ? c6 * scale : c6 / determinant;
265
+ const o7 = reciprocal ? c7 * scale : c7 / determinant;
266
+ const o8 = reciprocal ? c8 * scale : c8 / determinant;
267
+ // Within INVERSE_LIMIT_, the exact entries, within about 2^-40 of the
268
+ // largest of these, round to finite numbers; past it, or not finite, the
269
+ // rescaled path decides.
270
+ if (!(Math.max(Math.abs(o0), Math.abs(o1), Math.abs(o2), Math.abs(o3), Math.abs(o4), Math.abs(o5), Math.abs(o6), Math.abs(o7), Math.abs(o8)) <= INVERSE_LIMIT_)) {
271
+ return false;
272
+ }
273
+ out[0] = o0;
274
+ out[1] = o1;
275
+ out[2] = o2;
276
+ out[3] = o3;
277
+ out[4] = o4;
278
+ out[5] = o5;
279
+ out[6] = o6;
280
+ out[7] = o7;
281
+ out[8] = o8;
54
282
  return true;
55
283
  }
56
284
  /**
57
- * Creates a 3×3 rotation matrix that rotates around the X axis by the given
58
- * angle in radians (right-hand rule).
285
+ * Writes the rotation by `angle` radians about the x axis (right-hand rule)
286
+ * into `out`.
59
287
  */
60
- export function mat3x3FromRotationX(angle) {
288
+ export function mat3x3FromRotationX(out, angle) {
61
289
  const cos = Math.cos(angle);
62
290
  const sin = Math.sin(angle);
63
- return [
64
- [1, 0, 0],
65
- [0, cos, -sin],
66
- [0, sin, cos],
67
- ];
291
+ out[0] = 1;
292
+ out[1] = 0;
293
+ out[2] = 0;
294
+ out[3] = 0;
295
+ out[4] = cos;
296
+ out[5] = sin;
297
+ out[6] = 0;
298
+ out[7] = -sin;
299
+ out[8] = cos;
300
+ return out;
68
301
  }
69
302
  /**
70
- * Creates a 3×3 rotation matrix that rotates around the Y axis by the given
71
- * angle in radians (right-hand rule).
303
+ * Writes the rotation by `angle` radians about the y axis (right-hand rule)
304
+ * into `out`.
72
305
  */
73
- export function mat3x3FromRotationY(angle) {
306
+ export function mat3x3FromRotationY(out, angle) {
74
307
  const cos = Math.cos(angle);
75
308
  const sin = Math.sin(angle);
76
- return [
77
- [cos, 0, sin],
78
- [0, 1, 0],
79
- [-sin, 0, cos],
80
- ];
309
+ out[0] = cos;
310
+ out[1] = 0;
311
+ out[2] = -sin;
312
+ out[3] = 0;
313
+ out[4] = 1;
314
+ out[5] = 0;
315
+ out[6] = sin;
316
+ out[7] = 0;
317
+ out[8] = cos;
318
+ return out;
81
319
  }
82
320
  /**
83
- * Creates a 3×3 rotation matrix that rotates around the Z axis by the given
84
- * angle in radians (right-hand rule).
321
+ * Writes the rotation by `angle` radians about the z axis (right-hand rule)
322
+ * into `out`.
85
323
  */
86
- export function mat3x3FromRotationZ(angle) {
324
+ export function mat3x3FromRotationZ(out, angle) {
87
325
  const cos = Math.cos(angle);
88
326
  const sin = Math.sin(angle);
89
- return [
90
- [cos, -sin, 0],
91
- [sin, cos, 0],
92
- [0, 0, 1],
93
- ];
94
- }
95
- export function mat3x3FromScale(sx, sy, sz) {
96
- return [
97
- [sx, 0, 0],
98
- [0, sy, 0],
99
- [0, 0, sz],
100
- ];
101
- }
102
- export function mat3x3Invert(matrix) {
103
- const det = mat3x3Determinant(matrix);
104
- if (det === 0) {
105
- throw new Error("Matrix is singular and cannot be inverted.");
327
+ out[0] = cos;
328
+ out[1] = sin;
329
+ out[2] = 0;
330
+ out[3] = -sin;
331
+ out[4] = cos;
332
+ out[5] = 0;
333
+ out[6] = 0;
334
+ out[7] = 0;
335
+ out[8] = 1;
336
+ return out;
337
+ }
338
+ /** Writes the scale `diag(scale)` into `out`. */
339
+ export function mat3x3FromScale(out, scale) {
340
+ out[0] = scale[0];
341
+ out[1] = 0;
342
+ out[2] = 0;
343
+ out[3] = 0;
344
+ out[4] = scale[1];
345
+ out[5] = 0;
346
+ out[6] = 0;
347
+ out[7] = 0;
348
+ out[8] = scale[2];
349
+ return out;
350
+ }
351
+ /**
352
+ * Writes the rotation by `angle` radians about the unit vector `axis`
353
+ * (right-hand rule, Rodrigues' formula) into `out`. `axis` is not normalised
354
+ * here: for any other length the matrix is not a rotation.
355
+ */
356
+ export function mat3x3FromAxisAngle(out, axis, angle) {
357
+ const x = axis[0];
358
+ const y = axis[1];
359
+ const z = axis[2];
360
+ const sin = Math.sin(angle);
361
+ // 1 − cos as twice the squared half-angle sine, and the diagonal as one
362
+ // minus its share: exact in the first digits for the small angles where
363
+ // cos − 1 would lose them, and exactly 1 on the axis itself.
364
+ const half = Math.sin(angle / 2);
365
+ const rest = 2 * half * half;
366
+ out[0] = 1 - rest * (y * y + z * z);
367
+ out[1] = rest * x * y + sin * z;
368
+ out[2] = rest * x * z - sin * y;
369
+ out[3] = rest * x * y - sin * z;
370
+ out[4] = 1 - rest * (x * x + z * z);
371
+ out[5] = rest * y * z + sin * x;
372
+ out[6] = rest * x * z + sin * y;
373
+ out[7] = rest * y * z - sin * x;
374
+ out[8] = 1 - rest * (x * x + y * y);
375
+ return out;
376
+ }
377
+ /**
378
+ * Writes the rotation of the unit quaternion `q` into `out`. `q` must be unit
379
+ * length; it is not normalised here.
380
+ */
381
+ export function mat3x3FromQuaternion(out, q) {
382
+ const x = q[0];
383
+ const y = q[1];
384
+ const z = q[2];
385
+ const w = q[3];
386
+ const xx = x * x;
387
+ const yy = y * y;
388
+ const zz = z * z;
389
+ const xy = x * y;
390
+ const xz = x * z;
391
+ const yz = y * z;
392
+ const wx = w * x;
393
+ const wy = w * y;
394
+ const wz = w * z;
395
+ out[0] = 1 - 2 * (yy + zz);
396
+ out[1] = 2 * (xy + wz);
397
+ out[2] = 2 * (xz - wy);
398
+ out[3] = 2 * (xy - wz);
399
+ out[4] = 1 - 2 * (xx + zz);
400
+ out[5] = 2 * (yz + wx);
401
+ out[6] = 2 * (xz + wy);
402
+ out[7] = 2 * (yz - wx);
403
+ out[8] = 1 - 2 * (xx + yy);
404
+ return out;
405
+ }
406
+ /** Writes the upper-left 3 × 3 block of `m`: its linear part, into `out`. */
407
+ export function mat3x3FromMat4x4(out, m) {
408
+ out[0] = m[0];
409
+ out[1] = m[1];
410
+ out[2] = m[2];
411
+ out[3] = m[4];
412
+ out[4] = m[5];
413
+ out[5] = m[6];
414
+ out[6] = m[8];
415
+ out[7] = m[9];
416
+ out[8] = m[10];
417
+ return out;
418
+ }
419
+ /** Writes the linear part of `transform`, without its translation, into `out`. */
420
+ export function mat3x3FromAffine3(out, transform) {
421
+ out[0] = transform[0];
422
+ out[1] = transform[1];
423
+ out[2] = transform[2];
424
+ out[3] = transform[3];
425
+ out[4] = transform[4];
426
+ out[5] = transform[5];
427
+ out[6] = transform[6];
428
+ out[7] = transform[7];
429
+ out[8] = transform[8];
430
+ return out;
431
+ }
432
+ /**
433
+ * Writes the 2D affine `transform` as the homogeneous 3 × 3 matrix `[a, c, e;
434
+ * b, d, f; 0, 0, 1]` into `out`: the bottom row, which an {@link Affine2} does
435
+ * not store, is `0, 0, 1`.
436
+ */
437
+ export function mat3x3FromAffine2(out, transform) {
438
+ out[0] = transform[0];
439
+ out[1] = transform[1];
440
+ out[2] = 0;
441
+ out[3] = transform[2];
442
+ out[4] = transform[3];
443
+ out[5] = 0;
444
+ out[6] = transform[4];
445
+ out[7] = transform[5];
446
+ out[8] = 1;
447
+ return out;
448
+ }
449
+ /**
450
+ * Turns the matrix in `out` into its inverse transpose `(A⁻¹)ᵀ`, the normal
451
+ * matrix, through {@link mat3x3Invert}: false with `NaN` throughout when there
452
+ * is no finite inverse.
453
+ */
454
+ function normalInPlace_(out) {
455
+ if (!mat3x3Invert(out, out)) {
456
+ return false;
106
457
  }
107
- const [[a, b, c], [d, e, f], [g, h, i]] = matrix;
108
- const adjugate = [
109
- [
110
- diffOfProducts(e, i, f, h),
111
- diffOfProducts(c, h, b, i),
112
- diffOfProducts(b, f, c, e),
113
- ],
114
- [
115
- diffOfProducts(f, g, d, i),
116
- diffOfProducts(a, i, c, g),
117
- diffOfProducts(c, d, a, f),
118
- ],
119
- [
120
- diffOfProducts(d, h, e, g),
121
- diffOfProducts(b, g, a, h),
122
- diffOfProducts(a, e, b, d),
123
- ],
124
- ];
125
- return mat3x3MultiplyScalar(adjugate, 1 / det);
126
- }
127
- export function mat3x3Multiply(a, b) {
128
- const columns = mat3x3Transpose(b);
129
- return [
130
- [
131
- dotPrecise(a[0], columns[0]),
132
- dotPrecise(a[0], columns[1]),
133
- dotPrecise(a[0], columns[2]),
134
- ],
135
- [
136
- dotPrecise(a[1], columns[0]),
137
- dotPrecise(a[1], columns[1]),
138
- dotPrecise(a[1], columns[2]),
139
- ],
140
- [
141
- dotPrecise(a[2], columns[0]),
142
- dotPrecise(a[2], columns[1]),
143
- dotPrecise(a[2], columns[2]),
144
- ],
145
- ];
146
- }
147
- export function mat3x3MultiplyCols(matrix, vector) {
148
- return [
149
- [
150
- matrix[0][0] * vector[0],
151
- matrix[0][1] * vector[1],
152
- matrix[0][2] * vector[2],
153
- ],
154
- [
155
- matrix[1][0] * vector[0],
156
- matrix[1][1] * vector[1],
157
- matrix[1][2] * vector[2],
158
- ],
159
- [
160
- matrix[2][0] * vector[0],
161
- matrix[2][1] * vector[1],
162
- matrix[2][2] * vector[2],
163
- ],
164
- ];
165
- }
166
- export function mat3x3MultiplyRows(matrix, vector) {
167
- return [
168
- [
169
- matrix[0][0] * vector[0],
170
- matrix[0][1] * vector[0],
171
- matrix[0][2] * vector[0],
172
- ],
173
- [
174
- matrix[1][0] * vector[1],
175
- matrix[1][1] * vector[1],
176
- matrix[1][2] * vector[1],
177
- ],
178
- [
179
- matrix[2][0] * vector[2],
180
- matrix[2][1] * vector[2],
181
- matrix[2][2] * vector[2],
182
- ],
183
- ];
184
- }
185
- export function mat3x3MultiplyScalar(matrix, scalar) {
186
- return [
187
- [matrix[0][0] * scalar, matrix[0][1] * scalar, matrix[0][2] * scalar],
188
- [matrix[1][0] * scalar, matrix[1][1] * scalar, matrix[1][2] * scalar],
189
- [matrix[2][0] * scalar, matrix[2][1] * scalar, matrix[2][2] * scalar],
190
- ];
191
- }
192
- export function mat3x3Negate(matrix) {
193
- return [
194
- [-matrix[0][0], -matrix[0][1], -matrix[0][2]],
195
- [-matrix[1][0], -matrix[1][1], -matrix[1][2]],
196
- [-matrix[2][0], -matrix[2][1], -matrix[2][2]],
197
- ];
198
- }
199
- export function mat3x3Sub(a, b) {
200
- return [
201
- [a[0][0] - b[0][0], a[0][1] - b[0][1], a[0][2] - b[0][2]],
202
- [a[1][0] - b[1][0], a[1][1] - b[1][1], a[1][2] - b[1][2]],
203
- [a[2][0] - b[2][0], a[2][1] - b[2][1], a[2][2] - b[2][2]],
204
- ];
205
- }
206
- export function mat3x3Transpose(matrix) {
207
- return [
208
- [matrix[0][0], matrix[1][0], matrix[2][0]],
209
- [matrix[0][1], matrix[1][1], matrix[2][1]],
210
- [matrix[0][2], matrix[1][2], matrix[2][2]],
211
- ];
458
+ const m1 = out[1];
459
+ const m2 = out[2];
460
+ const m5 = out[5];
461
+ out[1] = out[3];
462
+ out[2] = out[6];
463
+ out[5] = out[7];
464
+ out[3] = m1;
465
+ out[6] = m2;
466
+ out[7] = m5;
467
+ return true;
468
+ }
469
+ /**
470
+ * Writes the normal matrix of `m` into `out`: the inverse transpose of its
471
+ * upper-left 3 × 3 block, the matrix that carries surface normals through `m`
472
+ * (they stay perpendicular to the surface even under a non-uniform scale; scale
473
+ * the result to unit length afterwards). Returns `false` when that block has no
474
+ * finite inverse (it is singular, not finite, or the inverse overflows): `out`
475
+ * then holds `NaN` throughout, never a stale or partial matrix, and nothing
476
+ * throws. Plain arithmetic, as {@link mat3x3Invert}.
477
+ */
478
+ export function mat3x3NormalFromMat4x4(out, m) {
479
+ mat3x3FromMat4x4(out, m);
480
+ return normalInPlace_(out);
481
+ }
482
+ /**
483
+ * Writes the normal matrix of `transform` into `out`: the inverse transpose of
484
+ * its linear part, as {@link mat3x3NormalFromMat4x4}. Returns `false`, with
485
+ * `NaN` throughout, when the linear part has no finite inverse.
486
+ */
487
+ export function mat3x3NormalFromAffine3(out, transform) {
488
+ mat3x3FromAffine3(out, transform);
489
+ return normalInPlace_(out);
212
490
  }
213
491
  //# sourceMappingURL=mat3x3.js.map