@johnhenry/math 0.0.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 (242) hide show
  1. package/README.md +165 -0
  2. package/dist/Bivector4.d.ts +52 -0
  3. package/dist/Bivector4.d.ts.map +1 -0
  4. package/dist/Bivector4.js +105 -0
  5. package/dist/Bivector4.js.map +1 -0
  6. package/dist/CellGraph.d.ts +259 -0
  7. package/dist/CellGraph.d.ts.map +1 -0
  8. package/dist/CellGraph.js +637 -0
  9. package/dist/CellGraph.js.map +1 -0
  10. package/dist/Clifford4Internal.d.ts +35 -0
  11. package/dist/Clifford4Internal.d.ts.map +1 -0
  12. package/dist/Clifford4Internal.js +86 -0
  13. package/dist/Clifford4Internal.js.map +1 -0
  14. package/dist/Combinatorics.d.ts +61 -0
  15. package/dist/Combinatorics.d.ts.map +1 -0
  16. package/dist/Combinatorics.js +186 -0
  17. package/dist/Combinatorics.js.map +1 -0
  18. package/dist/CombinatoricsGenerators.d.ts +44 -0
  19. package/dist/CombinatoricsGenerators.d.ts.map +1 -0
  20. package/dist/CombinatoricsGenerators.js +148 -0
  21. package/dist/CombinatoricsGenerators.js.map +1 -0
  22. package/dist/ComplexMath.d.ts +46 -0
  23. package/dist/ComplexMath.d.ts.map +1 -0
  24. package/dist/ComplexMath.js +98 -0
  25. package/dist/ComplexMath.js.map +1 -0
  26. package/dist/ComplexNumber.d.ts +174 -0
  27. package/dist/ComplexNumber.d.ts.map +1 -0
  28. package/dist/ComplexNumber.js +443 -0
  29. package/dist/ComplexNumber.js.map +1 -0
  30. package/dist/Cycle.d.ts +42 -0
  31. package/dist/Cycle.d.ts.map +1 -0
  32. package/dist/Cycle.js +120 -0
  33. package/dist/Cycle.js.map +1 -0
  34. package/dist/Decimal.d.ts +45 -0
  35. package/dist/Decimal.d.ts.map +1 -0
  36. package/dist/Decimal.js +135 -0
  37. package/dist/Decimal.js.map +1 -0
  38. package/dist/Distributions.d.ts +55 -0
  39. package/dist/Distributions.d.ts.map +1 -0
  40. package/dist/Distributions.js +222 -0
  41. package/dist/Distributions.js.map +1 -0
  42. package/dist/DualNumber.d.ts +42 -0
  43. package/dist/DualNumber.d.ts.map +1 -0
  44. package/dist/DualNumber.js +91 -0
  45. package/dist/DualNumber.js.map +1 -0
  46. package/dist/Environment.d.ts +44 -0
  47. package/dist/Environment.d.ts.map +1 -0
  48. package/dist/Environment.js +129 -0
  49. package/dist/Environment.js.map +1 -0
  50. package/dist/Expression.d.ts +19 -0
  51. package/dist/Expression.d.ts.map +1 -0
  52. package/dist/Expression.js +43 -0
  53. package/dist/Expression.js.map +1 -0
  54. package/dist/FFT.d.ts +21 -0
  55. package/dist/FFT.d.ts.map +1 -0
  56. package/dist/FFT.js +136 -0
  57. package/dist/FFT.js.map +1 -0
  58. package/dist/GeometricAlgebra4.d.ts +73 -0
  59. package/dist/GeometricAlgebra4.d.ts.map +1 -0
  60. package/dist/GeometricAlgebra4.js +115 -0
  61. package/dist/GeometricAlgebra4.js.map +1 -0
  62. package/dist/Geometry.d.ts +41 -0
  63. package/dist/Geometry.d.ts.map +1 -0
  64. package/dist/Geometry.js +167 -0
  65. package/dist/Geometry.js.map +1 -0
  66. package/dist/Graph.d.ts +72 -0
  67. package/dist/Graph.d.ts.map +1 -0
  68. package/dist/Graph.js +337 -0
  69. package/dist/Graph.js.map +1 -0
  70. package/dist/Graph3DUtils.d.ts +80 -0
  71. package/dist/Graph3DUtils.d.ts.map +1 -0
  72. package/dist/Graph3DUtils.js +156 -0
  73. package/dist/Graph3DUtils.js.map +1 -0
  74. package/dist/GraphUtils.d.ts +67 -0
  75. package/dist/GraphUtils.d.ts.map +1 -0
  76. package/dist/GraphUtils.js +72 -0
  77. package/dist/GraphUtils.js.map +1 -0
  78. package/dist/GroupTheory.d.ts +73 -0
  79. package/dist/GroupTheory.d.ts.map +1 -0
  80. package/dist/GroupTheory.js +194 -0
  81. package/dist/GroupTheory.js.map +1 -0
  82. package/dist/IntUtils.d.ts +33 -0
  83. package/dist/IntUtils.d.ts.map +1 -0
  84. package/dist/IntUtils.js +185 -0
  85. package/dist/IntUtils.js.map +1 -0
  86. package/dist/Interval.d.ts +44 -0
  87. package/dist/Interval.d.ts.map +1 -0
  88. package/dist/Interval.js +165 -0
  89. package/dist/Interval.js.map +1 -0
  90. package/dist/Lattice.d.ts +48 -0
  91. package/dist/Lattice.d.ts.map +1 -0
  92. package/dist/Lattice.js +65 -0
  93. package/dist/Lattice.js.map +1 -0
  94. package/dist/Logic.d.ts +22 -0
  95. package/dist/Logic.d.ts.map +1 -0
  96. package/dist/Logic.js +34 -0
  97. package/dist/Logic.js.map +1 -0
  98. package/dist/MatrixMath.d.ts +71 -0
  99. package/dist/MatrixMath.d.ts.map +1 -0
  100. package/dist/MatrixMath.js +302 -0
  101. package/dist/MatrixMath.js.map +1 -0
  102. package/dist/NumberTheory.d.ts +49 -0
  103. package/dist/NumberTheory.d.ts.map +1 -0
  104. package/dist/NumberTheory.js +230 -0
  105. package/dist/NumberTheory.js.map +1 -0
  106. package/dist/Numerical.d.ts +85 -0
  107. package/dist/Numerical.d.ts.map +1 -0
  108. package/dist/Numerical.js +343 -0
  109. package/dist/Numerical.js.map +1 -0
  110. package/dist/Permutation.d.ts +43 -0
  111. package/dist/Permutation.d.ts.map +1 -0
  112. package/dist/Permutation.js +142 -0
  113. package/dist/Permutation.js.map +1 -0
  114. package/dist/Polygon.d.ts +64 -0
  115. package/dist/Polygon.d.ts.map +1 -0
  116. package/dist/Polygon.js +221 -0
  117. package/dist/Polygon.js.map +1 -0
  118. package/dist/PolynomialRing.d.ts +74 -0
  119. package/dist/PolynomialRing.d.ts.map +1 -0
  120. package/dist/PolynomialRing.js +231 -0
  121. package/dist/PolynomialRing.js.map +1 -0
  122. package/dist/Quaternion.d.ts +37 -0
  123. package/dist/Quaternion.d.ts.map +1 -0
  124. package/dist/Quaternion.js +106 -0
  125. package/dist/Quaternion.js.map +1 -0
  126. package/dist/Rational.d.ts +34 -0
  127. package/dist/Rational.d.ts.map +1 -0
  128. package/dist/Rational.js +136 -0
  129. package/dist/Rational.js.map +1 -0
  130. package/dist/RealMath.d.ts +112 -0
  131. package/dist/RealMath.d.ts.map +1 -0
  132. package/dist/RealMath.js +262 -0
  133. package/dist/RealMath.js.map +1 -0
  134. package/dist/Rotor4.d.ts +180 -0
  135. package/dist/Rotor4.d.ts.map +1 -0
  136. package/dist/Rotor4.js +388 -0
  137. package/dist/Rotor4.js.map +1 -0
  138. package/dist/SpecialFunctions.d.ts +32 -0
  139. package/dist/SpecialFunctions.d.ts.map +1 -0
  140. package/dist/SpecialFunctions.js +161 -0
  141. package/dist/SpecialFunctions.js.map +1 -0
  142. package/dist/SpecialOperator.d.ts +21 -0
  143. package/dist/SpecialOperator.d.ts.map +1 -0
  144. package/dist/SpecialOperator.js +26 -0
  145. package/dist/SpecialOperator.js.map +1 -0
  146. package/dist/Statistics.d.ts +50 -0
  147. package/dist/Statistics.d.ts.map +1 -0
  148. package/dist/Statistics.js +131 -0
  149. package/dist/Statistics.js.map +1 -0
  150. package/dist/StringEvaluator.d.ts +48 -0
  151. package/dist/StringEvaluator.d.ts.map +1 -0
  152. package/dist/StringEvaluator.js +190 -0
  153. package/dist/StringEvaluator.js.map +1 -0
  154. package/dist/Structure.d.ts +130 -0
  155. package/dist/Structure.d.ts.map +1 -0
  156. package/dist/Structure.js +394 -0
  157. package/dist/Structure.js.map +1 -0
  158. package/dist/Symbolic.d.ts +501 -0
  159. package/dist/Symbolic.d.ts.map +1 -0
  160. package/dist/Symbolic.js +4077 -0
  161. package/dist/Symbolic.js.map +1 -0
  162. package/dist/Type.d.ts +31 -0
  163. package/dist/Type.d.ts.map +1 -0
  164. package/dist/Type.js +47 -0
  165. package/dist/Type.js.map +1 -0
  166. package/dist/Utilities.d.ts +27 -0
  167. package/dist/Utilities.d.ts.map +1 -0
  168. package/dist/Utilities.js +47 -0
  169. package/dist/Utilities.js.map +1 -0
  170. package/dist/Vec4.d.ts +39 -0
  171. package/dist/Vec4.d.ts.map +1 -0
  172. package/dist/Vec4.js +82 -0
  173. package/dist/Vec4.js.map +1 -0
  174. package/dist/Vector.d.ts +83 -0
  175. package/dist/Vector.d.ts.map +1 -0
  176. package/dist/Vector.js +173 -0
  177. package/dist/Vector.js.map +1 -0
  178. package/dist/VectorCalculus.d.ts +42 -0
  179. package/dist/VectorCalculus.d.ts.map +1 -0
  180. package/dist/VectorCalculus.js +82 -0
  181. package/dist/VectorCalculus.js.map +1 -0
  182. package/dist/VectorUtils.d.ts +172 -0
  183. package/dist/VectorUtils.d.ts.map +1 -0
  184. package/dist/VectorUtils.js +706 -0
  185. package/dist/VectorUtils.js.map +1 -0
  186. package/dist/differentiation-rules.d.ts +21 -0
  187. package/dist/differentiation-rules.d.ts.map +1 -0
  188. package/dist/differentiation-rules.js +52 -0
  189. package/dist/differentiation-rules.js.map +1 -0
  190. package/dist/index.d.ts +52 -0
  191. package/dist/index.d.ts.map +1 -0
  192. package/dist/index.js +66 -0
  193. package/dist/index.js.map +1 -0
  194. package/package.json +68 -0
  195. package/src/Bivector4.ts +136 -0
  196. package/src/CellGraph.ts +669 -0
  197. package/src/Clifford4Internal.ts +90 -0
  198. package/src/Combinatorics.ts +183 -0
  199. package/src/CombinatoricsGenerators.ts +144 -0
  200. package/src/ComplexMath.ts +162 -0
  201. package/src/ComplexNumber.ts +491 -0
  202. package/src/Cycle.ts +126 -0
  203. package/src/Decimal.ts +145 -0
  204. package/src/Distributions.ts +269 -0
  205. package/src/DualNumber.ts +105 -0
  206. package/src/Environment.ts +127 -0
  207. package/src/Expression.ts +48 -0
  208. package/src/FFT.ts +146 -0
  209. package/src/GeometricAlgebra4.ts +157 -0
  210. package/src/Geometry.ts +173 -0
  211. package/src/Graph.ts +337 -0
  212. package/src/Graph3DUtils.ts +269 -0
  213. package/src/GraphUtils.ts +159 -0
  214. package/src/GroupTheory.ts +214 -0
  215. package/src/IntUtils.ts +200 -0
  216. package/src/Interval.ts +171 -0
  217. package/src/Lattice.ts +87 -0
  218. package/src/Logic.ts +38 -0
  219. package/src/MatrixMath.ts +326 -0
  220. package/src/NumberTheory.ts +228 -0
  221. package/src/Numerical.ts +390 -0
  222. package/src/Permutation.ts +148 -0
  223. package/src/Polygon.ts +229 -0
  224. package/src/PolynomialRing.ts +242 -0
  225. package/src/Quaternion.ts +125 -0
  226. package/src/Rational.ts +148 -0
  227. package/src/RealMath.ts +346 -0
  228. package/src/Rotor4.ts +440 -0
  229. package/src/SpecialFunctions.ts +152 -0
  230. package/src/SpecialOperator.ts +28 -0
  231. package/src/Statistics.ts +170 -0
  232. package/src/StringEvaluator.ts +208 -0
  233. package/src/Structure.ts +481 -0
  234. package/src/Symbolic.ts +4313 -0
  235. package/src/Type.ts +48 -0
  236. package/src/Utilities.ts +49 -0
  237. package/src/Vec4.ts +98 -0
  238. package/src/Vector.ts +189 -0
  239. package/src/VectorCalculus.ts +87 -0
  240. package/src/VectorUtils.ts +797 -0
  241. package/src/differentiation-rules.ts +73 -0
  242. package/src/index.ts +140 -0
