summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/mk/mk-calcmath.tex
blob: 71673118294415ac7f75a2bde957b9105039b943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
% language=uk

\startcomponent mk-calcmath

\environment mk-environment

\chapter{An example: CalcMath}

\usemodule[calcmath]

\subject{introduction}

For a long time \TEX's way of coding math has dominated the
typesetting world. However, this kind of coding is not that well
suited for non academics, like schoolkids. Often kids do know how
to key in math because they use advanced calculators. So, when a
couple of years ago we were implementing a workflow where kids
could fill in their math workbooks (with exercises) on||line, it
made sense to support so called Texas Instruments math input.
Because we had to parse the form data anyway, we could use a \type
{[[} and \type {]]} as math delimiters instead of \type {$}. The
conversion too place right after the form was received by the web
server.

\start

\def\test#1%
  {\NC \vrule height 2ex depth 2ex width 0pt\type{#1}
   \NC \calcmath{#1}
   \NC \NR}

\setupinterlinespace[1.5]

\starttabulate
\test{sin(x) + x^2 + x^(1+x) + 1/x^2}
\test{mean(x+mean(y))}
\test{int(a,b,c)}
\test{(1+x)/(1+x) + (1+x)/(1+(1+x)/(1+x))}
\test{10E-2}
\test{(1+x)/x}
\test{(1+x)/12}
\test{(1+x)/-12}
\test{1/-12}
\test{12x/(1+x)}
\test{exp(x+exp(x+1))}
\test{abs(x+abs(x+1)) + pi + inf}
\test{Dx Dy}
\test{D(x+D(y))}
\test{Df(x)}
\test{g(x)}
\test{sqrt(sin^2(x)+cos^2(x))}
\stoptabulate

\stop

By combining \LUA\ with \TEX, we can do the conversion from
calculator math to \TEX\ immediately, without auxiliary programs
or complex parsing using \TEX\ macros.

\subject{tex}

In a \CONTEXT\ source one can use the \type {\calcmath} command, as
in:

\starttyping
The strange formula \calcmath {sqrt(sin^2(x)+cos^2(x))} boils
down to ...
\stoptyping

One needs to load the module first, using:

\starttyping
\usemodule[calcmath]
\stoptyping

Because the amount of code involved is rather small, eventually we
may decide to add this support to the \MKIV\ kernel.

\subject{xml}

Coding math in \TEX\ is rather efficient. In \XML\ one needs way
more code. Presentation \MATHML\ provides a few basic constructs
and boils down to combining those building blocks. Content \MATHML\
is better, especially from the perspective of applications that
need to do interpret the formulas. It permits for instance the
\CONTEXT\ content \MATHML\ handler to adapt the rendering to
cultural driven needs. The \OPENMATH\ way of coding is like
content \MATHML, but more verbose with less tags. Calculator math
is more restrictive than \TEX\ math and less verbose than any of
the \XML\ variants. It looks like:

\starttyping
<icm>sqrt(sin^2(x)+cos^2(x))</icm> test
\stoptyping

And in display mode:

\starttyping
<dcm>sqrt(sin^2(x)+cos^2(x))</dcm> test
\stoptyping

\subject{speed}

This script (which you can find in the \CONTEXT\ distribution as
soon as the \MKIV\ code variants are added) is the first real
\TEX\ related \LUA\ code that I wrote; so far I had only written
some wrapping and spell checking code for the \SCITE\ editor. It
also made a nice demo for a couple of talks that I held at
usergroup meetings. The script has a lot of expressions. These
convert one string into another. They are less powerful than
regular expressions, but pretty fast and adequate. The feature I
miss most is alternation like \type {(l|st)uck} but it's a small
price to pay. As the \LUA\ manual explains: adding a \POSIX\
compliant regexp parser would take more lines of code than \LUA\
currently does.

On my machine, running this first version took 3.5 seconds for 2500
times typesetting the previously shown square root of sine and
cosine. Of this, 2.1 seconds were spent on typesetting and 1.4 seconds
on converting. After optimizing the code, 0.8 seconds were used for
conversion. A stand alone \LUA\ takes .65 seconds, which includes loading
the interpreter. On a test of 25.000 sample conversions, we could gain
some 20\% conversion time using the \LUAJIT\ just in time compiler.

% \usemodule[narrowtt] \typefile[ntyping]{m-calcmath.lua} % narrow, no color

% \def\inlinecalcmath    #1{\mathematics{\ctxlua{calcmath.tex("#1",1)}}}
% \def\displaycalcmath   #1{\startformula\ctxlua{calcmath.tex("#1",2)}\stopformula}

\stopcomponent