@danielsimonjr/mathts-core 0.1.5 → 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 +86 -3
- package/dist/index.js +97 -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,54 @@ 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
|
+
|
|
419
467
|
/**
|
|
420
468
|
* Fraction (rational number) implementation
|
|
421
469
|
* @module @danielsimonjr/mathts-core/types/fraction
|
|
@@ -1300,6 +1348,41 @@ declare function initTypedWasm(options?: {
|
|
|
1300
1348
|
*/
|
|
1301
1349
|
declare function isTypedWasmAvailable(): boolean;
|
|
1302
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
|
+
}
|
|
1303
1386
|
/**
|
|
1304
1387
|
* Type definition for typed-function
|
|
1305
1388
|
*/
|
|
@@ -1406,7 +1489,7 @@ declare function createMathTSTyped(): TypedInstance;
|
|
|
1406
1489
|
* add(1, new Complex(2, 3)); // Complex(3, 3) - auto-converts
|
|
1407
1490
|
* ```
|
|
1408
1491
|
*/
|
|
1409
|
-
declare const mathTyped:
|
|
1492
|
+
declare const mathTyped: MathTSTyped;
|
|
1410
1493
|
|
|
1411
1494
|
/**
|
|
1412
1495
|
* TypeRegistry class for managing custom type registrations
|
|
@@ -1600,4 +1683,4 @@ declare const math: {
|
|
|
1600
1683
|
|
|
1601
1684
|
declare const VERSION = "0.1.0";
|
|
1602
1685
|
|
|
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 };
|
|
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
|
@@ -553,6 +553,97 @@ 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
|
+
|
|
556
647
|
// src/types/fraction.ts
|
|
557
648
|
function isFraction(value) {
|
|
558
649
|
return value instanceof Fraction;
|
|
@@ -840,7 +931,7 @@ var Fraction = class _Fraction {
|
|
|
840
931
|
return this.inverse().pow(-exp);
|
|
841
932
|
}
|
|
842
933
|
let result = new _Fraction(1n, 1n);
|
|
843
|
-
let base = this;
|
|
934
|
+
let base = this.clone();
|
|
844
935
|
let e = exp;
|
|
845
936
|
while (e > 0n) {
|
|
846
937
|
if (e % 2n === 1n) {
|
|
@@ -1564,7 +1655,7 @@ var BigNumber = class _BigNumber {
|
|
|
1564
1655
|
return _BigNumber.fromNumber(1).divide(this.pow(-exp));
|
|
1565
1656
|
}
|
|
1566
1657
|
let result = _BigNumber.fromNumber(1);
|
|
1567
|
-
let base = this;
|
|
1658
|
+
let base = this.clone();
|
|
1568
1659
|
let e = exp;
|
|
1569
1660
|
while (e > 0) {
|
|
1570
1661
|
if (e % 2 === 1) {
|
|
@@ -2972,7 +3063,8 @@ var MATHTS_TYPES = [
|
|
|
2972
3063
|
{ name: "SparseMatrix", test: isSparseMatrix },
|
|
2973
3064
|
{ name: "Matrix", test: isMatrix },
|
|
2974
3065
|
// Other types
|
|
2975
|
-
{ name: "Unit", test: isUnit2 }
|
|
3066
|
+
{ name: "Unit", test: isUnit2 },
|
|
3067
|
+
{ name: "Dual", test: isDual }
|
|
2976
3068
|
];
|
|
2977
3069
|
var MATHTS_CONVERSIONS = [
|
|
2978
3070
|
// -------------------------------------------------------------------------
|
|
@@ -3408,6 +3500,7 @@ export {
|
|
|
3408
3500
|
DERIVED_UNITS,
|
|
3409
3501
|
DIMENSIONLESS,
|
|
3410
3502
|
DimensionMismatchError,
|
|
3503
|
+
Dual,
|
|
3411
3504
|
FRACTION_HALF,
|
|
3412
3505
|
FRACTION_NEG_ONE,
|
|
3413
3506
|
FRACTION_ONE,
|
|
@@ -3439,6 +3532,7 @@ export {
|
|
|
3439
3532
|
isBoolean,
|
|
3440
3533
|
isComplex,
|
|
3441
3534
|
isDenseMatrix,
|
|
3535
|
+
isDual,
|
|
3442
3536
|
isFraction,
|
|
3443
3537
|
isFunction,
|
|
3444
3538
|
isMatrix,
|