Skip to content

Commit

Permalink
fix: make matrix indexing symbolic when not numeric so that summation…
Browse files Browse the repository at this point in the history
… with matrices works

Tests need to be added for summation with matrices
  • Loading branch information
mgreminger committed Oct 25, 2024
1 parent 88db36e commit 507dea8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,8 @@ def UniversalInverse(expression: Expr) -> Expr:

def IndexMatrix(expression: Expr, i: Expr, j: Expr) -> Expr:
for subscript in cast(list[ExprWithAssumptions], (i,j)):
if not (subscript.is_real and subscript.is_finite and subscript.is_integer and cast(int, subscript) > 0):
raise Exception("Matrix indices must evaluate to a finite real integer and be greater than 0")
if subscript.is_real and cast(int, subscript) <= 0:
raise Exception("Matrix indices must be greater than 0")

return expression[i-1, j-1] # type: ignore

Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const INLINE_SHORTCUTS = {
'~': '\\approx',
'sqrt': '\\sqrt{#?}',
'$int': '\\int _{#?}^{#?}\\left(#?\\right)\\mathrm{d}\\left(#?\\right)',
'$sum': '\\sum_{#?=#?}^{#?}\left(#?\\right)',
'$sum': '\\sum_{#?=#?}^{#?}\\left(#?\\right)',
'$prime': '\\frac{\\mathrm{d}}{\\mathrm{d}\\left(#?\\right)}\\left(#?\\right)',
'$doubleprime': '\\frac{\\mathrm{d}^{2}}{\\mathrm{d}\\left(#?\\right)^{2}}\\left(#?\\right)',
'$tripleprime': '\\frac{\\mathrm{d}^{3}}{\\mathrm{d}\\left(#?\\right)^{3}}\\left(#?\\right)',
Expand Down
9 changes: 5 additions & 4 deletions tests/test_matrix_indexing.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ test('Zero index', async () => {

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

await expect(page.locator("text=Matrix indices must evaluate to a finite real integer and be greater than 0")).toBeVisible();
await expect(page.locator("text=Matrix indices must be greater than 0")).toBeVisible();
});

test('Negative index', async () => {
await page.setLatex(0, String.raw`\begin{bmatrix}a & b\\ c & d\\ e & f\end{bmatrix}_{1,-1}=`);

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

await expect(page.locator("text=Matrix indices must evaluate to a finite real integer and be greater than 0")).toBeVisible();
await expect(page.locator("text=Matrix indices must be greater than 0")).toBeVisible();
});

test('Out of range index', async () => {
Expand All @@ -105,15 +105,16 @@ test('Noninteger index', async () => {

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

await expect(page.locator("text=Matrix indices must evaluate to a finite real integer and be greater than 0")).toBeVisible();
await expect(page.locator("text=IndexError, Invalid index")).toBeVisible();
});

test('Nonnumeric index', async () => {
await page.setLatex(0, String.raw`\begin{bmatrix}a & b\\ c & d\\ e & f\end{bmatrix}_{1,z}=`);

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

await expect(page.locator("text=Matrix indices must evaluate to a finite real integer and be greater than 0")).toBeVisible();
let content = await page.textContent('#result-value-0');
expect(content).toBe(String.raw`\left[\begin{matrix}a & b\\c & d\\e & f\end{matrix}\right]_{0, z - 1}`);
});

test('Indexing with expression', async () => {
Expand Down

0 comments on commit 507dea8

Please sign in to comment.