@ac-kit/math-linear 0.2.1 → 0.3.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 +257 -0
  2. package/README.md +229 -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 +19 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +48 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +15 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +47 -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 +144 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +375 -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 +192 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +718 -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 +67 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +150 -94
  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 +131 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +434 -193
  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 +195 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +849 -335
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +98 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +369 -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 +104 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +294 -181
  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 +189 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +424 -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 +189 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +458 -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 +366 -273
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +155 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +401 -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 +57 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +94 -246
  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 +18 -9
package/CHANGELOG.md CHANGED
@@ -1,5 +1,262 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 34e8cfa: Buffers of vectors, matrices and transforms: a `Float64Array` of `count × N`
8
+ numbers, element `i` at `[i·N, i·N + N)`, handled by dedicated kernels instead
9
+ of a load, compute and store loop per element. The value types and their
10
+ functions are unchanged; a buffer is never an argument of a value function and
11
+ a value is never one of a kernel.
12
+
13
+ - **Boundary, for every value type with a buffer** (`vec2/3/4`, `mat2x2`,
14
+ `mat3x3`, `mat4x4`, `affine2`, `affine3`, `quaternion`): `…Load(out, array,
15
+ index)` and `…Store(array, index, value)` copy element `index`;
16
+ `…LoadStrided` and `…StoreStrided` take `(offset, stride)` in numbers for
17
+ interleaved data; `…ArrayIsFinite(array)`; `…ArrayCreate(count)` (vectors,
18
+ `mat4x4`, `affine2`, `affine3`, `quaternion`). `…Load` past the end of the
19
+ array writes `NaN` and never `undefined`; `…Store` past the end writes
20
+ nothing. `affine2StoreFloat32` and `affine3StoreFloat32` keep their
21
+ signatures.
22
+ - **Vector kernels** (`vec2/3/4`): `…ArrayAdd`, `…ArraySubtract`,
23
+ `…ArrayMultiplyScalar`, `…ArrayAddScaled`, `…ArrayTranslate`, `…ArrayLerp`,
24
+ `…ArrayNormalize`, `…ArrayMean`, `…ArrayWeightedMean` and
25
+ `…ArrayWeightedMeanPrecise` (exact sums through `PreciseSum`, for products
26
+ between about `1e−290` and `1e300`: the replacement of 0.2.1's
27
+ `vecNWeightedMean(items, get)`), `…ArrayFromValues` and `…ArrayToValues`.
28
+ `vec2`/`vec3` add `…ArrayMeanDistance` and `…ArrayNarrowRelative`; `vec2` adds
29
+ `vec2ArrayFindNearestXy`, `vec3` `vec3ArrayFindNearest`. Transforms:
30
+ `vec2ArrayTransformAffine2`, `vec2ArrayTransformMat3x3`,
31
+ `vec3ArrayTransformMat3x3`, `…TransformNormal` (transform and normalise in
32
+ one pass), `…TransformAffine3`, `…TransformDirectionAffine3`,
33
+ `…TransformMat4x4` (divides by `w`) and `vec4ArrayTransformMat4x4`.
34
+ - **Interleaved and f32.** `…Strided` kernels take `(out, outOffset,
35
+ outStride, input, inputOffset, inputStride, count, …)` in numbers:
36
+ `vec2ArrayTransformAffine2Strided`, `vec3ArrayTransformAffine3Strided`,
37
+ `vec3ArrayTransformMat4x4Strided`. `…ArrayNarrowStrided` writes a tight
38
+ binary64 buffer into an interleaved `Float32Array`, `…ArrayWidenStrided` reads
39
+ one back, and `…ArrayNarrowRelative(Strided)` subtracts an origin before
40
+ narrowing, so large coordinates keep their precision. No kernel computes on a
41
+ `Float32Array`; whole-array narrowing is `Float32Array.prototype.set`.
42
+ - **Contract.** The element count is `input.length / N`. A kernel checks once,
43
+ in O(1), that its lengths agree and are a multiple of `N` (strided ones that
44
+ `count`, the offsets and the strides are non-negative integers, the strides at
45
+ least `N` from two elements on, and the elements fit; `out` and `input` may
46
+ overlap only as the same elements at the same offset and stride; narrowing and
47
+ widening need views that share no memory) and throws a `RangeError` before
48
+ writing anything; there is no per-element check. `out` may be the input array
49
+ itself, not a partially overlapping view of it. `…ArrayCreate(count)` takes a
50
+ non-negative integer. Empty buffers are fine; means of nothing are `NaN`.
51
+ - 34e8cfa: `Quaternion` is now the branded `[x, y, z, w]` tuple of `@ac-kit/math-complex`
52
+ and `@ac-kit/math-linear` re-exports its quaternion functions
53
+ (`quaternionOf`, `quaternionMultiply`, `quaternionInvert`, …), all out first. An
54
+ array literal no longer type-checks as a `Quaternion`: build one with
55
+ `quaternionOf(x, y, z, w)`. The rotation functions write such values. `quaternionNlerp` and `quaternionSlerp` take the
56
+ shorter side by the plain dot product's sign, and by the exact sign where the
57
+ plain one is within its rounding error, so two rotations whose dot product
58
+ cancels to a few ulp, even to `−2⁻¹²⁰`, no longer interpolate the long way
59
+ round.
60
+ - 34e8cfa: Vectors, matrices and the other value types are flat, branded arrays made by
61
+ creators, and every function takes `out` first. This part covers the vectors,
62
+ the value types, the matrices, the affine and TRS forms, the rotations, the
63
+ projections and views, the viewport and the uploads; the buffer kernels follow.
64
+
65
+ - **Values.** `Vec2`/`Vec3`/`Vec4`, `Mat2x2`/`Mat3x3`/`Mat4x4` (flat,
66
+ **column-major**: the entry of row `r`, column `c` of an `N×N` matrix is at
67
+ `N·c + r`), `Affine2`, `Affine3`, `Transform2`, `Transform3` and `Viewport`
68
+ are arrays of numbers with a type-level brand. An array literal, a
69
+ `number[]` or a `Float64Array` no longer type-checks: build one with
70
+ `vec3Create()`, `vec3Of(x, y, z)`, `vec3Clone(v)` or `vec3FromArray(out,
71
+ source, offset?)` (the matrices add `mat4x4OfRows(…)`, rows as written on
72
+ paper). Every creator yields a V8 `PACKED_DOUBLE_ELEMENTS` array, integers
73
+ included. `…FromArray` is the checked way in from JSON or messages: an
74
+ element that is not a number, or lies past `source.length`, is stored as
75
+ `NaN`. `…IsFinite` checks a value after parsing.
76
+ - **Operations.** Each takes the destination first, writes it, returns it,
77
+ allocates nothing and accepts any of its inputs as `out`
78
+ (`vec3Add(a, a, b)`). They never throw: normalising the zero vector gives
79
+ zero, `…Invert` of a matrix with no finite inverse (singular, a non-finite
80
+ entry, or an inverse beyond the range of binary64) returns `false` and fills
81
+ `out` with `NaN`, never `true` with a non-finite result (a matrix of one huge
82
+ or tiny magnitude, `diag(1e-150)` or `diag(1e150)`, is scaled by a power of
83
+ two first and inverted), a perspective divide by zero gives non-finite
84
+ components.
85
+ - **Plain arithmetic.** Sums, dots, lengths and products are rounded as written,
86
+ so results can differ from 0.2.1 in the last bits. The compensated forms are
87
+ separate: `…DotPrecise`, `vec2CrossPrecise`, `vec3CrossPrecise`,
88
+ `…TransformMat…Precise`, `mat2x2/3x3/4x4MultiplyPrecise`. The compensated
89
+ forms compute what math-scalar's `sumOfProducts`, `dotPrecise3` and
90
+ `dotPrecise4` do, so a sum that overflows on the way to a finite result is
91
+ still found. The lengths and normalisations are right between about `1e-150`
92
+ and `1e150`; beyond that, `…LengthScaled` and `…NormalizeScaled`. Each
93
+ function that squares components (`…Length`, `…Distance`, `…ClampLength`,
94
+ `…Project`, `…Reject`, `vec3AngleTo`, `…Slerp`) states its range and the
95
+ scaled form to use in its documentation.
96
+ - **Boundary.** `…Load`, `…Store`, `…LoadStrided`, `…StoreStrided`,
97
+ `…StoreFloat32` and `…ArrayIsFinite` move values between a `Float64Array` and
98
+ a vector or matrix. A `Float64Array` is never an argument of a value function.
99
+ - **Vectors in full**: component-wise arithmetic (`Add`, `Subtract`,
100
+ `Multiply`, `Divide`, scalar forms, `AddScaled`, `Negate`, `Abs`, `Round`,
101
+ `Min`, `Max`, `Clamp`, `Lerp`), `Equals`/`IsClose`, `CompareLexicographic`,
102
+ dot, cross, lengths and distances (Euclidean, squared, Manhattan, Chebyshev),
103
+ `Normalize`, `ClampLength`, `Project`, `Reject`, `Reflect`, angles
104
+ (`vec2Angle`, `…AngleTo`, `…AngleBetween`, in `(−π, π]`), `…Slerp` (constant
105
+ angular speed, also when `t` leaves `[0, 1]`, and in the plane of the two
106
+ vectors however close to opposite they are),
107
+ `vec3RotateAxisAngle`, `vec3Perpendicular`, `vec3OrthonormalBasis`,
108
+ `…Random`, and the transforms by matrices, affine forms and quaternions
109
+ (`vec3TransformMat4x4`, `vec3TransformDirectionMat4x4`,
110
+ `vec3TransformAffine3`, `vec3TransformQuaternion`, …).
111
+ - **Matrices.** The 0.2.1 operations on the flat layout: `Multiply`,
112
+ `ScaleColumns`, `ScaleRows`, `Transpose`, `Determinant`, `Invert`, and the
113
+ builders `mat2x2FromRotation`, `mat3x3FromRotationX/Y/Z`,
114
+ `mat4x4FromTranslation`, `FromScale`, `FromRotationX/Y/Z`,
115
+ `FromQuaternion`, `Compose`. Their element-wise families (`Add`, `Subtract`,
116
+ `MultiplyScalar`, `Negate`, `Equals`, `IsClose`, creators and setters) are
117
+ new. New builders: `mat3x3FromQuaternion`, `mat3x3FromAxisAngle`,
118
+ `mat4x4FromAxisAngle`, `…FromMat3x3`, `…FromMat4x4`, `…FromAffine2`,
119
+ `…FromAffine3`; the normal matrices `mat3x3NormalFromMat4x4` and
120
+ `mat3x3NormalFromAffine3`; `mat4x4Decompose` (the inverse of `Compose`),
121
+ `mat4x4MultiplyAffine3`, `mat4x4Translate`, `mat4x4GetTranslation`,
122
+ `mat4x4GetScale`, `mat4x4GetColumn`.
123
+ - **Affine and TRS.** `Affine2` (Canvas's `a, b, c, d, e, f`) and `Affine3` (four
124
+ columns of three, a WGSL `mat4x3f`) with `Multiply`, `Invert` (`false`, with
125
+ `NaN`, for a singular transform or an inverse that does not fit in binary64),
126
+ `Determinant`, the builders (`FromTranslation`, `FromScale`, `FromRotation…`,
127
+ `FromScaleAbout`, `FromRotationAbout`, `FromQuaternion`, `Compose`,
128
+ `affine2Orthographic`), `Translate`/`Rotate`/`Scale` in the local space (as
129
+ Canvas's), `affine3Decompose` and `Equals`/`IsClose`. `Transform2` and
130
+ `Transform3` are translate-rotate-scale records (glTF nodes, animation keys)
131
+ with `Lerp` (exact at both ends; the angle by the shorter arc, the rotation
132
+ by slerp) and conversions to and from the affine forms. Every decomposition
133
+ returns `false`, with `NaN` in its outputs, for a zero scale, a shear (a
134
+ cosine of more than about `1e-6` between two columns) or non-finite input; a
135
+ reflection puts its sign in a scale.
136
+ - **Rotations and Euler angles.** `quaternionFromAxisAngle`, `quaternionAxis`,
137
+ `quaternionAngle`, `quaternionFromVec3ToVec3`, `quaternionFromMat3x3`,
138
+ `quaternionFromMat4x4`, `quaternionRotateX/Y/Z`, `quaternionSlerp`,
139
+ `quaternionNlerp`, `quaternionAngleBetween`. Euler angles in all twelve orders
140
+ (`EulerOrder`), intrinsic, the angles a `Vec3` in the order's sequence:
141
+ `quaternionFromEuler`, `eulerFromQuaternion`, `mat3x3FromEuler`,
142
+ `eulerFromMat3x3`. The first and third angles are in `(−π, π]`; at gimbal
143
+ lock (within `1e-10` rad) the third is `0`.
144
+ - **Projections and views.** `mat4x4Perspective`, `mat4x4Frustum` and
145
+ `mat4x4Orthographic` take the depth range as a required last argument,
146
+ `"zero-to-one"` (WebGPU), `"negative-one-to-one"` (OpenGL) or `"one-to-zero"`
147
+ (reversed depth); the perspective and frustum builders accept `far =
148
+ Infinity` (an orthographic `far` must be finite); `mat4x4LookAt` and
149
+ `affine3TargetTo` build the view and the camera's world transform (right-handed,
150
+ forward along −Z, axes normalised at any magnitude, an orthonormal basis to the
151
+ last bit). They are the builders called once per frame, so they throw a
152
+ `RangeError` for an invalid frustum or view (an `up` vector parallel to the
153
+ view direction to within the rounding of a cross product, a projection scale
154
+ that underflows to zero) and leave `out` untouched. `DepthRange` is the type
155
+ of the argument.
156
+ - **Viewport.** `vec2NdcToViewport`, `vec2ViewportToNdc` (pixels top-left, `y`
157
+ down; NDC `y` up), `vec3ProjectToViewport` (`NaN` on or behind the eye),
158
+ `vec3UnprojectFromViewport` and the bulk kernel `vec3ArrayProjectToViewport`.
159
+ - **Uploads and the checked layer.** `mat2x2/3x3/4x4`, `affine2/3` and
160
+ `quaternion` `…StoreFloat32` (unrolled); the uniform-block forms
161
+ `mat3x3StoreStd140Float32`, `affine3StoreStd140Float32` and
162
+ `affine2StoreStd140Float32` write every float of the field, padding as `0`.
163
+ The new `@ac-kit/math-linear/checked` subpath exports `assertFiniteValue` and
164
+ `assertFiniteBuffer`, which throw a `RangeError` naming the argument and the
165
+ first bad component.
166
+
167
+ Migration. A literal `[1, 2, 3]` becomes `vec3Of(1, 2, 3)`, and a nested matrix
168
+ `[[a, b], [c, d]]` becomes `mat2x2OfRows(a, b, c, d)`. Results are written into
169
+ the first argument: `vec3Add(a, b)` becomes `vec3Add(vec3Create(), a, b)`, or
170
+ reuse an `out`. Renamed: `…Sub` → `…Subtract`; `…SubScalar` →
171
+ `…AddScalar` with a negated scalar; `…SubScaled` → `…AddScaled` with a negated
172
+ scalar; `…LengthSq` → `…LengthSquared`; `…DistanceTo` → `…Distance`;
173
+ `…DistanceToSq` → `…DistanceSquared`; `vec2ApplyMat2` → `vec2TransformMat2x2`;
174
+ `vec2ApplyMat3` → `vec2TransformMat3x3`; `vec3ApplyMat3` →
175
+ `vec3TransformMat3x3`; `vec3ApplyMat4` → `vec3TransformMat4x4`;
176
+ `vec4ApplyMat4` → `vec4TransformMat4x4`; `vec3ApplyQuaternion` →
177
+ `vec3TransformQuaternion`; `vec3RotateAroundAxis` → `vec3RotateAxisAngle`;
178
+ `vec4PerspectiveDivide` → `vec3FromHomogeneous`; `matNxNMultiplyRows` and
179
+ `…MultiplyCols` → `…ScaleRows` and `…ScaleColumns`; `matNxNSub` →
180
+ `matNxNSubtract`. `vecNMinmax` and `vecNWeightedMean` are removed (the bounds
181
+ come with the box functions, the mean with the buffer functions). `DistanceType`
182
+ is removed: the dedicated `…Distance`, `…DistanceManhattan` and
183
+ `…DistanceChebyshev` replace it. `mat2x2Invert`, `mat3x3Invert` and
184
+ `mat4x4Invert` return `boolean` and write the inverse into `out`, in place of a
185
+ nested result or an exception. Code that stored a matrix in a nested tuple
186
+ indexes `m[column * N + row]` now. Rotations: `quaternionToRotationMatrix(q)`
187
+ and `mat4x4FromQuaternion(q)` → `mat4x4FromQuaternion(out, q)` (or
188
+ `mat3x3FromQuaternion`); `quaternionFromRotationMatrix(m)` →
189
+ `quaternionFromMat4x4(out, m)` or `quaternionFromMat3x3`; `quaternionLerp` is
190
+ `quaternionNlerp` and now **normalises** its result; `quaternionAxis(q)` →
191
+ `quaternionAxis(out, q)`, which no longer returns `[1, 0, 0]` for every rotation
192
+ under about `0.1°` and flips with the sign of the scalar part;
193
+ `quaternionAngle` is in `[0, π]` (it was `2·acos(w)`, up to `2π`);
194
+ `quaternionFromAxisAngle`, `quaternionFromVec3ToVec3` and `quaternionSlerp` take
195
+ `out` first, and `quaternionFromVec3ToVec3` no longer rounds an arc under about
196
+ `1.4e-5` rad to the identity. The `~acKitMathType` property of the brand
197
+ is reserved.
198
+ - 34e8cfa: Build a view matrix straight from a camera's axes with
199
+ `mat4x4ViewFromBasis(out, right, up, back, eye)`: the rows are the axes and the
200
+ translation is `−axis · eye`. It takes the axes as given and never throws, so
201
+ it is exact for a camera looking straight up or down, where `mat4x4LookAt` has
202
+ no roll to take from `up`.
203
+ - 34e8cfa: New: `tridiagonalSolve(out, lower, diagonal, upper, right, scratch)` solves a
204
+ tridiagonal system in O(n) without allocating (the Thomas algorithm). `lower`
205
+ and `upper` hold the `n − 1` off-diagonal entries, `scratch` is `n − 1` numbers
206
+ of working space (it may be `upper` itself), and `out` may be `right`. It
207
+ returns `false`, with `out` filled with `NaN`, when the elimination meets a zero
208
+ pivot (the only test on the values, so it wins over a `NaN` elsewhere);
209
+ otherwise non-finite values propagate and it returns `true`. It is stable
210
+ without pivoting for diagonally dominant or symmetric positive definite
211
+ matrices, the systems of splines and finite differences, while the entries lie
212
+ within about [1e−150, 1e150]. A `RangeError` reports lengths that disagree.
213
+
214
+ ### Patch Changes
215
+
216
+ - 34e8cfa: An error message that quotes a numeric argument describes it with
217
+ `@ac-kit/core`'s `describeValue`, so building the message can no longer throw
218
+ on a symbol. The text changes: `-0` keeps its sign (`got -0`), a bigint shows
219
+ its digits (a digit-count bound when it is too large to show) and any other
220
+ kind of value is named by its kind. Affected:
221
+ the argument checks of `factorial`, the biquad, window, DCT, FFT size,
222
+ log-frequency map, spectral flux, level meter and novelty peak picker
223
+ functions, every probability distribution's parameters and `quantile`,
224
+ `normalQuantile`, `chiSquaredTest`, `confidenceInterval`, `histogram`,
225
+ `SlidingMedian`, the incomplete beta and gamma functions (and their inverses),
226
+ `trapezoidalRule`, the decimal and rational formatting digit counts and radix,
227
+ the fixed-width integer bit width and shift count, `linspace`, `quantize`,
228
+ `bigIntDigitAt`, `bigIntDigitCount`, `sampleRamp` and `KlActivations`.
229
+ - Updated dependencies [34e8cfa]
230
+ - Updated dependencies [34e8cfa]
231
+ - Updated dependencies [34e8cfa]
232
+ - Updated dependencies [34e8cfa]
233
+ - Updated dependencies [34e8cfa]
234
+ - Updated dependencies [34e8cfa]
235
+ - Updated dependencies [34e8cfa]
236
+ - Updated dependencies [34e8cfa]
237
+ - Updated dependencies [34e8cfa]
238
+ - Updated dependencies [34e8cfa]
239
+ - Updated dependencies [34e8cfa]
240
+ - Updated dependencies [34e8cfa]
241
+ - Updated dependencies [34e8cfa]
242
+ - Updated dependencies [34e8cfa]
243
+ - Updated dependencies [34e8cfa]
244
+ - Updated dependencies [34e8cfa]
245
+ - Updated dependencies [34e8cfa]
246
+ - Updated dependencies [34e8cfa]
247
+ - Updated dependencies [34e8cfa]
248
+ - Updated dependencies [34e8cfa]
249
+ - Updated dependencies [34e8cfa]
250
+ - Updated dependencies [34e8cfa]
251
+ - Updated dependencies [34e8cfa]
252
+ - Updated dependencies [34e8cfa]
253
+ - Updated dependencies [34e8cfa]
254
+ - Updated dependencies [34e8cfa]
255
+ - Updated dependencies [34e8cfa]
256
+ - @ac-kit/core@0.12.0
257
+ - @ac-kit/math-scalar@0.3.0
258
+ - @ac-kit/math-complex@0.2.0
259
+
3
260
  ## 0.2.1
