| 1 |
--Version=1.4, Date=04-Aug-2023 |
| 2 |
-- provides module for complex numbers |
| 3 |
--Contains a modified version of the file complex.lua. It is availalbe on the link https://github.com/davidm/lua-matrix/blob/master/lua/complex.lua. This is licensed under the same terms as Lua itself. This license allows to freely copy, modify and distribute the file for any purpose and without any restrictions. |
| 4 |
--This file is also licensed under the same terms as Lua itself. This license allows to freely copy, modify and distribute the file for any purpose and without any restrictions. |
| 5 |
|
| 6 |
frac= require("luamaths-fractions") |
| 7 |
complex = {} |
| 8 |
complex_meta = {} |
| 9 |
|
| 10 |
local function parse_scalar(s, pos0) |
| 11 |
local x, n, pos = s:match('^([+-]?[%d%.]+)(.?)()', pos0) |
| 12 |
if not x then return end |
| 13 |
if n == 'e' or n == 'E' then |
| 14 |
local x2, n2, pos2 = s:match('^([+-]?%d+)(.?)()', pos) |
| 15 |
if not x2 then error 'number format error' end |
| 16 |
x = tonumber(x..n..x2) |
| 17 |
if not x then error 'number format error' end |
| 18 |
return x, n2, pos2 |
| 19 |
else |
| 20 |
x = tonumber(x) |
| 21 |
if not x then error 'number format error' end |
| 22 |
return x, n, pos |
| 23 |
end |
| 24 |
end |
| 25 |
local function parse_component(s, pos0) |
| 26 |
local x, n, pos = parse_scalar(s, pos0) |
| 27 |
if not x then |
| 28 |
local x2, n2, pos2 = s:match('^([+-]?)(i)()$', pos0) |
| 29 |
if not x2 then error 'number format error' end |
| 30 |
return (x2=='-' and -1 or 1), n2, pos2 |
| 31 |
end |
| 32 |
if n == '/' then |
| 33 |
local x2, n2, pos2 = parse_scalar(s, pos) |
| 34 |
x = x / x2 |
| 35 |
return x, n2, pos2 |
| 36 |
end |
| 37 |
return x, n, pos |
| 38 |
end |
| 39 |
local function parse_complex(s) |
| 40 |
local x, n, pos = parse_component(s, 1) |
| 41 |
if n == '+' or n == '-' then |
| 42 |
local x2, n2, pos2 = parse_component(s, pos) |
| 43 |
if n2 ~= 'i' or pos2 ~= #s+1 then error 'number format error' end |
| 44 |
if n == '-' then x2 = - x2 end |
| 45 |
return x, x2 |
| 46 |
elseif n == '' then |
| 47 |
return x, 0 |
| 48 |
elseif n == 'i' then |
| 49 |
if pos ~= #s+1 then error 'number format error' end |
| 50 |
return 0, x |
| 51 |
else |
| 52 |
error 'number format error' |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
function complex.to( num ) |
| 57 |
-- check for table type |
| 58 |
if type( num ) == "table" then |
| 59 |
-- check for a complex number |
| 60 |
if getmetatable( num ) == complex_meta then |
| 61 |
return num |
| 62 |
end |
| 63 |
|
| 64 |
if getmetatable( num) == frac_mt then |
| 65 |
return setmetatable( { num, 0 }, complex_meta ) |
| 66 |
end |
| 67 |
|
| 68 |
local real,imag = tonumber( num[1] ),tonumber( num[2] ) |
| 69 |
if real and imag then |
| 70 |
return setmetatable( { real,imag }, complex_meta ) |
| 71 |
end |
| 72 |
return |
| 73 |
end |
| 74 |
local isnum = tonumber( num ) |
| 75 |
if isnum then |
| 76 |
return setmetatable( { isnum,0 }, complex_meta ) |
| 77 |
end |
| 78 |
if type( num ) == "string" then |
| 79 |
local real, imag = parse_complex(num) |
| 80 |
return setmetatable( { real, imag }, complex_meta ) |
| 81 |
end |
| 82 |
end |
| 83 |
|
| 84 |
setmetatable( complex, { __call = function( _,num ) return complex.to( num ) end } ) |
| 85 |
|
| 86 |
function complex.new( x,y) |
| 87 |
return setmetatable( {x,y}, complex_meta ) |
| 88 |
end |
| 89 |
|
| 90 |
lcomplex = complex.new |
| 91 |
function complex.type( arg ) |
| 92 |
if getmetatable( arg ) == complex_meta then |
| 93 |
return "complex" |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
function complex.convpolar( radius, phi ) |
| 98 |
return setmetatable( { radius * math.cos( phi ), radius * math.sin( phi ) }, complex_meta ) |
| 99 |
end |
| 100 |
|
| 101 |
function complex.convpolardeg( radius, phi ) |
| 102 |
phi = phi/180 * math.pi |
| 103 |
return setmetatable( { radius * math.cos( phi ), radius * math.sin( phi ) }, complex_meta ) |
| 104 |
end |
| 105 |
|
| 106 |
function complex.tostring( cx,formatstr ) |
| 107 |
imunit = "\\imUnit" |
| 108 |
local real,imag = cx[1],cx[2] |
| 109 |
if type(cx[1]) ~= "table" and type(cx[2]) ~= "table" then |
| 110 |
if imag == 0 and math.floor(real)==real then |
| 111 |
return math.floor(real) |
| 112 |
end |
| 113 |
if real == 0 and math.floor(imag)==imag and math.abs(math.floor(imag))~=1 then |
| 114 |
return math.floor(imag)..imunit |
| 115 |
end |
| 116 |
if imag == 0 then |
| 117 |
return real |
| 118 |
elseif real == 0 then |
| 119 |
return ((imag==1 and "") or (imag==-1 and "-") or imag)..imunit |
| 120 |
elseif imag > 0 then |
| 121 |
return real.."+"..(imag==1 and "" or imag)..imunit |
| 122 |
end |
| 123 |
return real..(imag==-1 and "-" or imag)..imunit |
| 124 |
end |
| 125 |
|
| 126 |
if type(cx[1]) == "table" and type(cx[2]) ~= "table" then |
| 127 |
if cx[2] == 0 then |
| 128 |
return frac.tostring(cx[1]) |
| 129 |
end |
| 130 |
if cx[2] > 0 then |
| 131 |
return frac.tostring(cx[1]).. "+"..(imag==1 and "" or imag)..imunit |
| 132 |
end |
| 133 |
if cx[2] < 0 then |
| 134 |
return frac.tostring(cx[1])..(imag==-1 and "-" or imag)..imunit |
| 135 |
end |
| 136 |
|
| 137 |
end |
| 138 |
|
| 139 |
if type(cx[1]) ~= "table" and type(cx[2]) == "table" then |
| 140 |
if frac.toFnumber(cx[2])==0 then return cx[1] end |
| 141 |
return cx[1].."+"..frac.tostring(cx[2])..imunit |
| 142 |
end |
| 143 |
|
| 144 |
if type(cx[1]) == "table" and type(cx[2]) == "table" then |
| 145 |
if frac.toFnumber(cx[1]) == 0 and frac.toFnumber(cx[2]) ~= 0 then |
| 146 |
return frac.tostring(cx[2])..imunit |
| 147 |
end |
| 148 |
|
| 149 |
if frac.toFnumber(cx[2]) == 0 then |
| 150 |
return frac.tostring(cx[1] + 0) |
| 151 |
end |
| 152 |
|
| 153 |
if cx[2].d == 1 then |
| 154 |
if cx[2].n > 0 then |
| 155 |
return frac.tostring(cx[1]).. "+"..(cx[2].n==1 and "" or math.floor(cx[2].n))..imunit |
| 156 |
end |
| 157 |
if cx[2].n < 0 then |
| 158 |
return frac.tostring(cx[1])..(cx[2].n==-1 and "-" or math.floor(cx[2].n))..imunit |
| 159 |
end |
| 160 |
end |
| 161 |
end |
| 162 |
return frac.tostring(cx[1]).. "+"..frac.tostring(cx[2])..imunit |
| 163 |
end |
| 164 |
|
| 165 |
function complex.print( ... ) |
| 166 |
print( complex.tostring( ... ) ) |
| 167 |
end |
| 168 |
|
| 169 |
function complex.polar( cx ) |
| 170 |
return math.sqrt( cx[1]^2 + cx[2]^2 ), math.atan2( cx[2], cx[1] ) |
| 171 |
end |
| 172 |
|
| 173 |
function complex.polardeg( cx ) |
| 174 |
return math.sqrt( cx[1]^2 + cx[2]^2 ), math.atan2( cx[2], cx[1] ) / math.pi * 180 |
| 175 |
end |
| 176 |
|
| 177 |
function complex.norm2( cx ) |
| 178 |
return cx[1]^2 + cx[2]^2 |
| 179 |
end |
| 180 |
|
| 181 |
function complex.abs( cx ) |
| 182 |
return math.sqrt( cx[1]^2 + cx[2]^2 ) |
| 183 |
end |
| 184 |
|
| 185 |
function complex.get( cx ) |
| 186 |
return cx[1],cx[2] |
| 187 |
end |
| 188 |
|
| 189 |
function complex.set( cx,real,imag ) |
| 190 |
cx[1],cx[2] = real,imag |
| 191 |
end |
| 192 |
|
| 193 |
function complex.is( cx,real,imag ) |
| 194 |
if cx[1] == real and cx[2] == imag then |
| 195 |
return true |
| 196 |
end |
| 197 |
return false |
| 198 |
end |
| 199 |
|
| 200 |
function complex.copy( cx ) |
| 201 |
return setmetatable( { cx[1],cx[2] }, complex_meta ) |
| 202 |
end |
| 203 |
|
| 204 |
function complex.add( cx1,cx2 ) |
| 205 |
return setmetatable( { cx1[1]+cx2[1], cx1[2]+cx2[2] }, complex_meta ) |
| 206 |
end |
| 207 |
|
| 208 |
function complex.sub( cx1,cx2 ) |
| 209 |
return setmetatable( { cx1[1]-cx2[1], cx1[2]-cx2[2] }, complex_meta ) |
| 210 |
end |
| 211 |
|
| 212 |
function complex.mul( cx1,cx2 ) |
| 213 |
return setmetatable( { cx1[1]*cx2[1] - cx1[2]*cx2[2],cx1[1]*cx2[2] + cx1[2]*cx2[1] }, complex_meta ) |
| 214 |
end |
| 215 |
|
| 216 |
function complex.mulnum( cx,num ) |
| 217 |
return setmetatable( { cx[1]*num,cx[2]*num }, complex_meta ) |
| 218 |
end |
| 219 |
|
| 220 |
function complex.div( cx1,cx2 ) |
| 221 |
local val = cx2[1]*cx2[1] + cx2[2]*cx2[2] |
| 222 |
return setmetatable( { (cx1[1]*cx2[1]+cx1[2]*cx2[2])/val,(cx1[2]*cx2[1]-cx1[1]*cx2[2])/val }, complex_meta ) |
| 223 |
end |
| 224 |
|
| 225 |
function complex.divnum( cx,num ) |
| 226 |
return setmetatable( { cx[1]/num,cx[2]/num }, complex_meta ) |
| 227 |
end |
| 228 |
|
| 229 |
function complex.pow( cx,num ) |
| 230 |
if math.floor( num ) == num then |
| 231 |
if num < 0 then |
| 232 |
local val = cx[1]^2 + cx[2]^2 |
| 233 |
cx = { cx[1]/val,-cx[2]/val } |
| 234 |
num = -num |
| 235 |
end |
| 236 |
local real,imag = cx[1],cx[2] |
| 237 |
for i = 2,num do |
| 238 |
real,imag = real*cx[1] - imag*cx[2],real*cx[2] + imag*cx[1] |
| 239 |
end |
| 240 |
return setmetatable( { real,imag }, complex_meta ) |
| 241 |
end |
| 242 |
local length,phi = math.sqrt( cx[1]^2 + cx[2]^2 )^num, math.atan2( cx[2], cx[1] )*num |
| 243 |
return setmetatable( { length * math.cos( phi ), length * math.sin( phi ) }, complex_meta ) |
| 244 |
end |
| 245 |
|
| 246 |
function complex.sqrt( cx ) |
| 247 |
local h |
| 248 |
local k |
| 249 |
|
| 250 |
if type(cx[1]) ~= "table" then h = cx[1] end |
| 251 |
if type(cx[2]) ~= "table" then k = cx[2] end |
| 252 |
|
| 253 |
if type(cx[1]) == "table" then h = frac.toFnumber(cx[1]) end |
| 254 |
if type(cx[2]) == "table" then k = frac.toFnumber(cx[2]) end |
| 255 |
local len = math.sqrt( h^2 + k^2 ) |
| 256 |
local sign = ( h<0 and -1) or 1 |
| 257 |
return setmetatable( { math.sqrt((h +len)/2), sign*math.sqrt((len-h)/2) }, complex_meta ) |
| 258 |
end |
| 259 |
|
| 260 |
function complex.ln( cx ) |
| 261 |
return setmetatable( { math.log(math.sqrt( cx[1]^2 + cx[2]^2 )), |
| 262 |
math.atan2( cx[2], cx[1] ) }, complex_meta ) |
| 263 |
end |
| 264 |
|
| 265 |
function complex.exp( cx ) |
| 266 |
local expreal = math.exp(cx[1]) |
| 267 |
return setmetatable( { expreal*math.cos(cx[2]), expreal*math.sin(cx[2]) }, complex_meta ) |
| 268 |
end |
| 269 |
|
| 270 |
function complex.conjugate( cx ) |
| 271 |
return setmetatable( { cx[1], -cx[2] }, complex_meta ) |
| 272 |
end |
| 273 |
|
| 274 |
function Xround(num, numDecimalPlaces) |
| 275 |
if type(num)=="number" then |
| 276 |
if num==math.floor(num) then |
| 277 |
return math.floor(num) |
| 278 |
end |
| 279 |
end |
| 280 |
if type(num)=="number" then |
| 281 |
local mult = 10^(numDecimalPlaces or 0) |
| 282 |
return math.floor(num * mult + 0.5) / mult |
| 283 |
end |
| 284 |
return num |
| 285 |
end |
| 286 |
|
| 287 |
function complex.round( cx,idp ) |
| 288 |
local mult =10^( idp or 0 ) |
| 289 |
if type(cx[1]) ~= "table" and type(cx[2]) ~= "table" then |
| 290 |
return setmetatable( {Xround(cx[1],idp), Xround(cx[2],idp)}, complex_meta ) |
| 291 |
end |
| 292 |
if type(cx[1]) ~= "table" and type(cx[2]) == "table" then |
| 293 |
return setmetatable( {Xround(cx[1],idp), cx[2]}, complex_meta ) |
| 294 |
end |
| 295 |
if type(cx[1]) == "table" and type(cx[2]) ~= "table" then |
| 296 |
return setmetatable({cx[1],Xround(cx[2],idp)}, complex_meta ) |
| 297 |
end |
| 298 |
if type(cx[1]) == "table" and type(cx[2]) == "table" then |
| 299 |
return setmetatable({cx[1],cx[2]}, complex_meta ) |
| 300 |
end |
| 301 |
end |
| 302 |
|
| 303 |
complex.zero = complex.new(0, 0) |
| 304 |
complex.one = complex.new(1, 0) |
| 305 |
|
| 306 |
complex_meta.__add = function( cx1,cx2 ) |
| 307 |
local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 ) |
| 308 |
return complex.add( cx1,cx2 ) |
| 309 |
end |
| 310 |
complex_meta.__sub = function( cx1,cx2 ) |
| 311 |
local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 ) |
| 312 |
return complex.sub( cx1,cx2 ) |
| 313 |
end |
| 314 |
complex_meta.__mul = function( cx1,cx2 ) |
| 315 |
local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 ) |
| 316 |
return complex.mul( cx1,cx2 ) |
| 317 |
end |
| 318 |
complex_meta.__div = function( cx1,cx2 ) |
| 319 |
local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 ) |
| 320 |
return complex.div( cx1,cx2 ) |
| 321 |
end |
| 322 |
complex_meta.__pow = function( cx,num ) |
| 323 |
if num == "*" then |
| 324 |
return complex.conjugate( cx ) |
| 325 |
end |
| 326 |
return complex.pow( cx,num ) |
| 327 |
end |
| 328 |
complex_meta.__unm = function( cx ) |
| 329 |
return setmetatable( { -cx[1], -cx[2] }, complex_meta ) |
| 330 |
end |
| 331 |
complex_meta.__eq = function( cx1,cx2 ) |
| 332 |
if cx1[1] == cx2[1] and cx1[2] == cx2[2] then |
| 333 |
return true |
| 334 |
end |
| 335 |
return false |
| 336 |
end |
| 337 |
complex_meta.__tostring = function( cx ) |
| 338 |
return tostring( complex.tostring( cx ) ) |
| 339 |
end |
| 340 |
complex_meta.__concat = function( cx,cx2 ) |
| 341 |
return tostring(cx)..tostring(cx2) |
| 342 |
end |
| 343 |
-- cx( cx, formatstr ) |
| 344 |
complex_meta.__call = function( ... ) |
| 345 |
print( complex.tostring( ... ) ) |
| 346 |
end |
| 347 |
complex_meta.__index = {} |
| 348 |
for k,v in pairs( complex ) do |
| 349 |
complex_meta.__index[k] = v |
| 350 |
end |
| 351 |
|
| 352 |
return complex |
| 353 |
|