Debian Codegraph @

texmf-dist/tex/lualatex/luacomplex/luacomplex.sty

raw
1 % The luacomplex package
2 % Version 1.4 Date: 17-Nov-2023
3 % Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.
4 % Authors: Chetan Shirore and Ajit Kumar
5
6 \ProvidesPackage{luacomplex}[1.4]
7 \RequirePackage{xkeyval}
8 \RequirePackage{amsmath}
9 \RequirePackage{luacode}
10 \RequirePackage{luamaths}
11 \begin{luacode*}
12 complexZ = {} -- global complex numbers registry
13 CM = {} -- the module
14 local mt = {} --metatable for complex numbers
15 setmetatable(_ENV, {__index = complexZ})
16 function CM.new (r, i)
17 local cp = {}
18 cp = {r=r, i=i}
19 return setmetatable(cp,mt)
20 end
21
22 -- create constant 'i'.
23 CM.i = CM.new(0, 1)
24 -- adding functions to the CM module.
25 function CM.add (c1, c2)
26 return CM.new(c1.r + c2.r, c1.i + c2.i)
27 end
28
29 function CM.sub (c1, c2)
30 return CM.new(c1.r - c2.r, c1.i - c2.i)
31 end
32
33 function CM.minus (c1)
34 return CM.new(-c1.r, -c1.i)
35 end
36
37 function CM.mul (c1, c2)
38 return CM.new(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r)
39 end
40
41 function CM.inv (c)
42 local n = c.r^2 + c.i^2
43 return CM.new(c.r/n, -c.i/n)
44 end
45
46 function CM.div (c1, c2)
47 return CM.mul(c1, CM.inv(c2))
48 end
49
50 function CM.re (c)
51 return CM.new(c.r,0)
52 end
53
54 function CM.im (c)
55 return CM.new(c.i,0)
56 end
57
58 function CM.mod (c)
59 local n = c.r^2 + c.i^2
60 return CM.new(n,0)
61 end
62
63 function CM.prinarg(c)
64 local arg
65 if c.r > 0 then
66 arg = math.atan(c.i/c.r)
67 elseif c.r < 0 and c.i >= 0 then
68 arg = math.atan(c.i/c.r) + math.pi
69 elseif c.r < 0 and c.i < 0 then
70 arg = math.atan(c.i/c.r) - math.pi
71 elseif c.r == 0 and c.i > 0 then
72 arg = math.pi / 2
73 elseif c.r == 0 and c.i < 0 then
74 arg = - math.pi / 2
75 else
76 error("Principal argument not defined.")
77 end
78 return CM.new(arg,0)
79 end
80
81 function CM.op (...)
82 return ...
83 end
84
85 function CM.tostring (c,imgunit)
86 imgunit = "\\imgUnit"
87 if c.i ==0 then
88 return string.format("%g", c.r)
89 elseif c.i> 0 and c.i==1 then
90 return string.format("%g+"..imgunit, c.r)
91 elseif c.i> 0 and c.i~=1 then
92 return string.format("%g+%g"..imgunit, c.r, c.i)
93 else
94 return string.format("%g%g"..imgunit, c.r, c.i) --to avoid +-
95 end
96 end
97
98 --Setting Metatable operations.
99 mt.__add = CM.add
100 mt.__mul = CM.mul
101 mt.__sub = CM.sub
102 mt.__unm = CM.minus
103 mt.__tostring = CM.tostring
104 \end{luacode*}
105 \newcommand\cpxNew[2]{%
106 \directlua{%
107 complexZ[\luastringN{#1}] = CM.new(#2)
108 }%
109 }
110 \newcommand\cpxPrint[1]{%
111 \directlua{tex.sprint(CM.tostring(complexZ[\luastringN{#1}]))}%
112 }
113 \newcommand\cpxAdd[3]{%
114 \directlua{%
115 complexZ[\luastringN{#1}] = CM.add(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
116 }%
117 }
118 \newcommand\cpxSub[3]{%
119 \directlua{%
120 complexZ[\luastringN{#1}] = CM.sub(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
121 }%
122 }
123 \newcommand\cpxMul[3]{%
124 \directlua{%
125 complexZ[\luastringN{#1}] = CM.mul(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
126 }%
127 }
128 \newcommand\cpxDiv[3]{%
129 \directlua{%
130 complexZ[\luastringN{#1}] = CM.div(complexZ[\luastringN{#2}],complexZ[\luastringN{#3}])
131 }%
132 }
133 \newcommand\cpxInv[2]{%
134 \directlua{%
135 complexZ[\luastringN{#1}] = CM.inv(complexZ[\luastringN{#2}])
136 }%
137 }
138 \newcommand\cpxRe[2]{%
139 \directlua{%
140 complexZ[\luastringN{#1}] = CM.re(complexZ[\luastringN{#2}])
141 }%
142 }
143 \newcommand\cpxIm[2]{%
144 \directlua{%
145 complexZ[\luastringN{#1}] = CM.im(complexZ[\luastringN{#2}])
146 }%
147 }
148 \newcommand\cpxMod[2]{%
149 \directlua{%
150 complexZ[\luastringN{#1}] = CM.mod(complexZ[\luastringN{#2}])
151 }%
152 }
153
154 \newcommand\cpxPrinArg[2]{%
155 \directlua{%
156 complexZ[\luastringN{#1}] = CM.prinarg(complexZ[\luastringN{#2}])
157 }%
158 }
159
160 \newcommand\cpxOp[2]{%
161 \directlua{%
162 complexZ[\luastringN{#1}] = CM.op(#2)
163 }%
164 }
165
166 \newcommand{\imgUnit}{i}
167 \endinput