package/dist/Rotor4.js ADDED
@@ -0,0 +1,388 @@
1
+ /**
2
+ * Rotor4 — a rotation operator in 4D space: `scalar + bivector (6) + pseudoscalar (1)`,
3
+ * 8 components total. This is the 4D generalization of {@link Quaternion}
4
+ * (which is really the 3D rotor, `scalar + bivector (3)`, that happens to
5
+ * have the same component count as a 3D vector — a coincidence unique to 3D
6
+ * that doesn't hold in 4D or any other dimension).
7
+ *
8
+ * **Double rotations.** A general 3D rotor always describes rotation in a
9
+ * single 2-plane. A general 4D rotor can describe a *double rotation*:
10
+ * simultaneous, independent rotation in two mutually orthogonal 2-planes at
11
+ * once — this is the origin of the nonzero pseudoscalar component, and it
12
+ * has no 3D analogue. See {@link Rotor4.multiply} composing two
13
+ * single-plane rotors in orthogonal planes for how a double rotation arises.
14
+ *
15
+ * **Normalization caveat.** {@link Rotor4.normalize} performs the naive
16
+ * quaternion-style renormalization (divide by magnitude). This is only
17
+ * exactly correct for *simple* rotors (single rotation plane) — for a
18
+ * genuine double rotation it does not lie on the same manifold as a simple
19
+ * rotor, so naive renormalization after numerical drift can silently produce
20
+ * an invalid rotor. {@link Rotor4.factor} and {@link Rotor4.renormalize} are
21
+ * the proper fix (Perwass-style rotor factorization): decompose into two
22
+ * simple rotors in orthogonal planes, each renormalizable exactly, then
23
+ * recompose — see the `N-Dimensional Rigid Body Dynamics` (SIGGRAPH 2020)
24
+ * paper §3.3 for why this matters for repeated physics integration.
25
+ */
26
+ import { Bivector4 } from "./Bivector4.js";
27
+ import { E1, E2, E3, E4, geometricProductMV, mvZero, PSEUDOSCALAR4 } from "./Clifford4Internal.js";
28
+ import { bivectorFromMV, bivectorToMV, leftContraction, wedgeProduct } from "./GeometricAlgebra4.js";
29
+ import { MatrixMath } from "./MatrixMath.js";
30
+ import { Vec4 } from "./Vec4.js";
31
+ import { Vector } from "./Vector.js";
32
+ export class Rotor4 {
33
+ scalar;
34
+ bivector;
35
+ pseudoscalar;
36
+ static Identity = new Rotor4(1, Bivector4.Zero, 0);
37
+ constructor(scalar = 1, bivector = Bivector4.Zero, pseudoscalar = 0) {
38
+ this.scalar = scalar;
39
+ this.bivector = bivector;
40
+ this.pseudoscalar = pseudoscalar;
41
+ }
42
+ toMV() {
43
+ const mv = mvZero();
44
+ mv[0] = this.scalar;
45
+ const bmv = bivectorToMV(this.bivector);
46
+ for (let i = 0; i < bmv.length; i++)
47
+ mv[i] += bmv[i];
48
+ mv[PSEUDOSCALAR4] = this.pseudoscalar;
49
+ return mv;
50
+ }
51
+ static fromMV(mv) {
52
+ return new Rotor4(mv[0], bivectorFromMV(mv), mv[PSEUDOSCALAR4]);
53
+ }
54
+ add(r) {
55
+ return new Rotor4(this.scalar + r.scalar, this.bivector.add(r.bivector), this.pseudoscalar + r.pseudoscalar);
56
+ }
57
+ subtract(r) {
58
+ return new Rotor4(this.scalar - r.scalar, this.bivector.subtract(r.bivector), this.pseudoscalar - r.pseudoscalar);
59
+ }
60
+ negate() {
61
+ return new Rotor4(-this.scalar, this.bivector.negate(), -this.pseudoscalar);
62
+ }
63
+ scale(s) {
64
+ return new Rotor4(this.scalar * s, this.bivector.scale(s), this.pseudoscalar * s);
65
+ }
66
+ /** Component-wise inner product over all 8 components (scalar, 6 bivector, pseudoscalar) -- treats a Rotor4 as an 8-vector, exactly {@link Quaternion.dot}'s role in that class's own `slerp`. */
67
+ dot(r) {
68
+ return this.scalar * r.scalar + this.bivector.dot(r.bivector) + this.pseudoscalar * r.pseudoscalar;
69
+ }
70
+ /** The geometric product `this * r` — composes two rotations (apply `r` first, then `this`). */
71
+ multiply(r) {
72
+ return Rotor4.fromMV(geometricProductMV(this.toMV(), r.toMV()));
73
+ }
74
+ /**
75
+ * The reverse `R~`: reverses the order of vector factors, which negates the
76
+ * bivector (grade 2, picks up (-1)^1) but leaves the scalar (grade 0) and
77
+ * pseudoscalar (grade 4, picks up (-1)^6 = +1) unchanged. For a unit rotor,
78
+ * this is also the inverse — the 4D analogue of a quaternion's conjugate
79
+ * ({@link Quaternion.conjugate}). **Not** the inverse for a non-unit rotor
80
+ * (see {@link inverse}): a rotor that has drifted off the unit manifold
81
+ * during repeated composition (e.g. per-timestep physics integration,
82
+ * before renormalization runs) will silently produce a wrong,
83
+ * `|R|²`-scaled result from `reverse()` where an inverse was intended.
84
+ */
85
+ reverse() {
86
+ return new Rotor4(this.scalar, this.bivector.negate(), this.pseudoscalar);
87
+ }
88
+ /**
89
+ * Multiplicative inverse `R⁻¹ = R~ / |R|²`, distinct from {@link reverse}
90
+ * for a non-unit rotor — the 4D analogue of {@link Quaternion.inverse}.
91
+ * `this.multiply(this.inverse())` is `Identity` regardless of `this`'s
92
+ * magnitude; `this.multiply(this.reverse())` only is when `this` is
93
+ * already a unit rotor.
94
+ */
95
+ inverse() {
96
+ const m2 = this.magnitudeSquared;
97
+ if (m2 === 0)
98
+ throw new Error("The zero rotor has no inverse.");
99
+ return this.reverse().scale(1 / m2);
100
+ }
101
+ get magnitudeSquared() {
102
+ return this.scalar * this.scalar + this.bivector.magnitudeSquared + this.pseudoscalar * this.pseudoscalar;
103
+ }
104
+ get magnitude() {
105
+ return Math.sqrt(this.magnitudeSquared);
106
+ }
107
+ /**
108
+ * Naive renormalization (divide by magnitude). Exact for simple rotors;
109
+ * see the class-level doc comment for why this is *not* generally correct
110
+ * for double rotations, and where the real fix belongs.
111
+ */
112
+ normalize() {
113
+ const m = this.magnitude;
114
+ if (m === 0)
115
+ throw new Error("Cannot normalize the zero rotor.");
116
+ return this.scale(1 / m);
117
+ }
118
+ /** Apply this rotor to a vector via the sandwich product `R v R~`. Rotates `v`. */
119
+ apply(v) {
120
+ const vmv = mvZero();
121
+ vmv[E1] = v.x;
122
+ vmv[E2] = v.y;
123
+ vmv[E3] = v.z;
124
+ vmv[E4] = v.w;
125
+ const mv = geometricProductMV(geometricProductMV(this.toMV(), vmv), this.reverse().toMV());
126
+ return new Vec4(mv[E1], mv[E2], mv[E3], mv[E4]);
127
+ }
128
+ /** Apply this rotor to a bivector via the sandwich product `R B R~`. Rotates the 2-plane `B`. */
129
+ applyToBivector(b) {
130
+ const mv = geometricProductMV(geometricProductMV(this.toMV(), bivectorToMV(b)), this.reverse().toMV());
131
+ return bivectorFromMV(mv);
132
+ }
133
+ /**
134
+ * Perwass-style rotor factorization: decompose this rotor into two simple
135
+ * rotors `[R1, R2]` in mutually orthogonal planes such that `R2.multiply(R1)`
136
+ * has the same *action* as this rotor (equal up to the usual rotor
137
+ * double-cover sign, like any rotor comparison) — the proper fix for
138
+ * {@link normalize}'s documented limitation on double rotations. Works
139
+ * even when this rotor has drifted away from being a valid unit rotor
140
+ * (the intended use case: repeated physics integration).
141
+ *
142
+ * Method: find this rotor's two invariant 2-planes as eigenspaces of the
143
+ * *symmetric* part `S = M + Mᵀ` of its 4x4 matrix representation `M`
144
+ * (restricted to an invariant plane with rotation angle `θ`, `M + Mᵀ` is
145
+ * `2cos(θ)` times the identity on that plane — so same-angle-magnitude
146
+ * planes share an eigenvalue, and a repeated eigenvalue of `S` signals an
147
+ * isoclinic-type degeneracy where *any* orthonormal basis of that
148
+ * eigenspace is a genuinely valid invariant-plane choice, not just an
149
+ * approximation). `eigenSymmetric`'s eigenvector order isn't guaranteed to
150
+ * group same-plane pairs together when eigenvalues coincide (or are
151
+ * numerically indistinguishable), so the 3 possible pairings of the 4
152
+ * eigenvectors into 2 pairs are explicitly checked for genuine
153
+ * `M`-invariance (zero "leakage" into the other pair) before use, rather
154
+ * than assumed from sort order. Once a valid pair `(u, v)` spanning an
155
+ * invariant plane is known, the angle and correctly-oriented plane come
156
+ * directly from how `M` actually acts on `u` within that plane —
157
+ * `atan2(v·(Mu), u·(Mu))` — not from this rotor's own (possibly drifted)
158
+ * scalar/bivector/pseudoscalar components.
159
+ */
160
+ factor() {
161
+ const basis = [Vec4.Ex, Vec4.Ey, Vec4.Ez, Vec4.Ew];
162
+ const columns = basis.map((e) => this.apply(e).toArray());
163
+ const M = (i, j) => columns[j]?.[i];
164
+ const S = Vector.fromArray([0, 1, 2, 3].map((i) => Vector.fromArray([0, 1, 2, 3].map((j) => M(i, j) + M(j, i)))));
165
+ const { vectors } = MatrixMath.eigenSymmetric(S);
166
+ const eig = [0, 1, 2, 3].map((i) => new Vec4(vectors[0]?.[i], vectors[1]?.[i], vectors[2]?.[i], vectors[3]?.[i]));
167
+ const pairings = [
168
+ [
169
+ [0, 1],
170
+ [2, 3],
171
+ ],
172
+ [
173
+ [0, 2],
174
+ [1, 3],
175
+ ],
176
+ [
177
+ [0, 3],
178
+ [1, 2],
179
+ ],
180
+ ];
181
+ let bestLeak = Number.POSITIVE_INFINITY;
182
+ let best = pairings[0];
183
+ for (const [[i, j], [k, l]] of pairings) {
184
+ const ei = eig[i];
185
+ const ej = eig[j];
186
+ const ek = eig[k];
187
+ const el = eig[l];
188
+ const mi = this.apply(ei);
189
+ const mj = this.apply(ej);
190
+ const leak = Math.abs(mi.dot(ek)) + Math.abs(mi.dot(el)) + Math.abs(mj.dot(ek)) + Math.abs(mj.dot(el));
191
+ if (leak < bestLeak) {
192
+ bestLeak = leak;
193
+ best = [
194
+ [i, j],
195
+ [k, l],
196
+ ];
197
+ }
198
+ }
199
+ const planeAndAngle = (u, v) => {
200
+ const mu = this.apply(u);
201
+ const angle = Math.atan2(v.dot(mu), u.dot(mu));
202
+ const plane = wedgeProduct(v, u).normalize();
203
+ return Rotor4.fromBivectorAngle(plane, angle);
204
+ };
205
+ const [[a, b], [c, d]] = best;
206
+ return [planeAndAngle(eig[a], eig[b]), planeAndAngle(eig[c], eig[d])];
207
+ }
208
+ /**
209
+ * The proper (Perwass-style) fix for numerical drift: {@link factor} this
210
+ * rotor into two simple rotors — each exactly renormalizable, unlike a
211
+ * compound rotor — and recompose. Prefer this over {@link normalize} once
212
+ * a rotor may have accumulated drift through repeated integration and
213
+ * could be a genuine double rotation; `normalize` remains correct (and
214
+ * cheaper) for rotors known to be simple.
215
+ */
216
+ renormalize() {
217
+ // factor() reads this rotor's action via apply() (the sandwich product
218
+ // R v R~), which scales vectors by |R|^2 for a non-unit R -- corrupting
219
+ // the "M is a pure rotation matrix" assumption factor()'s eigenstructure
220
+ // analysis depends on. A cheap magnitude normalize first removes that
221
+ // radial drift; the exact plane/angle recovery in factor() then handles
222
+ // the part naive normalize alone gets wrong (the double-rotation
223
+ // manifold), on an already magnitude-correct input.
224
+ const [r1, r2] = this.normalize().factor();
225
+ return r2.multiply(r1);
226
+ }
227
+ /**
228
+ * Build the rotor for a rotation of `angle` radians in the 2-plane `plane`
229
+ * (analogous to {@link Quaternion.fromAxisAngle}, but a *plane* rather than
230
+ * an *axis* — axes only work as rotation generators in 3D). `plane` must be
231
+ * simple (see {@link Bivector4.isSimple}); to build a double rotation,
232
+ * construct two simple rotors in orthogonal planes and {@link multiply}
233
+ * them together.
234
+ */
235
+ static fromBivectorAngle(plane, angle) {
236
+ if (!plane.isSimple()) {
237
+ throw new Error("Rotor4.fromBivectorAngle requires a simple (single-plane) bivector. " +
238
+ "To build a double rotation, multiply() two simple rotors in orthogonal planes instead.");
239
+ }
240
+ const unitPlane = plane.normalize();
241
+ const half = angle / 2;
242
+ return new Rotor4(Math.cos(half), unitPlane.scale(Math.sin(half)), 0);
243
+ }
244
+ /**
245
+ * `exp(B)`: the rotor generated by the bivector `B` -- for a *simple* `B`,
246
+ * this is a rotation of `|B|` radians in `B`'s plane, matching
247
+ * {@link fromBivectorAngle}'s convention for the common physics case of an
248
+ * already-scaled generator (e.g. `angularVelocity * dt`, where the
249
+ * bivector's magnitude *is* the rotation angle and its direction *is* the
250
+ * plane). For a *compound* `B` (e.g. an angular velocity spinning
251
+ * simultaneously in two independent planes, entirely possible physically),
252
+ * the naive `cos|B| + sin|B|·B̂` formula this reduces to for simple `B` is
253
+ * wrong: `B² ≠ -|B|²` in general (it has a nonzero pseudoscalar term too),
254
+ * so this instead decomposes `B` into its two orthogonal simple
255
+ * components `m1·Π1 + m2·Π2` and returns `exp(m2·Π2)·exp(m1·Π1)` (valid
256
+ * since orthogonal-plane generators commute). Decomposition method:
257
+ * `B`'s invariant planes are the eigenspaces of `-A²`, where `A` is `B`'s
258
+ * contraction-action matrix (`A(v) = v⌋B`, antisymmetric, so `A²` is
259
+ * symmetric negative-semidefinite) -- the bivector analogue of how
260
+ * {@link factor} finds a *rotor's* invariant planes via `M + Mᵀ`, right
261
+ * down to needing the same explicit pairing-invariance check (eigenvalue
262
+ * sort order isn't reliable when eigenvalues coincide). Returns
263
+ * {@link Identity} for the zero bivector.
264
+ */
265
+ static exp(bivector) {
266
+ if (bivector.magnitude === 0)
267
+ return Rotor4.Identity;
268
+ const basis = [Vec4.Ex, Vec4.Ey, Vec4.Ez, Vec4.Ew];
269
+ const columns = basis.map((e) => leftContraction(e, bivector).toArray());
270
+ const A = (i, j) => columns[j]?.[i];
271
+ const A2 = Vector.fromArray([0, 1, 2, 3].map((i) => Vector.fromArray([0, 1, 2, 3].map((j) => {
272
+ let sum = 0;
273
+ for (let k = 0; k < 4; k++)
274
+ sum += A(i, k) * A(k, j);
275
+ return sum;
276
+ }))));
277
+ const { vectors } = MatrixMath.eigenSymmetric(A2);
278
+ const eig = [0, 1, 2, 3].map((i) => new Vec4(vectors[0]?.[i], vectors[1]?.[i], vectors[2]?.[i], vectors[3]?.[i]));
279
+ const pairings = [
280
+ [
281
+ [0, 1],
282
+ [2, 3],
283
+ ],
284
+ [
285
+ [0, 2],
286
+ [1, 3],
287
+ ],
288
+ [
289
+ [0, 3],
290
+ [1, 2],
291
+ ],
292
+ ];
293
+ let bestLeak = Number.POSITIVE_INFINITY;
294
+ let best = pairings[0];
295
+ for (const [[i, j], [k, l]] of pairings) {
296
+ const ei = eig[i];
297
+ const ej = eig[j];
298
+ const ek = eig[k];
299
+ const el = eig[l];
300
+ const ai = leftContraction(ei, bivector);
301
+ const aj = leftContraction(ej, bivector);
302
+ const leak = Math.abs(ai.dot(ek)) + Math.abs(ai.dot(el)) + Math.abs(aj.dot(ek)) + Math.abs(aj.dot(el));
303
+ if (leak < bestLeak) {
304
+ bestLeak = leak;
305
+ best = [
306
+ [i, j],
307
+ [k, l],
308
+ ];
309
+ }
310
+ }
311
+ const planeAndMagnitude = (u, v) => {
312
+ const au = leftContraction(u, bivector);
313
+ return { m: -v.dot(au), plane: wedgeProduct(v, u).normalize() };
314
+ };
315
+ const [[a, b], [c, d]] = best;
316
+ const p1 = planeAndMagnitude(eig[a], eig[b]);
317
+ const p2 = planeAndMagnitude(eig[c], eig[d]);
318
+ return Rotor4.fromBivectorAngle(p2.plane, p2.m).multiply(Rotor4.fromBivectorAngle(p1.plane, p1.m));
319
+ }
320
+ /**
321
+ * `log()`: the bivector `B` such that `exp(B) = this`, for a *simple*
322
+ * rotor -- the inverse of {@link exp}. Throws for a compound
323
+ * (double-rotation) rotor, same as {@link toBivectorAngle} (which this is
324
+ * built on): decomposing a general rotor's log is the Perwass-factorization
325
+ * work deferred to the physics layer.
326
+ */
327
+ log(epsilon = 1e-9) {
328
+ const { plane, angle } = this.toBivectorAngle(epsilon);
329
+ return plane.scale(angle);
330
+ }
331
+ /**
332
+ * Recover `(plane, angle)` for a *simple* rotor (pseudoscalar ≈ 0). Throws
333
+ * for a compound (double-rotation) rotor — see the class-level doc comment;
334
+ * decomposing a general rotor's log is the Perwass-factorization work
335
+ * deferred to the physics layer.
336
+ */
337
+ toBivectorAngle(epsilon = 1e-9) {
338
+ if (Math.abs(this.pseudoscalar) > epsilon) {
339
+ throw new Error("Rotor4.toBivectorAngle only supports simple rotors (pseudoscalar ≈ 0); " +
340
+ "this rotor is a compound double rotation.");
341
+ }
342
+ const bMag = this.bivector.magnitude;
343
+ const angle = 2 * Math.atan2(bMag, this.scalar);
344
+ if (bMag === 0)
345
+ return { plane: Bivector4.Zero, angle: 0 };
346
+ return { plane: this.bivector.normalize(), angle };
347
+ }
348
+ /**
349
+ * Spherical linear interpolation between two rotors, `t` in `[0, 1]` --
350
+ * the 4D analogue of {@link Quaternion.slerp}, same component-wise
351
+ * treatment (an 8-vector of scalar/bivector(6)/pseudoscalar, via
352
+ * {@link dot}) and the same near-parallel fallback to a normalized
353
+ * linear interpolation to avoid dividing by a near-zero `sin(theta0)`.
354
+ *
355
+ * **Same manifold caveat as {@link normalize}**: this is only exactly
356
+ * correct for interpolating between *simple* rotors (single rotation
357
+ * plane) — a compound double rotation doesn't lie on the sphere this
358
+ * construction assumes, so slerping one is an approximation, not a
359
+ * geodesic. For a double rotation, {@link factor} each endpoint into its
360
+ * two orthogonal simple rotors and slerp each pair independently instead.
361
+ */
362
+ static slerp(a, b, t) {
363
+ const ra = a.normalize();
364
+ let rb = b.normalize();
365
+ let cos = ra.dot(rb);
366
+ if (cos < 0) {
367
+ rb = rb.scale(-1);
368
+ cos = -cos;
369
+ }
370
+ if (cos > 0.9995)
371
+ return ra.add(rb.subtract(ra).scale(t)).normalize();
372
+ const theta0 = Math.acos(cos);
373
+ const theta = theta0 * t;
374
+ const sinTheta0 = Math.sin(theta0);
375
+ const s0 = Math.sin(theta0 - theta) / sinTheta0;
376
+ const s1 = Math.sin(theta) / sinTheta0;
377
+ return ra.scale(s0).add(rb.scale(s1));
378
+ }
379
+ equals(r, epsilon = 0) {
380
+ return ((epsilon === 0 ? this.scalar === r.scalar : Math.abs(this.scalar - r.scalar) <= epsilon) &&
381
+ this.bivector.equals(r.bivector, epsilon) &&
382
+ (epsilon === 0 ? this.pseudoscalar === r.pseudoscalar : Math.abs(this.pseudoscalar - r.pseudoscalar) <= epsilon));
383
+ }
384
+ toString() {
385
+ return `${this.scalar} + (${this.bivector.toString()}) + ${this.pseudoscalar}e1234`;
386
+ }
387
+ }
388
+ //# sourceMappingURL=Rotor4.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Rotor4.js","sourceRoot":"","sources":["../src/Rotor4.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,kBAAkB,EAAqB,MAAM,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACtH,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,OAAO,MAAM;IACR,MAAM,CAAS;IACf,QAAQ,CAAY;IACpB,YAAY,CAAS;IAE9B,MAAM,CAAU,QAAQ,GAAG,IAAI,MAAM,CAAC,CAAC,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAE5D,YAAY,MAAM,GAAG,CAAC,EAAE,WAAsB,SAAS,CAAC,IAAI,EAAE,YAAY,GAAG,CAAC;QAC5E,IAAI,CAAC,MAAM,GAAG,MAAM,C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AK,CAAC,EAAE,CAAC,CAAC,CAAC;IACxC,CAAC;IAED,MAAM,CAAC,CAAS,EAAE,OAAO,GAAG,CAAC;QAC3B,OAAO,CACL,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC;YACxF,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC;YACzC,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,CACjH,CAAC;IACJ,CAAC;IAED,QAAQ;QACN,OAAO,GAAG,IAAI,CAAC,MAAM,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,IAAI,CAAC,YAAY,OAAO,CAAC;IACtF,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * SpecialFunctions — the higher transcendental functions that statistics and
3
+ * analysis rely on: the gamma and log-gamma functions (Lanczos), the beta
4
+ * function, the error function `erf`/`erfc`, and the regularised incomplete
5
+ * gamma and beta functions (which give the distribution CDFs in
6
+ * {@link Distributions} exact closed forms rather than numeric integration).
7
+ */
8
+ export declare class SpecialFunctions {
9
+ /** The gamma function Γ(x) (Lanczos approximation). */
10
+ static gamma(x: number): number;
11
+ /** The natural log of |Γ(x)| — stable for large arguments. */
12
+ static lnGamma(x: number): number;
13
+ /** The beta function B(a, b) = Γ(a)Γ(b)/Γ(a+b). */
14
+ static beta(a: number, b: number): number;
15
+ /** The error function erf(x). */
16
+ static erf(x: number): number;
17
+ /** The complementary error function erfc(x) = 1 − erf(x). */
18
+ static erfc(x: number): number;
19
+ /**
20
+ * The regularised lower incomplete gamma function P(a, x) = γ(a, x)/Γ(a).
21
+ * (This is the CDF of the gamma/chi-square distributions.)
22
+ */
23
+ static regularizedGammaP(a: number, x: number): number;
24
+ /** The regularised upper incomplete gamma function Q(a, x) = 1 − P(a, x). */
25
+ static regularizedGammaQ(a: number, x: number): number;
26
+ /**
27
+ * The regularised incomplete beta function I_x(a, b). (This is the CDF of the
28
+ * beta distribution and underlies the Student-t and F distributions.)
29
+ */
30
+ static regularizedIncompleteBeta(x: number, a: number, b: number): number;
31
+ }
32
+ //# sourceMappingURL=SpecialFunctions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SpecialFunctions.d.ts","sourceRoot":"","sources":["../src/SpecialFunctions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,qBAAa,gBAAgB;IAC3B,uDAAuD;IACvD,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAS/B,8DAA8D;IAC9D,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IASjC,mDAAmD;IACnD,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;IAIzC,iCAAiC;IACjC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAY7B,6DAA6D;IAC7D,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI9B;;;OAGG;IACH,MAAM,CAAC,iBAAiB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;IAsCtD,6EAA6E;IAC7E,MAAM,CAAC,iBAAiB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;IAItD;;;OAGG;IACH,MAAM,CAAC,yBAAyB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM;CAS1E"}
@@ -0,0 +1,161 @@
1
+ /**
2
+ * SpecialFunctions — the higher transcendental functions that statistics and
3
+ * analysis rely on: the gamma and log-gamma functions (Lanczos), the beta
4
+ * function, the error function `erf`/`erfc`, and the regularised incomplete
5
+ * gamma and beta functions (which give the distribution CDFs in
6
+ * {@link Distributions} exact closed forms rather than numeric integration).
7
+ */
8
+ const LANCZOS_G = 7;
9
+ const LANCZOS = [
10
+ 0.9999999999998099, 676.5203681218851, -1259.1392167224028, 771.3234287776531, -176.6150291621406, 12.507343278686905,
11
+ -0.13857109526572012, 9.984369578019572e-6, 1.5056327351493116e-7,
12
+ ];
13
+ export class SpecialFunctions {
14
+ /** The gamma function Γ(x) (Lanczos approximation). */
15
+ static gamma(x) {
16
+ if (x < 0.5)
17
+ return Math.PI / (Math.sin(Math.PI * x) * SpecialFunctions.gamma(1 - x));
18
+ const z = x - 1;
19
+ let a = LANCZOS[0];
20
+ const t = z + LANCZOS_G + 0.5;
21
+ for (let i = 1; i < LANCZOS.length; i++)
22
+ a += LANCZOS[i] / (z + i);
23
+ return Math.sqrt(2 * Math.PI) * t ** (z + 0.5) * Math.exp(-t) * a;
24
+ }
25
+ /** The natural log of |Γ(x)| — stable for large arguments. */
26
+ static lnGamma(x) {
27
+ if (x < 0.5)
28
+ return Math.log(Math.abs(Math.PI / Math.sin(Math.PI * x))) - SpecialFunctions.lnGamma(1 - x);
29
+ const z = x - 1;
30
+ let a = LANCZOS[0];
31
+ const t = z + LANCZOS_G + 0.5;
32
+ for (let i = 1; i < LANCZOS.length; i++)
33
+ a += LANCZOS[i] / (z + i);
34
+ return 0.5 * Math.log(2 * Math.PI) + (z + 0.5) * Math.log(t) - t + Math.log(a);
35
+ }
36
+ /** The beta function B(a, b) = Γ(a)Γ(b)/Γ(a+b). */
37
+ static beta(a, b) {
38
+ return Math.exp(SpecialFunctions.lnGamma(a) + SpecialFunctions.lnGamma(b) - SpecialFunctions.lnGamma(a + b));
39
+ }
40
+ /** The error function erf(x). */
41
+ static erf(x) {
42
+ const sign = x < 0 ? -1 : 1;
43
+ const ax = Math.abs(x);
44
+ const t = 1 / (1 + 0.3275911 * ax);
45
+ const y = 1 -
46
+ ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) *
47
+ t *
48
+ Math.exp(-ax * ax);
49
+ return sign * y;
50
+ }
51
+ /** The complementary error function erfc(x) = 1 − erf(x). */
52
+ static erfc(x) {
53
+ return 1 - SpecialFunctions.erf(x);
54
+ }
55
+ /**
56
+ * The regularised lower incomplete gamma function P(a, x) = γ(a, x)/Γ(a).
57
+ * (This is the CDF of the gamma/chi-square distributions.)
58
+ */
59
+ static regularizedGammaP(a, x) {
60
+ if (x < 0 || a <= 0)
61
+ return Number.NaN;
62
+ if (x === 0)
63
+ return 0;
64
+ if (x < a + 1) {
65
+ // series expansion
66
+ let ap = a;
67
+ let sum = 1 / a;
68
+ let del = sum;
69
+ for (let n = 0; n < 200; n++) {
70
+ ap++;
71
+ del *= x / ap;
72
+ sum += del;
73
+ if (Math.abs(del) < Math.abs(sum) * 1e-15)
74
+ break;
75
+ }
76
+ return sum * Math.exp(-x + a * Math.log(x) - SpecialFunctions.lnGamma(a));
77
+ }
78
+ // continued fraction for the upper part Q, then P = 1 - Q
79
+ const tiny = 1e-300;
80
+ let b = x + 1 - a;
81
+ let c = 1 / tiny;
82
+ let d = 1 / b;
83
+ let h = d;
84
+ for (let i = 1; i < 200; i++) {
85
+ const an = -i * (i - a);
86
+ b += 2;
87
+ d = an * d + b;
88
+ if (Math.abs(d) < tiny)
89
+ d = tiny;
90
+ c = b + an / c;
91
+ if (Math.abs(c) < tiny)
92
+ c = tiny;
93
+ d = 1 / d;
94
+ const del = d * c;
95
+ h *= del;
96
+ if (Math.abs(del - 1) < 1e-15)
97
+ break;
98
+ }
99
+ const q = Math.exp(-x + a * Math.log(x) - SpecialFunctions.lnGamma(a)) * h;
100
+ return 1 - q;
101
+ }
102
+ /** The regularised upper incomplete gamma function Q(a, x) = 1 − P(a, x). */
103
+ static regularizedGammaQ(a, x) {
104
+ return 1 - SpecialFunctions.regularizedGammaP(a, x);
105
+ }
106
+ /**
107
+ * The regularised incomplete beta function I_x(a, b). (This is the CDF of the
108
+ * beta distribution and underlies the Student-t and F distributions.)
109
+ */
110
+ static regularizedIncompleteBeta(x, a, b) {
111
+ if (x <= 0)
112
+ return 0;
113
+ if (x >= 1)
114
+ return 1;
115
+ const lbeta = SpecialFunctions.lnGamma(a + b) - SpecialFunctions.lnGamma(a) - SpecialFunctions.lnGamma(b);
116
+ const front = Math.exp(lbeta + a * Math.log(x) + b * Math.log(1 - x));
117
+ // Use the continued fraction that converges fastest, swapping (a,b,x) as needed.
118
+ if (x < (a + 1) / (a + b + 2))
119
+ return (front * betaContinuedFraction(a, b, x)) / a;
120
+ return 1 - (front * betaContinuedFraction(b, a, 1 - x)) / b;
121
+ }
122
+ }
123
+ /** Lentz's continued fraction for the incomplete beta (Numerical Recipes `betacf`). */
124
+ function betaContinuedFraction(a, b, x) {
125
+ const tiny = 1e-300;
126
+ const qab = a + b;
127
+ const qap = a + 1;
128
+ const qam = a - 1;
129
+ let c = 1;
130
+ let d = 1 - (qab * x) / qap;
131
+ if (Math.abs(d) < tiny)
132
+ d = tiny;
133
+ d = 1 / d;
134
+ let h = d;
135
+ for (let m = 1; m < 200; m++) {
136
+ const m2 = 2 * m;
137
+ let aa = (m * (b - m) * x) / ((qam + m2) * (a + m2));
138
+ d = 1 + aa * d;
139
+ if (Math.abs(d) < tiny)
140
+ d = tiny;
141
+ c = 1 + aa / c;
142
+ if (Math.abs(c) < tiny)
143
+ c = tiny;
144
+ d = 1 / d;
145
+ h *= d * c;
146
+ aa = (-(a + m) * (qab + m) * x) / ((a + m2) * (qap + m2));
147
+ d = 1 + aa * d;
148
+ if (Math.abs(d) < tiny)
149
+ d = tiny;
150
+ c = 1 + aa / c;
151
+ if (Math.abs(c) < tiny)
152
+ c = tiny;
153
+ d = 1 / d;
154
+ const del = d * c;
155
+ h *= del;
156
+ if (Math.abs(del - 1) < 1e-15)
157
+ break;
158
+ }
159
+ return h;
160
+ }
161
+ //# sourceMappingURL=SpecialFunctions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SpecialFunctions.js","sourceRoot":"","sources":["../src/SpecialFunctions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,MAAM,OAAO,GAAG;IACd,kBAAkB,EAAE,iBAAiB,EAAE,CAAC,kBAAkB,EAAE,iBAAiB,EAAE,CAAC,iBAAiB,EAAE,kBAAkB;IACrH,CAAC,mBAAmB,EAAE,oBAAoB,EAAE,qBAAqB;CAClE,CAAC;AAEF,MAAM,OAAO,gBAAgB;IAC3B,uDAAuD;IACvD,MAAM,CAAC,KAAK,CAAC,CAAS;QACpB,IAAI,CAAC,GAAG,GAAG;YAAE,OAAO,IAAI,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACtF,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QACnB,MAAM,CAAC,GAAG,CAAC,GAAG,SAAS,GAAG,GAAG,CAAC;QAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE;YAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACnE,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,GAAG,CAAC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AAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI;YAAE,CAAC,GAAG,IAAI,CAAC;QACjC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACV,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACX,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC;QAC1D,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACf,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI;YAAE,CAAC,GAAG,IAAI,CAAC;QACjC,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACf,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI;YAAE,CAAC,GAAG,IAAI,CAAC;QACjC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACV,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC,IAAI,GAAG,CAAC;QACT,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,KAAK;YAAE,MAAM;IACvC,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * SpecialOperator — a named symbolic operator (`+`, `-`, `*`, …) pairing a
3
+ * textual representation (`rep`) with the name of the function that implements it
4
+ * (`funct`). Ported from Mallory's ActionScript `SpecialOperator`; used by the
5
+ * expression evaluator.
6
+ */
7
+ export declare class SpecialOperator {
8
+ readonly rep: string;
9
+ readonly funct: string | null;
10
+ constructor(rep: string, funct: string | null);
11
+ toString(): string;
12
+ static readonly Plus: SpecialOperator;
13
+ static readonly Minus: SpecialOperator;
14
+ static readonly Times: SpecialOperator;
15
+ static readonly Divided: SpecialOperator;
16
+ static readonly Power: SpecialOperator;
17
+ static readonly Modulous: SpecialOperator;
18
+ static readonly Elipses: SpecialOperator;
19
+ static readonly System: SpecialOperator;
20
+ }
21
+ //# sourceMappingURL=SpecialOperator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SpecialOperator.d.ts","sourceRoot":"","sources":["../src/SpecialOperator.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,eAAe;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;gBAElB,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAK7C,QAAQ,IAAI,MAAM;IAIlB,MAAM,CAAC,QAAQ,CAAC,IAAI,kBAAmC;IACvD,MAAM,CAAC,QAAQ,CAAC,KAAK,kBAAwC;IAC7D,MAAM,CAAC,QAAQ,CAAC,KAAK,kBAAwC;IAC7D,MAAM,CAAC,QAAQ,CAAC,OAAO,kBAAsC;IAC7D,MAAM,CAAC,QAAQ,CAAC,KAAK,kBAAqC;IAC1D,MAAM,CAAC,QAAQ,CAAC,QAAQ,kBAAmC;IAC3D,MAAM,CAAC,QAAQ,CAAC,OAAO,kBAAoC;IAC3D,MAAM,CAAC,QAAQ,CAAC,MAAM,kBAAkC;CACzD"}