Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle errors in sympy symbolic expression simplification #255

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .wrangler/state/kv/SHEETS/AqVAmzYYfKrQkGZ8BvUC7T

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,11 @@ def custom_latex(expression: Expr) -> str:
piecewise = Function('piecewise')
new_expression = expression.replace(Piecewise, piecewise)

result_latex = latex(new_expression)
try:
result_latex = latex(new_expression)
except ValueError as e:
result_latex = f"\\text{{Error generating symbolic result: {e}}}"


result_latex = result_latex.replace('_{as variable}','')

Expand Down Expand Up @@ -1736,7 +1740,10 @@ def get_evaluated_expression(expression: Expr,
expression = cast(Expr, expression.doit())
if not is_matrix(expression):
if simplify_symbolic_expressions:
symbolic_expression = custom_latex(cancel(expression))
try:
symbolic_expression = custom_latex(cancel(expression))
except ValueError as e:
symbolic_expression = custom_latex(expression)
else:
symbolic_expression = custom_latex(expression)
else:
Expand All @@ -1746,7 +1753,10 @@ def get_evaluated_expression(expression: Expr,
symbolic_expression.append(row)
for j in range(expression.cols):
if simplify_symbolic_expressions:
row.append(custom_latex(cancel(expression[i,j])))
try:
row.append(custom_latex(cancel(expression[i,j])))
except ValueError as e:
row.append(custom_latex(cast(Expr, expression[i,j])))
else:
row.append(custom_latex(cast(Expr, expression[i,j])))

Expand Down
16 changes: 16 additions & 0 deletions tests/test_symbolic_expression_error_handling.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test, expect } from '@playwright/test';

import { precision, pyodideLoadTimeout, compareImages, parseLatexFloat } from './utility.mjs';

test('Test handling of symbolic expression error', async ({ page, browserName }) => {

await page.goto('/AqVAmzYYfKrQkGZ8BvUC7T');
await page.locator('h3 >> text=Retrieving Sheet').waitFor({state: 'detached'});

await page.locator("text=Accept").click();

await page.locator('text=Updating...').waitFor({state: 'detached', timeout: pyodideLoadTimeout});

let content = await page.locator('#result-value-21').textContent();
expect(parseLatexFloat(content)).toBeCloseTo(57168.5056551697, precision);
});
Loading