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

Make special case differential operator #108

Merged
merged 1 commit into from
Dec 2, 2023
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
30 changes: 25 additions & 5 deletions demo/test-cases.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h2>Operators</h2>
</test-case>

<test-case display="block">
bf F @ bf G : U sube RR^3 -&gt; RR^2
bf F @ bf G : U sube RR^3 -&gt; RR^2
</test-case>

<test-case display="block">e = sum_(n=0)^oo 1 / n!</test-case>
Expand All @@ -85,10 +85,12 @@ <h2>Operators</h2>
f&apos;(x), f&apos;&apos;(x), f&apos;&apos;&apos;(x), f&apos;&apos;&apos;&apos;(x)
</test-case>

<test-case display="block">p(a | b) = p(b | a)p(a) / p(b)</test-case>
<test-case display="block">p(a | b) = p(b | a)p(a) / p(b)</test-case>

<test-case display="block">f(a) - f(b) = int_b^a f'(x) dx</test-case>

<test-case display="block">
P_k(x) = f(a) + f&apos;(a)(x-a) + f&apos;&apos;(a) / 2! (x-a)^2 + cdots + f^(k)(a) / k! (x-a)^k
P_k(x) = f(a) + f'(a)(x-a) + f''(a) / 2! (x-a)^2 + cdots + f^(k)(a) / k! (x-a)^k
</test-case>

<test-case display="block">
Expand All @@ -98,6 +100,10 @@ <h2>Operators</h2>
<test-case display="block">
oint_(del S) bf F * d bf s = dint_S grad xx bf F * d bf s
</test-case>

<test-case display="block">
oint_C (L dx + M dy) = dint_D ((del M)/(del x) - (del L)/(del y)) dx dy
</test-case>
</section>

<!-- prettier-ignore -->
Expand All @@ -109,6 +115,8 @@ <h2>Whitespace</h2>
<test-case>a b</test-case>
<test-case>a b</test-case>
<test-case>a b</test-case>
<test-case>a b</test-case>
<test-case>a b</test-case>
</section>

<!-- prettier-ignore -->
Expand Down Expand Up @@ -181,6 +189,18 @@ <h2>Fractions</h2>
<test-case>a/</test-case>
<test-case>b // </test-case>

<test-case display="block">
df = df / dx dx
</test-case>

<test-case display="block">
f'(x) = df / dx (x)
</test-case>

<test-case display="block">
f''(x) = (d^2 f) / dx^2 (x)
</test-case>

<test-case display="block">
phi = 1+1/ 1+1/ 1+1/ 1+1/ 1+ddots
</test-case>
Expand Down Expand Up @@ -237,7 +257,7 @@ <h2>Groupings</h2>
<test-case>NN = {1, 2, 3, ...}</test-case>

<test-case display="block">
(: V(t)^2 :) = lim_(T-&gt;oo) 1/T int_(-T/2)^(T/2) V(t)^2 dt
(: V(t)^2 :) = lim_(T-&gt;oo) 1/T int_(-T/2)^(T/2) V(t)^2 dt
</test-case>
</section>

Expand All @@ -254,7 +274,7 @@ <h2>Super and Subscripts</h2>
<test-case>(X^T)_ i.,j = X_ j.,i</test-case>

<test-case display="block">
ln x = int_1^x 1/t dt
ln x = int_1^x 1/t dt
</test-case>
</section>

Expand Down
17 changes: 13 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1>Mathup</h1>
<div class="input">
<p><label for="try-input">You write…</label></p>

<textarea id="try-input">E[X] = int_(-oo)^oo x f(x) dx</textarea>
<textarea id="try-input">E[X] = int_(-oo)^oo x f(x) dx</textarea>

<div class="options">
<label for="try-decimalmark">Decimal mark</label>
Expand Down Expand Up @@ -441,6 +441,9 @@ <h3>Basics</h3>
write. If a character comes from one of the mathematical operator
code blocks it will wrap it in an <code>&lt;mo&gt;</code> tag,
otherwise it will be wrapped in an <code>&lt;mi&gt;</code> tag.
Additionally <code>d</code> will be wrapped in an
<code>&lt;mo&gt;</code> tag if it obviously a part of a
differential.
</p>

