@danielsimonjr/mathts-core 0.1.4 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TypedInstance, TypedFunction } from 'typed-function';
1
+ import { TypedInstance, ReferTo, ReferToSelf, TypedFunction, SignatureFunction } from 'typed-function';
2
2
  export { ReferTo, ReferToSelf, SignatureFunction, TypedFunction, TypedInstance, create, default as typed } from 'typed-function';
3
3
 
4
4
  /**
@@ -243,6 +243,10 @@ declare class Complex implements IComplex {
243
243
  * Division: (a + bi)/(c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²)
244
244
  */
245
245
  divide(other: Scalar): Complex;
246
+ sub(other: Scalar): Complex;
247
+ mul(other: Scalar): Complex;
248
+ div(other: Scalar): Complex;
249
+ neg(): Complex;
246
250
  /**
247
251
  * Negation: -(a + bi) = -a - bi
248
252
  */
@@ -412,6 +416,54 @@ declare const COMPLEX_ZERO: Complex;
412
416
  declare const COMPLEX_ONE: Complex;
413
417
  declare const COMPLEX_NEG_ONE: Complex;
414
418
 
419
+ /**
420
+ * Dual number for forward-mode automatic differentiation.
421
+ *
422
+ * A dual number `a + b·ε` (with ε² = 0) carries a value and its first
423
+ * derivative. Evaluating a function built from the overloaded elementary
424
+ * operations on `Dual.variable(x)` propagates the derivative automatically:
425
+ *
426
+ * derivative(x => multiply(sin(x), x), 2)
427
+ * // sin(2)·1 + 2·cos(2) — exact, no finite differences
428
+ *
429
+ * The derivative rules live here as methods so the typed-function signatures in
430
+ * the `functions` package are one-liners (e.g. `Dual: (a) => a.sin()`).
431
+ */
432
+ declare class Dual {
433
+ /** Function value (the real part). */
434
+ readonly value: number;
435
+ /** First derivative (the ε coefficient). */
436
+ readonly deriv: number;
437
+ constructor(value: number, deriv?: number);
438
+ /** A constant (derivative 0). */
439
+ static constant(v: number): Dual;
440
+ /** A seed variable (derivative 1) — the point of differentiation. */
441
+ static variable(v: number): Dual;
442
+ add(o: Dual): Dual;
443
+ sub(o: Dual): Dual;
444
+ mul(o: Dual): Dual;
445
+ div(o: Dual): Dual;
446
+ neg(): Dual;
447
+ /** Power with a constant real exponent: d(xᵏ) = k·xᵏ⁻¹·dx. */
448
+ powConst(k: number): Dual;
449
+ /** General power a^b: d = a^b·(b'·ln a + b·a'/a). */
450
+ pow(o: Dual): Dual;
451
+ sin(): Dual;
452
+ cos(): Dual;
453
+ tan(): Dual;
454
+ exp(): Dual;
455
+ log(): Dual;
456
+ sqrt(): Dual;
457
+ square(): Dual;
458
+ abs(): Dual;
459
+ sinh(): Dual;
460
+ cosh(): Dual;
461
+ tanh(): Dual;
462
+ toString(): string;
463
+ }
464
+ /** Type guard for {@link Dual}. */
465
+ declare function isDual(x: unknown): x is Dual;
466
+
415
467
  /**
416
468
  * Fraction (rational number) implementation
417
469
  * @module @danielsimonjr/mathts-core/types/fraction
@@ -502,6 +554,9 @@ declare class Fraction implements IFraction {
502
554
  * Division: a/b ÷ c/d = ad / bc
503
555
  */
504
556
  divide(other: Scalar): Fraction;
557
+ mul(other: Scalar): Fraction;
558
+ sub(other: Scalar): Fraction;
559
+ div(other: Scalar): Fraction;
505
560
  /**
506
561
  * Negation: -(a/b) = -a/b
507
562
  */
