@danielsimonjr/mathts-core 0.1.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +116 -3
- package/dist/index.js +119 -3
- package/package.json +1 -1
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
|
/**
|
|
@@ -416,6 +416,84 @@ declare const COMPLEX_ZERO: Complex;
|
|
|
416
416
|
declare const COMPLEX_ONE: Complex;
|
|
417
417
|
declare const COMPLEX_NEG_ONE: Complex;
|
|
418
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
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Named mathematical constants (plain `number`).
|
|
469
|
+
*
|
|
470
|
+
* These mirror the constants documented in `docs/reference/constants.md` and the
|
|
471
|
+
* familiar `Math.*` values, exported as first-class named bindings so callers can
|
|
472
|
+
* `import { PI, E, TAU } from '@danielsimonjr/mathts-core'`. The imaginary unit
|
|
473
|
+
* `I` and the type-specific constants (`COMPLEX_*`, `BIGNUMBER_*`, `FRACTION_*`)
|
|
474
|
+
* live with their respective types.
|
|
475
|
+
*/
|
|
476
|
+
/** Ratio of a circle's circumference to its diameter. */
|
|
477
|
+
declare const PI: number;
|
|
478
|
+
/** Euler's number, the base of the natural logarithm. */
|
|
479
|
+
declare const E: number;
|
|
480
|
+
/** Full-circle constant, `2·PI`. */
|
|
481
|
+
declare const TAU: number;
|
|
482
|
+
/** Golden ratio, `(1 + √5) / 2`. */
|
|
483
|
+
declare const PHI: number;
|
|
484
|
+
/** Square root of 2. */
|
|
485
|
+
declare const SQRT2: number;
|
|
486
|
+
/** Square root of one-half. */
|
|
487
|
+
declare const SQRT1_2: number;
|
|
488
|
+
/** Natural logarithm of 2. */
|
|
489
|
+
declare const LN2: number;
|
|
490
|
+
/** Natural logarithm of 10. */
|
|
491
|
+
declare const LN10: number;
|
|
492
|
+
/** Base-2 logarithm of E. */
|
|
493
|
+
declare const LOG2E: number;
|
|
494
|
+
/** Base-10 logarithm of E. */
|
|
495
|
+
declare const LOG10E: number;
|
|
496
|
+
|
|
419
497
|
/**
|
|
420
498
|
* Fraction (rational number) implementation
|
|
421
499
|
* @module @danielsimonjr/mathts-core/types/fraction
|
|
@@ -1300,6 +1378,41 @@ declare function initTypedWasm(options?: {
|
|
|
1300
1378
|
*/
|
|
1301
1379
|
declare function isTypedWasmAvailable(): boolean;
|
|
1302
1380
|
|
|
1381
|
+
/**
|
|
1382
|
+
* Any concrete implementation accepted when *declaring* a typed signature.
|
|
1383
|
+
*
|
|
1384
|
+
* typed-function's published `SignatureFunction` is `(...args: unknown[]) =>
|
|
1385
|
+
* unknown`, which is correct for *internal/output* positions (where the stored
|
|
1386
|
+
* impl is later CALLED with validated args) but wrong as an *input* type:
|
|
1387
|
+
* under `strictFunctionTypes`, function parameters are contravariant, so a
|
|
1388
|
+
* concrete impl such as `(a: number, b: number) => number` is NOT assignable
|
|
1389
|
+
* to an `unknown[]` parameter list (`unknown` is not assignable to `number`).
|
|
1390
|
+
* The correct top-type for "any function" in an input position uses `never`
|
|
1391
|
+
* parameters — every function is assignable to it because `never` is
|
|
1392
|
+
* assignable to every parameter type. This is exactly the set of values
|
|
1393
|
+
* typed-function genuinely accepts when declaring signatures.
|
|
1394
|
+
*/
|
|
1395
|
+
type SignatureImpl = (...args: never[]) => unknown;
|
|
1396
|
+
/** Signature record accepted by the MathTS typed factory (see {@link SignatureImpl}). */
|
|
1397
|
+
type SignatureRecord = Record<string, SignatureImpl | ReferTo | ReferToSelf>;
|
|
1398
|
+
/**
|
|
1399
|
+
* The MathTS typed-function factory type.
|
|
1400
|
+
*
|
|
1401
|
+
* Structurally identical to typed-function's {@link TypedInstance} except its
|
|
1402
|
+
* call signatures accept implementations declared with concrete parameter
|
|
1403
|
+
* types (see {@link SignatureImpl}). All instance methods are inherited
|
|
1404
|
+
* unchanged via `Omit<TypedInstance, never>` (a mapped type, which drops the
|
|
1405
|
+
* overly-strict published call signatures while preserving every method).
|
|
1406
|
+
*/
|
|
1407
|
+
interface MathTSTyped extends Omit<TypedInstance, never> {
|
|
1408
|
+
(name: string, signatures: SignatureRecord): TypedFunction;
|
|
1409
|
+
(signatures: SignatureRecord): TypedFunction;
|
|
1410
|
+
(...args: Array<string | SignatureRecord | TypedFunction | (SignatureImpl & {
|
|
1411
|
+
signature: string;
|
|
1412
|
+
})>): TypedFunction;
|
|
1413
|
+
referToSelf: (callback: (self: TypedFunction) => SignatureImpl) => ReferToSelf;
|
|
1414
|
+
referTo: (...args: [...string[], (...fns: SignatureFunction[]) => SignatureImpl]) => ReferTo;
|
|
1415
|
+
}
|
|
1303
1416
|
/**
|
|
1304
1417
|
* Type definition for typed-function
|
|
1305
1418
|
*/
|
|
@@ -1406,7 +1519,7 @@ declare function createMathTSTyped(): TypedInstance;
|
|
|
1406
1519
|
* add(1, new Complex(2, 3)); // Complex(3, 3) - auto-converts
|
|
1407
1520
|
* ```
|
|
1408
1521
|
*/
|
|
1409
|
-
declare const mathTyped:
|
|
1522
|
+
declare const mathTyped: MathTSTyped;
|
|
1410
1523
|
|
|
1411
1524
|
/**
|
|
1412
1525
|
* TypeRegistry class for managing custom type registrations
|
|
@@ -1600,4 +1713,4 @@ declare const math: {
|
|
|
1600
1713
|
|
|
1601
1714
|
declare const VERSION = "0.1.0";
|
|
1602
1715
|
|
|
1603
|
-
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 };
|
|
1716
|
+
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, E, 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, LN10, LN2, LOG10E, LOG2E, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericType, PHI, PI, type RoundingMode, SI_PREFIXES, SQRT1_2, SQRT2, type Scalar, TAU, 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
|
@@ -553,6 +553,109 @@ var COMPLEX_ZERO = new Complex(0, 0);
|
|
|
553
553
|
var COMPLEX_ONE = new Complex(1, 0);
|
|
554
554
|
var COMPLEX_NEG_ONE = new Complex(-1, 0);
|
|
555
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
|
+
|
|
647
|
+
// src/constants.ts
|
|
648
|
+
var PI = Math.PI;
|
|
649
|
+
var E = Math.E;
|
|
650
|
+
var TAU = 2 * Math.PI;
|
|
651
|
+
var PHI = (1 + Math.sqrt(5)) / 2;
|
|
652
|
+
var SQRT2 = Math.SQRT2;
|
|
653
|
+
var SQRT1_2 = Math.SQRT1_2;
|
|
654
|
+
var LN2 = Math.LN2;
|
|
655
|
+
var LN10 = Math.LN10;
|
|
656
|
+
var LOG2E = Math.LOG2E;
|
|
657
|
+
var LOG10E = Math.LOG10E;
|
|
658
|
+
|
|
556
659
|
// src/types/fraction.ts
|
|
557
660
|
function isFraction(value) {
|
|
558
661
|
return value instanceof Fraction;
|
|
@@ -840,7 +943,7 @@ var Fraction = class _Fraction {
|
|
|
840
943
|
return this.inverse().pow(-exp);
|
|
841
944
|
}
|
|
842
945
|
let result = new _Fraction(1n, 1n);
|
|
843
|
-
let base = this;
|
|
946
|
+
let base = this.clone();
|
|
844
947
|
let e = exp;
|
|
845
948
|
while (e > 0n) {
|
|
846
949
|
if (e % 2n === 1n) {
|
|
@@ -1564,7 +1667,7 @@ var BigNumber = class _BigNumber {
|
|
|
1564
1667
|
return _BigNumber.fromNumber(1).divide(this.pow(-exp));
|
|
1565
1668
|
}
|
|
1566
1669
|
let result = _BigNumber.fromNumber(1);
|
|
1567
|
-
let base = this;
|
|
1670
|
+
let base = this.clone();
|
|
1568
1671
|
let e = exp;
|
|
1569
1672
|
while (e > 0) {
|
|
1570
1673
|
if (e % 2 === 1) {
|
|
@@ -2972,7 +3075,8 @@ var MATHTS_TYPES = [
|
|
|
2972
3075
|
{ name: "SparseMatrix", test: isSparseMatrix },
|
|
2973
3076
|
{ name: "Matrix", test: isMatrix },
|
|
2974
3077
|
// Other types
|
|
2975
|
-
{ name: "Unit", test: isUnit2 }
|
|
3078
|
+
{ name: "Unit", test: isUnit2 },
|
|
3079
|
+
{ name: "Dual", test: isDual }
|
|
2976
3080
|
];
|
|
2977
3081
|
var MATHTS_CONVERSIONS = [
|
|
2978
3082
|
// -------------------------------------------------------------------------
|
|
@@ -3408,6 +3512,8 @@ export {
|
|
|
3408
3512
|
DERIVED_UNITS,
|
|
3409
3513
|
DIMENSIONLESS,
|
|
3410
3514
|
DimensionMismatchError,
|
|
3515
|
+
Dual,
|
|
3516
|
+
E,
|
|
3411
3517
|
FRACTION_HALF,
|
|
3412
3518
|
FRACTION_NEG_ONE,
|
|
3413
3519
|
FRACTION_ONE,
|
|
@@ -3417,9 +3523,18 @@ export {
|
|
|
3417
3523
|
Fraction,
|
|
3418
3524
|
FunctionRegistry,
|
|
3419
3525
|
I,
|
|
3526
|
+
LN10,
|
|
3527
|
+
LN2,
|
|
3528
|
+
LOG10E,
|
|
3529
|
+
LOG2E,
|
|
3420
3530
|
MATHTS_CONVERSIONS,
|
|
3421
3531
|
MATHTS_TYPES,
|
|
3532
|
+
PHI,
|
|
3533
|
+
PI,
|
|
3422
3534
|
SI_PREFIXES,
|
|
3535
|
+
SQRT1_2,
|
|
3536
|
+
SQRT2,
|
|
3537
|
+
TAU,
|
|
3423
3538
|
TypeRegistry,
|
|
3424
3539
|
UNIT_ALIASES,
|
|
3425
3540
|
Unit,
|
|
@@ -3439,6 +3554,7 @@ export {
|
|
|
3439
3554
|
isBoolean,
|
|
3440
3555
|
isComplex,
|
|
3441
3556
|
isDenseMatrix,
|
|
3557
|
+
isDual,
|
|
3442
3558
|
isFraction,
|
|
3443
3559
|
isFunction,
|
|
3444
3560
|
isMatrix,
|