Skip to content

Commit

Permalink
fix: enable factorial function in data table cell
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreminger committed Sep 28, 2024
1 parent 5d9f77d commit c84f306
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
35 changes: 25 additions & 10 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from json import loads, dumps

from math import prod
import math

from sympy import (
Float,
Expand Down Expand Up @@ -1121,14 +1121,29 @@ def IndexMatrix(expression: Expr, i: Expr, j: Expr) -> Expr:

return cast(Expr, cast(Matrix, expression)[i, j])

def custom_factorial(expression: ExprWithAssumptions) -> Expr:
if expression.is_number:
if not (expression.is_real and expression.is_finite and expression.is_integer and cast(int, expression) >= 0):
raise Exception("The factorial function can only be evaluated a nonnegative integer")
return factorial(expression)
class CustomFactorial(Function):
is_real = True

# case of symbolic input, doesn't currently work with data tables
return factorial(expression)
@staticmethod
def _imp_(arg1: float):
if arg1.is_integer() and arg1 >= 0.0:
return math.factorial(int(arg1))
else:
raise ValueError("The factorial function can only be evaluated a nonnegative integer")

def _eval_evalf(self, prec):
if self.args[0].is_number:
if not (self.args[0].is_real and
cast(ExprWithAssumptions, self.args[0]).is_finite and
cast(ExprWithAssumptions, self.args[0]).is_integer and cast(int, self.args[0]) >= 0):
raise ValueError("The factorial function can only be evaluated a nonnegative integer")
return factorial(self.args[0])._eval_evalf(prec) # type: ignore

# case of symbolic input
return factorial(self.args[0])

def _latex(self, printer):
return rf"\left({printer._print(self.args[0])}\right)!"

def custom_norm(expression: Matrix):
return expression.norm()
Expand Down Expand Up @@ -1471,7 +1486,7 @@ def get_next_id(self):
cast(Function, Function('_Derivative')) : {"dim_func": custom_derivative_dims, "sympy_func": custom_derivative},
cast(Function, Function('_Integral')) : {"dim_func": custom_integral_dims, "sympy_func": custom_integral},
cast(Function, Function('_range')) : {"dim_func": custom_range, "sympy_func": custom_range},
cast(Function, Function('_factorial')) : {"dim_func": factorial, "sympy_func": custom_factorial},
cast(Function, Function('_factorial')) : {"dim_func": factorial, "sympy_func": CustomFactorial},
}

global_placeholder_set = set(global_placeholder_map.keys())
Expand Down Expand Up @@ -1542,7 +1557,7 @@ def replace_placeholder_funcs(expr: Expr,

if len(matrix_args) > 0 and len(scalar_args) > 0:
first_matrix = matrix_args[0]
scalar = prod(scalar_args)
scalar = math.prod(scalar_args)
new_rows = []
for i in range(first_matrix.rows):
new_row = []
Expand Down
12 changes: 10 additions & 2 deletions tests/test_basic.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ test('Test factorial function', async () => {
await page.setLatex(0, String.raw`4!=`);

await page.click('#add-math-cell');
await page.setLatex(1, String.raw`5-4!=`);
await page.setLatex(1, String.raw`5-4.000!=`);

await page.click('#add-math-cell');
await page.setLatex(2, String.raw`\frac{10!}{9!}=`);
Expand Down Expand Up @@ -1764,7 +1764,7 @@ test('Test factorial function', async () => {
expect(content).toBe('');

content = await page.textContent('#result-value-6');
expect(content).toBe('a!');
expect(content).toBe(String.raw`\left(a\right)!`);
content = await page.textContent('#result-units-6');
expect(content).toBe('');

Expand Down Expand Up @@ -1793,4 +1793,12 @@ test('Test factorial error check for negative input', async () => {
await page.waitForSelector('text=Updating...', {state: 'detached'});

await expect(page.locator('text=The factorial function can only be evaluated a nonnegative integer')).toBeVisible();
});

test('Test factorial error check for input with units', async () => {
await page.setLatex(0, String.raw`1[m]!=`);

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

await expect(page.locator('#cell-0 >> text=Dimension Error')).toBeVisible();
});
22 changes: 22 additions & 0 deletions tests/test_data_table.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,4 +1307,26 @@ test('Test interpolation and polfit with numerical solve', async () => {
content = await page.textContent('#result-units-1');
expect(content).toBe('s');

});

test('Test factorial function in data table', async () => {
await page.setLatex(0, String.raw`Col2=`);

await page.locator('#add-data-table-cell').click();

await expect(page.locator('#data-table-input-1-0-0')).toBeFocused();

await page.keyboard.type('1');
await page.keyboard.press('Enter');
await page.keyboard.type('2');
await page.keyboard.press('Enter');
await page.keyboard.type('3.00');

await page.setLatex(1, String.raw`Col2=Col1!=`, 1);

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

let content = await page.textContent(`#result-value-0`);
expect(content).toBe(String.raw`\begin{bmatrix} 1 \\ 2 \\ 6 \end{bmatrix}`);

});

0 comments on commit c84f306

Please sign in to comment.