<aside class="examples">
Expand Down Expand Up @@ -1028,6 +1031,9 @@ <h3>Operators</h3>
P_k(x) = f(a) + f'(a)(x-a) + f''(a) / 2! (x-a)^2 + cdots + f^(k)(a) / k! (x-a)^k
</use-example>
</li>
<li>
<use-example> f(a) - f(b) = int_b^a f'(x) dx </use-example>
</li>
<li>
<!-- prettier-ignore -->
<use-example>
Expand Down Expand Up @@ -1480,6 +1486,9 @@ <h3>Fractions</h3>
<ul>
<li><use-example>1+2 / 3+4</use-example></li>
<li><use-example>1+2/3+4</use-example></li>
<li><use-example>df = df / dx dx</use-example></li>
<li><use-example>f'(x) = dy / dx</use-example></li>
<li><use-example>f''(x) = (d^2 y) / dx^2</use-example></li>
<li>
<!-- prettier-ignore -->
<use-example>phi = 1+1/ 1+1/ 1+1/ 1+1/ 1+ddots</use-example>
Expand Down Expand Up @@ -1562,7 +1571,7 @@ <h3>Sub and superscripts</h3>
<li><use-example>a^2 + b^2 = c^2</use-example></li>

<!-- prettier-ignore -->
<li><use-example>ln x = int_1^x 1/t dt</use-example></li>
<li><use-example>ln x = int_1^x 1/t dt</use-example></li>

<li>
<!-- prettier-ignore -->
Expand Down Expand Up @@ -1794,7 +1803,7 @@ <h3>Fenced Groups</h3>
<li>
<!-- prettier-ignore -->
<use-example>
&lt;&lt;V(t)^2&gt;&gt; = lim_(T-&gt;oo) 1/T int_(-T/2)^(T/2) V(t)^2 dt
&lt;&lt;V(t)^2&gt;&gt; = lim_(T-&gt;oo) 1/T int_(-T/2)^(T/2) V(t)^2 dt
</use-example>
</li>
</ul>
Expand Down Expand Up @@ -2159,7 +2168,7 @@ <h3>Font commands</h3>
<li>
<!-- prettier-ignore -->
<use-example>
bb E[X] = int_(-oo)^oo x f(x) bb it d x
bb E[X] = int_(-oo)^oo x f(x) bb it \d x
</use-example>
</li>
</ul>
Expand Down
10 changes: 6 additions & 4 deletions docs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,29 @@ function removePolyfill(outputEl, mathupEl) {
}

const DEMO_EQUATIONS = [
"E[X] = int_(-oo)^oo x f(x) dx",
"E[X] = int_(-oo)^oo x f(x) dx",
"sum_(n=0)^k a_n = a_0 + a_1 + cdots + a_k",
"e = sum_(n=0)^oo 1 / n!",
"vec x = a hat i + b hat j + c hat k",
"f(a) - f(b) = int_b^a f'(x) dx",
"grad f(x,y) = ((del f)/(del x) (x, y), (del f)/(del y)(x,y))",
"oint_(del S) bf F * d bf s = dint_S grad xx bf F * d bf s",
"oint_C (L dx + M dy) = dint_D ((del M)/(del x) - (del L)/(del y)) dx dy",
"cc N(x | μ, σ^2) = 1 / (σ sqrt(2π)) e^(-1/2 (x-μ / σ)^2)",
"`Gamma`(theta | alpha, beta) = beta^alpha / Gamma(alpha) theta^(alpha - 1) e^(-beta theta)",
"P(A | B) = P(B | A)P(A) / P(B)",
String.raw`phi =.^\def a/b = a+b / a`,
"binom(n, k) = n! / k!(n-k)!",
"|(Psi(t):) = int Psi(x, t) |(x:) dx",
"<<V(t)^2>> = lim_(T->oo) 1/T int_(-T/2)^(T/2) V(t)^2 dt",
"|(Psi(t):) = int Psi(x, t) |(x:) dx",
"<<V(t)^2>> = lim_(T->oo) 1/T int_(-T/2)^(T/2) V(t)^2 dt",
`[λ_0, λ_1, ...;] [p_(0 0), p_(0 1), ...
p_(1 0), p_(1 1), ...
vdots, vdots, ddots]`,
"||(bf x)||^2 = [x_1 ; x_2 ; x_3]*[x_1 ; x_2 ; x_3]",
`n! = { 1, if n=0 or n=1
n(n-1)!, if n > 1`,
"x = -b+-sqrt(b^2 - 4ac) / 2a",
"ln x = int_1^x 1/t dt",
"ln x = int_1^x 1/t dt",
"bf F @ bf G: U sube RR^3 -> RR^2",
];