@@ -1293,6 +1348,41 @@ declare function initTypedWasm(options?: {
1293
1348
  */
1294
1349
  declare function isTypedWasmAvailable(): boolean;
1295
1350
 
1351
+ /**
1352
+ * Any concrete implementation accepted when *declaring* a typed signature.
1353
+ *
1354
+ * typed-function's published `SignatureFunction` is `(...args: unknown[]) =>
1355
+ * unknown`, which is correct for *internal/output* positions (where the stored
1356
+ * impl is later CALLED with validated args) but wrong as an *input* type:
1357
+ * under `strictFunctionTypes`, function parameters are contravariant, so a
1358
+ * concrete impl such as `(a: number, b: number) => number` is NOT assignable
1359
+ * to an `unknown[]` parameter list (`unknown` is not assignable to `number`).
1360
+ * The correct top-type for "any function" in an input position uses `never`
1361
+ * parameters — every function is assignable to it because `never` is
1362
+ * assignable to every parameter type. This is exactly the set of values
1363
+ * typed-function genuinely accepts when declaring signatures.
1364
+ */
1365
+ type SignatureImpl = (...args: never[]) => unknown;
1366
+ /** Signature record accepted by the MathTS typed factory (see {@link SignatureImpl}). */
1367
+ type SignatureRecord = Record<string, SignatureImpl | ReferTo | ReferToSelf>;
1368
+ /**
1369
+ * The MathTS typed-function factory type.
1370
+ *
1371
+ * Structurally identical to typed-function's {@link TypedInstance} except its
1372
+ * call signatures accept implementations declared with concrete parameter
1373
+ * types (see {@link SignatureImpl}). All instance methods are inherited
1374
+ * unchanged via `Omit<TypedInstance, never>` (a mapped type, which drops the
1375
+ * overly-strict published call signatures while preserving every method).
1376
+ */
1377
+ interface MathTSTyped extends Omit<TypedInstance, never> {
1378
+ (name: string, signatures: SignatureRecord): TypedFunction;
1379
+ (signatures: SignatureRecord): TypedFunction;
1380
+ (...args: Array<string | SignatureRecord | TypedFunction | (SignatureImpl & {
1381
+ signature: string;
1382
+ })>): TypedFunction;
1383
+ referToSelf: (callback: (self: TypedFunction) => SignatureImpl) => ReferToSelf;
1384
+ referTo: (...args: [...string[], (...fns: SignatureFunction[]) => SignatureImpl]) => ReferTo;
1385
+ }
1296
1386
  /**
1297
1387
  * Type definition for typed-function
1298
1388
  */
@@ -1399,7 +1489,7 @@ declare function createMathTSTyped(): TypedInstance;
1399
1489
  * add(1, new Complex(2, 3)); // Complex(3, 3) - auto-converts
1400
1490
  * ```
1401
1491
  */
1402
- declare const mathTyped: TypedInstance;
1492
+ declare const mathTyped: MathTSTyped;
1403
1493
 
1404
1494
  /**
1405
1495
  * TypeRegistry class for managing custom type registrations
@@ -1593,4 +1683,4 @@ declare const math: {
1593
1683
 
1594
1684
  declare const VERSION = "0.1.0";
1595
1685
 
1596
- export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, type BackendType, BigNumber, type BigNumberConfig, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, type ConversionDef, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DimensionMismatchError, type Dimensions, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, type FactoryDependencies, type FactoryFunction, type FactoryImport, Fraction, FunctionRegistry, I, type IBigNumber, type IComplex, type IFraction, type IMatrix, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericType, type RoundingMode, SI_PREFIXES, type Scalar, type TypeDef, TypeRegistry, UNIT_ALIASES, Unit, type UnitDef, UnitParseError, VERSION, createFactory, createMathTSTyped, createTypedFunction, dim, getPrefix, getUnitDef, initTypedWasm, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isFraction, isFunction, isMatrix, isNull, isNumber, isObject, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, isUnit$1 as isUnitValue, math, mathTyped, registerNativeTypes, registry };
1686
+ export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, type BackendType, BigNumber, type BigNumberConfig, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, type ConversionDef, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DimensionMismatchError, type Dimensions, Dual, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, type FactoryDependencies, type FactoryFunction, type FactoryImport, Fraction, FunctionRegistry, I, type IBigNumber, type IComplex, type IFraction, type IMatrix, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericType, type RoundingMode, SI_PREFIXES, type Scalar, type TypeDef, TypeRegistry, UNIT_ALIASES, Unit, type UnitDef, UnitParseError, VERSION, createFactory, createMathTSTyped, createTypedFunction, dim, getPrefix, getUnitDef, initTypedWasm, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isDual, isFraction, isFunction, isMatrix, isNull, isNumber, isObject, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, isUnit$1 as isUnitValue, math, mathTyped, registerNativeTypes, registry };
package/dist/index.js CHANGED
@@ -217,6 +217,27 @@ var Complex = class _Complex {
217
217
  }
218
218
  return new _Complex(this.re / n, this.im / n);
219
219
  }
220
+ // ------------------------------------------------------------
221
+ // Short-name aliases (mathjs-functions calling convention)
222
+ //
223
+ // The functions-package typed-function arithmetic (subtractScalar,
224
+ // multiplyScalar, divideScalar, …) invokes `.add/.sub/.mul/.div/.neg`
225
+ // on scalars. A core Complex (e.g. produced by `sqrt(-4)`) can flow into
226
+ // those code paths, so it must answer to the short names too — otherwise
227
+ // `x.sub is not a function`. Mirrors the Fraction short-alias fix.
228
+ // ------------------------------------------------------------
229
+ sub(other) {
230
+ return this.subtract(other);
231
+ }
232
+ mul(other) {
233
+ return this.multiply(other);
234
+ }
235
+ div(other) {
236
+ return this.divide(other);
237
+ }
238
+ neg() {
239
+ return this.negate();
240
+ }
220
241
  /**
221
242
  * Negation: -(a + bi) = -a - bi
222
243
  */
@@ -532,6 +553,97 @@ var COMPLEX_ZERO = new Complex(0, 0);
532
553
  var COMPLEX_ONE = new Complex(1, 0);
533
554
  var COMPLEX_NEG_ONE = new Complex(-1, 0);
534
555
 
556
+ // src/types/dual.ts
557
+ var Dual = class _Dual {
558
+ /** Function value (the real part). */
559
+ value;
560
+ /** First derivative (the ε coefficient). */
561
+ deriv;
562
+ constructor(value, deriv = 0) {
563
+ this.value = value;
564
+ this.deriv = deriv;
565
+ }
566
+ /** A constant (derivative 0). */
567
+ static constant(v) {
568
+ return new _Dual(v, 0);
569
+ }
570
+ /** A seed variable (derivative 1) — the point of differentiation. */
571
+ static variable(v) {
572
+ return new _Dual(v, 1);
573
+ }
574
+ // --- arithmetic ----------------------------------------------------------
575
+ add(o) {
576
+ return new _Dual(this.value + o.value, this.deriv + o.deriv);
577
+ }
578
+ sub(o) {
579
+ return new _Dual(this.value - o.value, this.deriv - o.deriv);
580
+ }
581
+ mul(o) {
582
+ return new _Dual(this.value * o.value, this.deriv * o.value + this.value * o.deriv);
583
+ }
584
+ div(o) {
585
+ const d = (this.deriv * o.value - this.value * o.deriv) / (o.value * o.value);
586
+ return new _Dual(this.value / o.value, d);
587
+ }
588
+ neg() {
589
+ return new _Dual(-this.value, -this.deriv);
590
+ }
591
+ /** Power with a constant real exponent: d(xᵏ) = k·xᵏ⁻¹·dx. */
592
+ powConst(k) {
593
+ return new _Dual(Math.pow(this.value, k), this.deriv * k * Math.pow(this.value, k - 1));
594
+ }
595
+ /** General power a^b: d = a^b·(b'·ln a + b·a'/a). */
596
+ pow(o) {
597
+ const v = Math.pow(this.value, o.value);
598
+ const d = v * (o.deriv * Math.log(this.value) + o.value * this.deriv / this.value);
599
+ return new _Dual(v, d);
600
+ }
601
+ // --- elementary functions ------------------------------------------------
602
+ sin() {
603
+ return new _Dual(Math.sin(this.value), this.deriv * Math.cos(this.value));
604
+ }
605
+ cos() {
606
+ return new _Dual(Math.cos(this.value), -this.deriv * Math.sin(this.value));
607
+ }
608
+ tan() {
609
+ const c = Math.cos(this.value);
610
+ return new _Dual(Math.tan(this.value), this.deriv / (c * c));
611
+ }
612
+ exp() {
613
+ const e = Math.exp(this.value);
614
+ return new _Dual(e, this.deriv * e);
615
+ }
616
+ log() {
617
+ return new _Dual(Math.log(this.value), this.deriv / this.value);
618
+ }
619
+ sqrt() {
620
+ const s = Math.sqrt(this.value);
621
+ return new _Dual(s, this.deriv / (2 * s));
622
+ }
623
+ square() {
624
+ return new _Dual(this.value * this.value, 2 * this.value * this.deriv);
625
+ }
626
+ abs() {
627
+ return new _Dual(Math.abs(this.value), this.deriv * Math.sign(this.value));
628
+ }
629
+ sinh() {
630
+ return new _Dual(Math.sinh(this.value), this.deriv * Math.cosh(this.value));
631
+ }
632
+ cosh() {
633
+ return new _Dual(Math.cosh(this.value), this.deriv * Math.sinh(this.value));
634
+ }
635
+ tanh() {
636
+ const t = Math.tanh(this.value);
637
+ return new _Dual(t, this.deriv * (1 - t * t));
638
+ }
639
+ toString() {
640
+ return `Dual(${this.value}, ${this.deriv})`;
641
+ }
642
+ };
643
+ function isDual(x) {
644
+ return x instanceof Dual;
645
+ }
646
+
535
647
  // src/types/fraction.ts
536
648
  function isFraction(value) {
537
649
  return value instanceof Fraction;
@@ -773,6 +885,19 @@ var Fraction = class _Fraction {
773
885
  }
774
886
  return this.divide(_Fraction.fromNumber(Number(other.valueOf())));
775
887
  }
888
+ // Short-name aliases (mathjs Fraction API). Ported mathjs functions such as
889
+ // multiplyScalar/subtractScalar/divideScalar call x.mul/x.sub/x.div; without
890
+ // these, mixed number/Fraction dispatch (e.g. affine unit conversion like
891
+ // celsius→fahrenheit) threw "x.mul is not a function".
892
+ mul(other) {
893
+ return this.multiply(other);
894
+ }
895
+ sub(other) {
896
+ return this.subtract(other);
897
+ }
898
+ div(other) {
899
+ return this.divide(other);
900
+ }
776
901
  /**
777
902
  * Negation: -(a/b) = -a/b
778
903
  */
@@ -806,7 +931,7 @@ var Fraction = class _Fraction {
806
931
  return this.inverse().pow(-exp);
807
932
  }
808
933
  let result = new _Fraction(1n, 1n);
809
- let base = this;
934
+ let base = this.clone();
810
935
  let e = exp;
811
936
  while (e > 0n) {
812
937
  if (e % 2n === 1n) {
@@ -1530,7 +1655,7 @@ var BigNumber = class _BigNumber {
1530
1655
  return _BigNumber.fromNumber(1).divide(this.pow(-exp));
1531
1656
  }
1532
1657
  let result = _BigNumber.fromNumber(1);
1533
- let base = this;
1658
+ let base = this.clone();
1534
1659
  let e = exp;
1535
1660
  while (e > 0) {
1536
1661
  if (e % 2 === 1) {
@@ -2938,7 +3063,8 @@ var MATHTS_TYPES = [
2938
3063
  { name: "SparseMatrix", test: isSparseMatrix },
2939
3064
  { name: "Matrix", test: isMatrix },
2940
3065
  // Other types
2941
- { name: "Unit", test: isUnit2 }
3066
+ { name: "Unit", test: isUnit2 },
3067
+ { name: "Dual", test: isDual }
2942
3068
  ];
2943
3069
  var MATHTS_CONVERSIONS = [
2944
3070
  // -------------------------------------------------------------------------
@@ -3374,6 +3500,7 @@ export {
3374
3500
  DERIVED_UNITS,
3375
3501
  DIMENSIONLESS,
3376
3502
  DimensionMismatchError,
3503
+ Dual,
3377
3504
  FRACTION_HALF,
3378
3505
  FRACTION_NEG_ONE,
3379
3506
  FRACTION_ONE,
@@ -3405,6 +3532,7 @@ export {
3405
3532
  isBoolean,
3406
3533
  isComplex,
3407
3534
  isDenseMatrix,
3535
+ isDual,
3408
3536
  isFraction,
3409
3537
  isFunction,
3410
3538
  isMatrix,
package/package.json CHANGED
@@ -1,55 +1,55 @@
1
- {
2
- "name": "@danielsimonjr/mathts-core",
3
- "version": "0.1.4",
4
- "description": "Core types, utilities, and configuration for MathTS",
5
- "author": "Daniel Simon Jr.",
6
- "license": "MIT",
7
- "type": "module",
8
- "main": "./dist/index.js",
9
- "module": "./dist/index.js",
10
- "types": "./dist/index.d.ts",
11
- "exports": {
12
- ".": {
13
- "import": "./dist/index.js",
14
- "types": "./dist/index.d.ts"
15
- }
16
- },
17
- "files": [
18
- "dist",
19
- "README.md"
20
- ],
21
- "scripts": {
22
- "build": "tsup src/index.ts --format esm --dts --clean",
23
- "dev": "tsup src/index.ts --format esm --dts --watch",
24
- "test": "vitest run",
25
- "test:watch": "vitest",
26
- "test:coverage": "vitest run --coverage",
27
- "typecheck": "tsc --noEmit",
28
- "lint": "eslint src --ext .ts",
29
- "lint:fix": "eslint src --ext .ts --fix",
30
- "clean": "rm -rf dist",
31
- "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
32
- },
33
- "dependencies": {
34
- "typed-function": "github:danielsimonjr/typed-function"
35
- },
36
- "devDependencies": {
37
- "@types/node": "^25.5.2",
38
- "tsup": "^8.0.0",
39
- "typescript": "^5.3.0",
40
- "vitest": "^4.1.5"
41
- },
42
- "publishConfig": {
43
- "access": "public"
44
- },
45
- "repository": {
46
- "type": "git",
47
- "url": "https://github.com/danielsimonjr/mathts",
48
- "directory": "core"
49
- },
50
- "keywords": [
51
- "math",
52
- "typescript",
53
- "core"
54
- ]
55
- }
1
+ {
2
+ "name": "@danielsimonjr/mathts-core",
3
+ "version": "0.2.0",
4
+ "description": "Core types, utilities, and configuration for MathTS",
5
+ "author": "Daniel Simon Jr.",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup src/index.ts --format esm --dts --clean",
23
+ "dev": "tsup src/index.ts --format esm --dts --watch",
24
+ "test": "vitest run",
25
+ "test:watch": "vitest",
26
+ "test:coverage": "vitest run --coverage",
27
+ "typecheck": "tsc --noEmit",
28
+ "lint": "eslint src --ext .ts",
29
+ "lint:fix": "eslint src --ext .ts --fix",
30
+ "clean": "rm -rf dist",
31
+ "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
32
+ },
33
+ "dependencies": {
34
+ "typed-function": "github:danielsimonjr/typed-function"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^25.5.2",
38
+ "tsup": "^8.0.0",
39
+ "typescript": "^5.3.0",
40
+ "vitest": "^4.1.5"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/danielsimonjr/mathts",
48
+ "directory": "core"
49
+ },
50
+ "keywords": [
51
+ "math",
52
+ "typescript",
53
+ "core"
54
+ ]
55
+ }