@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.
- package/CHANGELOG.md +337 -0
- package/README.md +232 -16
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_invert.d.ts +214 -0
- package/dist/_invert.d.ts.map +1 -0
- package/dist/_invert.js +884 -0
- package/dist/_invert.js.map +1 -0
- package/dist/_slerp.d.ts +29 -0
- package/dist/_slerp.d.ts.map +1 -1
- package/dist/_slerp.js +103 -0
- package/dist/_slerp.js.map +1 -1
- package/dist/_strided-range.d.ts +23 -0
- package/dist/_strided-range.d.ts.map +1 -0
- package/dist/_strided-range.js +75 -0
- package/dist/_strided-range.js.map +1 -0
- package/dist/_trs.d.ts +38 -0
- package/dist/_trs.d.ts.map +1 -0
- package/dist/_trs.js +132 -0
- package/dist/_trs.js.map +1 -0
- package/dist/affine2-array.d.ts +53 -0
- package/dist/affine2-array.d.ts.map +1 -0
- package/dist/affine2-array.js +107 -0
- package/dist/affine2-array.js.map +1 -0
- package/dist/affine2-componentwise.d.ts +28 -0
- package/dist/affine2-componentwise.d.ts.map +1 -0
- package/dist/affine2-componentwise.js +66 -0
- package/dist/affine2-componentwise.js.map +1 -0
- package/dist/affine2.d.ts +156 -0
- package/dist/affine2.d.ts.map +1 -0
- package/dist/affine2.js +437 -0
- package/dist/affine2.js.map +1 -0
- package/dist/affine3-array.d.ts +53 -0
- package/dist/affine3-array.d.ts.map +1 -0
- package/dist/affine3-array.js +137 -0
- package/dist/affine3-array.js.map +1 -0
- package/dist/affine3-componentwise.d.ts +32 -0
- package/dist/affine3-componentwise.d.ts.map +1 -0
- package/dist/affine3-componentwise.js +100 -0
- package/dist/affine3-componentwise.js.map +1 -0
- package/dist/affine3.d.ts +205 -0
- package/dist/affine3.d.ts.map +1 -0
- package/dist/affine3.js +792 -0
- package/dist/affine3.js.map +1 -0
- package/dist/checked/index.d.ts +35 -0
- package/dist/checked/index.d.ts.map +1 -0
- package/dist/checked/index.js +58 -0
- package/dist/checked/index.js.map +1 -0
- package/dist/euler.d.ts +65 -0
- package/dist/euler.d.ts.map +1 -0
- package/dist/euler.js +217 -0
- package/dist/euler.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -1
- package/dist/kl-activations.d.ts.map +1 -1
- package/dist/kl-activations.js +4 -3
- package/dist/kl-activations.js.map +1 -1
- package/dist/mat2x2-array.d.ts +36 -0
- package/dist/mat2x2-array.d.ts.map +1 -0
- package/dist/mat2x2-array.js +71 -0
- package/dist/mat2x2-array.js.map +1 -0
- package/dist/mat2x2-componentwise.d.ts +86 -0
- package/dist/mat2x2-componentwise.d.ts.map +1 -0
- package/dist/mat2x2-componentwise.js +199 -0
- package/dist/mat2x2-componentwise.js.map +1 -0
- package/dist/mat2x2.d.ts +79 -25
- package/dist/mat2x2.d.ts.map +1 -1
- package/dist/mat2x2.js +192 -93
- package/dist/mat2x2.js.map +1 -1
- package/dist/mat3x3-array.d.ts +36 -0
- package/dist/mat3x3-array.d.ts.map +1 -0
- package/dist/mat3x3-array.js +91 -0
- package/dist/mat3x3-array.js.map +1 -0
- package/dist/mat3x3-componentwise.d.ts +86 -0
- package/dist/mat3x3-componentwise.d.ts.map +1 -0
- package/dist/mat3x3-componentwise.js +299 -0
- package/dist/mat3x3-componentwise.js.map +1 -0
- package/dist/mat3x3.d.ts +153 -48
- package/dist/mat3x3.d.ts.map +1 -1
- package/dist/mat3x3.js +470 -192
- package/dist/mat3x3.js.map +1 -1
- package/dist/mat4x4-array.d.ts +46 -0
- package/dist/mat4x4-array.d.ts.map +1 -0
- package/dist/mat4x4-array.js +133 -0
- package/dist/mat4x4-array.js.map +1 -0
- package/dist/mat4x4-componentwise.d.ts +87 -0
- package/dist/mat4x4-componentwise.d.ts.map +1 -0
- package/dist/mat4x4-componentwise.js +434 -0
- package/dist/mat4x4-componentwise.js.map +1 -0
- package/dist/mat4x4.d.ts +221 -67
- package/dist/mat4x4.d.ts.map +1 -1
- package/dist/mat4x4.js +972 -334
- package/dist/mat4x4.js.map +1 -1
- package/dist/projection.d.ts +113 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +418 -0
- package/dist/projection.js.map +1 -0
- package/dist/quaternion-array.d.ts +47 -0
- package/dist/quaternion-array.d.ts.map +1 -0
- package/dist/quaternion-array.js +86 -0
- package/dist/quaternion-array.js.map +1 -0
- package/dist/quaternion.d.ts +106 -22
- package/dist/quaternion.d.ts.map +1 -1
- package/dist/quaternion.js +253 -182
- package/dist/quaternion.js.map +1 -1
- package/dist/transform.d.ts +220 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +412 -0
- package/dist/transform.js.map +1 -0
- package/dist/tridiagonal.d.ts +52 -0
- package/dist/tridiagonal.d.ts.map +1 -0
- package/dist/tridiagonal.js +90 -0
- package/dist/tridiagonal.js.map +1 -0
- package/dist/upload.d.ts +55 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +129 -0
- package/dist/upload.js.map +1 -0
- package/dist/vec2-array-componentwise.d.ts +207 -0
- package/dist/vec2-array-componentwise.d.ts.map +1 -0
- package/dist/vec2-array-componentwise.js +472 -0
- package/dist/vec2-array-componentwise.js.map +1 -0
- package/dist/vec2-array.d.ts +53 -0
- package/dist/vec2-array.d.ts.map +1 -0
- package/dist/vec2-array.js +123 -0
- package/dist/vec2-array.js.map +1 -0
- package/dist/vec2-componentwise.d.ts +311 -0
- package/dist/vec2-componentwise.d.ts.map +1 -0
- package/dist/vec2-componentwise.js +542 -0
- package/dist/vec2-componentwise.js.map +1 -0
- package/dist/vec2.d.ts +97 -82
- package/dist/vec2.d.ts.map +1 -1
- package/dist/vec2.js +121 -216
- package/dist/vec2.js.map +1 -1
- package/dist/vec3-array-componentwise.d.ts +207 -0
- package/dist/vec3-array-componentwise.d.ts.map +1 -0
- package/dist/vec3-array-componentwise.js +510 -0
- package/dist/vec3-array-componentwise.js.map +1 -0
- package/dist/vec3-array.d.ts +102 -0
- package/dist/vec3-array.d.ts.map +1 -0
- package/dist/vec3-array.js +299 -0
- package/dist/vec3-array.js.map +1 -0
- package/dist/vec3-componentwise.d.ts +310 -0
- package/dist/vec3-componentwise.d.ts.map +1 -0
- package/dist/vec3-componentwise.js +594 -0
- package/dist/vec3-componentwise.js.map +1 -0
- package/dist/vec3.d.ts +177 -87
- package/dist/vec3.d.ts.map +1 -1
- package/dist/vec3.js +391 -272
- package/dist/vec3.js.map +1 -1
- package/dist/vec4-array-componentwise.d.ts +198 -0
- package/dist/vec4-array-componentwise.d.ts.map +1 -0
- package/dist/vec4-array-componentwise.js +521 -0
- package/dist/vec4-array-componentwise.js.map +1 -0
- package/dist/vec4-array.d.ts +12 -0
- package/dist/vec4-array.d.ts.map +1 -0
- package/dist/vec4-array.js +43 -0
- package/dist/vec4-array.js.map +1 -0
- package/dist/vec4-componentwise.d.ts +299 -0
- package/dist/vec4-componentwise.d.ts.map +1 -0
- package/dist/vec4-componentwise.js +637 -0
- package/dist/vec4-componentwise.js.map +1 -0
- package/dist/vec4.d.ts +59 -53
- package/dist/vec4.d.ts.map +1 -1
- package/dist/vec4.js +74 -249
- package/dist/vec4.js.map +1 -1
- package/dist/view-basis.d.ts +15 -0
- package/dist/view-basis.d.ts.map +1 -0
- package/dist/view-basis.js +29 -0
- package/dist/view-basis.js.map +1 -0
- package/dist/viewport.d.ts +114 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +250 -0
- package/dist/viewport.js.map +1 -0
- package/package.json +19 -9
package/dist/mat3x3.js
CHANGED
|
@@ -1,213 +1,491 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
[0
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
*
|
|
58
|
-
*
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
*
|
|
71
|
-
*
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
*
|
|
84
|
-
*
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
];
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
export function
|
|
128
|
-
|
|
129
|
-
return
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|