Expand Down
20 changes: 13 additions & 7 deletions src/compiler/parser/handlers/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ function insertTransformNode(node, transforms) {
}

/**
* @param {import("../parse.js").State} State
* @param {import("../parse.js").State} state
* @returns {{ node: Operation | Term; end: number }}
*/
export default function command({ start, tokens }) {
const token = tokens[start];
export default function command(state) {
const token = state.tokens[state.start];

if (!token.name) {
throw new Error("Got command token without a name");
Expand Down Expand Up @@ -84,8 +84,8 @@ export default function command({ start, tokens }) {

handleCommandToken(token);

let pos = start + 1;
let nextToken = tokens[pos];
let pos = state.start + 1;
let nextToken = state.tokens[pos];
while (
nextToken &&
(nextToken.type === "command" || nextToken.type === "space")
Expand All @@ -95,10 +95,16 @@ export default function command({ start, tokens }) {
}

pos += 1;
nextToken = tokens[pos];
nextToken = state.tokens[pos];
}

const next = expr({ stack: [], start: pos, tokens });
const next = expr({
...state,
stack: [],
start: pos,
nestLevel: state.nestLevel + 1,
textTransforms,
});

if (textTransforms.length === 0) {
// Only apply styles.
Expand Down
42 changes: 30 additions & 12 deletions src/compiler/parser/handlers/group.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { addZeroLSpaceToOperator } from "../utils.js";

import expr from "./expr.js";

/**
Expand All @@ -7,12 +9,12 @@ import expr from "./expr.js";
*/

/**
* @param {import("../parse.js").State} State
* @param {import("../parse.js").State} state
* @returns {{ node: FencedGroup | MatrixGroup, end: number }}
*/
export default function group({ start, tokens }) {
let i = start;
let token = tokens[i];
export default function group(state) {
let i = state.start;
let token = state.tokens[i];

const open = token;

Expand All @@ -29,26 +31,35 @@ export default function group({ start, tokens }) {
const rows = [];

i += 1;
token = tokens[i];
token = state.tokens[i];

if (token && token.type === "space") {
// Ignore leading space.
i += 1;
token = tokens[i];
token = state.tokens[i];
}

while (token && token.type !== "paren.close") {
if (token.type === "space" && token.value === " ") {
// No need to add tokens which don’t render elements to our
// cell.
i += 1;
token = state.tokens[i];

continue;
}

if (token.type === "sep.col") {
seps.push(token.value);
cols.push(cell);
cell = [];
i += 1;
token = tokens[i];
token = state.tokens[i];

// Ignore leading space.
if (token && token.type === "space") {
i += 1;
token = tokens[i];
token = state.tokens[i];
}

continue;
Expand All @@ -60,26 +71,33 @@ export default function group({ start, tokens }) {
cell = [];
cols = [];
i += 1;
token = tokens[i];
token = state.tokens[i];

// Ignore leading space.
if (token && token.type === "space") {
i += 1;
token = tokens[i];
token = state.tokens[i];
}

continue;
}

if (cell.length === 1) {
// If first element is an operator it may throw alignment out
// with its implicit lspace.
addZeroLSpaceToOperator(cell[0]);
}

const next = expr({
tokens,
...state,
start: i,
stack: cell,
nestLevel: state.nestLevel + 1,
});

cell.push(next.node);
i = next.end;
token = tokens[i];
token = state.tokens[i];
}

if (cell.length > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/parser/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import space from "./space.js";
* @typedef {import("../../tokenizer/index.js").TokenType} TokenType
* @typedef {import("../parse.js").State} State
* @typedef {import("../index.js").Node} Node
* @typedef {import("../index.js").Literal} Literal
* @typedef {(state: State) => { node: Node, end: number }} Handler
* @typedef {"Ident" | "Number" | "Operator" | "Text"} LiteralType
*/
Expand Down
Loading