summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/math-ali.lmt
blob: 87ea67c727cb39618aca41d230cb9f5c98c2d47e (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
if not modules then modules = { } end modules ['math-ali'] = {
    version   = 1.001,
    comment   = "companion to math-ali.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local unpack = unpack
local gsub = string.gsub
local sort, keys = table.sort, table.keys
local settings_to_array = utilities.parsers.settings_to_array
local P, R, S, C, Cc, Ct, Cs = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cs
local lpegmatch = lpeg.match

local context = context

do

    local rows = utilities.parsers.groupedsplitat(";")
    local cols = utilities.parsers.groupedsplitat(",")

    local actions = {
        transpose = function(m)
            local t = { }
            for j=1,#m[1] do
                local r = { }
                for i=1,#m do
                    r[i] = m[i][j]
                end
                t[j] = r
            end
            return t
        end,
        negate = function(m)
            for i=1,#m do
                local mi = m[i]
                for j=1,#mi do
                    mi[j] = - mi[j]
                end
            end
            return m
        end,
        scale = function(m,s)
            s = tonumber(s)
            if s then
                for i=1,#m do
                    local mi = m[i]
                    for j=1,#mi do
                        mi[j] = s*mi[j]
                    end
                end
            end
            return m
        end,
    }

    local useractions = {
    }

    interfaces.implement {
        name      = "simplematrix",
        arguments = "2 strings",
        actions   = function(method,data)
            local m = lpegmatch(rows,(gsub(data,"%s+"," ")))
            for i=1,#m do
                m[i] = lpegmatch(cols,m[i])
            end
            local methods = settings_to_array(method)
            for i=1,#methods do
                local detail = settings_to_array(methods[i])
                local method = detail[1]
                local action = actions[method] or useractions[method]
                if action then
                    m = action(m,unpack(detail,2)) or m
                end
            end
            for i=1,#m do
                context("\\NC %{ \\NC }t \\NR",m[i])
            end
        end
    }

    function mathematics.registersimplematrix(name,action)
        if type(action) == "function" then
            useractions[name] = action
        end
    end

    -- \cases{1, x>0 ; -1, x<0 }

    interfaces.implement {
        name      = "simplecases",
        arguments = "2 strings",
        actions   = function(method,data)
            -- no methods yet
            local m = lpegmatch(rows,(gsub(data,"%s+"," ")))
            for i=1,#m do
                m[i] = lpegmatch(cols,m[i])
            end
            for i=1,#m do
                context("\\NC %{ \\NC }t \\NR",m[i])
            end
        end
    }

end

do

    local relations = {
        ["<"]   = "<", [">"]   = ">",
        ["!<"]  = "≮", ["!>"]  = "≯",
        ["<<"]  = "≪", [">>"] = "≫",
        ["="]   = "=", ["=="]  = "=",
        ["<>"]  = "≠", ["!="]  = "≠",
        ["<="]  = "≤", [">="]  = "≥",
        ["=<"]  = "≦", ["=>"]  = "≧",
        ["!=<"] = "≰", ["!=>"] = "≱",
        ["~"]   = "≈", ["~~"]  = "≈",
    }

    for k, v in next, table.copy(relations) do relations[v] = v end

 -- local binaries = {
 --     ["+"] = "+",
 --     ["-"] = "-",
 --     ["/"] = "/",
 --     ["*"] = "",
 -- }

    local separators = {
        [","] = true,
        [";"] = true,
    }

    local alternatives = {
        -- not that many
    }

    local p_sign     = S("-+")
    local p_script   = S("^")^1
    local p_number   = R("09","..")^1 + lpeg.patterns.nestedbraces
    local p_alpha    = R("az","AZ")^1
    local p_variable = p_alpha * (P("_") * (p_number + p_alpha))^-1

    local spacing    = P(" ")^0
    local script     = Cs(p_script * Cc("{") * p_sign^0 * (p_number + p_variable) * Cc("}"))
    local sign       = C(p_sign) + Cc("+")
    local number     = C(p_number)
    local variable   = C(p_variable)
 -- local binary     = lpeg.utfchartabletopattern(binaries) / binaries
    local relation   = lpeg.utfchartabletopattern(relations) / relations
    local separator  = lpeg.utfchartabletopattern(separators)

    local snippet = Ct (
        (
              spacing * sign * spacing * number    * spacing * variable
            + spacing * sign * spacing * number    * spacing * Cc(false)
            + spacing * sign * spacing * Cc(false) * spacing * variable
        ) * (script + Cc(false))
    )

    local parser = Ct ( Ct (
        ( Ct ( snippet^1 ) * spacing * relation    )^1
      * ( Ct ( snippet^1 ) * spacing * separator^0 )
    )^1 )

    local num = "!"

    local ctx_NC, ctx_NR = context.NC, context.NR
    local ctx_typ = context.typ
    local ctx_ord, ctx_rel = context.mathord, context.mathrel

    local clean = table.setmetatableindex(function(t,k)
        local v = gsub(k,"[{}]","")
        t[k] = v
        return v
    end)

    alternatives.equationsystem = function(action,str)
        local rows = lpegmatch(parser,str)
        if not rows then
            ctx_typ("bad system, case 1: %s",1,str)
            return
        end
        local nrow = #rows
        local all = table.setmetatableindex("table")
        for r=1,nrow do
            local row = rows[r]
            for s=1,#row,2 do
                local set = row[s]
                for v=1,#set do
                    local vvv = set[v]
                    local var = vvv[3]
                    if not var then
                        var = num
                    end
                    if set[var] then
                        print("bad system, two constants")
                    end
                    set[var] = set[v]
                    all[s][var] = true
                    set[v] = nil
                end
            end
        end
        --
        local cnt = #rows[1]
        for r=1,nrow do
            if #rows[r] ~= cnt then
                ctx_typ("bad system, case %i: %s",2,str)
                return
            end
        end
        for r=1,#rows[1] do
            local a = keys(all[r])
            sort(a,function(a,b)
                return clean[a] < clean[b]
            end)
            all[r] = a
        end
        --
        for r=1,nrow do
            local row = rows[r]
            for r=1,#row do
                local set = row[r]
                local how = all[r]
                if #how > 0 then
                 -- local done = false
                    for i=1,#how do
                        local k = how[i]
                        local e = set[k]
                        if e then
                            ctx_NC()
                         -- if not done and e[1] == "+" then
                            if i == 1 and e[1] == "+" then
                             -- ctx_ord("")
                             -- ctx_hphantom(e[1])
                             -- ctx_ord("")
                            else
                                ctx_ord("")
                                context(e[1])
                                ctx_ord("")
                             -- done = true
                            end
                            ctx_NC()
                            if e[2] then
                                context(e[2])
                             -- done = true
                            end
                            if e[3] then
                                context(e[3])
                             -- done = true
                            end
                            if e[4] then
                                context(e[4])
                             -- done = true
                            end
                        else
                            ctx_NC()
                         -- ctx_mathord("")
                            ctx_NC()
                        end
                    end
                else
                    ctx_NC()
                    ctx_ord("")
                    context(set)
                    ctx_ord("")
                end
            end
            ctx_NR()
        end
    end

    interfaces.implement {
        name      = "simplealign",
     -- public    = true,
        protected = true,
        arguments = "3 strings",
        actions   = function(alternative,action,str)
            local a = alternatives[alternative]
            if a then
                a(action,str)
            else
                context(str)
            end
        end
    }

end