VISUAL PHYSICS
Unconventional Explorations into Uninhabited Areas of Physics
Through Thought Experiments in the Form of Simulations...


This is NOT an educational site. The views expressed here are not those of mainstream physics.
If you want to contribute to the wiki, email me at the address given in the Contact page.
Legend:  sim  : Article with simulation --  stb  : Article that needs development (stub).

Help:Formula

From Visual Physics Wiki
Revision as of 17:59, 21 November 2021 by George Barouxis (talk | contribs) (Undo revision 1465 by George Barouxis (talk))
Jump to navigation Jump to search

ASCIIMathML.js (ver 2.0.9): Syntax and List of Constants

You can use your favorite editor to write HTML pages that use this JavaScript program. If the page is viewed by a browser that does not support MathML or JavaScript, the ASCII formulas are still quite readable. Most users will not have to read the technicalities on this page. If you type

\`x^2\` or \`a_(mn)\` or \`a_{mn}\` or \`(x+1)/y\` or \`sqrtx\`

you pretty much get what you expect: `x^2` or `a_(mn)` or `a_{mn}` or `(x+1)/y` or `sqrtx`. The choice of grouping parenthesis is up to you (they don't have to match either). If the displayed expression can be parsed uniquely without them, they are omitted. Printing the table of constant symbols (below) may be helpful (but is not necessary if you know the LaTeX equivalents).

It is hoped that this simple input format for MathML will further encourage its use on the web. The remainder of this page gives a fairly detailed specification of the ASCII syntax. The expressions described here correspond to a wellspecified subset of Presentation MathML and behave in a predictable way.

The syntax is very permissive and does not generate syntax errors. This allows mathematically incorrect expressions to be displayed, which is important for teaching purposes. It also causes less frustration when previewing formulas.

The parser uses no operator precedence and only respects the grouping brackets, subscripts, superscript, fractions and (square) roots. This is done for reasons of efficiency and generality. The resulting MathML code can quite easily be processed further to ensure additional syntactic requirements of any particular application.

The grammar

Here is a definition of the grammar used to parse ASCIIMathML expressions. In the Backus-Naur form given below, the letter on the left of the ::= represents a category of symbols that could be one of the possible sequences of symbols listed on the right. The vertical bar | separates the alternatives.

c ::= [A-z] | numbers | greek letters | other constant symbols (see below)
u ::= 'sqrt' | 'text' | 'bb' |     other unary symbols for font commands
b ::= 'frac' | 'root' | 'stackrel' binary symbols
l ::= ( | [ | { | (: | {:          left brackets
r ::= ) | ] | } | :) | :}          right brackets
S ::= c | lEr | uS | bSS | "any"   simple expression
E ::= SE | S/S |S_S | S^S | S_S^S  expression (fraction, sub-, super-, subsuperscript)



The translation rules

Each terminal symbol is translated into a corresponding MathML node. The constants are mostly converted to their respective Unicode symbols. The other expressions are converted as follows:

l`S`r`to`<mrow>l`S`r</mrow>(note that any pair of brackets can be used to delimit subexpressions, they don't have to match)
sqrt `S``to`<msqrt>`S'`</msqrt>
text `S``to`<mtext>`S'`</mtext>
"any"`to`<mtext>any</mtext>
frac `S_1` `S_2``to`<mfrac>`S_1'` `S_2'`</mfrac>
root `S_1` `S_2``to`<mroot>`S_2'` `S_1'`</mroot>
stackrel `S_1` `S_2``to`<mover>`S_2'` `S_1'`</mover>
`S_1`/`S_2``to`<mfrac>`S_1'` `S_2'`</mfrac>
`S_1`_`S_2``to`<msub>`S_1` `S_2'`</msub>
`S_1`^`S_2``to`<msup>`S_1` `S_2'`</msup>
`S_1`_`S_2`^`S_3``to`<msubsup>`S_1` `S_2'` `S_3'`</msubsup> or <munderover>`S_1` `S_2'` `S_3'`</munderover> (in some cases)

In the rules above, the expression `S'` is the same as `S`, except that if `S` has an outer level of brackets, then `S'` is the expression inside these brackets.

Matrices:

