@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.
- package/README.md +165 -0
- package/dist/Bivector4.d.ts +52 -0
- package/dist/Bivector4.d.ts.map +1 -0
- package/dist/Bivector4.js +105 -0
- package/dist/Bivector4.js.map +1 -0
- package/dist/CellGraph.d.ts +259 -0
- package/dist/CellGraph.d.ts.map +1 -0
- package/dist/CellGraph.js +637 -0
- package/dist/CellGraph.js.map +1 -0
- package/dist/Clifford4Internal.d.ts +35 -0
- package/dist/Clifford4Internal.d.ts.map +1 -0
- package/dist/Clifford4Internal.js +86 -0
- package/dist/Clifford4Internal.js.map +1 -0
- package/dist/Combinatorics.d.ts +61 -0
- package/dist/Combinatorics.d.ts.map +1 -0
- package/dist/Combinatorics.js +186 -0
- package/dist/Combinatorics.js.map +1 -0
- package/dist/CombinatoricsGenerators.d.ts +44 -0
- package/dist/CombinatoricsGenerators.d.ts.map +1 -0
- package/dist/CombinatoricsGenerators.js +148 -0
- package/dist/CombinatoricsGenerators.js.map +1 -0
- package/dist/ComplexMath.d.ts +46 -0
- package/dist/ComplexMath.d.ts.map +1 -0
- package/dist/ComplexMath.js +98 -0
- package/dist/ComplexMath.js.map +1 -0
- package/dist/ComplexNumber.d.ts +174 -0
- package/dist/ComplexNumber.d.ts.map +1 -0
- package/dist/ComplexNumber.js +443 -0
- package/dist/ComplexNumber.js.map +1 -0
- package/dist/Cycle.d.ts +42 -0
- package/dist/Cycle.d.ts.map +1 -0
- package/dist/Cycle.js +120 -0
- package/dist/Cycle.js.map +1 -0
- package/dist/Decimal.d.ts +45 -0
- package/dist/Decimal.d.ts.map +1 -0
- package/dist/Decimal.js +135 -0
- package/dist/Decimal.js.map +1 -0
- package/dist/Distributions.d.ts +55 -0
- package/dist/Distributions.d.ts.map +1 -0
- package/dist/Distributions.js +222 -0
- package/dist/Distributions.js.map +1 -0
- package/dist/DualNumber.d.ts +42 -0
- package/dist/DualNumber.d.ts.map +1 -0
- package/dist/DualNumber.js +91 -0
- package/dist/DualNumber.js.map +1 -0
- package/dist/Environment.d.ts +44 -0
- package/dist/Environment.d.ts.map +1 -0
- package/dist/Environment.js +129 -0
- package/dist/Environment.js.map +1 -0
- package/dist/Expression.d.ts +19 -0
- package/dist/Expression.d.ts.map +1 -0
- package/dist/Expression.js +43 -0
- package/dist/Expression.js.map +1 -0
- package/dist/FFT.d.ts +21 -0
- package/dist/FFT.d.ts.map +1 -0
- package/dist/FFT.js +136 -0
- package/dist/FFT.js.map +1 -0
- package/dist/GeometricAlgebra4.d.ts +73 -0
- package/dist/GeometricAlgebra4.d.ts.map +1 -0
- package/dist/GeometricAlgebra4.js +115 -0
- package/dist/GeometricAlgebra4.js.map +1 -0
- package/dist/Geometry.d.ts +41 -0
- package/dist/Geometry.d.ts.map +1 -0
- package/dist/Geometry.js +167 -0
- package/dist/Geometry.js.map +1 -0
- package/dist/Graph.d.ts +72 -0
- package/dist/Graph.d.ts.map +1 -0
- package/dist/Graph.js +337 -0
- package/dist/Graph.js.map +1 -0
- package/dist/Graph3DUtils.d.ts +80 -0
- package/dist/Graph3DUtils.d.ts.map +1 -0
- package/dist/Graph3DUtils.js +156 -0
- package/dist/Graph3DUtils.js.map +1 -0
- package/dist/GraphUtils.d.ts +67 -0
- package/dist/GraphUtils.d.ts.map +1 -0
- package/dist/GraphUtils.js +72 -0
- package/dist/GraphUtils.js.map +1 -0
- package/dist/GroupTheory.d.ts +73 -0
- package/dist/GroupTheory.d.ts.map +1 -0
- package/dist/GroupTheory.js +194 -0
- package/dist/GroupTheory.js.map +1 -0
- package/dist/IntUtils.d.ts +33 -0
- package/dist/IntUtils.d.ts.map +1 -0
- package/dist/IntUtils.js +185 -0
- package/dist/IntUtils.js.map +1 -0
- package/dist/Interval.d.ts +44 -0
- package/dist/Interval.d.ts.map +1 -0
- package/dist/Interval.js +165 -0
- package/dist/Interval.js.map +1 -0
- package/dist/Lattice.d.ts +48 -0
- package/dist/Lattice.d.ts.map +1 -0
- package/dist/Lattice.js +65 -0
- package/dist/Lattice.js.map +1 -0
- package/dist/Logic.d.ts +22 -0
- package/dist/Logic.d.ts.map +1 -0
- package/dist/Logic.js +34 -0
- package/dist/Logic.js.map +1 -0
- package/dist/MatrixMath.d.ts +71 -0
- package/dist/MatrixMath.d.ts.map +1 -0
- package/dist/MatrixMath.js +302 -0
- package/dist/MatrixMath.js.map +1 -0
- package/dist/NumberTheory.d.ts +49 -0
- package/dist/NumberTheory.d.ts.map +1 -0
- package/dist/NumberTheory.js +230 -0
- package/dist/NumberTheory.js.map +1 -0
- package/dist/Numerical.d.ts +85 -0
- package/dist/Numerical.d.ts.map +1 -0
- package/dist/Numerical.js +343 -0
- package/dist/Numerical.js.map +1 -0
- package/dist/Permutation.d.ts +43 -0
- package/dist/Permutation.d.ts.map +1 -0
- package/dist/Permutation.js +142 -0
- package/dist/Permutation.js.map +1 -0
- package/dist/Polygon.d.ts +64 -0
- package/dist/Polygon.d.ts.map +1 -0
- package/dist/Polygon.js +221 -0
- package/dist/Polygon.js.map +1 -0
- package/dist/PolynomialRing.d.ts +74 -0
- package/dist/PolynomialRing.d.ts.map +1 -0
- package/dist/PolynomialRing.js +231 -0
- package/dist/PolynomialRing.js.map +1 -0
- package/dist/Quaternion.d.ts +37 -0
- package/dist/Quaternion.d.ts.map +1 -0
- package/dist/Quaternion.js +106 -0
- package/dist/Quaternion.js.map +1 -0
- package/dist/Rational.d.ts +34 -0
- package/dist/Rational.d.ts.map +1 -0
- package/dist/Rational.js +136 -0
- package/dist/Rational.js.map +1 -0
- package/dist/RealMath.d.ts +112 -0
- package/dist/RealMath.d.ts.map +1 -0
- package/dist/RealMath.js +262 -0
- package/dist/RealMath.js.map +1 -0
- package/dist/Rotor4.d.ts +180 -0
- package/dist/Rotor4.d.ts.map +1 -0
- package/dist/Rotor4.js +388 -0
- package/dist/Rotor4.js.map +1 -0
- package/dist/SpecialFunctions.d.ts +32 -0
- package/dist/SpecialFunctions.d.ts.map +1 -0
- package/dist/SpecialFunctions.js +161 -0
- package/dist/SpecialFunctions.js.map +1 -0
- package/dist/SpecialOperator.d.ts +21 -0
- package/dist/SpecialOperator.d.ts.map +1 -0
- package/dist/SpecialOperator.js +26 -0
- package/dist/SpecialOperator.js.map +1 -0
- package/dist/Statistics.d.ts +50 -0
- package/dist/Statistics.d.ts.map +1 -0
- package/dist/Statistics.js +131 -0
- package/dist/Statistics.js.map +1 -0
- package/dist/StringEvaluator.d.ts +48 -0
- package/dist/StringEvaluator.d.ts.map +1 -0
- package/dist/StringEvaluator.js +190 -0
- package/dist/StringEvaluator.js.map +1 -0
- package/dist/Structure.d.ts +130 -0
- package/dist/Structure.d.ts.map +1 -0
- package/dist/Structure.js +394 -0
- package/dist/Structure.js.map +1 -0
- package/dist/Symbolic.d.ts +501 -0
- package/dist/Symbolic.d.ts.map +1 -0
- package/dist/Symbolic.js +4077 -0
- package/dist/Symbolic.js.map +1 -0
- package/dist/Type.d.ts +31 -0
- package/dist/Type.d.ts.map +1 -0
- package/dist/Type.js +47 -0
- package/dist/Type.js.map +1 -0
- package/dist/Utilities.d.ts +27 -0
- package/dist/Utilities.d.ts.map +1 -0
- package/dist/Utilities.js +47 -0
- package/dist/Utilities.js.map +1 -0
- package/dist/Vec4.d.ts +39 -0
- package/dist/Vec4.d.ts.map +1 -0
- package/dist/Vec4.js +82 -0
- package/dist/Vec4.js.map +1 -0
- package/dist/Vector.d.ts +83 -0
- package/dist/Vector.d.ts.map +1 -0
- package/dist/Vector.js +173 -0
- package/dist/Vector.js.map +1 -0
- package/dist/VectorCalculus.d.ts +42 -0
- package/dist/VectorCalculus.d.ts.map +1 -0
- package/dist/VectorCalculus.js +82 -0
- package/dist/VectorCalculus.js.map +1 -0
- package/dist/VectorUtils.d.ts +172 -0
- package/dist/VectorUtils.d.ts.map +1 -0
- package/dist/VectorUtils.js +706 -0
- package/dist/VectorUtils.js.map +1 -0
- package/dist/differentiation-rules.d.ts +21 -0
- package/dist/differentiation-rules.d.ts.map +1 -0
- package/dist/differentiation-rules.js +52 -0
- package/dist/differentiation-rules.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
- package/src/Bivector4.ts +136 -0
- package/src/CellGraph.ts +669 -0
- package/src/Clifford4Internal.ts +90 -0
- package/src/Combinatorics.ts +183 -0
- package/src/CombinatoricsGenerators.ts +144 -0
- package/src/ComplexMath.ts +162 -0
- package/src/ComplexNumber.ts +491 -0
- package/src/Cycle.ts +126 -0
- package/src/Decimal.ts +145 -0
- package/src/Distributions.ts +269 -0
- package/src/DualNumber.ts +105 -0
- package/src/Environment.ts +127 -0
- package/src/Expression.ts +48 -0
- package/src/FFT.ts +146 -0
- package/src/GeometricAlgebra4.ts +157 -0
- package/src/Geometry.ts +173 -0
- package/src/Graph.ts +337 -0
- package/src/Graph3DUtils.ts +269 -0
- package/src/GraphUtils.ts +159 -0
- package/src/GroupTheory.ts +214 -0
- package/src/IntUtils.ts +200 -0
- package/src/Interval.ts +171 -0
- package/src/Lattice.ts +87 -0
- package/src/Logic.ts +38 -0
- package/src/MatrixMath.ts +326 -0
- package/src/NumberTheory.ts +228 -0
- package/src/Numerical.ts +390 -0
- package/src/Permutation.ts +148 -0
- package/src/Polygon.ts +229 -0
- package/src/PolynomialRing.ts +242 -0
- package/src/Quaternion.ts +125 -0
- package/src/Rational.ts +148 -0
- package/src/RealMath.ts +346 -0
- package/src/Rotor4.ts +440 -0
- package/src/SpecialFunctions.ts +152 -0
- package/src/SpecialOperator.ts +28 -0
- package/src/Statistics.ts +170 -0
- package/src/StringEvaluator.ts +208 -0
- package/src/Structure.ts +481 -0
- package/src/Symbolic.ts +4313 -0
- package/src/Type.ts +48 -0
- package/src/Utilities.ts +49 -0
- package/src/Vec4.ts +98 -0
- package/src/Vector.ts +189 -0
- package/src/VectorCalculus.ts +87 -0
- package/src/VectorUtils.ts +797 -0
- package/src/differentiation-rules.ts +73 -0
- package/src/index.ts +140 -0
package/dist/RealMath.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { Vector } from "./Vector.js";
|
|
2
|
+
import { VectorUtils } from "./VectorUtils.js";
|
|
3
|
+
// -- constants ---------------------------------------------------------------
|
|
4
|
+
export const Zero = 0;
|
|
5
|
+
export const One = 1;
|
|
6
|
+
export const E = Math.E;
|
|
7
|
+
export const PI = Math.PI;
|
|
8
|
+
export const PHI = 1.61803399;
|
|
9
|
+
export const PositiveInfinity = Infinity;
|
|
10
|
+
export const NegativeInfinity = -Infinity;
|
|
11
|
+
// -- Chapter 1: utilities -----------------------------------------------------
|
|
12
|
+
export function radiansToDegrees(r) {
|
|
13
|
+
return (180 / Math.PI) * r;
|
|
14
|
+
}
|
|
15
|
+
export function degreesToRadians(d) {
|
|
16
|
+
return (Math.PI / 180) * d;
|
|
17
|
+
}
|
|
18
|
+
// -- Chapter 2: comparison -----------------------------------------------------
|
|
19
|
+
/** Equality within `distance` (bug fix: AS3 ignored the tolerance). */
|
|
20
|
+
export function equal(a, b, distance = 0) {
|
|
21
|
+
return Math.abs(a - b) <= distance;
|
|
22
|
+
}
|
|
23
|
+
export function compare(a, b) {
|
|
24
|
+
if (a > b)
|
|
25
|
+
return 1;
|
|
26
|
+
if (a < b)
|
|
27
|
+
return -1;
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
export function lessThan(a, b) {
|
|
31
|
+
return a < b;
|
|
32
|
+
}
|
|
33
|
+
export function greaterThan(a, b) {
|
|
34
|
+
return a > b;
|
|
35
|
+
}
|
|
36
|
+
// -- Chapter 3: unary -----------------------------------------------------------
|
|
37
|
+
export function negative(a = 0) {
|
|
38
|
+
return -a;
|
|
39
|
+
}
|
|
40
|
+
/** `1/a` (matches `divide`'s directed-infinity convention rather than always returning NaN). */
|
|
41
|
+
export function reciprocal(a = 1) {
|
|
42
|
+
return 1 / a;
|
|
43
|
+
}
|
|
44
|
+
/** Round away tiny floating-point noise (rounds to 10 decimal places). */
|
|
45
|
+
export function identity(a = 1) {
|
|
46
|
+
return roundTo(a);
|
|
47
|
+
}
|
|
48
|
+
// -- Chapter 4: binary -----------------------------------------------------------
|
|
49
|
+
export function add(a = 0, b = 0) {
|
|
50
|
+
return a + b;
|
|
51
|
+
}
|
|
52
|
+
/** Subtraction (bug fix: AS3 returned `a*b`). */
|
|
53
|
+
export function subtract(a = 0, b = 0) {
|
|
54
|
+
return a - b;
|
|
55
|
+
}
|
|
56
|
+
export function multiply(a = 1, b = 1) {
|
|
57
|
+
return a * b;
|
|
58
|
+
}
|
|
59
|
+
export function divide(a = 1, b = 1) {
|
|
60
|
+
if (b === 0) {
|
|
61
|
+
if (a > 0)
|
|
62
|
+
return Infinity;
|
|
63
|
+
if (a < 0)
|
|
64
|
+
return -Infinity;
|
|
65
|
+
return NaN;
|
|
66
|
+
}
|
|
67
|
+
return a / b;
|
|
68
|
+
}
|
|
69
|
+
// -- Chapter 5: exponential / logarithmic -----------------------------------------
|
|
70
|
+
export function power(a = 1, exponent = 1) {
|
|
71
|
+
return a ** exponent;
|
|
72
|
+
}
|
|
73
|
+
export function square(a = 1) {
|
|
74
|
+
return a ** 2;
|
|
75
|
+
}
|
|
76
|
+
export function squareRoot(a = 1) {
|
|
77
|
+
return Math.sqrt(a);
|
|
78
|
+
}
|
|
79
|
+
export function logarithm(a = 1, base = Math.E) {
|
|
80
|
+
return Math.log(a) / Math.log(base);
|
|
81
|
+
}
|
|
82
|
+
// -- Chapter 6: trigonometry -----------------------------------------------------
|
|
83
|
+
export function sine(a = 0) {
|
|
84
|
+
return identity(Math.sin(a));
|
|
85
|
+
}
|
|
86
|
+
export function cosine(a = 0) {
|
|
87
|
+
return identity(Math.cos(a));
|
|
88
|
+
}
|
|
89
|
+
export function tangent(a = 0) {
|
|
90
|
+
return identity(Math.tan(a));
|
|
91
|
+
}
|
|
92
|
+
export function cosecant(a = 0) {
|
|
93
|
+
return identity(1 / Math.sin(a));
|
|
94
|
+
}
|
|
95
|
+
export function secant(a = 0) {
|
|
96
|
+
return identity(1 / Math.cos(a));
|
|
97
|
+
}
|
|
98
|
+
export function cotangent(a = 0) {
|
|
99
|
+
return identity(1 / Math.tan(a));
|
|
100
|
+
}
|
|
101
|
+
export function hyperbolicSine(a) {
|
|
102
|
+
return identity(0.5 * (Math.exp(a) - Math.exp(-a)));
|
|
103
|
+
}
|
|
104
|
+
export function hyperbolicCosine(a) {
|
|
105
|
+
return identity(0.5 * (Math.exp(a) + Math.exp(-a)));
|
|
106
|
+
}
|
|
107
|
+
export function hyperbolicTangent(a) {
|
|
108
|
+
return hyperbolicSine(a) / hyperbolicCosine(a);
|
|
109
|
+
}
|
|
110
|
+
export function hyperbolicCosecant(a) {
|
|
111
|
+
return 1 / hyperbolicSine(a);
|
|
112
|
+
}
|
|
113
|
+
export function hyperbolicSecant(a) {
|
|
114
|
+
return 1 / hyperbolicCosine(a);
|
|
115
|
+
}
|
|
116
|
+
export function hyperbolicCotangent(a) {
|
|
117
|
+
return hyperbolicCosine(a) / hyperbolicSine(a);
|
|
118
|
+
}
|
|
119
|
+
// -- Chapter 7: series, algebra, calculus -----------------------------------------
|
|
120
|
+
export function sumSeries(unaryOperation, start = 0, end = 0) {
|
|
121
|
+
return VectorUtils.collapse(VectorUtils.transform(VectorUtils.arithmeticSequence(start, end), unaryOperation), (a, b) => a + b, 0);
|
|
122
|
+
}
|
|
123
|
+
export function productSeries(unaryOperation, start = 0, end = 0) {
|
|
124
|
+
return VectorUtils.collapse(VectorUtils.transform(VectorUtils.arithmeticSequence(start, end), unaryOperation), (a, b) => a * b, 1);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Newton–Raphson root finder for `unaryOperation(x) == target`.
|
|
128
|
+
*
|
|
129
|
+
* Bug fix: the AS3 version updated `guess -= (f(guess) - target)`, i.e. Newton
|
|
130
|
+
* with an assumed derivative of 1 — it diverges for almost any nonlinear
|
|
131
|
+
* function. Here the derivative is estimated numerically ({@link differentiateN})
|
|
132
|
+
* so the iteration actually converges, and unbounded recursion is replaced by a
|
|
133
|
+
* bounded loop.
|
|
134
|
+
*/
|
|
135
|
+
export function solveN(unaryOperation, target, desiredAccuracy = 0.01, guess = 0, maxIterations = 10000) {
|
|
136
|
+
let x = guess;
|
|
137
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
138
|
+
const difference = unaryOperation(x) - target;
|
|
139
|
+
if (Math.abs(difference) <= desiredAccuracy)
|
|
140
|
+
return x;
|
|
141
|
+
const slope = differentiateN(unaryOperation, x, 1e-6);
|
|
142
|
+
if (slope === 0 || !Number.isFinite(slope))
|
|
143
|
+
break;
|
|
144
|
+
x = x - difference / slope;
|
|
145
|
+
}
|
|
146
|
+
return x;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Numeric integral via the midpoint rule (bug fix: the AS3 code sampled at
|
|
150
|
+
* `2*lowerLimit+interval` and had an unreachable return).
|
|
151
|
+
*/
|
|
152
|
+
export function integrateN(unaryOperation, lowerLimit, upperLimit, interval = 0.01) {
|
|
153
|
+
if (interval <= 0)
|
|
154
|
+
throw new RangeError(`RealMath.integrateN: interval must be positive, got ${interval}`);
|
|
155
|
+
let result = 0;
|
|
156
|
+
for (let x = lowerLimit; x < upperLimit; x += interval) {
|
|
157
|
+
result += unaryOperation(x + interval / 2);
|
|
158
|
+
}
|
|
159
|
+
return interval * result;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Numeric derivative via the symmetric difference quotient (bug fix: the AS3
|
|
163
|
+
* code was a forward difference despite the "symmetric" comment).
|
|
164
|
+
*/
|
|
165
|
+
export function differentiateN(unaryOperation, point, limit = 0.01) {
|
|
166
|
+
if (limit <= 0)
|
|
167
|
+
throw new RangeError(`RealMath.differentiateN: limit must be positive, got ${limit}`);
|
|
168
|
+
return (unaryOperation(point + limit) - unaryOperation(point - limit)) / (2 * limit);
|
|
169
|
+
}
|
|
170
|
+
// -- Chapter 9: probability -----------------------------------------------------
|
|
171
|
+
export function normalDistribution(mean, sDev) {
|
|
172
|
+
return (x) => (1 / (sDev * Math.sqrt(2 * Math.PI))) * Math.E ** (-0.5 * ((x - mean) / sDev) ** 2);
|
|
173
|
+
}
|
|
174
|
+
export function standardNormalDistribution(x) {
|
|
175
|
+
return normalDistribution(0, 1)(x);
|
|
176
|
+
}
|
|
177
|
+
export function normalProbability(x, mean, sDev, negativeInfinityApproximation = -5, integrationInterval = 0.0001) {
|
|
178
|
+
return integrateN(normalDistribution(mean, sDev), negativeInfinityApproximation, x, integrationInterval);
|
|
179
|
+
}
|
|
180
|
+
export function standardNormalProbability(z, negativeInfinityApproximation = -5, interval = 0.0001) {
|
|
181
|
+
return integrateN(standardNormalDistribution, negativeInfinityApproximation, z, interval);
|
|
182
|
+
}
|
|
183
|
+
// -- Chapter 8: precision & randomness -----------------------------------------
|
|
184
|
+
/** Round to `precision` decimal places (replaces the buggy Flex NumberFormatter). */
|
|
185
|
+
export function roundTo(num, precision = 10) {
|
|
186
|
+
if (!Number.isFinite(num))
|
|
187
|
+
return num;
|
|
188
|
+
const factor = 10 ** precision;
|
|
189
|
+
return Math.round(num * factor) / factor;
|
|
190
|
+
}
|
|
191
|
+
/** Uniform random in `[min(r1,r2), max(r1,r2)]`. */
|
|
192
|
+
export function random(r1 = 1, r2 = 0, _inclusive = true) {
|
|
193
|
+
const low = Math.min(r1, r2);
|
|
194
|
+
const high = Math.max(r1, r2);
|
|
195
|
+
return low + Math.random() * (high - low);
|
|
196
|
+
}
|
|
197
|
+
/** Placeholder for the random.org fetch; always falls back to {@link random}. */
|
|
198
|
+
export function randomOrg(lower = 0, upper = 1, inclusive = true, fallback = true) {
|
|
199
|
+
if (fallback)
|
|
200
|
+
return random(lower, upper, inclusive);
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
export function jitter(a, magnitude = 0.1) {
|
|
204
|
+
return a + (2 * Math.random() - 1) * magnitude;
|
|
205
|
+
}
|
|
206
|
+
// -- Part II Chapter 1: vectors -----------------------------------------------
|
|
207
|
+
export function addVector(alpha, beta) {
|
|
208
|
+
return VectorUtils.combine(alpha, beta, add);
|
|
209
|
+
}
|
|
210
|
+
export function scaleVector(alpha, scalar) {
|
|
211
|
+
return VectorUtils.transform(alpha, (x) => multiply(scalar, x));
|
|
212
|
+
}
|
|
213
|
+
export function negativeVector(alpha) {
|
|
214
|
+
return scaleVector(alpha, -1);
|
|
215
|
+
}
|
|
216
|
+
export function subtractVector(alpha, beta) {
|
|
217
|
+
return addVector(alpha, negativeVector(beta));
|
|
218
|
+
}
|
|
219
|
+
export function dotProduct(alpha, beta) {
|
|
220
|
+
const collapsable = VectorUtils.combine(alpha, beta, multiply, 0);
|
|
221
|
+
return VectorUtils.collapse(collapsable, add);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 3D cross product (bug fix: the AS3 code read index 3 for the z-component
|
|
225
|
+
* and used truthiness tests that dropped legitimate zero components).
|
|
226
|
+
*/
|
|
227
|
+
export function crossProduct(alpha, beta) {
|
|
228
|
+
const a = [0, 0, 0];
|
|
229
|
+
const b = [0, 0, 0];
|
|
230
|
+
for (let i = 0; i < 3; i++) {
|
|
231
|
+
if (i < alpha.length && alpha[i] != null)
|
|
232
|
+
a[i] = alpha[i];
|
|
233
|
+
if (i < beta.length && beta[i] != null)
|
|
234
|
+
b[i] = beta[i];
|
|
235
|
+
}
|
|
236
|
+
const i = a[1] * b[2] - a[2] * b[1];
|
|
237
|
+
const j = a[2] * b[0] - a[0] * b[2];
|
|
238
|
+
const k = a[0] * b[1] - a[1] * b[0];
|
|
239
|
+
return Vector.fromArray([i, j, k]);
|
|
240
|
+
}
|
|
241
|
+
export function kroneckerProduct(A, B) {
|
|
242
|
+
let kronecker = VectorUtils.constantVector(A.length, null);
|
|
243
|
+
kronecker = VectorUtils.fillByIndex(kronecker, (i) => scaleVector(B, A[i]));
|
|
244
|
+
return VectorUtils.flattenSDLevels(kronecker, 2);
|
|
245
|
+
}
|
|
246
|
+
export function distanceVector(alpha, beta, norm = 2) {
|
|
247
|
+
return pNorm(subtractVector(alpha, beta), norm);
|
|
248
|
+
}
|
|
249
|
+
export function pNorm(alpha, norm = 2) {
|
|
250
|
+
if (norm === 0)
|
|
251
|
+
return Math.max(...VectorUtils.transform(alpha, Math.abs));
|
|
252
|
+
const summable = VectorUtils.transform(alpha, (x) => Math.abs(x) ** norm);
|
|
253
|
+
const sum = VectorUtils.collapse(summable, (a, b) => a + b, 0);
|
|
254
|
+
return sum ** (1 / norm);
|
|
255
|
+
}
|
|
256
|
+
export function magnitudeVector(alpha) {
|
|
257
|
+
return pNorm(alpha, 2);
|
|
258
|
+
}
|
|
259
|
+
export function angleBetween(alpha, beta) {
|
|
260
|
+
return Math.acos(dotProduct(alpha, beta) / (magnitudeVector(alpha) * magnitudeVector(beta)));
|
|
261
|
+
}
|
|
262
|
+
//# sourceMappingURL=RealMath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RealMath.js","sourceRoot":"","sources":["../src/RealMath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAyB/C,+EAA+E;AAE/E,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC;AACtB,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC;AACrB,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;AACxB,MAAM,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;AAC1B,MAAM,CAAC,MAAM,GAAG,GAAG,UAAU,CAAC;AAC9B,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAQ,CAAC;AACzC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,QAAQ,CAAC;AAE1C,gFAAgF;AAEhF,MAAM,UAAU,gBAAgB,CAAC,CAAS;IACx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AC1E,MAAM,GAAG,GAAG,WAAW,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAW,CAAC;IACzE,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;AAC3B,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAW;IACzC,OAAO,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;AACzB,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAAW,EAAE,IAAU;IAClD,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,KAAK,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/F,CAAC"}
|
package/dist/Rotor4.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
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.ts";
|
|
27
|
+
import { Vec4 } from "./Vec4.ts";
|
|
28
|
+
export declare class Rotor4 {
|
|
29
|
+
readonly scalar: number;
|
|
30
|
+
readonly bivector: Bivector4;
|
|
31
|
+
readonly pseudoscalar: number;
|
|
32
|
+
static readonly Identity: Rotor4;
|
|
33
|
+
constructor(scalar?: number, bivector?: Bivector4, pseudoscalar?: number);
|
|
34
|
+
private toMV;
|
|
35
|
+
private static fromMV;
|
|
36
|
+
add(r: Rotor4): Rotor4;
|
|
37
|
+
subtract(r: Rotor4): Rotor4;
|
|
38
|
+
negate(): Rotor4;
|
|
39
|
+
scale(s: number): Rotor4;
|
|
40
|
+
/** 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`. */
|
|
41
|
+
dot(r: Rotor4): number;
|
|
42
|
+
/** The geometric product `this * r` — composes two rotations (apply `r` first, then `this`). */
|
|
43
|
+
multiply(r: Rotor4): Rotor4;
|
|
44
|
+
/**
|
|
45
|
+
* The reverse `R~`: reverses the order of vector factors, which negates the
|
|
46
|
+
* bivector (grade 2, picks up (-1)^1) but leaves the scalar (grade 0) and
|
|
47
|
+
* pseudoscalar (grade 4, picks up (-1)^6 = +1) unchanged. For a unit rotor,
|
|
48
|
+
* this is also the inverse — the 4D analogue of a quaternion's conjugate
|
|
49
|
+
* ({@link Quaternion.conjugate}). **Not** the inverse for a non-unit rotor
|
|
50
|
+
* (see {@link inverse}): a rotor that has drifted off the unit manifold
|
|
51
|
+
* during repeated composition (e.g. per-timestep physics integration,
|
|
52
|
+
* before renormalization runs) will silently produce a wrong,
|
|
53
|
+
* `|R|²`-scaled result from `reverse()` where an inverse was intended.
|
|
54
|
+
*/
|
|
55
|
+
reverse(): Rotor4;
|
|
56
|
+
/**
|
|
57
|
+
* Multiplicative inverse `R⁻¹ = R~ / |R|²`, distinct from {@link reverse}
|
|
58
|
+
* for a non-unit rotor — the 4D analogue of {@link Quaternion.inverse}.
|
|
59
|
+
* `this.multiply(this.inverse())` is `Identity` regardless of `this`'s
|
|
60
|
+
* magnitude; `this.multiply(this.reverse())` only is when `this` is
|
|
61
|
+
* already a unit rotor.
|
|
62
|
+
*/
|
|
63
|
+
inverse(): Rotor4;
|
|
64
|
+
get magnitudeSquared(): number;
|
|
65
|
+
get magnitude(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Naive renormalization (divide by magnitude). Exact for simple rotors;
|
|
68
|
+
* see the class-level doc comment for why this is *not* generally correct
|
|
69
|
+
* for double rotations, and where the real fix belongs.
|
|
70
|
+
*/
|
|
71
|
+
normalize(): Rotor4;
|
|
72
|
+
/** Apply this rotor to a vector via the sandwich product `R v R~`. Rotates `v`. */
|
|
73
|
+
apply(v: Vec4): Vec4;
|
|
74
|
+
/** Apply this rotor to a bivector via the sandwich product `R B R~`. Rotates the 2-plane `B`. */
|
|
75
|
+
applyToBivector(b: Bivector4): Bivector4;
|
|
76
|
+
/**
|
|
77
|
+
* Perwass-style rotor factorization: decompose this rotor into two simple
|
|
78
|
+
* rotors `[R1, R2]` in mutually orthogonal planes such that `R2.multiply(R1)`
|
|
79
|
+
* has the same *action* as this rotor (equal up to the usual rotor
|
|
80
|
+
* double-cover sign, like any rotor comparison) — the proper fix for
|
|
81
|
+
* {@link normalize}'s documented limitation on double rotations. Works
|
|
82
|
+
* even when this rotor has drifted away from being a valid unit rotor
|
|
83
|
+
* (the intended use case: repeated physics integration).
|
|
84
|
+
*
|
|
85
|
+
* Method: find this rotor's two invariant 2-planes as eigenspaces of the
|
|
86
|
+
* *symmetric* part `S = M + Mᵀ` of its 4x4 matrix representation `M`
|
|
87
|
+
* (restricted to an invariant plane with rotation angle `θ`, `M + Mᵀ` is
|
|
88
|
+
* `2cos(θ)` times the identity on that plane — so same-angle-magnitude
|
|
89
|
+
* planes share an eigenvalue, and a repeated eigenvalue of `S` signals an
|
|
90
|
+
* isoclinic-type degeneracy where *any* orthonormal basis of that
|
|
91
|
+
* eigenspace is a genuinely valid invariant-plane choice, not just an
|
|
92
|
+
* approximation). `eigenSymmetric`'s eigenvector order isn't guaranteed to
|
|
93
|
+
* group same-plane pairs together when eigenvalues coincide (or are
|
|
94
|
+
* numerically indistinguishable), so the 3 possible pairings of the 4
|
|
95
|
+
* eigenvectors into 2 pairs are explicitly checked for genuine
|
|
96
|
+
* `M`-invariance (zero "leakage" into the other pair) before use, rather
|
|
97
|
+
* than assumed from sort order. Once a valid pair `(u, v)` spanning an
|
|
98
|
+
* invariant plane is known, the angle and correctly-oriented plane come
|
|
99
|
+
* directly from how `M` actually acts on `u` within that plane —
|
|
100
|
+
* `atan2(v·(Mu), u·(Mu))` — not from this rotor's own (possibly drifted)
|
|
101
|
+
* scalar/bivector/pseudoscalar components.
|
|
102
|
+
*/
|
|
103
|
+
factor(): [Rotor4, Rotor4];
|
|
104
|
+
/**
|
|
105
|
+
* The proper (Perwass-style) fix for numerical drift: {@link factor} this
|
|
106
|
+
* rotor into two simple rotors — each exactly renormalizable, unlike a
|
|
107
|
+
* compound rotor — and recompose. Prefer this over {@link normalize} once
|
|
108
|
+
* a rotor may have accumulated drift through repeated integration and
|
|
109
|
+
* could be a genuine double rotation; `normalize` remains correct (and
|
|
110
|
+
* cheaper) for rotors known to be simple.
|
|
111
|
+
*/
|
|
112
|
+
renormalize(): Rotor4;
|
|
113
|
+
/**
|
|
114
|
+
* Build the rotor for a rotation of `angle` radians in the 2-plane `plane`
|
|
115
|
+
* (analogous to {@link Quaternion.fromAxisAngle}, but a *plane* rather than
|
|
116
|
+
* an *axis* — axes only work as rotation generators in 3D). `plane` must be
|
|
117
|
+
* simple (see {@link Bivector4.isSimple}); to build a double rotation,
|
|
118
|
+
* construct two simple rotors in orthogonal planes and {@link multiply}
|
|
119
|
+
* them together.
|
|
120
|
+
*/
|
|
121
|
+
static fromBivectorAngle(plane: Bivector4, angle: number): Rotor4;
|
|
122
|
+
/**
|
|
123
|
+
* `exp(B)`: the rotor generated by the bivector `B` -- for a *simple* `B`,
|
|
124
|
+
* this is a rotation of `|B|` radians in `B`'s plane, matching
|
|
125
|
+
* {@link fromBivectorAngle}'s convention for the common physics case of an
|
|
126
|
+
* already-scaled generator (e.g. `angularVelocity * dt`, where the
|
|
127
|
+
* bivector's magnitude *is* the rotation angle and its direction *is* the
|
|
128
|
+
* plane). For a *compound* `B` (e.g. an angular velocity spinning
|
|
129
|
+
* simultaneously in two independent planes, entirely possible physically),
|
|
130
|
+
* the naive `cos|B| + sin|B|·B̂` formula this reduces to for simple `B` is
|
|
131
|
+
* wrong: `B² ≠ -|B|²` in general (it has a nonzero pseudoscalar term too),
|
|
132
|
+
* so this instead decomposes `B` into its two orthogonal simple
|
|
133
|
+
* components `m1·Π1 + m2·Π2` and returns `exp(m2·Π2)·exp(m1·Π1)` (valid
|
|
134
|
+
* since orthogonal-plane generators commute). Decomposition method:
|
|
135
|
+
* `B`'s invariant planes are the eigenspaces of `-A²`, where `A` is `B`'s
|
|
136
|
+
* contraction-action matrix (`A(v) = v⌋B`, antisymmetric, so `A²` is
|
|
137
|
+
* symmetric negative-semidefinite) -- the bivector analogue of how
|
|
138
|
+
* {@link factor} finds a *rotor's* invariant planes via `M + Mᵀ`, right
|
|
139
|
+
* down to needing the same explicit pairing-invariance check (eigenvalue
|
|
140
|
+
* sort order isn't reliable when eigenvalues coincide). Returns
|
|
141
|
+
* {@link Identity} for the zero bivector.
|
|
142
|
+
*/
|
|
143
|
+
static exp(bivector: Bivector4): Rotor4;
|
|
144
|
+
/**
|
|
145
|
+
* `log()`: the bivector `B` such that `exp(B) = this`, for a *simple*
|
|
146
|
+
* rotor -- the inverse of {@link exp}. Throws for a compound
|
|
147
|
+
* (double-rotation) rotor, same as {@link toBivectorAngle} (which this is
|
|
148
|
+
* built on): decomposing a general rotor's log is the Perwass-factorization
|
|
149
|
+
* work deferred to the physics layer.
|
|
150
|
+
*/
|
|
151
|
+
log(epsilon?: number): Bivector4;
|
|
152
|
+
/**
|
|
153
|
+
* Recover `(plane, angle)` for a *simple* rotor (pseudoscalar ≈ 0). Throws
|
|
154
|
+
* for a compound (double-rotation) rotor — see the class-level doc comment;
|
|
155
|
+
* decomposing a general rotor's log is the Perwass-factorization work
|
|
156
|
+
* deferred to the physics layer.
|
|
157
|
+
*/
|
|
158
|
+
toBivectorAngle(epsilon?: number): {
|
|
159
|
+
plane: Bivector4;
|
|
160
|
+
angle: number;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Spherical linear interpolation between two rotors, `t` in `[0, 1]` --
|
|
164
|
+
* the 4D analogue of {@link Quaternion.slerp}, same component-wise
|
|
165
|
+
* treatment (an 8-vector of scalar/bivector(6)/pseudoscalar, via
|
|
166
|
+
* {@link dot}) and the same near-parallel fallback to a normalized
|
|
167
|
+
* linear interpolation to avoid dividing by a near-zero `sin(theta0)`.
|
|
168
|
+
*
|
|
169
|
+
* **Same manifold caveat as {@link normalize}**: this is only exactly
|
|
170
|
+
* correct for interpolating between *simple* rotors (single rotation
|
|
171
|
+
* plane) — a compound double rotation doesn't lie on the sphere this
|
|
172
|
+
* construction assumes, so slerping one is an approximation, not a
|
|
173
|
+
* geodesic. For a double rotation, {@link factor} each endpoint into its
|
|
174
|
+
* two orthogonal simple rotors and slerp each pair independently instead.
|
|
175
|
+
*/
|
|
176
|
+
static slerp(a: Rotor4, b: Rotor4, t: number): Rotor4;
|
|
177
|
+
equals(r: Rotor4, epsilon?: number): boolean;
|
|
178
|
+
toString(): string;
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=Rotor4.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Rotor4.d.ts","sourceRoot":"","sources":["../src/Rotor4.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAI3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,qBAAa,MAAM;IACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAE9B,MAAM,CAAC,QAAQ,CAAC,QAAQ,SAAoC;gBAEhD,MAAM,SAAI,EAAE,QAAQ,GAAE,SAA0B,EAAE,YAAY,SAAI;IAM9E,OAAO,CAAC,IAAI;IASZ,OAAO,CAAC,MAAM,CAAC,MAAM;IAIrB,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAItB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3B,MAAM,IAAI,MAAM;IAIhB,KAAK,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAIxB,kMAAkM;IAClM,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAItB,gGAAgG;IAChG,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3B;;;;;;;;;;OAUG;IACH,OAAO,IAAI,MAAM;IAIjB;;;;;;OAMG;IACH,OAAO,IAAI,MAAM;IAMjB,IAAI,gBAAgB,IAAI,MAAM,CAE7B;IAED,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED;;;;OAIG;IACH,SAAS,IAAI,MAAM;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