forked from postwait/numlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeall.lua
62 lines (54 loc) · 1.39 KB
/
seeall.lua
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
--[[
-- seeall.lua
-- Easy prototyping in NumericLua
-- Luis Carvalho (lexcarvalho@gmail.com)
-- See Copyright Notice in numlua.h
--]]
require "numlua"
pcall(require, "help")
local classes = {"math", "mathx", "complex", "matrix"}
local words = setmetatable({}, {__index = function() return 0 end})
for _, c in pairs(classes) do
for k in pairs(_G[c]) do
words[k] = words[k] + 1
end
end
local nltype = numlua.type
local function registermath (name)
_G[name] = function (x, ...)
local xtype = nltype(x)
if xtype == "number" then
local f = math[name] or mathx[name] or complex[name]
return f(x, ...)
elseif xtype == "complex" then
return complex[name](x, ...)
else
return matrix[name](x, ...)
end
end
end
local register = function (t, prefix)
for k, v in pairs(t) do
if not _G[k] then
_G[k] = v
elseif prefix then
_G[prefix .. k] = v
end
end
end
-- register common math functions first
for k, v in pairs(words) do
if v > 1 then -- conflict?
registermath(k) -- register common function
end
end
register(math, "x")
register(mathx, "x")
register(complex, "c")
register(matrix, "m")
register(stat, "s")
register(rng, "r") -- conflicts with matrix: new, copy
type = numlua.type -- override
opmode = numlua.opmode
-- set matrix __tostring to `pretty`: more convenient for interpreter
getmetatable(matrix(1)).__tostring = matrix.pretty