Megaddd

complex

Jul 12th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. -- complex 0.3.0
  2. -- Lua 5.1
  3.  
  4. -- 'complex' provides common tasks with complex numbers
  5.  
  6. -- function complex.to( arg ); complex( arg )
  7. -- returns a complex number on success, nil on failure
  8. -- arg := number or { number,number } or ( "(-)<number>" and/or "(+/-)<number>i" )
  9. -- e.g. 5; {2,3}; "2", "2+i", "-2i", "2^2*3+1/3i"
  10. -- note: 'i' is always in the numerator, spaces are not allowed
  11.  
  12. -- a complex number is defined as carthesic complex number
  13. -- complex number := { real_part, imaginary_part }
  14. -- this gives fast access to both parts of the number for calculation
  15. -- the access is faster than in a hash table
  16. -- the metatable is just a add on, when it comes to speed, one is faster using a direct function call
  17.  
  18. -- http://luaforge.net/projects/LuaMatrix
  19. -- http://lua-users.org/wiki/ComplexNumbers
  20.  
  21. -- Licensed under the same terms as Lua itself.
  22.  
  23. --/////////////--
  24. --// complex //--
  25. --/////////////--
  26.  
  27. -- link to complex table
  28. local complex = {}
  29.  
  30. -- link to complex metatable
  31. local complex_meta = {}
  32.  
  33. -- complex.to( arg )
  34. -- return a complex number on success
  35. -- return nil on failure
  36. local _retone = function() return 1 end
  37. local _retminusone = function() return -1 end
  38. function complex.to( num )
  39. -- check for table type
  40. if type( num ) == "table" then
  41. -- check for a complex number
  42. if getmetatable( num ) == complex_meta then
  43. return num
  44. end
  45. local real,imag = tonumber( num[1] ),tonumber( num[2] )
  46. if real and imag then
  47. return setmetatable( { real,imag }, complex_meta )
  48. end
  49. return
  50. end
  51. -- check for number
  52. local isnum = tonumber( num )
  53. if isnum then
  54. return setmetatable( { isnum,0 }, complex_meta )
  55. end
  56. if type( num ) == "string" then
  57. -- check for real and complex
  58. -- number chars [%-%+%*%^%d%./Ee]
  59. local real,sign,imag = string.match( num, "^([%-%+%*%^%d%./Ee]*%d)([%+%-])([%-%+%*%^%d%./Ee]*)i$" )
  60. if real then
  61. if string.lower(string.sub(real,1,1)) == "e"
  62. or string.lower(string.sub(imag,1,1)) == "e" then
  63. return
  64. end
  65. if imag == "" then
  66. if sign == "+" then
  67. imag = _retone
  68. else
  69. imag = _retminusone
  70. end
  71. elseif sign == "+" then
  72. imag = loadstring("return tonumber("..imag..")")
  73. else
  74. imag = loadstring("return tonumber("..sign..imag..")")
  75. end
  76. real = loadstring("return tonumber("..real..")")
  77. if real and imag then
  78. return setmetatable( { real(),imag() }, complex_meta )
  79. end
  80. return
  81. end
  82. -- check for complex
  83. local imag = string.match( num,"^([%-%+%*%^%d%./Ee]*)i$" )
  84. if imag then
  85. if imag == "" then
  86. return setmetatable( { 0,1 }, complex_meta )
  87. elseif imag == "-" then
  88. return setmetatable( { 0,-1 }, complex_meta )
  89. end
  90. if string.lower(string.sub(imag,1,1)) ~= "e" then
  91. imag = loadstring("return tonumber("..imag..")")
  92. if imag then
  93. return setmetatable( { 0,imag() }, complex_meta )
  94. end
  95. end
  96. return
  97. end
  98. -- should be real
  99. local real = string.match( num,"^(%-*[%d%.][%-%+%*%^%d%./Ee]*)$" )
  100. if real then
  101. real = loadstring( "return tonumber("..real..")" )
  102. if real then
  103. return setmetatable( { real(),0 }, complex_meta )
  104. end
  105. end
  106. end
  107. end
  108.  
  109. -- complex( arg )
  110. -- same as complex.to( arg )
  111. -- set __call behaviour of complex
  112. setmetatable( complex, { __call = function( _,num ) return complex.to( num ) end } )
  113.  
  114. -- complex.new( real, complex )
  115. -- fast function to get a complex number, not invoking any checks
  116. function complex.new( ... )
  117. return setmetatable( { ... }, complex_meta )
  118. end
  119.  
  120. -- complex.type( arg )
  121. -- is argument of type complex
  122. function complex.type( arg )
  123. if getmetatable( arg ) == complex_meta then
  124. return "complex"
  125. end
  126. end
  127.  
  128. -- complex.convpolar( r, phi )
  129. -- convert polar coordinates ( r*e^(i*phi) ) to carthesic complex number
  130. -- r (radius) is a number
  131. -- phi (angle) must be in radians; e.g. [0 - 2pi]
  132. function complex.convpolar( radius, phi )
  133. return setmetatable( { radius * math.cos( phi ), radius * math.sin( phi ) }, complex_meta )
  134. end
  135.  
  136. -- complex.convpolardeg( r, phi )
  137. -- convert polar coordinates ( r*e^(i*phi) ) to carthesic complex number
  138. -- r (radius) is a number
  139. -- phi must be in degrees; e.g. [0° - 360°]
  140. function complex.convpolardeg( radius, phi )
  141. phi = phi/180 * math.pi
  142. return setmetatable( { radius * math.cos( phi ), radius * math.sin( phi ) }, complex_meta )
  143. end
  144.  
  145. --// complex number functions only
  146.  
  147. -- complex.tostring( cx [, formatstr] )
  148. -- to string or real number
  149. -- takes a complex number and returns its string value or real number value
  150. function complex.tostring( cx,formatstr )
  151. local real,imag = cx[1],cx[2]
  152. if formatstr then
  153. if imag == 0 then
  154. return string.format( formatstr, real )
  155. elseif real == 0 then
  156. return string.format( formatstr, imag ).."i"
  157. elseif imag > 0 then
  158. return string.format( formatstr, real ).."+"..string.format( formatstr, imag ).."i"
  159. end
  160. return string.format( formatstr, real )..string.format( formatstr, imag ).."i"
  161. end
  162. if imag == 0 then
  163. return real
  164. elseif real == 0 then
  165. return ((imag==1 and "") or (imag==-1 and "-") or imag).."i"
  166. elseif imag > 0 then
  167. return real.."+"..(imag==1 and "" or imag).."i"
  168. end
  169. return real..(imag==-1 and "-" or imag).."i"
  170. end
  171.  
  172. -- complex.print( cx [, formatstr] )
  173. -- print a complex number
  174. function complex.print( ... )
  175. print( complex.tostring( ... ) )
  176. end
  177.  
  178. -- complex.polar( cx )
  179. -- from complex number to polar coordinates
  180. -- output in radians; [-pi,+pi]
  181. -- returns r (radius), phi (angle)
  182. function complex.polar( cx )
  183. return math.sqrt( cx[1]^2 + cx[2]^2 ), math.atan( cx[2], cx[1] )
  184. end
  185.  
  186. -- complex.polardeg( cx )
  187. -- from complex number to polar coordinates
  188. -- output in degrees; [-180°,180°]
  189. -- returns r (radius), phi (angle)
  190. function complex.polardeg( cx )
  191. return math.sqrt( cx[1]^2 + cx[2]^2 ), math.atan( cx[2], cx[1] ) / math.pi * 180
  192. end
  193.  
  194. -- complex.mulconjugate( cx )
  195. -- multiply with conjugate, function returning a number
  196. function complex.mulconjugate( cx )
  197. return cx[1]^2 + cx[2]^2
  198. end
  199.  
  200. -- complex.abs( cx )
  201. -- get the absolute value of a complex number
  202. function complex.abs( cx )
  203. return math.sqrt( cx[1]^2 + cx[2]^2 )
  204. end
  205.  
  206. -- complex.get( cx )
  207. -- returns real_part, imaginary_part
  208. function complex.get( cx )
  209. return cx[1],cx[2]
  210. end
  211.  
  212. -- complex.set( cx, real, imag )
  213. -- sets real_part = real and imaginary_part = imag
  214. function complex.set( cx,real,imag )
  215. cx[1],cx[2] = real,imag
  216. end
  217.  
  218. -- complex.is( cx, real, imag )
  219. -- returns true if, real_part = real and imaginary_part = imag
  220. -- else returns false
  221. function complex.is( cx,real,imag )
  222. if cx[1] == real and cx[2] == imag then
  223. return true
  224. end
  225. return false
  226. end
  227.  
  228. --// functions returning a new complex number
  229.  
  230. -- complex.copy( cx )
  231. -- copy complex number
  232. function complex.copy( cx )
  233. return setmetatable( { cx[1],cx[2] }, complex_meta )
  234. end
  235.  
  236. -- complex.add( cx1, cx2 )
  237. -- add two numbers; cx1 + cx2
  238. function complex.add( cx1,cx2 )
  239. return setmetatable( { cx1[1]+cx2[1], cx1[2]+cx2[2] }, complex_meta )
  240. end
  241.  
  242. -- complex.sub( cx1, cx2 )
  243. -- subtract two numbers; cx1 - cx2
  244. function complex.sub( cx1,cx2 )
  245. return setmetatable( { cx1[1]-cx2[1], cx1[2]-cx2[2] }, complex_meta )
  246. end
  247.  
  248. -- complex.mul( cx1, cx2 )
  249. -- multiply two numbers; cx1 * cx2
  250. function complex.mul( cx1,cx2 )
  251. return setmetatable( { cx1[1]*cx2[1] - cx1[2]*cx2[2],cx1[1]*cx2[2] + cx1[2]*cx2[1] }, complex_meta )
  252. end
  253.  
  254. -- complex.mulnum( cx, num )
  255. -- multiply complex with number; cx1 * num
  256. function complex.mulnum( cx,num )
  257. return setmetatable( { cx1[1]*cx2[1] - cx1[2]*cx2[2],cx1[1]*cx2[2] + cx1[2]*cx2[1] }, complex_meta )
  258. end
  259.  
  260. -- complex.mulnum( cx, num )
  261. -- multiply complex with number; cx1 * num
  262. function complex.mulnum( cx,num )
  263. return setmetatable( { cx[1]*num,cx[2]*num }, complex_meta )
  264. end
  265.  
  266. -- complex.div( cx1, cx2 )
  267. -- divide 2 numbers; cx1 / cx2
  268. function complex.div( cx1,cx2 )
  269. -- get complex value
  270. local val = cx2[1]^2 + cx2[2]^2
  271. -- multiply cx1 with conjugate complex of cx2 and divide through val
  272. return setmetatable( { (cx1[1]*cx2[1]+cx1[2]*cx2[2])/val,(cx1[2]*cx2[1]-cx1[1]*cx2[2])/val }, complex_meta )
  273. end
  274.  
  275. -- complex.divnum( cx, num )
  276. -- divide through a number
  277. function complex.divnum( cx,num )
  278. return setmetatable( { cx[1]/num,cx[2]/num }, complex_meta )
  279. end
  280.  
  281. -- complex.pow( cx, num )
  282. -- get the power of a complex number
  283. function complex.pow( cx,num )
  284. if math.floor( num ) == num then
  285. if num < 0 then
  286. local val = cx[1]^2 + cx[2]^2
  287. cx = { cx[1]/val,-cx[2]/val }
  288. num = -num
  289. end
  290. local real,imag = cx[1],cx[2]
  291. for i = 2,num do
  292. real,imag = real*cx[1] - imag*cx[2],real*cx[2] + imag*cx[1]
  293. end
  294. return setmetatable( { real,imag }, complex_meta )
  295. end
  296. -- we calculate the polar complex number now
  297. -- since then we have the versatility to calc any potenz of the complex number
  298. -- then we convert it back to a carthesic complex number, we loose precision here
  299. local length,phi = math.sqrt( cx[1]^2 + cx[2]^2 )^num, math.atan( cx[2], cx[1] )*num
  300. return setmetatable( { length * math.cos( phi ), length * math.sin( phi ) }, complex_meta )
  301. end
  302.  
  303. -- complex.sqrt( cx )
  304. -- get the first squareroot of a complex number, more accurate than cx^.5
  305. function complex.sqrt( cx )
  306. local len = math.sqrt( cx[1]^2+cx[2]^2 )
  307. local sign = (cx[2]<0 and -1) or 1
  308. return setmetatable( { math.sqrt((cx[1]+len)/2), sign*math.sqrt((len-cx[1])/2) }, complex_meta )
  309. end
  310.  
  311. -- complex.ln( cx )
  312. -- natural logarithm of cx
  313. function complex.ln( cx )
  314. return setmetatable( { math.log(math.sqrt( cx[1]^2 + cx[2]^2 )),
  315. math.atan( cx[2], cx[1] ) }, complex_meta )
  316. end
  317.  
  318. -- complex.exp( cx )
  319. -- exponent of cx (e^cx)
  320. function complex.exp( cx )
  321. local expreal = math.exp(cx[1])
  322. return setmetatable( { expreal*math.cos(cx[2]), expreal*math.sin(cx[2]) }, complex_meta )
  323. end
  324.  
  325. -- complex.conjugate( cx )
  326. -- get conjugate complex of number
  327. function complex.conjugate( cx )
  328. return setmetatable( { cx[1], -cx[2] }, complex_meta )
  329. end
  330.  
  331. -- complex.round( cx [,idp] )
  332. -- round complex numbers, by default to 0 decimal points
  333. function complex.round( cx,idp )
  334. local mult = 10^( idp or 0 )
  335. return setmetatable( { math.floor( cx[1] * mult + 0.5 ) / mult,
  336. math.floor( cx[2] * mult + 0.5 ) / mult }, complex_meta )
  337. end
  338.  
  339. --// metatable functions
  340.  
  341. complex_meta.__add = function( cx1,cx2 )
  342. local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 )
  343. return complex.add( cx1,cx2 )
  344. end
  345. complex_meta.__sub = function( cx1,cx2 )
  346. local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 )
  347. return complex.sub( cx1,cx2 )
  348. end
  349. complex_meta.__mul = function( cx1,cx2 )
  350. local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 )
  351. return complex.mul( cx1,cx2 )
  352. end
  353. complex_meta.__div = function( cx1,cx2 )
  354. local cx1,cx2 = complex.to( cx1 ),complex.to( cx2 )
  355. return complex.div( cx1,cx2 )
  356. end
  357. complex_meta.__pow = function( cx,num )
  358. if num == "*" then
  359. return complex.conjugate( cx )
  360. end
  361. return complex.pow( cx,num )
  362. end
  363. complex_meta.__unm = function( cx )
  364. return setmetatable( { -cx[1], -cx[2] }, complex_meta )
  365. end
  366. complex_meta.__eq = function( cx1,cx2 )
  367. if cx1[1] == cx2[1] and cx1[2] == cx2[2] then
  368. return true
  369. end
  370. return false
  371. end
  372. complex_meta.__tostring = function( cx )
  373. return tostring( complex.tostring( cx ) )
  374. end
  375. complex_meta.__concat = function( cx,cx2 )
  376. return tostring(cx)..tostring(cx2)
  377. end
  378. -- cx( cx, formatstr )
  379. complex_meta.__call = function( ... )
  380. print( complex.tostring( ... ) )
  381. end
  382. complex_meta.__index = {}
  383. for k,v in pairs( complex ) do
  384. complex_meta.__index[k] = v
  385. end
  386.  
  387. return complex
Advertisement
Add Comment
Please, Sign In to add comment