math-fun.tex /size: 3561 b    last modification: 2023-12-21 09:43
1% language=us runpath=texruns:manuals/math
2
3\environment math-layout
4
5\startcomponent math-fun
6
7\usemodule[mathfun]
8
9\startchapter[title=Fun stuff] % As is watching the funplex DVD.
10
11% \startsection[title=How it started]
12
13When I decided to add the \type {\uuid} macro (a trivial task because there was
14already a \LUA\ function for it in \CONTEXT) I wondered about other functions
15that could be added, like those for sine and cosine. There is no real need
16for that because we can already do this:
17
18\startbuffer[a]
19\luaexpr{math.sin(math.pi/4)}
20\stopbuffer
21
22\typebuffer[a]
23
24\startbuffer[b]
25\luaexpr[.4N]{math.sin(math.pi/4)}
26\stopbuffer
27
28which gives us \inlinebuffer[a]\ as result, but still one can ponder the
29usability of additional macros. When seeing this, one of the first things that
30probably comes to mind is how to get less digits, and indeed that can be
31achieved, as \inlinebuffer[b]\ demonstrates.
32
33\typebuffer[b]
34
35The optional argument between square brackets is a template as we know from other
36\CONTEXT\ commands without the leading percentage sign. But what if we don't want
37this expression and explicit math function call?
38
39\startbuffer
40$ \sin (x) = \the\sin{pi/8} $
41\stopbuffer
42
43\typebuffer
44
45This gives us a normal rendered sin function symbol at the left hand and a
46numeric result at the right hand: \inlinebuffer. The nice thing about it is that
47we don't need to come up with new macro names. \footnote {At some point \CONTEXT\
48might introduce a namespace mechanism to deal with possible conflicts between
49environments.} In a similar fashion we can do this:
50
51\startbuffer
52$ \sind(x) = \luaexpr[.4N]{math.sind(120)} = \the\sind[.4N]{120} $
53\stopbuffer
54
55\typebuffer
56
57Both calls give the same result: \inlinebuffer\ and in case you wonder why we
58have only three digits: the \type {N} formatter removes trailing zeros. However,
59the \type {\the} prefix is still not that nice, apart from the fact that we abuse
60a feature of the \LUA\ interface meant for other purposes (read: we cheat). So,
61in a next step in exploring this I cooked up:
62
63\startbuffer
64$ \sqrt(x) = \the\sqrt[.3N]     {2} $
65$ \sqrt(x) = \compute\sqrt[.3N] {2} $
66\stopbuffer
67
68\typebuffer
69
70There is still a prefix but \type {\compute} looks more natural. It is not an
71alias for \type {\the} but a shortcut for a prefix feature that can drive all
72kind of interpretations of in this case \type {\sin}, and that is probably where
73the real fun will start. Instead of functions we can also have constants:
74
75\startbuffer
76$ \pi = \compute\pi[.4N] $
77\stopbuffer
78
79In case you wonder how extensible this mechanism is, here is what happens in the
80\type {mathfun} module that needs to be loaded in the usual way. There you find:
81
82\pushoverloadmode
83    \ifdefined\normalpi \else\let\normalpi\pi \fi
84
85    \registermathfunction[sind,cosd,tand,sin,cos,tan]
86    \registermathfunction[sqrt]
87    \registermathfunction[pi][constant]
88\popoverloadmode
89
90The module also provides a few more expression variants (these can end up in the
91core if really needed much):
92
93\startbuffer
94$ \pi = \mathexpr[.40N]{pi}            $
95$ \pi = \mathexpr[.80N]{sqrt(11)}      $
96$ \pi = \decimalexpr[.80N]{sqrt(11)}   $
97$ \pi = \decimalexpr{sqrt(11)}         $
98$ c = \complexexpr{123 + new(456,789)} $
99\stopbuffer
100
101\typebuffer
102
103This gives:
104
105\startlines
106    \getbuffer
107\stoplines
108
109The question is: do we need this and if so, what more do we need? Feel free to
110bing it up on the \CONTEXT\ mailing list. It anyway is a nice demonstration of
111what can be done with the mix of languages.
112
113% \stopsection
114
115\stopchapter
116
117\stopcomponent
118
119