mk-calcmath.tex /size: 4145 b    last modification: 2023-12-21 09:43
1% language=us
2
3\startcomponent mk-calcmath
4
5\environment mk-environment
6
7\chapter{An example: CalcMath}
8
9\usemodule[calcmath]
10
11\subject{introduction}
12
13For a long time \TEX's way of coding math has dominated the
14typesetting world. However, this kind of coding is not that well
15suited for non academics, like schoolkids. Often kids do know how
16to key in math because they use advanced calculators. So, when a
17couple of years ago we were implementing a workflow where kids
18could fill in their math workbooks (with exercises) on||line, it
19made sense to support so called Texas Instruments math input.
20Because we had to parse the form data anyway, we could use a \type
21{[[} and \type {]]} as math delimiters instead of \type {$}. The
22conversion too place right after the form was received by the web
23server.
24
25\start
26
27\def\test#1%
28  {\NC \vrule height 2ex depth 2ex width 0pt\type{#1}
29   \NC \calcmath{#1}
30   \NC \NR}
31
32\setupinterlinespace[1.5]
33
34\starttabulate
35\test{sin(x) + x^2 + x^(1+x) + 1/x^2}
36\test{mean(x+mean(y))}
37\test{int(a,b,c)}
38\test{(1+x)/(1+x) + (1+x)/(1+(1+x)/(1+x))}
39\test{10E-2}
40\test{(1+x)/x}
41\test{(1+x)/12}
42\test{(1+x)/-12}
43\test{1/-12}
44\test{12x/(1+x)}
45\test{exp(x+exp(x+1))}
46\test{abs(x+abs(x+1)) + pi + inf}
47\test{Dx Dy}
48\test{D(x+D(y))}
49\test{Df(x)}
50\test{g(x)}
51\test{sqrt(sin^2(x)+cos^2(x))}
52\stoptabulate
53
54\stop
55
56By combining \LUA\ with \TEX, we can do the conversion from
57calculator math to \TEX\ immediately, without auxiliary programs
58or complex parsing using \TEX\ macros.
59
60\subject{tex}
61
62In a \CONTEXT\ source one can use the \type {\calcmath} command, as
63in:
64
65\starttyping
66The strange formula \calcmath {sqrt(sin^2(x)+cos^2(x))} boils
67down to ...
68\stoptyping
69
70One needs to load the module first, using:
71
72\starttyping
73\usemodule[calcmath]
74\stoptyping
75
76Because the amount of code involved is rather small, eventually we
77may decide to add this support to the \MKIV\ kernel.
78
79\subject{xml}
80
81Coding math in \TEX\ is rather efficient. In \XML\ one needs way
82more code. Presentation \MATHML\ provides a few basic constructs
83and boils down to combining those building blocks. Content \MATHML\
84is better, especially from the perspective of applications that
85need to do interpret the formulas. It permits for instance the
86\CONTEXT\ content \MATHML\ handler to adapt the rendering to
87cultural driven needs. The \OPENMATH\ way of coding is like
88content \MATHML, but more verbose with less tags. Calculator math
89is more restrictive than \TEX\ math and less verbose than any of
90the \XML\ variants. It looks like:
91
92\starttyping
93<icm>sqrt(sin^2(x)+cos^2(x))</icm> test
94\stoptyping
95
96And in display mode:
97
98\starttyping
99<dcm>sqrt(sin^2(x)+cos^2(x))</dcm> test
100\stoptyping
101
102\subject{speed}
103
104This script (which you can find in the \CONTEXT\ distribution as
105soon as the \MKIV\ code variants are added) is the first real
106\TEX\ related \LUA\ code that I wrote; so far I had only written
107some wrapping and spell checking code for the \SCITE\ editor. It
108also made a nice demo for a couple of talks that I held at
109usergroup meetings. The script has a lot of expressions. These
110convert one string into another. They are less powerful than
111regular expressions, but pretty fast and adequate. The feature I
112miss most is alternation like \type {(l|st)uck} but it's a small
113price to pay. As the \LUA\ manual explains: adding a \POSIX\
114compliant regexp parser would take more lines of code than \LUA\
115currently does.
116
117On my machine, running this first version took 3.5 seconds for 2500
118times typesetting the previously shown square root of sine and
119cosine. Of this, 2.1 seconds were spent on typesetting and 1.4 seconds
120on converting. After optimizing the code, 0.8 seconds were used for
121conversion. A stand alone \LUA\ takes .65 seconds, which includes loading
122the interpreter. On a test of 25.000 sample conversions, we could gain
123some 20\% conversion time using the \LUAJIT\ just in time compiler.
124
125% \usemodule[narrowtt] \typefile[ntyping]{m-calcmath.lua} % narrow, no color
126
127% \def\inlinecalcmath    #1{\mathematics{\ctxlua{calcmath.tex("#1",1)}}}
128% \def\displaycalcmath   #1{\startformula\ctxlua{calcmath.tex("#1",2)}\stopformula}
129
130\stopcomponent
131