summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkxl/m-mathfun.mkxl
blob: 581f50971e991604c1909cba4d06baf07b377c5a (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
%D \module
%D   [       file=m-mathfun,
%D        version=2021.02.20,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Wried Math Stuff,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Occasionaly some experiment results in some weird feature. It doesn't make sense
%D to make this core functionality but if also makes no sense to throw it away. By
%D making it a module the user can decide. It's actually an example of abusing a
%D \LUA\ interfacing mechanism that is meant for something else.

\ifdefined\compute \else
    \permanent\protected\def\compute{\thewithproperty\plusone}
\fi

%D At some point this will move to \type {m-mathfun.lmt}:

\startluacode
    local type, load = type, load
    local gmatch = string.gmatch

    local xmath         = xmath
    local xcomplex      = xcomplex
    local xdecimal      = xdecimal

    local context       = context
    local ctx_mfunction = context.mfunction

    local scanoptional  = tokens.scanners.optional
    local scanargument  = tokens.scanners.argument

    local createtoken   = tokens.create
    local defined       = tokens.defined

    local implement     = interfaces.implement

    local compute_code  = 1

    local function smart(what,name,kind)
        if what == "value" or what == compute_code then
            local temp = scanoptional()
            if temp and temp ~= "" then
                temp = "%" .. temp
            else
                temp = "%.6N"
            end
            if kind == "constant" then
                context(temp,math[name])
            else
                local code = scanargument()
                local func = load("return "..name.."("..code..")","mathfunction","t",math)
                if type(func) == "function" then
                    context(temp,func())
                else
                    context(code)
                end
            end
        elseif kind == "constant" then
          -- context[name]() -- recursion
            name = "normal"..name
            if defined(name) then
                context(createtoken(name))
            else
                context(name)
            end
        else
            ctx_mfunction(name)
        end
    end

    local template = { name = false, usage = "value", public = true, protected = true, actions = false, overload = true }

    local function install(str,kind)
        for name in gmatch(str,"[^ ,]+") do
            template.name    = name
            template.actions = function(what) smart(what,name,kind) end
            implement(template)
        end
    end

    local function mathexpr()
        local temp = scanoptional()
        local code = scanargument()
        local func = load("return " .. code,"mathexpr","t",xmath)
        if type(func) == "function" then
            if temp and temp ~= "" then
                temp = "%" .. temp
            else
                temp = "%.6N"
            end
            context(temp,func())
        else
            context(code)
        end
    end

    local tostring    = xdecimal.tostring
    local toengstring = xdecimal.toengstring -- todo

    local function decimalexpr()
        local temp = scanoptional()
        local code = scanargument()
        local func = load("return " .. code,"decimalexpr","t",xdecimal)
        if type(func) == "function" then
            local result = tostring(func())
            if temp and temp ~= "" then
                context("%"..temp,result)
            else
                context(result)
            end
        else
            context(code)
        end
    end

    local topair = xcomplex.topair

    local function complexexpr()
        local temp = scanoptional()
        local code = scanargument()
        local func = load("return " .. code,"complexexpr","t",xcomplex)
        if type(func) == "function" then
         -- local result = tostring(func())
         -- if temp and temp ~= "" then
         --     context("%"..temp,result)
         -- else
         --     context(result)
         -- end
            if temp and temp ~= "" then
                temp = "%" .. temp
            else
                temp = "%.6N + %.6Ni"
            end
            local result = func()
            context(temp,topair(result))
        else
            context(code)
        end
    end

    implement {
        name      = "registermathfunction",
        public    = true,
        protected = true,
        actions   = install,
        arguments = { "optional", "optional" },
    }

    implement {
        name    = "mathexpr",
        public  = true,
        actions = mathexpr,
    }

    implement {
        name    = "decimalexpr",
        public  = true,
        actions = decimalexpr,
    }

    implement {
        name    = "complexexpr",
        public  = true,
        actions = complexexpr,
    }

 -- install("sind cosd tand sin cos tan")
 -- install("sqrt")
\stopluacode

\pushoverloadmode

    \ifdefined\normalpi \else\let\normalpi\pi \fi

    \registermathfunction[sind,cosd,tand,sin,cos,tan]
    \registermathfunction[sqrt]
    \registermathfunction[pi][constant]

\popoverloadmode

\continueifinputfile{m-mathfun.mkxl}

% \pushoverloadmode
% \let\normalpi\pi
% \registermathfunction[sind,cosd,tand,sin,cos,tan]
% \registermathfunction[sqrt]
% \registermathfunction[pi][constant]
% \popoverloadmode

\usemodule[scite] \setupbodyfont[dejavu] \setuplayout[tight] \setuppapersize[A5]

\starttext

\startbuffer
$ \sin (x) = \luaexpr       {math.sin(math.pi/2)} $
$ \sin (x) = \luaexpr [.4N] {math.sin(math.pi/2)} $
$ \sin (x) = \the\sin                     {pi/2}  $
$ \sind(x) = \luaexpr [.4N] {math.sind(120)}      $
$ \sind(x) = \the\sind[.4N]           {120}       $
$ \sqrt(x) = \luaexpr       {math.sqrt(2)}        $
$ \sqrt(x) = \luaexpr [.6N] {math.sqrt(2)}        $
$ \sqrt(x) = \the\sqrt                {2}         $
$ \sqrt(x) = \the\sqrt[.3N]           {2}         $
$ \sqrt(x) = \compute\sqrt[.3N]       {2}         $
$ \sind(x) = \luaexpr [.4N] {math.pi}             $
$ \pi = \compute\pi[.4N]                          $
$ \pi = \mathexpr[.40N]{pi}                       $
$ \pi = \mathexpr[.80N]{sqrt(11)}                 $
$ \pi = \decimalexpr[.80N]{sqrt(11)}              $
$ \pi = \decimalexpr{sqrt(11)}                    $
$ c = \complexexpr{123 + new(456,789)}            $
\stopbuffer

Take your choice:

\typebuffer[option=TEX]

And get:

\startlines
    \getbuffer
\stoplines

\stoptext