@ac-kit/math-complex 0.1.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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # @ac-kit/math-complex
2
+
3
+ Complex numbers and their extensions: the finite-dimensional algebras over ℝ.
4
+
5
+ ## Scope
6
+
7
+ A value here is a fixed-arity tuple of binary64 components plus a multiplication
8
+ rule: the complex numbers ℂ and the quaternions ℍ. They belong together because
9
+ they are one construction — Cayley–Dickson generates ℂ → ℍ mechanically, and
10
+ both share the same shape, the same throughput concerns and the same algebraic
11
+ vocabulary.
12
+
13
+ These are deliberately not part of `@ac-kit/math-numbers`. That package owns the
14
+ number tower ℕ ⊆ ℤ ⊆ ℚ ⊆ ℝ, whose members promote between each other under a
15
+ shared rounding, overflow and exactness policy. Nothing here promotes, rounds or
16
+ overflows, so none of that machinery applies.
17
+
18
+ `@ac-kit/math-linear` builds on the ℍ defined here: this package owns the
19
+ division-ring operations (multiplication, conjugate, inverse, norm), while the
20
+ rotation reading of a unit quaternion — axis and angle, Euler conversion,
21
+ rotation matrices, slerp — lives there.
22
+
23
+ ## Contents
24
+
25
+ | Symbol | Description |
26
+ | ----------------------------------------------------------------------- | -------------------------------------- |
27
+ | `Complex` | `[re, im]` tuple |
28
+ | `COMPLEX_ZERO`, `COMPLEX_ONE`, `COMPLEX_I` | Constants |
29
+ | `complexAdd`, `complexSub`, `complexMul`, `complexDiv`, `complexNeg` | Field operations |
30
+ | `complexConjugate` | `a − bi` |
31
+ | `complexModulus`, `complexModulusSq` | `\|z\|` and `\|z\|²` |
32
+ | `complexArgument` | Principal argument in `(−π, π]` |
33
+ | `complexFromPolar`, `complexCis` | Polar construction and Euler's formula |
34
+ | `complexExp`, `complexLog` | Exponential and principal logarithm |
35
+ | `Quaternion`, `QUATERNION_IDENTITY` | `[x, y, z, w]` tuple and the unit |
36
+ | `quaternionAdd`, `quaternionSub`, `quaternionNeg`, `quaternionMultiply` | Ring operations |
37
+ | `quaternionConjugate`, `quaternionInverse` | Division-ring operations |
38
+ | `quaternionLength`, `quaternionLengthSq`, `quaternionNormalize` | The ℍ norm and its unit form |
39
+ | `quaternionDot`, `quaternionEquals`, `quaternionIsClose` | Inner product and comparison |
40
+ | `ComplexNum`, `QuaternionNum` | Boxed field/ring values over ℂ and ℍ |
41
+
42
+ `ComplexNum` and `QuaternionNum` box a value with its own field or ring API —
43
+ delegating every operation to the free functions above, no arithmetic of their
44
+ own. They are deliberately not a genericity mechanism: an algorithm that works
45
+ over an arbitrary field or division ring takes `Field<T>` or `DivisionRing<T>`
46
+ evidence from `@ac-kit/math-algebra`, which costs no virtual dispatch.
47
+
48
+ ## Division
49
+
50
+ `complexDiv` uses Smith's algorithm rather than the textbook
51
+ `(ac + bd) / (c² + d²)` form. The naive version squares both components of the
52
+ divisor, so it overflows for `|z| > √(MAX_VALUE)` and underflows for very small
53
+ divisors even when the true quotient is perfectly representable. Smith's
54
+ formulation divides by the larger component first, keeping every intermediate
55
+ within range.
56
+
57
+ ## Usage
58
+
59
+ ```ts
60
+ import { complexMul, complexModulus, COMPLEX_I } from "@ac-kit/math-complex";
61
+
62
+ complexMul(COMPLEX_I, COMPLEX_I); // [-1, 0]
63
+ complexModulus([3, 4]); // 5
64
+ ```
65
+
66
+ See the
67
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/math-complex/)
68
+ for the full reference.
@@ -0,0 +1,49 @@
1
+ import type { IField } from "@ac-kit/math-algebra";
2
+ import { Complex } from "./complex.js";
3
+ /**
4
+ * A complex number `re + im·i`, boxed as a value with its own field API.
5
+ *
6
+ * The contract shell over {@link Complex}. Every operation delegates to the
7
+ * free functions in `complex.ts`; no arithmetic happens in this class.
8
+ *
9
+ * ```ts
10
+ * const z = ComplexNum.from(3, 4);
11
+ * z.abs(); // 5
12
+ * z.mul(ComplexNum.I).re; // -4
13
+ * ```
14
+ */
15
+ export declare class ComplexNum implements IField<ComplexNum> {
16
+ #private;
17
+ private constructor();
18
+ /** The complex number `0 + 0i`. */
19
+ static readonly ZERO: ComplexNum;
20
+ /** The complex number `1 + 0i`. */
21
+ static readonly ONE: ComplexNum;
22
+ /** The imaginary unit `i`, satisfying `i² = −1`. */
23
+ static readonly I: ComplexNum;
24
+ /** Creates a complex number from its real and imaginary parts. */
25
+ static from(re: number, im?: number): ComplexNum;
26
+ /** Wraps an already-computed {@link Complex} tuple. */
27
+ static fromTuple(value: Complex): ComplexNum;
28
+ /** The underlying tuple, for callers working at the arithmetic layer. */
29
+ toTuple(): Complex;
30
+ /** Real part `a` of `a + bi`. */
31
+ get re(): number;
32
+ /** Imaginary part `b` of `a + bi`. */
33
+ get im(): number;
34
+ /** Modulus `|z| = √(re² + im²)`. Always non-negative. */
35
+ abs(): number;
36
+ /** Argument `arg(z) = atan2(im, re) ∈ (−π, π]`. */
37
+ arg(): number;
38
+ /** `conj(a + bi) = a − bi`. */
39
+ conj(): ComplexNum;
40
+ add(other: ComplexNum): ComplexNum;
41
+ sub(other: ComplexNum): ComplexNum;
42
+ neg(): ComplexNum;
43
+ mul(other: ComplexNum): ComplexNum;
44
+ /** @throws When called on zero. */
45
+ inv(): ComplexNum;
46
+ /** @throws When `other` is zero. */
47
+ div(other: ComplexNum): ComplexNum;
48
+ }
49
+ //# sourceMappingURL=complex-num.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"complex-num.d.ts","sourceRoot":"","sources":["../../src/complex/complex-num.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAIN,OAAO,EAUP,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;GAWG;AACH,qBAAa,UAAW,YAAW,MAAM,CAAC,UAAU,CAAC;;IAGpD,OAAO,eAEN;IAED,mCAAmC;IACnC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAgC;IAEhE,mCAAmC;IACnC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU,CAA+B;IAE9D,oDAAoD;IACpD,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,UAAU,CAA6B;IAE1D,kEAAkE;IAClE,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,SAAI,GAAG,UAAU,CAE1C;IAED,uDAAuD;IACvD,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAE3C;IAED,yEAAyE;IACzE,OAAO,IAAI,OAAO,CAEjB;IAED,iCAAiC;IACjC,IAAI,EAAE,IAAI,MAAM,CAEf;IAED,sCAAsC;IACtC,IAAI,EAAE,IAAI,MAAM,CAEf;IAED,yDAAyD;IACzD,GAAG,IAAI,MAAM,CAEZ;IAED,mDAAmD;IACnD,GAAG,IAAI,MAAM,CAEZ;IAED,+BAA+B;IAC/B,IAAI,IAAI,UAAU,CAEjB;IAED,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAEjC;IAED,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAEjC;IAED,GAAG,IAAI,UAAU,CAEhB;IAED,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAEjC;IAED,mCAAmC;IACnC,GAAG,IAAI,UAAU,CAEhB;IAED,oCAAoC;IACpC,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAEjC;CACD"}
@@ -0,0 +1,78 @@
1
+ import { COMPLEX_I, COMPLEX_ONE, COMPLEX_ZERO, complexAdd, complexArgument, complexConjugate, complexDiv, complexInverse, complexModulus, complexMul, complexNeg, complexSub, } from "./complex.js";
2
+ /**
3
+ * A complex number `re + im·i`, boxed as a value with its own field API.
4
+ *
5
+ * The contract shell over {@link Complex}. Every operation delegates to the
6
+ * free functions in `complex.ts`; no arithmetic happens in this class.
7
+ *
8
+ * ```ts
9
+ * const z = ComplexNum.from(3, 4);
10
+ * z.abs(); // 5
11
+ * z.mul(ComplexNum.I).re; // -4
12
+ * ```
13
+ */
14
+ export class ComplexNum {
15
+ #value;
16
+ constructor(value) {
17
+ this.#value = value;
18
+ }
19
+ /** The complex number `0 + 0i`. */
20
+ static ZERO = new ComplexNum(COMPLEX_ZERO);
21
+ /** The complex number `1 + 0i`. */
22
+ static ONE = new ComplexNum(COMPLEX_ONE);
23
+ /** The imaginary unit `i`, satisfying `i² = −1`. */
24
+ static I = new ComplexNum(COMPLEX_I);
25
+ /** Creates a complex number from its real and imaginary parts. */
26
+ static from(re, im = 0) {
27
+ return new ComplexNum([re, im]);
28
+ }
29
+ /** Wraps an already-computed {@link Complex} tuple. */
30
+ static fromTuple(value) {
31
+ return new ComplexNum(value);
32
+ }
33
+ /** The underlying tuple, for callers working at the arithmetic layer. */
34
+ toTuple() {
35
+ return this.#value;
36
+ }
37
+ /** Real part `a` of `a + bi`. */
38
+ get re() {
39
+ return this.#value[0];
40
+ }
41
+ /** Imaginary part `b` of `a + bi`. */
42
+ get im() {
43
+ return this.#value[1];
44
+ }
45
+ /** Modulus `|z| = √(re² + im²)`. Always non-negative. */
46
+ abs() {
47
+ return complexModulus(this.#value);
48
+ }
49
+ /** Argument `arg(z) = atan2(im, re) ∈ (−π, π]`. */
50
+ arg() {
51
+ return complexArgument(this.#value);
52
+ }
53
+ /** `conj(a + bi) = a − bi`. */
54
+ conj() {
55
+ return new ComplexNum(complexConjugate(this.#value));
56
+ }
57
+ add(other) {
58
+ return new ComplexNum(complexAdd(this.#value, other.#value));
59
+ }
60
+ sub(other) {
61
+ return new ComplexNum(complexSub(this.#value, other.#value));
62
+ }
63
+ neg() {
64
+ return new ComplexNum(complexNeg(this.#value));
65
+ }
66
+ mul(other) {
67
+ return new ComplexNum(complexMul(this.#value, other.#value));
68
+ }
69
+ /** @throws When called on zero. */
70
+ inv() {
71
+ return new ComplexNum(complexInverse(this.#value));
72
+ }
73
+ /** @throws When `other` is zero. */
74
+ div(other) {
75
+ return new ComplexNum(complexDiv(this.#value, other.#value));
76
+ }
77
+ }
78
+ //# sourceMappingURL=complex-num.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"complex-num.js","sourceRoot":"","sources":["../../src/complex/complex-num.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,SAAS,EACT,WAAW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,CAAC;IAED,+BAA+B;IAC/B,IAAI;QACH,OAAO,IAAI,UAAU,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IACtD,CAAC;IAED,GAAG,CAAC,KAAiB;QACpB,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED,GAAG,CAAC,KAAiB;QACpB,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED,GAAG;QACF,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAChD,CAAC;IAED,GAAG,CAAC,KAAiB;QACpB,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED,mCAAmC;IACnC,GAAG;QACF,OAAO,IAAI,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IACpD,CAAC;IAED,oCAAoC;IACpC,GAAG,CAAC,KAAiB;QACpB,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;CACD"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A complex number `re + im·i` over IEEE 754 binary64 components.
3
+ *
4
+ * Represented as a plain tuple so that arrays of complex values stay compact
5
+ * and allocation-free operations can write into a caller-provided buffer.
6
+ */
7
+ export type Complex = [re: number, im: number];
8
+ export declare const COMPLEX_ZERO: [0, 0];
9
+ export declare const COMPLEX_ONE: [1, 0];
10
+ /** The imaginary unit `i`, satisfying `i² = −1`. */
11
+ export declare const COMPLEX_I: [0, 1];
12
+ export declare function complexAdd(a: Complex, b: Complex): Complex;
13
+ export declare function complexSub(a: Complex, b: Complex): Complex;
14
+ /** Complex multiplication: `(a + bi)(c + di) = (ac − bd) + (ad + bc)i`. */
15
+ export declare function complexMul(a: Complex, b: Complex): Complex;
16
+ /**
17
+ * Complex division, using Smith's algorithm to avoid the intermediate overflow
18
+ * that the naive `(ac + bd) / (c² + d²)` form suffers for large components.
19
+ */
20
+ export declare function complexDiv(a: Complex, b: Complex): Complex;
21
+ export declare function complexNeg(a: Complex): Complex;
22
+ /** The complex conjugate `a − bi`. */
23
+ export declare function complexConjugate(a: Complex): Complex;
24
+ /**
25
+ * Multiplicative inverse `z⁻¹ = conj(z) / |z|²`.
26
+ *
27
+ * @throws When called on zero.
28
+ */
29
+ export declare function complexInverse(a: Complex): Complex;
30
+ /** The modulus `|z| = √(re² + im²)`. */
31
+ export declare function complexModulus(a: Complex): number;
32
+ /** The squared modulus, avoiding the square root when only ordering matters. */
33
+ export declare function complexModulusSq(a: Complex): number;
34
+ /** The principal argument `arg(z) ∈ (−π, π]`. */
35
+ export declare function complexArgument(a: Complex): number;
36
+ /** Builds a complex number from its polar form `r·e^(iθ)`. */
37
+ export declare function complexFromPolar(modulus: number, argument: number): Complex;
38
+ /** Euler's formula: `cis(θ) = cos θ + i·sin θ`, the unit vector at angle θ. */
39
+ export declare function complexCis(argument: number): Complex;
40
+ /** The complex exponential `e^z = e^re · (cos im + i·sin im)`. */
41
+ export declare function complexExp(a: Complex): Complex;
42
+ /** The principal complex logarithm `ln|z| + i·arg(z)`. */
43
+ export declare function complexLog(a: Complex): Complex;
44
+ //# sourceMappingURL=complex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"complex.d.ts","sourceRoot":"","sources":["../../src/complex/complex.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;AAE/C,eAAO,MAAM,YAAY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CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAE9C"}
@@ -0,0 +1,88 @@
1
+ import { diffOfProducts, sumOfProducts } from "@ac-kit/math-scalar";
2
+ export const COMPLEX_ZERO = [0, 0];
3
+ export const COMPLEX_ONE = [1, 0];
4
+ /** The imaginary unit `i`, satisfying `i² = −1`. */
5
+ export const COMPLEX_I = [0, 1];
6
+ export function complexAdd(a, b) {
7
+ return [a[0] + b[0], a[1] + b[1]];
8
+ }
9
+ export function complexSub(a, b) {
10
+ return [a[0] - b[0], a[1] - b[1]];
11
+ }
12
+ /** Complex multiplication: `(a + bi)(c + di) = (ac − bd) + (ad + bc)i`. */
13
+ export function complexMul(a, b) {
14
+ return [
15
+ diffOfProducts(a[0], b[0], a[1], b[1]),
16
+ sumOfProducts(a[0], b[1], a[1], b[0]),
17
+ ];
18
+ }
19
+ /**
20
+ * Complex division, using Smith's algorithm to avoid the intermediate overflow
21
+ * that the naive `(ac + bd) / (c² + d²)` form suffers for large components.
22
+ */
23
+ export function complexDiv(a, b) {
24
+ if (Math.abs(b[0]) >= Math.abs(b[1])) {
25
+ const ratio = b[1] / b[0];
26
+ const denominator = sumOfProducts(b[0], 1, b[1], ratio);
27
+ return [
28
+ sumOfProducts(a[0], 1, a[1], ratio) / denominator,
29
+ diffOfProducts(a[1], 1, a[0], ratio) / denominator,
30
+ ];
31
+ }
32
+ const ratio = b[0] / b[1];
33
+ const denominator = sumOfProducts(b[0], ratio, b[1], 1);
34
+ return [
35
+ sumOfProducts(a[0], ratio, a[1], 1) / denominator,
36
+ diffOfProducts(a[1], ratio, a[0], 1) / denominator,
37
+ ];
38
+ }
39
+ export function complexNeg(a) {
40
+ return [-a[0], -a[1]];
41
+ }
42
+ /** The complex conjugate `a − bi`. */
43
+ export function complexConjugate(a) {
44
+ return [a[0], -a[1]];
45
+ }
46
+ /**
47
+ * Multiplicative inverse `z⁻¹ = conj(z) / |z|²`.
48
+ *
49
+ * @throws When called on zero.
50
+ */
51
+ export function complexInverse(a) {
52
+ const normSq = complexModulusSq(a);
53
+ if (normSq === 0) {
54
+ throw new Error("Cannot invert a zero complex number.");
55
+ }
56
+ const conjugate = complexConjugate(a);
57
+ return [conjugate[0] / normSq, conjugate[1] / normSq];
58
+ }
59
+ /** The modulus `|z| = √(re² + im²)`. */
60
+ export function complexModulus(a) {
61
+ return Math.hypot(a[0], a[1]);
62
+ }
63
+ /** The squared modulus, avoiding the square root when only ordering matters. */
64
+ export function complexModulusSq(a) {
65
+ return a[0] * a[0] + a[1] * a[1];
66
+ }
67
+ /** The principal argument `arg(z) ∈ (−π, π]`. */
68
+ export function complexArgument(a) {
69
+ return Math.atan2(a[1], a[0]);
70
+ }
71
+ /** Builds a complex number from its polar form `r·e^(iθ)`. */
72
+ export function complexFromPolar(modulus, argument) {
73
+ return [modulus * Math.cos(argument), modulus * Math.sin(argument)];
74
+ }
75
+ /** Euler's formula: `cis(θ) = cos θ + i·sin θ`, the unit vector at angle θ. */
76
+ export function complexCis(argument) {
77
+ return [Math.cos(argument), Math.sin(argument)];
78
+ }
79
+ /** The complex exponential `e^z = e^re · (cos im + i·sin im)`. */
80
+ export function complexExp(a) {
81
+ const magnitude = Math.exp(a[0]);
82
+ return [magnitude * Math.cos(a[1]), magnitude * Math.sin(a[1])];
83
+ }
84
+ /** The principal complex logarithm `ln|z| + i·arg(z)`. */
85
+ export function complexLog(a) {
86
+ return [Math.log(complexModulus(a)), complexArgument(a)];
87
+ }
88
+ //# sourceMappingURL=complex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"complex.js","sourceRoot":"","sources":["../../src/complex/complex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAUpE,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,CAAC,CAA4B,CAAC;AAC9D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC,EAAE,CAAC,CAA4B,CAAC;AAE7D,oD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G,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,UAAU,CAAC,CAAU;IACpC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from "./complex/complex-num.js";
2
+ export * from "./complex/complex.js";
3
+ export * from "./quaternion/quaternion-num.js";
4
+ export * from "./quaternion/quaternion.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./complex/complex-num.js";
2
+ export * from "./complex/complex.js";
3
+ export * from "./quaternion/quaternion-num.js";
4
+ export * from "./quaternion/quaternion.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC"}
@@ -0,0 +1,48 @@
1
+ import type { IRing } from "@ac-kit/math-algebra";
2
+ import { Quaternion } from "./quaternion.js";
3
+ /**
4
+ * A quaternion `a + bi + cj + dk`, boxed as a value with its own ring API.
5
+ *
6
+ * The contract shell over {@link Quaternion}. Every operation delegates to the
7
+ * free functions in `quaternion.ts`; no arithmetic happens in this class. The
8
+ * underlying tuple stores components as `[b, c, d, a]` — vector, then scalar;
9
+ * {@link scalar} and {@link vector} present the `a + bi + cj + dk` reading.
10
+ */
11
+ export declare class QuaternionNum implements IRing<QuaternionNum> {
12
+ #private;
13
+ private constructor();
14
+ /** The quaternion `1 + 0i + 0j + 0k`. */
15
+ static readonly IDENTITY: QuaternionNum;
16
+ /** Creates a quaternion from its scalar part and its `i, j, k` vector part. */
17
+ static from(scalar: number, vector: readonly [number, number, number]): QuaternionNum;
18
+ /** Wraps an already-computed {@link Quaternion} tuple `[b, c, d, a]`. */
19
+ static fromTuple(value: Quaternion): QuaternionNum;
20
+ /**
21
+ * The underlying `[b, c, d, a]` tuple, for callers working at the arithmetic
22
+ * layer.
23
+ */
24
+ toTuple(): Quaternion;
25
+ /** Scalar (real) part: the `a` in `a + bi + cj + dk`. */
26
+ get scalar(): number;
27
+ /** Vector (pure-quaternion) part: `[b, c, d]` for `i`, `j`, `k` components. */
28
+ get vector(): readonly [number, number, number];
29
+ /**
30
+ * Conjugate `conj(a + bi + cj + dk) = a − bi − cj − dk`. Negates the vector
31
+ * part; leaves the scalar part unchanged.
32
+ */
33
+ conj(): QuaternionNum;
34
+ /** Norm `‖q‖ = √(a² + b² + c² + d²)`. Always non-negative. */
35
+ norm(): number;
36
+ /**
37
+ * Multiplicative inverse `q⁻¹ = conj(q) / ‖q‖²`.
38
+ *
39
+ * @throws When called on the zero quaternion.
40
+ */
41
+ inv(): QuaternionNum;
42
+ add(other: QuaternionNum): QuaternionNum;
43
+ sub(other: QuaternionNum): QuaternionNum;
44
+ neg(): QuaternionNum;
45
+ /** Hamilton product `this · other` — non-commutative. */
46
+ mul(other: QuaternionNum): QuaternionNum;
47
+ }
48
+ //# sourceMappingURL=quaternion-num.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quaternion-num.d.ts","sourceRoot":"","sources":["../../src/quaternion/quaternion-num.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAEN,UAAU,EAQV,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;GAOG;AACH,qBAAa,aAAc,YAAW,KAAK,CAAC,aAAa,CAAC;;IAGzD,OAAO,eAEN;IAED,yCAAyC;IACzC,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAErC;IAEF,+EAA+E;IAC/E,MAAM,CAAC,IAAI,CACV,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,GACvC,aAAa,CAEf;IAED,yEAAyE;IACzE,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,aAAa,CAEjD;IAED;;;OAGG;IACH,OAAO,IAAI,UAAU,CAEpB;IAED,yDAAyD;IACzD,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,+EAA+E;IAC/E,IAAI,MAAM,IAAI,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAE9C;IAED;;;OAGG;IACH,IAAI,IAAI,aAAa,CAEpB;IAED,8DAA8D;IAC9D,IAAI,IAAI,MAAM,CAEb;IAED;;;;OAIG;IACH,GAAG,IAAI,aAAa,CAEnB;IAED,GAAG,CAAC,KAAK,EAAE,aAAa,GAAG,aAAa,CAEvC;IAED,GAAG,CAAC,KAAK,EAAE,aAAa,GAAG,aAAa,CAEvC;IAED,GAAG,IAAI,aAAa,CAEnB;IAED,yDAAyD;IACzD,GAAG,CAAC,KAAK,EAAE,aAAa,GAAG,aAAa,CAEvC;CACD"}
@@ -0,0 +1,73 @@
1
+ import { QUATERNION_IDENTITY, quaternionAdd, quaternionConjugate, quaternionInverse, quaternionLength, quaternionMultiply, quaternionNeg, quaternionSub, } from "./quaternion.js";
2
+ /**
3
+ * A quaternion `a + bi + cj + dk`, boxed as a value with its own ring API.
4
+ *
5
+ * The contract shell over {@link Quaternion}. Every operation delegates to the
6
+ * free functions in `quaternion.ts`; no arithmetic happens in this class. The
7
+ * underlying tuple stores components as `[b, c, d, a]` — vector, then scalar;
8
+ * {@link scalar} and {@link vector} present the `a + bi + cj + dk` reading.
9
+ */
10
+ export class QuaternionNum {
11
+ #value;
12
+ constructor(value) {
13
+ this.#value = value;
14
+ }
15
+ /** The quaternion `1 + 0i + 0j + 0k`. */
16
+ static IDENTITY = new QuaternionNum(QUATERNION_IDENTITY);
17
+ /** Creates a quaternion from its scalar part and its `i, j, k` vector part. */
18
+ static from(scalar, vector) {
19
+ return new QuaternionNum([vector[0], vector[1], vector[2], scalar]);
20
+ }
21
+ /** Wraps an already-computed {@link Quaternion} tuple `[b, c, d, a]`. */
22
+ static fromTuple(value) {
23
+ return new QuaternionNum(value);
24
+ }
25
+ /**
26
+ * The underlying `[b, c, d, a]` tuple, for callers working at the arithmetic
27
+ * layer.
28
+ */
29
+ toTuple() {
30
+ return this.#value;
31
+ }
32
+ /** Scalar (real) part: the `a` in `a + bi + cj + dk`. */
33
+ get scalar() {
34
+ return this.#value[3];
35
+ }
36
+ /** Vector (pure-quaternion) part: `[b, c, d]` for `i`, `j`, `k` components. */
37
+ get vector() {
38
+ return [this.#value[0], this.#value[1], this.#value[2]];
39
+ }
40
+ /**
41
+ * Conjugate `conj(a + bi + cj + dk) = a − bi − cj − dk`. Negates the vector
42
+ * part; leaves the scalar part unchanged.
43
+ */
44
+ conj() {
45
+ return new QuaternionNum(quaternionConjugate(this.#value));
46
+ }
47
+ /** Norm `‖q‖ = √(a² + b² + c² + d²)`. Always non-negative. */
48
+ norm() {
49
+ return quaternionLength(this.#value);
50
+ }
51
+ /**
52
+ * Multiplicative inverse `q⁻¹ = conj(q) / ‖q‖²`.
53
+ *
54
+ * @throws When called on the zero quaternion.
55
+ */
56
+ inv() {
57
+ return new QuaternionNum(quaternionInverse(this.#value));
58
+ }
59
+ add(other) {
60
+ return new QuaternionNum(quaternionAdd(this.#value, other.#value));
61
+ }
62
+ sub(other) {
63
+ return new QuaternionNum(quaternionSub(this.#value, other.#value));
64
+ }
65
+ neg() {
66
+ return new QuaternionNum(quaternionNeg(this.#value));
67
+ }
68
+ /** Hamilton product `this · other` — non-commutative. */
69
+ mul(other) {
70
+ return new QuaternionNum(quaternionMultiply(this.#value, other.#value));
71
+ }
72
+ }
73
+ //# sourceMappingURL=quaternion-num.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quaternion-num.js","sourceRoot":"","sources":["../../src/quaternion/quaternion-num.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,mBAAmB,EAEnB,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,aAAa,EACb,aAAa,GACb,MAAM,iBAAiB,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,OAAO,aAAa;IAChB,MAAM,CAAa;IAE5B,YAAoB,KAAiB;QACpC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,yCAAyC;IACzC,MAAM,CAAU,QAAQ,GAAkB,IAAI,aAAa,CAC1D,mBAAmB,CACnB,CAAC;IAEF,+EAA+E;IAC/E,MAAM,CAAC,IAAI,CACV,MAAc,EACd,MAAyC;QAEzC,OAAO,IAAI,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;IACrE,CAAC;IAED,yEAAyE;IACzE,MAAM,CAAC,SAAS,CAAC,KAAiB;QACjC,OAAO,IAAI,aAAa,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IAED;;;OAGG;IACH,OAAO;QACN,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC;IAED,yDAAyD;IACzD,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACvB,CAAC;IAED,+EAA+E;IAC/E,IAAI,MAAM;QACT,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC;IAED;;;OAGG;IACH,IAAI;QACH,OAAO,IAAI,aAAa,CAAC,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5D,CAAC;IAED,8DAA8D;IAC9D,IAAI;QACH,OAAO,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACH,GAAG;QACF,OAAO,IAAI,aAAa,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1D,CAAC;IAED,GAAG,CAAC,KAAoB;QACvB,OAAO,IAAI,aAAa,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACpE,CAAC;IAED,GAAG,CAAC,KAAoB;QACvB,OAAO,IAAI,aAAa,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACpE,CAAC;IAED,GAAG;QACF,OAAO,IAAI,aAAa,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IACtD,CAAC;IAED,yDAAyD;IACzD,GAAG,CAAC,KAAoB;QACvB,OAAO,IAAI,aAAa,CAAC,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACzE,CAAC;CACD"}
@@ -0,0 +1,32 @@
1
+ import { type IsCloseToOptions } from "@ac-kit/core";
2
+ export type Quaternion = [number, number, number, number];
3
+ export declare const QUATERNION_IDENTITY: [0, 0, 0, 1];
4
+ export declare function quaternionAdd(q1: Quaternion, q2: Quaternion): Quaternion;
5
+ export declare function quaternionSub(q1: Quaternion, q2: Quaternion): Quaternion;
6
+ export declare function quaternionNeg(q: Quaternion): Quaternion;
7
+ export declare function quaternionConjugate(q: Quaternion): Quaternion;
8
+ export declare function quaternionDot(q1: Quaternion, q2: Quaternion): number;
9
+ /**
10
+ * Exact component-wise equality. See {@link quaternionIsClose} for a tolerant
11
+ * test.
12
+ *
13
+ * Compares the four components, so `q` and `-q` are not equal even though they
14
+ * denote the same rotation.
15
+ */
16
+ export declare function quaternionEquals(q1: Quaternion, q2: Quaternion): boolean;
17
+ /**
18
+ * Component-wise approximate equality.
19
+ *
20
+ * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
21
+ * and never a basis for ordering — use {@link quaternionEquals} there. Like the
22
+ * exact form it compares components, so `q` and `-q` are not close even though
23
+ * they denote the same rotation; compare `|quaternionDot(q1, q2)|` against `1`
24
+ * to test the rotations instead.
25
+ */
26
+ export declare function quaternionIsClose(q1: Quaternion, q2: Quaternion, tolerance: IsCloseToOptions): boolean;
27
+ export declare function quaternionInverse(q: Quaternion): Quaternion;
28
+ export declare function quaternionLength(q: Quaternion): number;
29
+ export declare function quaternionLengthSq(q: Quaternion): number;
30
+ export declare function quaternionMultiply(q1: Quaternion, q2: Quaternion): Quaternion;
31
+ export declare function quaternionNormalize(q: Quaternion): Quaternion;
32
+ //# sourceMappingURL=quaternion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quaternion.d.ts","sourceRoot":"","sources":["../../src/quaternion/quaternion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhE,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE1D,eAAO,MAAM,mBAAmB,cAA6C,CAAC;AAE9E,wBAAgB,aAAa,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,UAAU,CAExE;AAED,wBAAgB,aAAa,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,UAAU,CAExE;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU,CAEvD;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU,CAE7D;AAED,wBAAgB,aAAa,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,MAAM,CAEpE;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,OAAO,CAIxE;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAChC,EAAE,EAAE,UAAU,EACd,EAAE,EAAE,UAAU,EACd,SAAS,EAAE,gBAAgB,GACzB,OAAO,CAOT;AAED,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU,CAY3D;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,CAEtD;AAED,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,CAExD;AAED,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,UAAU,CAgB7E;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU,CAQ7D"}
@@ -0,0 +1,80 @@
1
+ import { isCloseTo } from "@ac-kit/core";
2
+ import { diffOfProducts, dotPrecise, sumOfProducts } from "@ac-kit/math-scalar";
3
+ export const QUATERNION_IDENTITY = [0, 0, 0, 1];
4
+ export function quaternionAdd(q1, q2) {
5
+ return [q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]];
6
+ }
7
+ export function quaternionSub(q1, q2) {
8
+ return [q1[0] - q2[0], q1[1] - q2[1], q1[2] - q2[2], q1[3] - q2[3]];
9
+ }
10
+ export function quaternionNeg(q) {
11
+ return [-q[0], -q[1], -q[2], -q[3]];
12
+ }
13
+ export function quaternionConjugate(q) {
14
+ return [-q[0], -q[1], -q[2], q[3]];
15
+ }
16
+ export function quaternionDot(q1, q2) {
17
+ return dotPrecise(q1, q2);
18
+ }
19
+ /**
20
+ * Exact component-wise equality. See {@link quaternionIsClose} for a tolerant
21
+ * test.
22
+ *
23
+ * Compares the four components, so `q` and `-q` are not equal even though they
24
+ * denote the same rotation.
25
+ */
26
+ export function quaternionEquals(q1, q2) {
27
+ return (q1[0] === q2[0] && q1[1] === q2[1] && q1[2] === q2[2] && q1[3] === q2[3]);
28
+ }
29
+ /**
30
+ * Component-wise approximate equality.
31
+ *
32
+ * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
33
+ * and never a basis for ordering — use {@link quaternionEquals} there. Like the
34
+ * exact form it compares components, so `q` and `-q` are not close even though
35
+ * they denote the same rotation; compare `|quaternionDot(q1, q2)|` against `1`
36
+ * to test the rotations instead.
37
+ */
38
+ export function quaternionIsClose(q1, q2, tolerance) {
39
+ return (isCloseTo(q1[0], q2[0], tolerance) &&
40
+ isCloseTo(q1[1], q2[1], tolerance) &&
41
+ isCloseTo(q1[2], q2[2], tolerance) &&
42
+ isCloseTo(q1[3], q2[3], tolerance));
43
+ }
44
+ export function quaternionInverse(q) {
45
+ const conjugate = quaternionConjugate(q);
46
+ const normSq = q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3];
47
+ if (normSq === 0) {
48
+ throw new Error("Cannot invert a zero-length quaternion.");
49
+ }
50
+ return [
51
+ conjugate[0] / normSq,
52
+ conjugate[1] / normSq,
53
+ conjugate[2] / normSq,
54
+ conjugate[3] / normSq,
55
+ ];
56
+ }
57
+ export function quaternionLength(q) {
58
+ return Math.sqrt(quaternionLengthSq(q));
59
+ }
60
+ export function quaternionLengthSq(q) {
61
+ return q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3];
62
+ }
63
+ export function quaternionMultiply(q1, q2) {
64
+ const x1 = q1[0], y1 = q1[1], z1 = q1[2], w1 = q1[3];
65
+ const x2 = q2[0], y2 = q2[1], z2 = q2[2], w2 = q2[3];
66
+ return [
67
+ sumOfProducts(w1, x2, x1, w2) + diffOfProducts(y1, z2, z1, y2),
68
+ diffOfProducts(w1, y2, x1, z2) + sumOfProducts(y1, w2, z1, x2),
69
+ sumOfProducts(w1, z2, x1, y2) + diffOfProducts(z1, w2, y1, x2),
70
+ diffOfProducts(w1, w2, x1, x2) - sumOfProducts(y1, y2, z1, z2),
71
+ ];
72
+ }
73
+ export function quaternionNormalize(q) {
74
+ const length = Math.sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
75
+ if (length === 0) {
76
+ throw new Error("Cannot normalize a zero-length quaternion.");
77
+ }
78
+ return [q[0] / length, q[1] / length, q[2] / length, q[3] / length];
79
+ }
80
+ //# sourceMappingURL=quaternion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quaternion.js","sourceRoot":"","sources":["../../src/quaternion/quaternion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAyB,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAIhF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAA+B,CAAC;AAE9E,MAAM,UAAU,aAAa,CAAC,EAAc,EAAE,EAAc;IAC3D,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,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AC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC;KAC9D,CAAC;AACH,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,CAAa;IAChD,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CACvB,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CACrD,CAAC;IACF,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;AACrE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@ac-kit/math-complex",
3
+ "version": "0.1.0",
4
+ "description": "Complex numbers and their extensions: the finite-dimensional algebras over the reals.",
5
+ "keywords": [
6
+ "algebra",
7
+ "complex",
8
+ "math",
9
+ "quaternion"
10
+ ],
11
+ "homepage": "https://anthochamp.github.io/node-essentials/api/math-complex/",
12
+ "bugs": {
13
+ "url": "https://github.com/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
20
+ "directory": "packages/math-complex"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "main": "./dist/index.js",
33
+ "exports": {
34
+ ".": "./dist/index.js"
35
+ },
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
40
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
41
+ "clean": "rimraf dist",
42
+ "build": "yarn clean && yarn compile",
43
+ "test": "yarn run -T vitest run",
44
+ "prepack": "yarn build"
45
+ },
46
+ "dependencies": {
47
+ "@ac-kit/core": "^0.1.0",
48
+ "@ac-kit/math-algebra": "^0.1.0",
49
+ "@ac-kit/math-scalar": "^0.1.0",
50
+ "@types/web": "^0.0.354"
51
+ },
52
+ "devDependencies": {
53
+ "@ac-kit/tsconfig": "0.1.0",
54
+ "@ac-kit/typedoc-config": "0.1.0",
55
+ "@ac-kit/vitest-config": "0.1.0",
56
+ "@typescript/native": "typescript@7.0.2",
57
+ "rimraf": "6.1.3",
58
+ "typedoc": "0.28.20",
59
+ "typescript": "@typescript/typescript6@6.0.2",
60
+ "vite": "8.3.0",
61
+ "vitest": "5.0.0"
62
+ },
63
+ "engines": {
64
+ "node": ">=24"
65
+ }
66
+ }