Skip to content

Commit

Permalink
Merge pull request #253 from mgreminger/max-matrix-cols
Browse files Browse the repository at this point in the history
feat: Increase max number of columns allowed for a matrix
  • Loading branch information
mgreminger authored Apr 19, 2024
2 parents 450686b + 23adbe1 commit 0ba41ca
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 20 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"idb-keyval": "^6.2.0",
"mathjax": "^3.2.2",
"mathjs": "^11.8.2",
"mathlive": "github:mgreminger/mathlive#23492aab778bf75b87b6b6f40c9fbe694757ec7e",
"mathlive": "github:mgreminger/mathlive#29b14918088a7f0ad9f8204f982447a7dbc6c8df",
"plotly.js-basic-dist": "github:mgreminger/plotly.js#dist-basic-for-ep",
"quick-lru": "^6.1.1",
"quill": "^1.3.7",
Expand Down
2 changes: 1 addition & 1 deletion src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
const apiUrl = window.location.origin;
const currentVersion = 20240406;
const currentVersion = 20240418;
const tutorialHash = "fFjTsnFoSQMLwcvteVoNtL";
const termsVersion = 20240110;
Expand Down
9 changes: 4 additions & 5 deletions src/CustomMatrixModal.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import type { MathField } from "./cells/MathField";
import { MAX_MATRIX_COLS } from "./constants";
import { getBlankMatrixLatex } from "./utility";
import { Modal, NumberInput } from "carbon-components-svelte";
Expand All @@ -11,12 +12,11 @@
let numColumns = 1;
function insertMatrix() {
if (numRows > 0 && numColumns > 0 && targetMathField && targetMathField.element) {
if (numRows > 0 && numColumns > 0 && numColumns <= MAX_MATRIX_COLS && targetMathField && targetMathField.element) {
const mathLiveField = targetMathField.element.getMathField();
mathLiveField.executeCommand(["insert", getBlankMatrixLatex(numRows, numColumns)]);
open = false;
}
open = false;
}
</script>
Expand Down Expand Up @@ -44,7 +44,6 @@
<div class="number-input">
<NumberInput
min={1}
max={20}
bind:value={numRows}
label="Matrix Rows"
/>
Expand All @@ -53,7 +52,7 @@
<div class="number-input">
<NumberInput
min={1}
max={20}
max={MAX_MATRIX_COLS}
bind:value={numColumns}
label="Matrix Columns"
/>
Expand Down
3 changes: 2 additions & 1 deletion src/MathField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { MathField } from "./cells/MathField";
import type { MathfieldElement } from "mathlive";
import { INLINE_SHORTCUTS } from "./constants";
import { INLINE_SHORTCUTS, MAX_MATRIX_COLS } from "./constants";
export let latex = "";
export let mathField: MathField | null = null;
Expand Down Expand Up @@ -287,6 +287,7 @@
<math-field
role="textbox math"
min-font-scale=0.75
max-matrix-cols={MAX_MATRIX_COLS}
on:focusin={handleFocusIn}
on:focusout={handleFocusOut}
on:input={handleMathFieldUpdate}
Expand Down
10 changes: 10 additions & 0 deletions src/Updates.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
}
</style>

<em>April 18, 2024</em>
<h4>Maximum Matrix Columns Limit Increased</h4>
<p>
The maximum number of columns allowed in a matrix has been increased to 50.
The previous limit was 10. The number of rows allowed is unlimited, however,
performance may become sluggish with large matrices.
</p>

<br>

<em>April 6, 2024</em>
<h4>Drag to Reorder Cells Improvements</h4>
<p>
Expand Down
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export const INLINE_SHORTCUTS = {
'$doubleprime': '\\frac{\\mathrm{d}^{2}}{\\mathrm{d}\\left(#?\\right)^{2}}\\left(#?\\right)',
'$tripleprime': '\\frac{\\mathrm{d}^{3}}{\\mathrm{d}\\left(#?\\right)^{3}}\\left(#?\\right)',
'log_': '\\log_{#?}(#?)',
};
};

export const MAX_MATRIX_COLS = 50;
16 changes: 12 additions & 4 deletions src/parser/LatexToSympy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { FieldTypes, Statement, QueryStatement, RangeQueryStatement, UserFu
ScatterXValuesQueryStatement, ScatterYValuesQueryStatement} from "./types";
import { RESERVED, GREEK_CHARS, UNASSIGNABLE, COMPARISON_MAP,
UNITS_WITH_OFFSET, TYPE_PARSING_ERRORS, BUILTIN_FUNCTION_MAP } from "./constants.js";
import { MAX_MATRIX_COLS } from "../constants";
import {
type GuessContext, type Guess_listContext, IdContext, type Id_listContext,
type StatementContext, type QueryContext, type AssignContext, type EqualityContext, type PiExprContext,
Expand Down Expand Up @@ -1733,18 +1734,27 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

while (ctx.u_insert_matrix(i)) {
const child = ctx.u_insert_matrix(i);
i++;

const numRows = parseFloat(child._numRows.text);
const numColumns = parseFloat(child._numColumns.text);

if (!Number.isInteger(numRows) || !Number.isInteger(numColumns)) {
this.addParsingErrorMessage('The requested number of rows or columns for a matrix must be integer values');
error = true;
continue;
}

if (numRows <= 0 || numColumns <= 0) {
this.addParsingErrorMessage('The requested number of rows or columns for a matrix must be positive values');;
error = true
this.addParsingErrorMessage('The requested number of rows or columns for a matrix must be positive values');
error = true;
continue;
}

if (numColumns > MAX_MATRIX_COLS) {
this.addParsingErrorMessage(`Matrices are limited to ${MAX_MATRIX_COLS} columns. The number of rows is unlimited`);
error = true;
continue;
}

const blankMatrixLatex = getBlankMatrixLatex(numRows, numColumns);
Expand Down Expand Up @@ -1777,8 +1787,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
deletionLength: endLocation - startLocation + 1,
text: blankMatrixLatex
});

i++;
}

if (error) {
Expand Down
34 changes: 33 additions & 1 deletion tests/test_matrix_keyboard.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('Matrix inverse keyboard entry', async () => {
expect(content).toBe(String.raw`\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}`);
});

test('Matrix inverse keyboard entry for two blank matrices', async () => {
test('Matrix keyboard entry for two blank matrices', async () => {
await page.locator('#cell-0 >> math-field.editable').type('([1,2]@[2,1])_1,1');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('=');
Expand Down Expand Up @@ -95,3 +95,35 @@ test('Matrix virtual keyboard entry', async () => {
let content = await page.textContent(`#result-value-0`);
expect(content).toBe(String.raw`\begin{bmatrix} a c + b d \end{bmatrix}`);
});

test('Matrix with more than 10 columns', async () => {
await page.locator('#cell-0 >> math-field.editable').type('[1,11]');
await page.locator('#cell-0 >> math-field.editable').type('=');
await page.locator('#cell-0 >> math-field.editable').press('Enter');
await page.locator('#cell-0 >> math-field.editable').type('a');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('b');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('c');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('d');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('e0');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('f');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('g');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('h');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('i0');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('j');
await page.locator('#cell-0 >> math-field.editable').press('Tab');
await page.locator('#cell-0 >> math-field.editable').type('k');

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

let content = await page.textContent(`#result-value-0`);
expect(content).toBe(String.raw`\begin{bmatrix} a & b & c & d & e_{0} & f & g & h & i_{0} & j & k \end{bmatrix}`);
});

0 comments on commit 0ba41ca

Please sign in to comment.