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
|
% The luacomplex package
% Version 1.3 Date: 08-Aug-2023
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.
% Authors: Chetan Shirore and Ajit Kumar
\ProvidesPackage{luacomplex}[1.3]
\RequirePackage{xkeyval}
\RequirePackage{amsmath}
\RequirePackage{luacode}
\RequirePackage{luamaths}
\begin{luacode*}
complexZ = {} -- global complex numbers registry
CM = {} -- the module
local mt = {} --metatable for complex numbers
setmetatable(_ENV, {__index = complexZ})
function CM.new (r, i)
local cp = {}
cp = {r=r, i=i}
return setmetatable(cp,mt)
end
-- create constant 'i'
CM.i = CM.new(0, 1)
function CM.add (c1, c2)
return CM.new(c1.r + c2.r, c1.i + c2.i)
end
function CM.sub (c1, c2)
return CM.new(c1.r - c2.r, c1.i - c2.i)
end
function CM.minus (c1)
return CM.new(-c1.r, -c1.i)
end
function CM.mul (c1, c2)
return CM.new(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r)
end
function CM.inv (c)
local n = c.r^2 + c.i^2
return CM.new(c.r/n, -c.i/n)
end
function CM.div (c1, c2)
return CM.mul(c1, CM.inv(c2))
end
function CM.re (c)
return CM.new(c.r,0)
end
function CM.im (c)
return CM.new(c.i,0)
end
function CM.mod (c)
local n = c.r^2 + c.i^2
return CM.new(n,0)
end
function CM.prinarg(c)
local arg
if c.r > 0 then
arg = math.atan(c.i/c.r)
elseif c.r < 0 and c.i >= 0 then
arg = math.atan(c.i/c.r) + math.pi
elseif c.r < 0 and c.i < 0 then
arg = math.atan(c.i/c.r) - math.pi
elseif c.r == 0 and c.i > 0 then
arg = math.pi / 2
elseif c.r == 0 and c.i < 0 then
arg = - math.pi / 2
else
error("Principal argument not defined.")
end
return CM.new(arg,0)
end
function CM.op (...)
return ...
end
function CM.tostring (c,imgunit)
imgunit = "\\imgUnit"
if c.i ==0 then
return string.format("%g", c.r)
elseif c.i> 0 and c.i==1 then
return string.format("%g+"..imgunit, c.r)
elseif c.i> 0 and c.i~=1 then
return string.format("%g+%g"..imgunit, c.r, c.i)
else
return string.format("%g%g"..imgunit, c.r, c.i) --to avoid +-
end
end
--Setting Metatable operations.
mt.__add = CM.add
mt.__mul = CM.mul
mt.__sub = CM.sub
mt.__unm = CM.minus
mt.__tostring = CM.tostring
\end{luacode*}
\newcommand\cpxNew[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.new(#2)
}%
}
\newcommand\cpxPrint[1]{%
\directlua{tex.sprint(CM.tostring(complexZ[\luastringN{#1}]))}%
}
\newcommand\cpxAdd[3]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.add(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
}%
}
\newcommand\cpxSub[3]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.sub(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
}%
}
\newcommand\cpxMul[3]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.mul(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
}%
}
\newcommand\cpxDiv[3]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.div(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
}%
}
\newcommand\cpxInv[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.inv(complexZ[\luastringN{#2}])
}%
}
\newcommand\cpxRe[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.re(complexZ[\luastringN{#2}])
}%
}
\newcommand\cpxIm[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.im(complexZ[\luastringN{#2}])
}%
}
\newcommand\cpxMod[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.mod(complexZ[\luastringN{#2}])
}%
}
\newcommand\cpxPrinArg[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.prinarg(complexZ[\luastringN{#2}])
}%
}
\newcommand\cpxOp[2]{%
\directlua{%
complexZ[\luastringN{#1}] = CM.op(#2)
}%
}
\newcommand{\imgUnit}{i}
\endinput
|