@danielsimonjr/mathts-functions 0.25.1 → 0.26.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.js CHANGED
@@ -36301,6 +36301,11 @@ var createSolveODE = /* @__PURE__ */ factory(
36301
36301
  }
36302
36302
  return true;
36303
36303
  }
36304
+ function _rmsNorm(v) {
36305
+ let s = 0;
36306
+ for (let i = 0; i < v.length; i++) s += v[i] * v[i];
36307
+ return Math.sqrt(s / v.length);
36308
+ }
36304
36309
  function _rkWasmLoop(f, tspan, y0, options, butcherTableau) {
36305
36310
  const wasm = wasmLoader.getModule();
36306
36311
  if (!wasm) return null;
@@ -36313,12 +36318,21 @@ var createSolveODE = /* @__PURE__ */ factory(
36313
36318
  const t0 = tspan[0];
36314
36319
  const tf = tspan[1];
36315
36320
  const isForwards = tf > t0;
36316
- const steps = 1;
36317
36321
  const tol = options.tol ? options.tol : 1e-4;
36318
36322
  const minDelta = options.minDelta ? options.minDelta : 0.2;
36319
36323
  const maxDelta = options.maxDelta ? options.maxDelta : 5;
36320
36324
  const maxIter = options.maxIter ? options.maxIter : 1e4;
36321
- let h = options.firstStep ? isForwards ? options.firstStep : -options.firstStep : (tf - t0) / steps;
36325
+ let h;
36326
+ if (options.firstStep) {
36327
+ h = isForwards ? options.firstStep : -options.firstStep;
36328
+ } else {
36329
+ const f0 = f(t0, y0);
36330
+ const d0 = _rmsNorm(y0);
36331
+ const d1 = _rmsNorm(Array.isArray(f0) ? f0 : [f0]);
36332
+ const h0 = d0 < 1e-5 || d1 < 1e-5 ? 1e-6 : 0.01 * (d0 / d1);
36333
+ const hMag = Math.min(h0, Math.abs(tf - t0));
36334
+ h = isForwards ? hMag : -hMag;
36335
+ }
36322
36336
  const maxStepVal = options.maxStep;
36323
36337
  const minStepVal = options.minStep;
36324
36338
  const deltaB = new Float64Array(numStages);
@@ -36468,10 +36482,31 @@ var createSolveODE = /* @__PURE__ */ factory(
36468
36482
  bignumber2(butcherTableau.b),
36469
36483
  bignumber2(butcherTableau.bp)
36470
36484
  ] : [butcherTableau.a, butcherTableau.c, butcherTableau.b, butcherTableau.bp];
36471
- let h = firstStep ? isForwards ? firstStep : unaryMinus2(firstStep) : divide2(subtract2(tf, t0), steps);
36485
+ let h;
36486
+ if (firstStep) {
36487
+ h = isForwards ? firstStep : unaryMinus2(firstStep);
36488
+ } else if (!hasBigNumbers && !tspan.some(isUnit) && y0.every((v) => typeof v === "number")) {
36489
+ const f0 = f(t0, y0);
36490
+ const f0arr = Array.isArray(f0) ? f0 : [f0];
36491
+ const d0 = _rmsNorm(y0);
36492
+ const d1 = _rmsNorm(f0arr);
36493
+ const h0 = d0 < 1e-5 || d1 < 1e-5 ? 1e-6 : 0.01 * (d0 / d1);
36494
+ const hMag = Math.min(h0, Math.abs(tf - t0));
36495
+ h = isForwards ? hMag : -hMag;
36496
+ } else {
36497
+ h = divide2(subtract2(tf, t0), steps);
36498
+ }
36472
36499
  const t = [t0];
36473
36500
  const y = [y0];
36474
36501
  const deltaB = subtract2(b, bp);
36502
+ function stageCombo(coeffs, stages) {
36503
+ const m = Math.min(coeffs.length, stages.length);
36504
+ let acc = multiply2(coeffs[0], stages[0]);
36505
+ for (let j = 1; j < m; j++) {
36506
+ acc = add2(acc, multiply2(coeffs[j], stages[j]));
36507
+ }
36508
+ return acc;
36509
+ }
36475
36510
  let n = 0;
36476
36511
  let iter = 0;
36477
36512
  const ongoing = _createOngoing(isForwards);
@@ -36481,14 +36516,14 @@ var createSolveODE = /* @__PURE__ */ factory(
36481
36516
  h = trimStep(t[n], tf, h);
36482
36517
  k.push(f(t[n], y[n]));
36483
36518
  for (let i = 1; i < c.length; ++i) {
36484
- k.push(f(add2(t[n], multiply2(c[i], h)), add2(y[n], multiply2(h, a[i], k))));
36519
+ k.push(f(add2(t[n], multiply2(c[i], h)), add2(y[n], multiply2(h, stageCombo(a[i], k)))));
36485
36520
  }
36486
- const TE = max2(
36487
- abs2(map2(multiply2(deltaB, k), (X) => isUnit(X) ? X.value : X))
36488
- );
36521
+ const errComb = stageCombo(deltaB, k);
36522
+ const errArr = Array.isArray(errComb) ? errComb : [errComb];
36523
+ const TE = max2(abs2(map2(errArr, (X) => isUnit(X) ? X.value : X)));
36489
36524
  if (TE < tol && tol / TE > 1 / 4) {
36490
36525
  t.push(add2(t[n], h));
36491
- y.push(add2(y[n], multiply2(h, b, k)));
36526
+ y.push(add2(y[n], multiply2(h, stageCombo(b, k))));
36492
36527
  n++;
36493
36528
  }
36494
36529
  let delta = 0.84 * (tol / TE) ** (1 / 5);
@@ -1 +1 @@
1
- {"version":3,"file":"solveODE.d.ts","sourceRoot":"","sources":["../../src/numeric/solveODE.ts"],"names":[],"mappings":"AAmEA,eAAO,MAAM,cAAc,yDA8kB1B,CAAC"}
1
+ {"version":3,"file":"solveODE.d.ts","sourceRoot":"","sources":["../../src/numeric/solveODE.ts"],"names":[],"mappings":"AAmEA,eAAO,MAAM,cAAc,yDA8nB1B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielsimonjr/mathts-functions",
3
- "version": "0.25.1",
3
+ "version": "0.26.0",
4
4
  "description": "Mathematical functions for MathTS - arithmetic, algebra, trigonometry, statistics, and more",
5
5
  "author": "Daniel Simon Jr.",
6
6
  "license": "MIT",