Allow manually derived formulas for things nerdimer can't handl

This commit is contained in:
Daniel Dunn
2025-04-04 01:11:50 -06:00
parent 32adf8effe
commit 1a9ed879d6
3 changed files with 26 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ export const areaSphere: GenericCalcType = {
{
name: 'r',
title: 'Radius',
formula: 'r = sqrt(A/pi) / 2',
unit: 'mm',
default: 1
}

View File

@@ -36,6 +36,10 @@ export interface GenericCalcType {
// If present and false, don't allow user to select this as output
solvable?: boolean;
// Alternate rearrangement of the formula, to be used when calculating this.
// If missing, the main formula is used with auto derivation.
formula?: string;
// Alternates are alternate ways of entering the exact same thing,
// like the diameter or radius. The formula for an alternate
// can use only one variable, always called v, which is the main

View File

@@ -25,6 +25,7 @@ import 'nerdamer-prime/Algebra';
import 'nerdamer-prime/Solve';
import 'nerdamer-prime/Calculus';
import Qty from 'js-quantities';
import { error } from 'console';
function numericSolveEquationFor(
equation: string,
@@ -444,10 +445,29 @@ export default async function makeTool(
setResult('Please select a solve for variable');
return;
}
let expr = nerdamer(calcData.formula);
let expr: nerdamer.Expression | null = null;
for (const i of calcData.variables) {
if (i.name === values.outputVariable) {
if (i.formula !== undefined) {
expr = nerdamer(i.formula);
}
}
}
if (expr == null) {
expr = nerdamer(calcData.formula);
}
if (expr == null) {
throw new Error('No formula found');
return;
}
Object.keys(values.vars).forEach((key) => {
if (key === values.outputVariable) return;
if (expr === null) {
throw new Error('Math fail');
}
expr = expr.sub(key, values.vars[key].value.toString());
});