summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/math-ali.lmt
blob: 718975a5017e80cbe41edab0ca689c2fd80c4604 (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
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 lpegmatch = lpeg.match
local settings_to_array = utilities.parsers.settings_to_array

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

local context = context

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
}