Skip to content

Commit 11ce660

Browse files
authored
Merge pull request #365 from mgreminger/simplify-fix
fix: only simplify symbolic result for non-numeric results
2 parents 83d1eec + 301f1cf commit 11ce660

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

public/dimensional_analysis.py

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,42 +3296,21 @@ def subs_wrapper(expression: Expr, subs: dict[str, str] | dict[str, Expr | float
32963296
def get_evaluated_expression(expression: Expr,
32973297
parameter_subs: dict[Symbol, Expr],
32983298
dim_subs: dict[Symbol, Expr],
3299-
simplify_symbolic_expressions: bool,
33003299
placeholder_map: dict[Function, PlaceholderFunction],
33013300
placeholder_set: set[Function],
3302-
variable_name_map: dict[Symbol, str]) -> tuple[ExprWithAssumptions, str | list[list[str]], Expr | None, Exception | None]:
3301+
variable_name_map: dict[Symbol, str]) -> tuple[ExprWithAssumptions, Expr | Matrix, Expr | None, Exception | None]:
33033302

33043303
expression, dim_expression, error = replace_placeholder_funcs(expression, None, True, parameter_subs, dim_subs,
33053304
placeholder_map,
33063305
placeholder_set,
33073306
DataTableSubs())
3308-
if not is_matrix(expression):
3309-
if simplify_symbolic_expressions:
3310-
try:
3311-
symbolic_expression = custom_latex(cancel(expression), variable_name_map)
3312-
except ValueError as e:
3313-
symbolic_expression = custom_latex(expression, variable_name_map)
3314-
else:
3315-
symbolic_expression = custom_latex(expression, variable_name_map)
3316-
else:
3317-
symbolic_expression = []
3318-
for i in range(expression.rows):
3319-
row = []
3320-
symbolic_expression.append(row)
3321-
for j in range(expression.cols):
3322-
if simplify_symbolic_expressions:
3323-
try:
3324-
row.append(custom_latex(cancel(expression[i,j]), variable_name_map))
3325-
except ValueError as e:
3326-
row.append(custom_latex(cast(Expr, expression[i,j]), variable_name_map))
3327-
else:
3328-
row.append(custom_latex(cast(Expr, expression[i,j]), variable_name_map))
33293307

33303308
evaluated_expression = cast(ExprWithAssumptions, expression.evalf(PRECISION))
3331-
return evaluated_expression, symbolic_expression, dim_expression, error
3309+
return evaluated_expression, expression, dim_expression, error
33323310

3333-
def get_result(evaluated_expression: ExprWithAssumptions, dimensional_analysis_expression: Expr | None,
3334-
dim_sub_error: Exception | None, symbolic_expression: str,
3311+
def get_result(evaluated_expression: ExprWithAssumptions, dimensional_analysis_expression: Expr | None,
3312+
simplify_symbolic_expressions: bool,
3313+
dim_sub_error: Exception | None, symbolic_expression: Expr,
33353314
isRange: bool, custom_base_units: CustomBaseUnits | None,
33363315
isSubQuery: bool, subQueryName: str,
33373316
variable_name_map: dict[Symbol, str]
@@ -3350,22 +3329,22 @@ def get_result(evaluated_expression: ExprWithAssumptions, dimensional_analysis_e
33503329

33513330
if evaluated_expression.is_number:
33523331
if evaluated_expression.is_real and evaluated_expression.is_finite:
3353-
result = Result(value=str(evaluated_expression), symbolicValue=symbolic_expression,
3332+
result = Result(value=str(evaluated_expression), symbolicValue=custom_latex(symbolic_expression, variable_name_map),
33543333
numeric=True, units=dim, unitsLatex=dim_latex, real=True, finite=True,
33553334
customUnitsDefined=custom_units_defined, customUnits=custom_units,
33563335
customUnitsLatex=custom_units_latex, isSubResult=isSubQuery,
33573336
subQueryName=subQueryName)
33583337
elif not evaluated_expression.is_finite:
33593338
result = Result(value=custom_latex(evaluated_expression, variable_name_map),
3360-
symbolicValue=symbolic_expression,
3339+
symbolicValue=custom_latex(symbolic_expression, variable_name_map),
33613340
numeric=True, units=dim, unitsLatex=dim_latex,
33623341
real=cast(bool, evaluated_expression.is_real),
33633342
finite=False, customUnitsDefined=custom_units_defined,
33643343
customUnits=custom_units, customUnitsLatex=custom_units_latex,
33653344
isSubResult=isSubQuery, subQueryName=subQueryName)
33663345
else:
33673346
result = FiniteImagResult(value=str(evaluated_expression).replace('I', 'i').replace('*', ''),
3368-
symbolicValue=symbolic_expression,
3347+
symbolicValue=custom_latex(symbolic_expression, variable_name_map),
33693348
numeric=True, units=dim, unitsLatex=dim_latex, real=False,
33703349
realPart=str(re(evaluated_expression)),
33713350
imagPart=str(im(evaluated_expression)),
@@ -3380,8 +3359,14 @@ def get_result(evaluated_expression: ExprWithAssumptions, dimensional_analysis_e
33803359
value=getattr(evaluated_expression, "render_value", ""),
33813360
dimensionError=dim if "Dimension Error" in dim else "")
33823361
else:
3362+
if simplify_symbolic_expressions:
3363+
try:
3364+
symbolic_expression = cancel(symbolic_expression)
3365+
except ValueError as e:
3366+
pass
3367+
33833368
result = Result(value=custom_latex(evaluated_expression, variable_name_map),
3384-
symbolicValue=symbolic_expression,
3369+
symbolicValue=custom_latex(symbolic_expression, variable_name_map),
33853370
numeric=False, units="", unitsLatex="",
33863371
real=False, finite=False, customUnitsDefined=False,
33873372
customUnits="", customUnitsLatex="",
@@ -3579,22 +3564,22 @@ def evaluate_statements(statements: list[InputAndSystemStatement],
35793564
evaluated_expression, symbolic_expression, dimensional_analysis_expression, dim_sub_error = get_evaluated_expression(expression,
35803565
parameter_subs,
35813566
dimensional_analysis_subs,
3582-
simplify_symbolic_expressions,
35833567
placeholder_map,
35843568
placeholder_set,
35853569
variable_name_map)
35863570

35873571
if not is_matrix(evaluated_expression):
35883572
results[index] = get_result(evaluated_expression, dimensional_analysis_expression,
3589-
dim_sub_error, cast(str, symbolic_expression),
3590-
item["isRange"],
3591-
custom_base_units,
3592-
item["isSubQuery"],
3593-
item["subQueryName"],
3594-
variable_name_map)
3573+
simplify_symbolic_expressions,
3574+
dim_sub_error, cast(Expr, symbolic_expression),
3575+
item["isRange"],
3576+
custom_base_units,
3577+
item["isSubQuery"],
3578+
item["subQueryName"],
3579+
variable_name_map)
35953580

35963581
elif is_matrix(evaluated_expression) and (dimensional_analysis_expression is None or \
3597-
is_matrix(dimensional_analysis_expression)) and isinstance(symbolic_expression, list) :
3582+
is_matrix(dimensional_analysis_expression)) and is_matrix(symbolic_expression) :
35983583

35993584
if dimensional_analysis_expression is not None and (
36003585
evaluated_expression.rows != dimensional_analysis_expression.rows and
@@ -3613,7 +3598,8 @@ def evaluate_statements(statements: list[InputAndSystemStatement],
36133598

36143599
current_result = get_result(cast(ExprWithAssumptions, evaluated_expression[i,j]),
36153600
cast(Expr, current_dimensional_analysis_expression),
3616-
dim_sub_error, symbolic_expression[i][j],
3601+
simplify_symbolic_expressions,
3602+
dim_sub_error, cast(Expr, symbolic_expression[i,j]),
36173603
item["isRange"],
36183604
custom_base_units,
36193605
item["isSubQuery"],

tests/test_basic.spec.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,3 +1938,26 @@ test('Test error units applied to variable name', async () => {
19381938

19391939
await expect(page.locator('#cell-0 >> text=Units cannot be applied directly to a variable name')).toBeVisible();
19401940
});
1941+
1942+
test('Test slow simplification issue', async ({ browserName }) => {
1943+
test.skip(browserName === "chromium", "Playwright does not currently support the File System Access API");
1944+
1945+
// open the sheet that causes the error
1946+
const path = "tests/test_sheet_slow_simplify.epxyz";
1947+
page.once('filechooser', async (fileChooser) => {
1948+
await fileChooser.setFiles(path);
1949+
});
1950+
1951+
await page.locator('#open-sheet').click();
1952+
1953+
await page.waitForTimeout(8000);
1954+
1955+
await page.locator('h3 >> text=Opening File').waitFor({state: 'detached', timeout: 5000});
1956+
1957+
await page.waitForSelector('text=Updating...', {state: 'detached', timeout: 120000});
1958+
1959+
let content = await page.textContent('#result-value-32');
1960+
expect(parseLatexFloat(content)).toBeCloseTo(87.033, precision);
1961+
content = await page.textContent('#result-units-32');
1962+
expect(content).toBe('lbf');
1963+
});

tests/test_sheet_slow_simplify.epxyz

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)