forked from pkulchenko/ZeroBraneEduPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.lua
1591 lines (1339 loc) · 49.8 KB
/
complex.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright (C) 2017 Deyan Dobromirov
-- A complex functionalities library
if not debug.getinfo(3) then
print("This is a module to load with `local complex = require('complex')`.")
os.exit(1)
end
local common = require("common")
local type = type
local math = math
local pcall = pcall
local tonumber = tonumber
local tostring = tostring
local getmetatable = getmetatable
local setmetatable = setmetatable
local table = table
local complex = {}
local metaComplex = {}
local metaData = {}
local isNil = common.isNil
local isBool = common.isBool
local isType = common.isType
local isTable = common.isTable
local isNumber = common.isNumber
local isString = common.isString
local isFunction = common.isFunction
local getPick = common.getPick
local getSign = common.getSign
local getRound = common.getRound
local getClamp = common.getClamp
local logStatus = common.logStatus
local logString = common.logString
local getSignNon = common.getSignNon
local getValueKeys = common.getValueKeys
local randomGetNumber = common.randomGetNumber
metaComplex.__type = "complex.complex"
metaComplex.__index = metaComplex
metaData.__valre = 0
metaData.__valim = 0
metaData.__cactf = {}
metaData.__ipmtx = {}
metaData.__valns = "X"
metaData.__curve = 100
metaData.__fulan = 360
metaData.__margn = 1e-10
metaData.__nanum = (0/0)
metaData.__basef = "%s,%s"
metaData.__getpi = math.pi
metaData.__expvl = math.exp(1)
metaData.__infum = math.huge
metaData.__fulpi = (2 * metaData.__getpi)
metaData.__bords = {"{([<|/","})]>|/"}
metaData.__ssyms = {"i", "I", "j", "J", "k", "K"}
metaData.__radeg = (180 / metaData.__getpi)
metaData.__kreal = {1,"Real","real","Re","re","R","r","X","x"}
metaData.__kimag = {2,"Imag","imag","Im","im","I","i","Y","y"}
function complex.extend()
metaData.__extlb = require("extensions").complex; return complex
end
function complex.isValid(cNum)
return (getmetatable(cNum) == metaComplex)
end
function complex.getType(cNum)
if(not cNum) then return metaComplex.__type end
local tM = getmetatable(cNum)
return ((tM and tM.__type) and tostring(tM.__type) or type(cNum))
end
function complex.setMargin(nM)
metaData.__margn = math.abs(tonumber(nM) or 0)
end
function complex.getMargin()
return metaData.__margn
end
local function getUnpackStack(R, I, E)
if(complex.isValid(R)) then
local nR, nI = R:getParts(); return nR, nI, I
elseif(isTable(R)) then
local nR = (getValueKeys(R, metaData.__kreal) or 0)
local nI = (getValueKeys(R, metaData.__kimag) or 0); return nR, nI, I
end; return (tonumber(R) or metaData.__valre), (tonumber(I) or metaData.__valim), E
end
function complex.getNew(nRe, nIm)
local Re, Im, self = 0, 0, {}
setmetatable(self, metaComplex)
if(complex.isValid(nRe)) then -- Copy-constructor
Re, Im = nRe:getParts()
elseif(isBool(nRe)) then -- Boolean to false=0/true=1
Re, Im = (nRe and 1 or 0), (nIm and 1 or 0)
elseif(isFunction(nRe)) then -- Function with table arguments nIm
local bS, nR, nI = pcall(nRe, nIm)
if(not bS) then return logStatus("complex.getNew: "..nR, nil) end
if(complex.isValid(nR)) then Re, Im = nR:getParts() else
Re = tonumber(nR) or metaData.__valre
Im = tonumber(nI) or metaData.__valim
end -- The function return value is not complex object
else -- Number or string conveted to number
Re = tonumber(nRe) or metaData.__valre
Im = tonumber(nIm) or metaData.__valim
end
function self:setReal(R) Re = (tonumber(R) or metaData.__valre); return self end
function self:setImag(I) Im = (tonumber(I) or metaData.__valim); return self end
function self:getReal() return Re end
function self:getImag() return Im end
function self:getParts() return Re, Im end
function self:Set(R, I)
local R, I = getUnpackStack(R, I)
Re, Im = R, I; return self
end
function self:Add(R, I)
local R, I = getUnpackStack(R, I)
Re, Im = (Re + R), (Im + I); return self
end
function self:Sub(R, I)
local R, I = getUnpackStack(R, I)
Re, Im = (Re - R), (Im - I); return self
end
function self:Rsz(vN) local nN = tonumber(vN)
if(nN) then Re, Im = (Re * nN), (Im * nN) end; return self
end
function self:Mul(R, I, E)
local C, D, U = getUnpackStack(R, I, E)
if(U) then Re, Im = (Re*C), (Im*D) else
Re, Im = (Re*C - Im*D), (Re*D + Im*C)
end; return self
end
function self:Mid(R, I)
local C, D = getUnpackStack(R, I)
Re, Im = ((Re + C) / 2), ((Im + D) / 2); return self
end
function self:Div(R, I, E)
local C, D, U = getUnpackStack(R, I, E)
if(U) then Re, Im = (Re/C), (Im/D) else local Z = (C*C + D*D)
Re, Im = ((Re*C + Im*D) / Z), ((Im*C - Re*D) / Z)
end; return self
end
function self:Frac(R, I)
local C, D = getUnpackStack(R, I)
local rI, rF = math.modf(R and C or Re)
local iI, iF = math.modf(R and D or Im)
Re, Im = rF, iF; return self
end
function self:Trunc(R, I, E)
local C, D, U = getUnpackStack(R, I, E)
local P = (U and math.ceil or math.floor)
local N = (U and math.floor or math.ceil )
C, D = (R and C or Re), (R and D or Im)
Re = (C > 0 and P(C) or N(C))
Im = (D > 0 and P(D) or N(D))
return self
end
function self:Mod(R, I, E)
local C, D, U = getUnpackStack(R, I, E)
if(U) then
Re, Im = (Re % C), (Im % D)
else local Z = self:getDiv(C, D)
return self:Sub(Z:Floor():Mul(C, D))
end
return self
end
function self:Rev()
local N = self:getNorm2()
Re, Im = (Re/N), (-Im/N); return self
end
function self:Pow(R, I, E)
local C, D, U = getUnpackStack(R, I, E)
if(U) then Re, Im = (Re^C), (Im^D) else
local N, G = self:getNorm2(), self:getAngRad()
local eK = N^(C/2) * math.exp(-D*G)
local eC = (C*G + 0.5*D*math.log(N))
Re, Im = (eK * math.cos(eC)), (eK * math.sin(eC))
end; return self
end
function self:Exp(R, I, E)
local X = metaData.__expvl
local C, D, U = getUnpackStack(R, I, E)
if(not R) then C, D = self:getParts() end
return self:setReal(X):setImag(0):Pow(C, D, U)
end
return self
end
function metaComplex:Action(aK,...)
if(not aK) then return false, self end
local fDr = metaData.__cactf[aK]; if(not common.isFunction(fDr)) then
return logStatus("complex.Action: No function", false), self end
return pcall(fDr,self,...)
end
function metaComplex:getNew(nR, nI)
local N = complex.getNew(self); if(nR or nI) then
local R, I = getUnpackStack(nR, nI); N:Set(R, I)
end; return N
end
function metaComplex:Random(nL, nU, vC)
local R = randomGetNumber(nL, nU, vC)
local I = randomGetNumber(nL, nU, vC)
return self:setReal(R):setImag(I)
end
function metaComplex:getRandom(...)
return self:getNew():Random(...)
end
function metaComplex:Apply(fF, bR, bI)
local R, I = self:getParts()
local br, sR, vR = getPick(isNil(bR), true, bR)
local bi, sI, vI = getPick(isNil(bI), true, bI)
if(br) then sR, vR = pcall(fF, R); if(not sR) then
return logStatus("complex.Apply(R): Failed: "..vR, self) end end
if(bi) then sI, vI = pcall(fF, I); if(not sI) then
return logStatus("complex.Apply(I): Failed: "..vI, self) end end
R, I = ((br) and vR or R), ((bi) and vI or I)
return self:setReal(R):setImag(I)
end
function metaComplex:getType () return metaComplex.__type end
function metaComplex:NegRe () return self:setReal(-self:getReal()) end
function metaComplex:NegIm () return self:setImag(-self:getImag()) end
function metaComplex:Conj () return self:NegIm() end
function metaComplex:Neg () return self:NegRe():NegIm() end
function metaComplex:getNegRe() return self:getNew():NegRe() end
function metaComplex:getNegIm() return self:getNew():NegIm() end
function metaComplex:getConj () return self:getNew():Conj() end
function metaComplex:getNeg () return self:getNew():Neg() end
function metaComplex:getNorm2()
local R, I = self:getParts(); return (R*R + I*I) end
function metaComplex:getNorm() return math.sqrt(self:getNorm2()) end
function metaComplex:setNorm(nN)
return self:Rsz((tonumber(nN) or 0) / self:getNorm())
end
function metaComplex:Unit(R, I)
if(R or I) then self:Set(R, I) end
return self:Rsz(1/self:getNorm())
end
function metaComplex:getUnit(...)
return self:getNew():Unit(...)
end
function metaComplex:getDot(R, I)
local sR, sI = self:getParts()
local vR, vI = getUnpackStack(R, I)
return (sR*vR + sI*vI)
end
function metaComplex:getAngRadVec(cV)
return (cV:getAngRad() - self:getAngRad())
end
function metaComplex:getMid(...)
return self:getNew():Mid(...)
end
function metaComplex:Mean(...) local tV = {...}
local fV, cV = tV[1], self:getNew(0,0)
if(isTable(fV)) then tV = tV[1] end
local nV = #tV; if(nV <= 0) then return self end
for iD = 1, nV do cV:Add(tV[iD]) end
return self:Set(cV:Rsz(1/nV))
end
function metaComplex:getMean(...)
return self:getNew():Mean(...)
end
function metaComplex:getDist2(R, I)
local C, D = self:getParts()
local R, I = getUnpackStack(R, I)
return ((C - R)^2 + (D - I)^2)
end
function metaComplex:getDist(R, I)
return math.sqrt(self:getDist2(R, I))
end
function metaComplex:getCross(R, I)
local C, D = self:getParts()
local R, I = getUnpackStack(R, I)
return (C*I - D*R)
end
function metaComplex:Sign(bE, bC, bN)
if(bE) then local R, I = self:getParts()
return ((R ~= 0) and getSign(R) or getSign(I)) end
if(bC) then return self:Apply(getSign) end
if(bN) then return self:Apply(getSignNon) end
return self:Unit()
end
function metaComplex:getSign(...)
return self:getNew():Sign(...)
end
function metaComplex:Swap()
local R, I = self:getParts()
return self:setReal(I):setImag(R)
end
function metaComplex:getSwap()
return self:getNew():Swap()
end
function metaComplex:Right()
return self:Swap():NegIm()
end
function metaComplex:getRight()
return self:getNew():Right()
end
function metaComplex:Left()
return self:Swap():NegRe()
end
function metaComplex:getLeft()
return self:getNew():Left()
end
function metaComplex:getSet(...)
return self:getNew():Set(...)
end
function metaComplex:getAdd(...)
return self:getNew():Add(...)
end
function metaComplex:getSub(...)
return self:getNew():Sub(...)
end
function metaComplex:getRsz(...)
return self:getNew():Rsz(...)
end
function metaComplex:getMul(...)
return self:getNew():Mul(...)
end
function metaComplex:getDiv(...)
return self:getNew():Div(...)
end
function metaComplex:getFrac(...)
return self:getNew():Frac(...)
end
function metaComplex:getTrunc(...)
return self:getNew():Trunc(...)
end
function metaComplex:getMod(...)
return self:getNew():Mod(...)
end
function metaComplex:getRev(...)
return self:getNew():Rev(...)
end
function metaComplex:getPow(...)
return self:getNew():Pow(...)
end
function metaComplex:getExp(...)
return self:getNew():Exp(...)
end
function metaComplex:AddPyth(...)
local cP = self:getNew(...)
return self:Pow(2):Add(cP:Pow(2)):Pow(0.5)
end
function metaComplex:getAddPyth(...)
return self:getNew():AddPyth(...)
end
function metaComplex:Margin(nM)
local nR, nI = self:getParts()
local nM = math.abs(tonumber(nM) or metaData.__margn)
if(math.abs(nR) < nM) then nR = 0 end
if(math.abs(nI) < nM) then nI = 0 end
return self:Set(nR, nI)
end
function metaComplex:getMargin(...)
return self:getNew():Margin(...)
end
function metaComplex:Deviation(sMsg, ...)
local tV, nV, cT, nM = {...}, 0, self:getNew(), metaData.__margn
local bC = complex.isValid(tV[1]); if(not bC) then tV = tV[1] end; nV = #tV
for iD = 1, nV do local nD = cT:Set(self):Sub(tV[iD]):getNorm(); if(nD > nM) then
logStatus("complex."..tostring(sMsg)..":"..tV[iD].."["..iD.."]: Displaced by "..nD) end
end; return self
end
function metaComplex:Nudge(vM, vR, vI)
local nM = (tonumber(vM) or metaData.__margn)
return self:Add(self:getUnit(vR, vI):Mul(nM))
end
function metaComplex:getNudge(...)
return self:getNew():Nudge(...)
end
function metaComplex:Bisect(...)
local cD = self:getNew(...)
local nS, nD = self:getNorm(), cD:getNorm()
return self:Mul(nD):Add(cD:Mul(nS))
end
function metaComplex:getBisect(...)
return self:getNew():Bisect(...)
end
function metaComplex:Sin()
local R, I = self:getParts()
local rE = math.sin(R)*math.cosh(I)
local iM = math.cos(R)*math.sinh(I)
return self:setReal(rE):setImag(iM)
end
function metaComplex:getSin()
return self:getNew():Sin()
end
function metaComplex:ArcSin()
local Z = self:getPow(2):Neg():Add(1):Pow(0.5)
return self:Mul(0, 1):Add(Z):Log():Mul(0, -1)
end
function metaComplex:getArcSin()
return self:getNew():ArcSin()
end
function metaComplex:Cos()
local R, I = self:getParts()
local rE = math.cos(R)*math.cosh(I)
local iM = -math.sin(R)*math.sinh(I)
return self:setReal(rE):setImag(iM)
end
function metaComplex:getCos()
return self:getNew():Cos()
end
function metaComplex:ArcCos()
local Z = self:getPow(2):Neg():Add(1):Pow(0.5)
return self:Add(Z:Mul(0, 1)):Log():Mul(0, -1)
end
function metaComplex:getArcCos()
return self:getNew():ArcCos()
end
function metaComplex:Tang()
local cC = self:getCos()
return self:Sin():Div(cC)
end
function metaComplex:getTang()
return self:getNew():Tang()
end
function metaComplex:ArcTang()
local cD = self:getAdd(0, 1)
local cN = self:getNeg():Add(0, 1)
return self:Set(cN:Div(cD):Log():Mul(0, -0.5))
end
function metaComplex:getArcTang()
return self:getNew():ArcTang()
end
function metaComplex:Cotg()
return self:Tang():Rev()
end
function metaComplex:getCotg()
return self:getNew():Cotg()
end
function metaComplex:ArcCotg()
return self:Rev():ArcTang()
end
function metaComplex:getArcCotg()
return self:getNew():ArcCotg()
end
function metaComplex:SinH()
local cE = self:getExp()
return self:Set(cE):Sub(cE:Rev()):Rsz(0.5)
end
function metaComplex:getSinH()
return self:getNew():SinH()
end
function metaComplex:ArcSinH()
local cZ = self:getPow(2):Add(1):Pow(0.5)
return self:Add(cZ):Log()
end
function metaComplex:getArcSinH()
return self:getNew():ArcSinH()
end
function metaComplex:CosH()
local cE = self:getExp()
return self:Set(cE):Add(cE:Rev()):Rsz(0.5)
end
function metaComplex:getCosH()
return self:getNew():CosH()
end
function metaComplex:ArcCosH()
local cP = self:getAdd(1):Pow(0.5)
local cN = self:getSub(1):Pow(0.5)
return self:Add(cP:Mul(cN)):Log()
end
function metaComplex:getArcCosH()
return self:getNew():ArcCosH()
end
function metaComplex:TangH()
local cC = self:getCosH()
return self:SinH():Div(cC)
end
function metaComplex:getTangH()
return self:getNew():TangH()
end
function metaComplex:ArcTangH()
local cN = self:getNeg():Add(1):Log()
return self:Add(1):Log():Sub(cN):Mul(0.5)
end
function metaComplex:getArcTangH()
return self:getNew():ArcTangH()
end
function metaComplex:CotgH()
return self:TangH():Rev()
end
function metaComplex:getCotgH()
return self:getNew():CotgH()
end
function metaComplex:ArcCotgH() self:Rev()
local cN = self:getNeg():Add(1):Log()
return self:Add(1):Log():Sub(cN):Mul(0.5)
end
function metaComplex:getArcCotgH()
return self:getNew():ArcCotgH()
end
function metaComplex:Log(nK)
local P, R, T = metaData.__getpi, self:getPolar()
return self:setReal(math.log(R)):setImag(T+2*(tonumber(nK) or 0)*P)
end
function metaComplex:getLog(...)
return self:getNew():Log(...)
end
function metaComplex:getApply(...)
return self:getNew():Apply(...)
end
function metaComplex:Abs(bR, bI)
return self:Apply(math.abs, bR, bI)
end
function metaComplex:getAbs(...)
return self:getNew():Abs(...)
end
function metaComplex:Floor(bR, bI)
return self:Apply(math.floor, bR, bI)
end
function metaComplex:getFloor(...)
return self:getNew():Floor(...)
end
function metaComplex:Ceil(bR, bI)
return self:Apply(math.ceil, bR, bI)
end
function metaComplex:getCeil(...)
return self:getNew():Ceil(...)
end
function metaComplex:getAngRad()
local R, I = self:getParts(); return math.atan2(I, R) end
function metaComplex:getTable(kR, kI)
local kR, kI = (kR or metaData.__kreal[1]), (kI or metaData.__kimag[1])
local R , I = self:getParts(); return {[kR] = R, [kI] = I}
end
function metaComplex:Println(sF,sS,sE)
local nR, nI = self:getParts()
local fB, sF = metaData.__basef, tostring(sF or "%f")
local sS, sE = tostring(sS or "{"), tostring(sE or "}")
return logStatus(sS..(fB:format(sF,sF):format(nR,nI))..sE, self)
end
function metaComplex:Print(sF,sS,sE)
local nR, nI = self:getParts()
local fB, sF = metaData.__basef, tostring(sF or "%f")
local sS, sE = tostring(sS or "{"), tostring(sE or "}")
return logString(sS..(fB:format(sF,sF):format(nR,nI))..sE, self)
end
function metaComplex:Euler(vR, vA)
if(vR or vA) then self:Set(vR, vA) end
local nR, vA = self:getParts()
return self:Set(math.cos(vA), math.sin(vA)):Rsz(nR)
end
function metaComplex:getEuler(...)
return self:getNew():Euler(...)
end
function metaComplex:Round(nF)
local R, I = self:getParts()
return self:setReal(getRound(R, nF)):setImag(getRound(I, nF))
end
function metaComplex:getRound(...)
return self:getNew():Round(...)
end
function metaComplex:getPolar()
return self:getNorm(), self:getAngRad()
end
function metaComplex:setAngRad(nA)
local nR, nP = self:getNorm(), (tonumber(nA) or 0)
return self:setReal(math.cos(nP)):setImag(math.sin(nP)):Rsz(nR)
end
function metaComplex:RotRad(nA)
return self:setAngRad(self:getAngRad() + (tonumber(nA) or 0))
end
function metaComplex:getRotRad(...)
return self:getNew():RotRad(...)
end
function metaComplex:setPolarRad(nN, nA)
return self:Set((tonumber(nN) or 0), 0):setAngRad(nA)
end
function metaComplex:ProjectRay(cO, cD)
local cV = self:getSub(cO)
local nK = cV:getCross(cD) / cD:getNorm2()
return self:Add(cD:getMul(nK, -nK, true):Swap())
end
function metaComplex:getProjectRay(...)
return self:getNew():ProjectRay(...)
end
function metaComplex:ProjectLine(cS, cE)
return self:ProjectRay(cS, cE:getSub(cS))
end
function metaComplex:getProjectLine(...)
return self:getNew():ProjectLine(...)
end
function metaComplex:ProjectCircle(cC, nR)
return self:Sub(cC):Unit():Mul(nR):Add(cC)
end
function metaComplex:getProjectCircle(...)
return self:getNew():ProjectCircle(...)
end
function metaComplex:getLayRay(cO, cD)
return self:getSub(cO):getCross(cD)
end
function metaComplex:getLayLine(cS, cE)
return self:getLayRay(cS, cE:getSub(cS))
end
function metaComplex:MirrorPoint(...)
return self:Add(self:getSet(...):Sub(self):Rsz(2))
end
function metaComplex:getMirrorPoint(...)
return self:getNew():MirrorPoint(...)
end
function metaComplex:MirrorRay(cO, cD)
local cP = self:getProjectRay(cO, cD)
return self:Add(cP:Sub(self):Rsz(2))
end
function metaComplex:getMirrorRay(...)
return self:getNew():MirrorRay(...)
end
function metaComplex:MirrorLine(cS, cE)
return self:MirrorRay(cS, cE:getSub(cS))
end
function metaComplex:getMirrorLine(...)
return self:getNew():MirrorLine(...)
end
function metaComplex:getAreaParallelogram(...)
return math.abs(self:getCross(...))
end
function metaComplex:getAreaTriangle(...)
return math.abs(self:getAreaParallelogram(...) / 2)
end
function complex.getAreaShoelace(...)
local tV, nP, nN = {...}, 0, 0
if(isTable(tV[1])) then tV = tV[1] end
local nE = #tV; tV[nE+1] = tV[1]
for ID = 1, nE do
local cB, cN = tV[ID], tV[ID+1]
nP = nP + (cB:getReal()*cN:getImag())
nN = nN + (cB:getImag()*cN:getReal())
end; return math.abs(0.5 * (nP - nN))
end
function complex.getAreaHeron(...)
local tV = {...}; if(isTable(tV[1])) then tV = tV[1] end
local nV = #tV; if(nV < 3) then local sV = tostring(nV or "")
return logStatus("complex.getAreaHeron: Vertexes lacking <"..sV..">", 0) end
if(nV > 3) then local sV = tostring(nV or "")
logStatus("complex.getAreaHeron: Vertexes extra <"..sV..">") end
local nA = tV[1]:getSub(tV[2]):getNorm2()
local nB = tV[2]:getSub(tV[3]):getNorm2()
local nC = tV[3]:getSub(tV[1]):getNorm2()
local nD = (4 * (nA*nB + nA*nC + nB*nC))
local nE = ((nA + nB + nC)^2)
return math.abs(0.25 * math.sqrt(nD - nE))
end
function metaComplex:isAmongLine(cS, cE, bF)
local nM = metaData.__margn
if(math.abs(self:getLayLine(cS, cE)) < nM) then
local dV = cE:getSub(cS)
local dS = self:getSub(cS):getDot(dV)
local dE = self:getSub(cE):getDot(dV)
if(not bF and dS * dE > 0) then return false end
return true
end; return false
end
function metaComplex:isAmongPoint(vR, vI)
return (self:getSub(vR, vI):getNorm() < metaData.__margn)
end
function metaComplex:Zero()
return self:setReal(0):setImag(0)
end
function metaComplex:getZero()
return self:getNew():Zero()
end
function metaComplex:isZeroReal()
return (math.abs(self:getReal()) < metaData.__margn)
end
function metaComplex:isZeroImag()
return (math.abs(self:getImag()) < metaData.__margn)
end
function metaComplex:isZero(bR, bI)
local bR = getPick(isNil(bR), true, bR)
local bI = getPick(isNil(bI), true, bI)
local zR, zI = self:isZeroReal(), self:isZeroImag()
if(bR and bI) then return (zR and zI) end
if(bR) then return zR end; if(bI) then return zI end
return logStatus("complex.isZero: Not applicable", nil)
end
function metaComplex:isInfReal(bR)
local mH, nR = metaData.__infum, self:getReal()
if(bR) then return (nR == -mH) end
return (nR == mH)
end
function metaComplex:isInfImag(bI)
local mH, nI = metaData.__infum, self:getImag()
if(bI) then return (nI == -mH) end
return (nI == mH)
end
function metaComplex:isInf(bR, bI)
return (self:isInfReal(bR) and self:isInfImag(bI))
end
function metaComplex:Inf(bR, bI)
local nH, sR, sI = metaData.__infum, self:getParts()
local nR = getPick(isNil(bR), sR, getPick(bR, -nH, nH))
local nI = getPick(isNil(bI), sI, getPick(bI, -nH, nH))
return self:setReal(nR):setImag(nI)
end
function metaComplex:getInf(...)
return self:getNew():Inf(...)
end
function metaComplex:isNanReal()
local nR = self:getReal(); return (nR ~= nR)
end
function metaComplex:isNanImag()
local nI = self:getImag(); return (nI ~= nI)
end
function metaComplex:isNan()
return (self:isNanReal() and self:isNanImag())
end
function metaComplex:Nan(bR, bI)
local sR, sI = self:getParts()
local nN = metaData.__nanum
local nR = getPick(bR, nN, sR)
local nI = getPick(bI, nN, sI)
return self:setReal(nR):setImag(nI)
end
function metaComplex:getNan(...)
return self:getNew():Nan(...)
end
function metaComplex:isAmongRay(cO, cD, bF)
local nM = metaData.__margn
if(math.abs(self:getLayRay(cO, cD)) < nM) then
local dO = self:getSub(cO):getDot(cD)
local dE = cO:getAdd(cD):Sub(self):Neg():getDot(cD)
if(dO < 0 and dE < 0) then return false end
if(not bF and dO > 0 and dE > 0) then return false end
return true
end; return false
end
function metaComplex:isOrthogonal(vC)
return (math.abs(self:getDot(vC)) < metaData.__margn)
end
function metaComplex:isCollinear(vC)
return (math.abs(self:getCross(vC)) < metaData.__margn)
end
function metaComplex:isInCircle(cO, vR)
local nM = metaData.__margn
local nR = getClamp(tonumber(vR) or 0, 0)
local nN = self:getSub(cO):getNorm()
return (nN < (nR+nM))
end
function metaComplex:isAmongCircle(cO, vR)
local nM = metaData.__margn
local nN = self:getSub(cO):getNorm()
local nR = getClamp(tonumber(vR) or 0, 0)
return ((nN < (nR+nM)) and (nN > (nR-nM)))
end
function metaComplex:getRoots(nNm)
local nN = math.floor(tonumber(nNm) or 0)
if(nN > 0) then local tRt = {}
local nPw, nA = (1 / nN), (metaData.__fulpi / nN)
local nRa = self:getNorm() ^ nPw
local nAn = self:getAngRad() * nPw
for k = 1, nN do
local cRe, cIm = (nRa * math.cos(nAn)), (nRa * math.sin(nAn))
tRt[k], nAn = self:getNew(cRe,cIm), (nAn + nA)
end; return tRt
end; return logStatus("complex.getRoots: Invalid <"..nN..">")
end
function metaComplex:getFormat(...)
local tArg = {...}
local sMod = tostring(tArg[1] or ""):lower()
if(isType(sMod, 5)) then
local tvB = metaData.__bords
local tkR, tkI = metaData.__kreal, metaData.__kimag
local sN, R, I = tostring(tArg[3] or "%f"), self:getParts()
local iS = math.floor((tvB[1]..tvB[2]):len()/2)
iB = getClamp(tonumber(tArg[4] or 1), 1, iS)
local eS = math.floor((#tkR + #tkI)/2)
iD = getClamp((tonumber(tArg[2]) or 1), 1, eS)
local sF, sB = tvB[1]:sub(iB,iB), tvB[2]:sub(iB,iB)
local kR, kI = (tArg[5] or tkR[iD]), (tArg[6] or tkI[iD])
if(not (kR and kI)) then return tostring(self) end
local qR, qI = isString(kR), isString(kI)
kR = qR and ("\""..kR.."\"") or tostring(kR)
kI = qI and ("\""..kI.."\"") or tostring(kI)
return (sF.."["..kR.."]="..sN:format(R)..
",["..kI.."]="..sN:format(I)..sB)
elseif(isType(sMod, 3)) then
local S, R, I = metaData.__ssyms, self:getParts()
local mI, bS = (getSign(I) * I), tArg[3]
local iD = getClamp(tonumber(tArg[2]) or 1, 1, #S)
local kI = tostring(tArg[4] or S[iD])
local sI = ((getSign(I) < 0) and "-" or "+")
if(bS) then return (R..sI..kI..mI)
else return (R..sI..mI..kI) end
end; return tostring(self)
end
metaComplex.__len = function(cNum) return cNum:getNorm() end
metaComplex.__call = function(cNum, sMth, ...)
return pcall(cNum[tostring(sMth)], cNum, ...)
end
metaComplex.__tostring = function(cNum)
local R = tostring(cNum:getReal() or metaData.__valns)
local I = tostring(cNum:getImag() or metaData.__valns)
return "{"..R..","..I.."}"
end
metaComplex.__unm = function(cNum)
return complex.getNew(cNum):Neg()
end
metaComplex.__add = function(C1,C2)
return complex.getNew(C1):Add(C2)
end
metaComplex.__sub = function(C1,C2)
return complex.getNew(C1):Sub(C2)
end
metaComplex.__mul = function(C1,C2)
return complex.getNew(C1):Mul(C2)
end
metaComplex.__div = function(C1,C2)
return complex.getNew(C1):Div(C2)
end
metaComplex.__mod = function(C1,C2)
return complex.getNew(C1):Mod(C2)
end
metaComplex.__pow = function(C1,C2)
return complex.getNew(C1):Pow(C2)
end
metaComplex.__concat = function(A,B)
return tostring(A)..tostring(B)
end
metaComplex.__eq = function(C1,C2)
local R1, I1 = getUnpackStack(C1)
local R2, I2 = getUnpackStack(C2)
if(R1 == R2 and I1 == I2) then return true end
return false