4
261
 
5
262
  ### Patch Changes
package/README.md CHANGED
@@ -6,8 +6,9 @@ fixed, small dimension.
6
6
  ## Scope
7
7
 
8
8
  The fixed-dimension types that 2D and 3D work needs — `Vec2`/`Vec3`/`Vec4`,
9
- `Mat2x2`/`Mat3x3`/`Mat4x4`, `Quaternion` — represented as plain tuples and
10
- operated on by free functions.
9
+ `Mat2x2`/`Mat3x3`/`Mat4x4`, `Affine2`/`Affine3`, `Transform2`/`Transform3`,
10
+ `Viewport`, `Quaternion` — represented as flat arrays of numbers carrying a
11
+ type-level brand, and operated on by free functions.
11
12
 
12
13
  Dimension is part of the type, not a runtime property. That is what makes the
13
14
  operations inlineable and allocation-predictable, and it is why an arbitrary
@@ -15,24 +16,143 @@ operations inlineable and allocation-predictable, and it is why an arbitrary
15
16
 
16
17
  ## Contents
17
18
 
18
- | Module | Contents |
19
- | ---------------------------- | -------------------------------------------------------------------------------------------------- |
20
- | `vec2`, `vec3`, `vec4` | Component-wise arithmetic, dot, cross, length, normalisation, interpolation, matrix application |
21
- | `mat2x2`, `mat3x3`, `mat4x4` | Multiplication, determinant, inverse, transpose, transform construction |
22
- | `kl-activations` | `KlActivations`: KL-divergence NMF activations of a sparse fixed dictionary, with per-column L1 penalties, iterated across calls |
23
- | `quaternion` | Rotation composition, slerp, conversion to and from axis-angle, Euler angles and rotation matrices |
19
+ | Module | Contents |
20
+ | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
21
+ | `vec2`, `vec3`, `vec4` | Creators, component-wise arithmetic, dot, cross, length, distance, normalisation, interpolation, angles, projection, matrix and quaternion transforms, boundary I/O |
22
+ | `mat2x2`, `mat3x3`, `mat4x4` | Creators, multiplication, determinant, inverse, transpose, the builders from rotations, scales, translations, quaternions, axis and angle, affine transforms, compose and decompose, normal matrices |
23
+ | `affine2`, `affine3` | 2D (`a…f` of Canvas and SVG) and 3D (a WGSL `mat4x3f`) affine transforms: multiply, invert, builders, translate/rotate/scale in the local space, compose and decompose |
24
+ | `transform` | `Transform2`, `Transform3`: translate-rotate-scale records (glTF nodes, animation keys), their interpolation and their conversion to and from the affine forms |
25
+ | `quaternion` | Rotations: axis and angle, shortest arc between two directions, matrices, `slerp` and `nlerp`, rotation about an axis, angle between rotations |
26
+ | `euler` | Euler angles in the twelve intrinsic orders, to and from quaternions and matrices |
27
+ | `projection` | `mat4x4Perspective`, `Frustum`, `Orthographic` (three depth ranges, an infinite far plane), `mat4x4LookAt`, `affine3TargetTo` |
28
+ | `view-basis` | `mat4x4ViewFromBasis`: a view matrix from a camera's right, up and back axes and its eye (exact looking straight up or down) |
29
+ | `viewport` | Pixels (top left, y down) and normalised device coordinates (y up): `vec3ProjectToViewport`, `vec3UnprojectFromViewport`, a bulk kernel |
30
+ | `vec2-array`, `vec3-array`, `vec4-array` | Buffers of vectors (a `Float64Array` of `count × N`): fused kernels (arithmetic, lerp, normalise, means, transforms by matrices and affine forms), strided kernels, narrowing to and widening from interleaved `Float32Array`, nearest point |
31
+ | `mat4x4-array`, `affine2-array`, `affine3-array`, `quaternion-array` | Instance buffers: `…ArrayCreate`, `…Load`, `…Store`, `…LoadStrided`, `…StoreStrided`, `…ArrayIsFinite` (also for `mat2x2` and `mat3x3`) |
32
+ | `tridiagonal` | `tridiagonalSolve(out, lower, diagonal, upper, right, scratch)`: the Thomas algorithm in O(n), `false` on a zero pivot; stable for diagonally dominant or symmetric positive definite systems |
33
+ | `upload` | Unrolled `…StoreFloat32` of matrices and quaternions, and the padded `…StoreStd140Float32` of uniform blocks |
34
+ | `checked` | The `@ac-kit/math-linear/checked` subpath: `assertFiniteValue`, `assertFiniteBuffer`, throwing assertions for the boundaries of a program |
35
+ | `kl-activations` | `KlActivations`: KL-divergence NMF activations of a sparse fixed dictionary, with per-column L1 penalties, iterated across calls |
36
+
37
+ ## Values and functions
38
+
39
+ A `Vec3` is an array `[x, y, z]`; a `Mat4x4` is sixteen numbers in
40
+ **column-major** order (entry of row `r`, column `c` at `4c + r`), the layout of
41
+ WebGL and WebGPU uniforms. The functions follow one standard:
42
+
43
+ - **Creators only.** `vec3Create()` (zero), `vec3Of(x, y, z)`, `vec3Clone(v)` and
44
+ `vec3FromArray(out, source, offset?)` make a vector; `mat4x4Create()` (the
45
+ identity), `mat4x4Of(…)`, `mat4x4OfRows(…)` (rows as you write them on paper),
46
+ `mat4x4Clone` and `mat4x4FromArray` make a matrix. An array literal, a
47
+ `number[]`, a `Float64Array` or any other array of the right length is rejected
48
+ by the compiler, so `vec3Of(1, 0, 0)` replaces `[1, 0, 0]`.
49
+ - **`out` first, alias-safe, no allocation.** Every operation writes into its
50
+ first argument and returns it, and that argument may be any of the inputs:
51
+ `vec3Add(a, a, b)` is `a ← a + b`. Only the creators allocate; keep them out of
52
+ hot loops and reuse an `out`. Functions that return a number or a boolean take
53
+ no `out`.
54
+ - **No throw.** Normalising the zero vector gives zero; inverting a singular
55
+ matrix returns `false` and fills `out` with `NaN`; `NaN` propagates.
56
+ Operations never check their arguments: `vec3IsFinite` is the check to run
57
+ after parsing untrusted data, and `vec3FromArray` is the way in from JSON,
58
+ messages or any array of unknown origin — it stores `NaN` for an element that
59
+ is not a number, never anything else.
60
+ - **Plain arithmetic.** A sum, a dot or a product is rounded as written. The
61
+ `…Precise` variants (`vec3DotPrecise`, `vec3CrossPrecise`,
62
+ `mat4x4MultiplyPrecise`, `vec3TransformMat3x3Precise`) are compensated, and
63
+ cost several times as much. The lengths and the normalisation are right
64
+ between about `1e-150` and `1e150`; beyond that use `vec3LengthScaled` and
65
+ `vec3NormalizeScaled`.
66
+ - **Buffers at the boundary.** `vec3Load(out, array, index)` and
67
+ `vec3Store(array, index, v)` move a vector between a `Float64Array` and a
68
+ value; `…LoadStrided`, `…StoreStrided` and `…StoreFloat32` cover interleaved
69
+ and single-precision buffers; `vec3ArrayIsFinite` scans a buffer. A
70
+ `Float64Array` is never passed to a function that takes a value. Many values
71
+ at once are a buffer, see [Buffers](#buffers).
72
+
73
+ Every creator copies a private array of non-integer doubles, so a value is a V8
74
+ `PACKED_DOUBLE_ELEMENTS` array even when its components are whole numbers. That
75
+ is what keeps the code that reads it fast, and it holds only while nothing else
76
+ is stored in one. For callers who bypass the types (JavaScript, a cast, `any`):
77
+
78
+ - never store anything but a number in a value, not even briefly;
79
+ - no holes, no `push`, no change of `length`;
80
+ - never pass a `Float64Array`, a hand-made array or any other array where a
81
+ value is expected, even once, even in a test: the engine keeps type feedback
82
+ per function, so one such call slows every later call of that function in the
83
+ process (`…FromArray`'s `source` is the one parameter that takes any array);
84
+ - do not make one with `structuredClone` or `[...float64Array]` — use
85
+ `…Clone` or `…FromArray`.
86
+
87
+ `JSON.parse` (typed as `JsonValue` in this repository), `Response.json()` and
88
+ `MessageEvent.data` are not checked values: pass them through `…FromArray`. The
89
+ `~acKitMathType` property of the brand is reserved.
90
+
91
+ ## Buffers
92
+
93
+ Thousands of points, normals or instance matrices are a `Float64Array` of
94
+ `count × N` numbers (3 per `Vec3`, 16 per `Mat4x4`, …), element `i` at
95
+ `[i·N, i·N + N)`, made by `vec3ArrayCreate(count)` (or `vec3ArrayFromValues`).
96
+ Each bulk operation is one dedicated kernel, so a pass over a million points is
97
+ a single loop with no call per element:
98
+
99
+ ```ts
100
+ import {
101
+ affine3Of,
102
+ vec3ArrayCreate,
103
+ vec3ArrayNarrowStrided,
104
+ vec3ArrayTransformAffine3,
105
+ vec3ArrayWidenStrided,
106
+ } from "@ac-kit/math-linear";
107
+
108
+ const positions = vec3ArrayCreate(1000);
109
+ const moved = vec3ArrayCreate(1000);
110
+ vec3ArrayTransformAffine3(
111
+ moved,
112
+ positions,
113
+ affine3Of(1, 0, 0, 0, 1, 0, 0, 0, 1, 10, 0, 0),
114
+ ); // translate by 10 along x; `moved` may be `positions` itself
115
+
116
+ // Into a vertex buffer of position (3), normal (3): positions at offset 0.
117
+ const vertices = new Float32Array(1000 * 6);
118
+ vec3ArrayNarrowStrided(vertices, 0, 6, moved);
119
+ ```
120
+
121
+ - **Kernels.** `vecNArrayAdd`, `…Subtract`, `…MultiplyScalar`, `…AddScaled`,
122
+ `…Translate`, `…Lerp`, `…Normalize`, `…Mean`, `…WeightedMean` (and
123
+ `…WeightedMeanPrecise`), and the transforms by matrices and affine forms
124
+ (`vec3ArrayTransformMat4x4` divides by `w`, `vec3ArrayTransformNormal`
125
+ normalises in the same pass). Out first, in place allowed (`out` may be the
126
+ input, not a partially overlapping view of it).
127
+ - **Checks.** The element count is `input.length / N`. One O(1) check per call
128
+ that the lengths agree and are a multiple of `N` throws a `RangeError`
129
+ before anything is written; there is no per-element check, so a `NaN` goes
130
+ through (`vec3ArrayIsFinite` and `assertFiniteBuffer` are the checks).
131
+ - **Interleaved data.** The `…Strided` kernels take `(out, outOffset,
132
+ outStride, input, inputOffset, inputStride, count, …)` in numbers, and tight
133
+ kernels take no stride.
134
+ - **Single precision.** Nothing computes on a `Float32Array`: `…ArrayNarrowStrided`
135
+ writes a tight binary64 buffer into an interleaved binary32 one,
136
+ `…ArrayWidenStrided` reads one back (a glTF accessor, a readback), and
137
+ `…ArrayNarrowRelative` subtracts an origin first so that large coordinates
138
+ keep their precision. A whole array without stride is
139
+ `Float32Array.prototype.set`.
140
+ - **Instances.** `mat4x4ArrayCreate`, `affine2ArrayCreate`, `affine3ArrayCreate`
141
+ and `quaternionArrayCreate` make instance buffers, written one element at a
142
+ time with `…Store` and read with `…Load`.
24
143
 
25
144
  ## Equality and tolerance
26
145
 
27
- `*Equals` is **exact**, component by component. It is transitive, so it is safe
28
- as a `Set` or `Map` key and as a dedupe basis. `*IsClose` is the tolerant
146
+ `*Equals` is **exact**, component by component (`−0` equals `+0`, `NaN` equals
147
+ nothing). It is safe as a basis for deduplication. `*IsClose` is the tolerant
29
148
  counterpart and is deliberately named differently, because it is _not_ an
30
149
  equality: `a ≈ b` and `b ≈ c` does not give `a ≈ c`, so it must never back an
31
150
  ordering or a container.
32
151
 
33
- `*IsClose` requires its tolerance — there is no default and no global config,
34
- unlike `@ac-kit/math-geometry`. A vector or matrix here is dimensionless algebra
35
- with no units of its own, so only the caller knows what counts as close:
152
+ `*IsClose` takes its tolerance as an option — none means exact, and there is no
153
+ global config, unlike `@ac-kit/math-geometry`. A vector or matrix here is
154
+ dimensionless algebra with no units of its own, so only the caller knows what
155
+ counts as close:
36
156
 
37
157
  ```ts
38
158
  import { vec3IsClose } from "@ac-kit/math-linear";
@@ -50,18 +170,111 @@ instead.
50
170
  ## Quaternion here versus quaternion in math-numbers
51
171
 
52
172
  The `Quaternion` in this package is a rotation operator: four floats, always
53
- used unit-length, optimised for composing and interpolating orientations.
173
+ used unit-length, optimised for composing and interpolating orientations. It is
174
+ the branded `[x, y, z, w]` tuple of `@ac-kit/math-complex`, re-exported here with
175
+ its out-first functions: build one with `quaternionOf(x, y, z, w)` or
176
+ `quaternionCreate()`, since an array literal does not type-check.
54
177
 
55
178
  `@ac-kit/math-complex`'s `QuaternionNum` is ℍ as an algebraic object — a
56
179
  division ring implementing the `@ac-kit/math-algebra` hierarchy. Same
57
180
  mathematics, different purpose; neither is a substitute for the other.
58
181
 
182
+ ## Rotations, transforms and cameras
183
+
184
+ - **Conventions.** Right-handed; a camera looks down **−Z**; column vectors, so
185
+ `mat4x4Multiply(out, a, b)` is `a · b` and applies `b` first. Every signed
186
+ angle a function returns is in `(−π, π]`.
187
+ - **Euler angles** are a `Vec3` of radians **in the order's sequence**, always
188
+ intrinsic: `quaternionFromEuler(out, angles, "ZXY")` is `Rz(a₀) · Rx(a₁) ·
189
+ Ry(a₂)`, which is also the extrinsic Y–X–Z sequence. All twelve orders are
190
+ there. A `DeviceOrientationEvent` is `"ZXY"` with `alpha`, `beta`, `gamma`
191
+ converted from degrees. `eulerFromQuaternion` gives the first and third angles
192
+ in `(−π, π]`, the middle one in its order's range, and at gimbal lock (within
193
+ `1e-10` rad) puts the whole of the combined rotation into the first angle, the
194
+ third being `0`.
195
+ - **Depth range** is a required argument of every projection:
196
+ `"zero-to-one"` (WebGPU and both backends of `@ac-kit/graphics-gpu`),
197
+ `"negative-one-to-one"` (OpenGL) or `"one-to-zero"` (reversed depth, near at
198
+ 1; its extra precision needs a floating-point depth buffer and depth produced
199
+ in `[0, 1]`). `far` may be `Infinity` in all three.
200
+ `mat4x4Perspective`, `mat4x4Frustum`, `mat4x4Orthographic`, `mat4x4LookAt` and
201
+ `affine3TargetTo` are built once per frame or resize, so they validate and
202
+ throw a `RangeError` (leaving `out` untouched) where the operations return
203
+ `false` or `NaN`.
204
+ - **Viewports.** Pixels have a top-left origin and `y` down, NDC has `y` up;
205
+ pixel `i` covers `[i, i + 1)` and NDC `−1` is the left edge, not a pixel
206
+ centre. `vec3ProjectToViewport` gives `[x px, y px, depth]` (`NaN` behind the
207
+ eye), `vec3UnprojectFromViewport` inverts it, and `vec3ArrayProjectToViewport`
208
+ does a whole buffer in one pass.
209
+ - **Affine forms.** `Affine2` is Canvas's `a, b, c, d, e, f`; `Affine3` is four
210
+ columns of three (the translation at 9, 10, 11). `affine2Translate`,
211
+ `…Rotate` and `…Scale` apply in the local space like Canvas's. `Transform2`
212
+ and `Transform3` are translate-rotate-scale records, composed through the
213
+ affine forms; `…FromAffine…` and `mat4x4Decompose` return `false`, with `NaN`
214
+ in the outputs, for a shear (a cosine between two columns above about `1e-6`),
215
+ a zero scale or non-finite input.
216
+
217
+ ## Uploads and the checked layer
218
+
219
+ `mat4x4StoreFloat32(array, offset, m)` writes a matrix into a `Float32Array` by
220
+ sixteen unrolled stores, a few nanoseconds where `Float32Array.prototype.set`
221
+ on an array costs fifteen. A uniform block pads matrix columns to four floats,
222
+ so `mat3x3StoreStd140Float32`, `affine3StoreStd140Float32` (a `mat4x3f`) and
223
+ `affine2StoreStd140Float32` (a `mat3x3f` holding the homogeneous matrix) write
224
+ every float of the field, padding included.
225
+
226
+ Operations never check, so the boundaries of a program do: the
227
+ `@ac-kit/math-linear/checked` subpath exports `assertFiniteValue(name, value)`
228
+ and `assertFiniteBuffer(name, array, elementSize, count?)`, which throw a
229
+ `RangeError` naming the argument and the first bad component. Call them after
230
+ parsing untrusted data, before an upload in a development build, and in tests.
231
+
59
232
  ## Usage
60
233
 
61
234
  ```ts
62
- import { vec3Cross, vec3Normalize } from "@ac-kit/math-linear";
235
+ import {
236
+ mat4x4Create,
237
+ mat4x4LookAt,
238
+ mat4x4Multiply,
239
+ mat4x4Perspective,
240
+ mat4x4StoreFloat32,
241
+ vec3Create,
242
+ vec3Cross,
243
+ vec3Normalize,
244
+ vec3Of,
245
+ vec3ProjectToViewport,
246
+ viewportOf,
247
+ } from "@ac-kit/math-linear";
248
+
249
+ const out = vec3Create();
250
+ vec3Normalize(out, vec3Cross(out, vec3Of(1, 0, 0), vec3Of(0, 1, 0))); // [0, 0, 1]
251
+
252
+ // A camera 10 units back, a WebGPU projection, and where the origin lands on
253
+ // an 800 × 600 canvas (the middle).
254
+ const view = mat4x4LookAt(
255
+ mat4x4Create(),
256
+ vec3Of(0, 0, 10),
257
+ vec3Of(0, 0, 0),
258
+ vec3Of(0, 1, 0),
259
+ );
260
+ const projection = mat4x4Perspective(
261
+ mat4x4Create(),
262
+ Math.PI / 3,
263
+ 800 / 600,
264
+ 0.1,
265
+ 100,
266
+ "zero-to-one",
267
+ );
268
+ const viewProjection = mat4x4Multiply(mat4x4Create(), projection, view);
269
+ vec3ProjectToViewport(
270
+ vec3Create(),
271
+ vec3Of(0, 0, 0),
272
+ viewProjection,
273
+ viewportOf(0, 0, 800, 600),
274
+ ); // [400, 300, depth in 0…1]
63
275
 
64
- vec3Normalize(vec3Cross([1, 0, 0], [0, 1, 0])); // [0, 0, 1]
276
+ const uniforms = new Float32Array(16);
277
+ mat4x4StoreFloat32(uniforms, 0, viewProjection);
65
278
  ```
66
279
 
67
280
  See the
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The brand of every value type of the math packages: a property with a string
3
+ * key that is stable across versions, so two installed copies of a package (two
4
+ * dependents pinning different minors) stay assignable to each other, which a
5
+ * per-package `unique symbol` would not allow.
6
+ *
7
+ * Declared identically and internally in each package, never imported across
8
+ * packages: the structure is the identity. The key is reserved, and the brand
9
+ * costs nothing at run time.
10
+ */
11
+ export type Brand_<Name extends string> = {
12
+ readonly "~acKitMathType": Name;
13
+ };
14
+ //# sourceMappingURL=_brand.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_brand.d.ts","sourceRoot":"","sources":["../src/_brand.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,MAAM,MAAM,CAAC,IAAI,SAAS,MAAM,IAAI;IACzC,QAAQ,CAAC,gBAAgB,EAAE,IAAI,CAAC;CAChC,CAAC"}
package/dist/_brand.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=_brand.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_brand.js","sourceRoot":"","sources":["../src/_brand.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The second chance of a matrix inversion. A matrix whose entries are all of
3
+ * one very large or very small magnitude has a determinant that overflows or
4
+ * underflows although its inverse is representable. Writes `m` scaled by `2^-k`
5
+ * into `out` (exact: a power of two), where `2^k` is the power of two at or
6
+ * below the largest entry, and returns `k`; the caller inverts `out` and passes
7
+ * the result to {@link unscaleInverse_}. Returns `NaN`, writing nothing, when
8
+ * an entry is not finite or every entry is zero, which no scaling helps. `out`
9
+ * may be `m`.
10
+ */
11
+ export declare function scaleForInversion_(out: number[], m: ArrayLike<number>): number;
12
+ /**
13
+ * Multiplies the inverse of the scaled matrix of {@link scaleForInversion_} by
14
+ * `2^-k` in place, which gives the inverse of the original, and returns whether
15
+ * every entry is finite (the inverse may still be beyond the range of a
16
+ * binary64 number).
17
+ */
18
+ export declare function unscaleInverse_(out: number[], exponent: number): boolean;
19
+ //# sourceMappingURL=_invert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_invert.d.ts","sourceRoot":"","sources":["../src/_invert.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CACjC,GAAG,EAAE,MAAM,EAAE,EACb,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAClB,MAAM,CAoBR;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CASxE"}
@@ -0,0 +1,48 @@
1
+ import { scaleByPowerOfTwo } from "@ac-kit/core";
2
+ /**
3
+ * The second chance of a matrix inversion. A matrix whose entries are all of
4
+ * one very large or very small magnitude has a determinant that overflows or
5
+ * underflows although its inverse is representable. Writes `m` scaled by `2^-k`
6
+ * into `out` (exact: a power of two), where `2^k` is the power of two at or
7
+ * below the largest entry, and returns `k`; the caller inverts `out` and passes
8
+ * the result to {@link unscaleInverse_}. Returns `NaN`, writing nothing, when
9
+ * an entry is not finite or every entry is zero, which no scaling helps. `out`
10
+ * may be `m`.
11
+ */
12
+ export function scaleForInversion_(out, m) {
13
+ let largest = 0;
14
+ for (let index = 0; index < out.length; index++) {
15
+ const magnitude = Math.abs(m[index]);
16
+ if (!Number.isFinite(magnitude)) {
17
+ return Number.NaN;
18
+ }
19
+ if (magnitude > largest) {
20
+ largest = magnitude;
21
+ }
22
+ }
23
+ if (largest === 0) {
24
+ return Number.NaN;
25
+ }
26
+ const exponent = Math.floor(Math.log2(largest));
27
+ for (let index = 0; index < out.length; index++) {
28
+ out[index] = scaleByPowerOfTwo(m[index], -exponent);
29
+ }
30
+ return exponent;
31
+ }
32
+ /**
33
+ * Multiplies the inverse of the scaled matrix of {@link scaleForInversion_} by
34
+ * `2^-k` in place, which gives the inverse of the original, and returns whether
35
+ * every entry is finite (the inverse may still be beyond the range of a
36
+ * binary64 number).
37
+ */
38
+ export function unscaleInverse_(out, exponent) {
39
+ for (let index = 0; index < out.length; index++) {
40
+ const value = scaleByPowerOfTwo(out[index], -exponent);
41
+ out[index] = value;
42
+ if (!Number.isFinite(value)) {
43
+ return false;
44
+ }
45
+ }
46
+ return true;
47
+ }
48
+ //# sourceMappingURL=_invert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_invert.js","sourceRoot":"","sources":["../src/_invert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CACjC,GAAa,EACb,CAAoB;IAEpB,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACjD,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAE,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;YACjC,OAAO,MAAM,CAAC,GAAG,CAAC;QACnB,CAAC;QACD,IAAI,SAAS,GAAG,OAAO,EAAE,CAAC;YACzB,OAAO,GAAG,SAAS,CAAC;QACrB,CAAC;IACF,CAAC;IACD,IAAI,OAAO,KAAK,CAAC,EAAE,CAAC;QACnB,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAChD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACjD,GAAG,CAAC,KAAK,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAE,EAAE,CAAC,QAAQ,CAAC,CAAC;IACtD,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,GAAa,EAAE,QAAgB;IAC9D,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACjD,MAAM,KAAK,GAAG,iBAAiB,CAAC,GAAG,CAAC,KAAK,CAAE,EAAE,CAAC,QAAQ,CAAC,CAAC;QACxD,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAC7B,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}