A simple syntax for matrices is also recognized:
l(`S_(11)`,...,`S_(1n)`),(...),(`S_(m1)`,...,`S_(mn)`)r     or     l[`S_(11)`,...,`S_(1n)`],[...],[`S_(m1)`,...,`S_(mn)`]r.

Here l and r stand for any of the left and right brackets (just like in the grammar they do not have to match). Both of these expressions are translated to

<mrow>l<mtable><mtr><mtd>`S_(11)`</mtd>... <mtd>`S_(1n)`<mtd></mtr>... <mtr><mtd>`S_(m1)`</mtd>... <mtd>`S_(mn)`</mtd></mtr></mtable></mrow>.

For example {(S_(11),...,S_(1n)),(vdots,ddots,vdots),(S_(m1),...,S_(mn))]displays as `{(S_(11),...,S_(1n)),(vdots,ddots,vdots),(S_(m1),...,S_(mn))]`.

Note that each row must have the same number of expressions, and there should be at least two rows.

Tokenization:

The input formula is broken into tokens using a "longest matching initial substring search". Suppose the input formula has been processed from left to right up to a fixed position. The longest string from the list of constants (given below) that matches the initial part of the remainder of the formula is the next token. If there is no matching string, then the first character of the remainder is the next token. The symbol table at the top of the ASCIIMathML.js script specifies whether a symbol is a math operator (surrounded by a <mo> tag) or a math identifier (surrounded by a <mi> tag). For single character tokens, letters are treated as math identifiers, and non-alphanumeric characters are treated as math operators. For digits, see "Numbers" below.

Spaces are significant when they separate characters and thus prevent a certain string of characters from matching one of the constants. Multiple spaces and end-of-line characters are equivalent to a single space.

Complete list of constants

(Standard LaTeX names also work.)

Numbers: A string of digits, optionally preceded by a minus sign, and optionally followed by a decimal point (a period) and another string of digits, is parsed as a single token and converted to a MathML number, i.e., enclosed with the <mn> tag. If it is not desirable to have a preceding minus sign be part of the number, a space should be inserted. Thus tt>x-1 is converted to <mi>x</mi><mn>-1</mn>, whereas x - 1 is converted to <mi>x</mi><mo>-</mo><mn>1</mn>.

Greek letters

alpha`alpha`
beta`beta`
chi`chi`
delta`delta`
Delta`Delta`
epsilon`epsilon`
varepsilon`varepsilon`
eta`eta`
gamma`gamma`
Gamma`Gamma`
iota`iota`
kappa`kappa`
lambda`lambda`
Lambda`Lambda`
mu`mu`
nu`nu`
omega`omega`
Omega`Omega`
phi`phi`
varphi`varphi`
Phi`Phi`
pi`pi`
Pi`Pi`
psi`psi`
Psi`Psi`
rho`rho`
sigma`sigma`
Sigma`Sigma`
tau`tau`
theta`theta`
vartheta`vartheta`
Theta`Theta`
upsilon`upsilon`
xi`xi`
Xi`Xi`
zeta`zeta`




Symbols

Operation symbolsRelation symbols Logical symbolsMiscellaneous symbols Standard functionsGrouping brackets AccentsFont commands Arrows
TypeSee
+`+`
-`-`
*`*`
**`**`
//`//`
\\`\\ `
xx`xx`
-:`-:`
@`@`
o+`o+`
ox`ox`
o.`o.`
sum`sum`
prod`prod`
^^`^^`
^^^`^^^`
vv`vv`
vvv`vvv`
nn`nn`
nnn`nnn`
uu`uu`
uuu`uuu`
TypeSee
=`=`
!=`!=`
< `<`
>`>`
<=`<=`
>=`>=`
-<`-<`
>-`>-`
in`in`
!in`notin`
sub`sub`
sup`sup`
sube`sube`
supe`supe`
-=`-=`
~=`~=`
~~`~~`
prop`prop`
TypeSee
and`and`
or`or`
not`not`
=>`=>`
if`if`
iff`iff`
AA`AA`
EE`EE`
_|_`_|_`
TT`TT`
|--`|--`
|==`|==`
TypeSee
int`int`
oint`oint`
del`del`
grad`grad`
+-`+-`
O/`O/`
oo`oo`
aleph`aleph`
/_`/_`
:.`:.`
|...||`...`|
|cdots||`cdots`|
vdots`vdots`
ddots`ddots`
|\ ||`\ `|
|quad||`quad`|
diamond`diamond`
square`square`
|__`|__`
__|`__|`
|~`|~`
~|`~|`
CC`CC`
NN`NN`
QQ`QQ`
RR`RR`
ZZ`ZZ`
TypeSee
sin`sin`
cos`cos`
tan`tan`
csc`csc`
sec`sec`
cot`cot`
sinh`sinh`
cosh`cosh`
tanh`tanh`
log`log`
ln`ln`
det`det`
dim`dim`
lim`lim`
mod`mod`
gcd`gcd`
lcm`lcm`
min`min`
max`max`
TypeSee
(`(`
)`)`
[`[`
]`]`
{`{`
}`}`
(:`(:`
:)`:)`
{:`{:`
:}`{::}`
TypeSee
hat x`hat x`
bar x`bar x`
ul x`ul x`
vec x`vec x`
dot x`dot x`
ddot x`ddot x`
TypeSee
bb A`bb A`
bbb A`bbb A`
cc A`cc A`
tt A`tt A`
fr A`fr A`
sf A`sf A`
TypeSee
uarr`uarr`
darr`darr`
rarr`rarr`
->`->`
|->`|->`
larr`larr`
harr`harr`
rArr`rArr`
lArr`lArr`
hArr`hArr`



