Skip to content

Commit

Permalink
fix: fix zero cancelling bug in dimensional analysis
Browse files Browse the repository at this point in the history
Test has been added
  • Loading branch information
mgreminger committed Jan 9, 2025
1 parent 0ba5eba commit c799317
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
12 changes: 8 additions & 4 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,10 +1516,14 @@ def replace_placeholder_funcs(expr: Expr,
dim_values_dict: dict[tuple[Basic,...], DimValues],
function_parents: list[Basic],
data_table_subs: DataTableSubs | None) -> Expr:

if (not is_matrix(expr)):
if isinstance(expr, Symbol) and expr.name == "_zero_delayed_substitution":
return sympify('0')

if (not is_matrix(expr)) and expr.func == function_id_wrapper:
function_parents.append(expr.args[0])
expr = cast(Expr, expr.args[1])
elif expr.func == function_id_wrapper:
function_parents.append(expr.args[0])
expr = cast(Expr, expr.args[1])

if is_matrix(expr):
rows = []
Expand Down Expand Up @@ -1736,7 +1740,7 @@ def get_sorted_statements(statements: list[Statement], custom_definition_names:
zero_place_holder: ImplicitParameter = {
"dimensions": [0]*9,
"original_value": "0",
"si_value": "0",
"si_value": "_zero_delayed_substitution",
"name": ZERO_PLACEHOLDER,
"units": ""
}
Expand Down
4 changes: 4 additions & 0 deletions src/parser/LatexToSympy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,10 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
}
}

if(Number(si_value) === 0) {
si_value = "_zero_delayed_substitution";
}

this.implicitParams.push({
name: newParamName,
units: unitBlockData.units,
Expand Down
16 changes: 15 additions & 1 deletion tests/test_basic.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,25 @@ test('Test function notation with exponents and units and nested functions', asy
await page.waitForSelector('text=Updating...', {state: 'detached'});

let content = await page.textContent('#result-value-0');
expect(parseLatexFloat(content)).toBeCloseTo(512, precision-1);
expect(parseLatexFloat(content)).toBeCloseTo(512, precision);
content = await page.textContent('#result-units-0');
expect(content).toBe('');
});

test('Test zero canceling bug with exponent', async () => {

await page.setLatex(0, String.raw`y=\frac{0\left\lbrack m\right\rbrack}{2^{x}}`);
await page.click('#add-math-cell');
await page.setLatex(1, String.raw`y\left(x=1\right)=`);

await page.waitForSelector('text=Updating...', {state: 'detached'});

let content = await page.textContent('#result-value-1');
expect(parseLatexFloat(content)).toBeCloseTo(0, precision);
content = await page.textContent('#result-units-1');
expect(content).toBe('m');
});


test('Test function notation with integrals', async () => {

Expand Down

0 comments on commit c799317

Please sign in to comment.