Examples

Type this See that Comment
\`x^2+y_1+z_12^34\` `x^2+y_1+z_12^34` subscripts as in TeX, but numbers are treated as a unit
\`sin^-1(x)\` `sin^-1(x)` function names are treated as constants
\`d/dxf(x)=lim_(h->0)(f(x+h)-f(x))/h\` `d/dxf(x)=lim_(h->0)(f(x+h)-f(x))/h` complex subscripts are bracketed, displayed under lim
\`\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}\` `\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}` standard LaTeX notation is an alternative
\`f(x)=sum_(n=0)^oo(f^((n))(a))/(n!)(x-a)^n\` `f(x)=sum_(n=0)^oo(f^((n))(a))/(n!)(x-a)^n` f^((n))(a) must be bracketed, else the numerator is only `a`
\`f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(a)}{n!}(x-a)^n\` `f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(a)}{n!}(x-a)^n` standard LaTeX produces the same result
\`int_0^1f(x)dx\` `int_0^1f(x)dx` subscripts must come before superscripts
\`[[a,b],[c,d]]((n),(k))\` `[[a,b],[c,d]]((n),(k))` matrices and column vectors are simple to type
\`x/x={(1,if x!=0),(text{undefined},if x=0):}\` `x/x={(1,if x!=0),(text{undefined},if x=0):}` piecewise defined function are based on matrix notation
\`a//b\` `a//b` use // for inline fractions
\`(a/b)/(c/d)\` `(a/b)/(c/d)` with brackets, multiple fraction work as expected
\`a/b/c/d\` `a/b/c/d` without brackets the parser chooses this particular expression
\`((a*b))/c\` `((a*b))/c` only one level of brackets is removed; * gives standard product
\`sqrtsqrtroot3x\` `sqrtsqrtroot3x` spaces are optional, only serve to split strings that should not match
\`(:a,b:) and {:(x,y),(u,v):}\` `(:a,b:) and {:(x,y),(u,v):}` angle brackets and invisible brackets
\`(a,b]={x in RR : a < x <= b}\` `(a,b]={x in RR : a < x <= b}` grouping brackets don't have to match
\`abc-123.45^-1.1\` `abc-123.45^-1.1` non-tokens are split into single characters,
but decimal numbers are parsed with possible sign
\`hat(ab) bar(xy) ulA vec v dotx ddot y\` `hat(ab) bar(xy) ulA vec v dotx ddot y` accents can be used on any expression (work well in IE)
\`bb{AB3}.bbb(AB].cc(AB).fr{AB}.tt[AB].sf(AB)\` `bb{AB3}.bbb(AB].cc(AB).fr{AB}.tt[AB].sf(AB)` font commands; can use any brackets around argument
\`stackrel"def"= or \stackrel{\Delta}{=}" "("or ":=)\` `stackrel"def"= or \stackrel{\Delta}{=}" "("or ":=)` symbols can be stacked
\`{::}_(\ 92)^238U\` `{::}_(\ 92)^238U` prescripts simulated by subsuperscripts