forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcomplex.cc
1316 lines (1154 loc) · 40.8 KB
/
complex.cc
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
// ****************************************************************************
// complex.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Complex numbers
//
// There are two representations for complex numbers:
// - rectangular representation is one of X;Y, X+ⅈY, X-ⅈY, X+Yⅈ or X-Yⅈ
// - polar representation is X∡Y, where X≥0 and Y is a ratio of π
//
// Some settings control how complex numbers are rendered.
//
//
//
// ****************************************************************************
// (C) 2023 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "complex.h"
#include "arithmetic.h"
#include "compare.h"
#include "functions.h"
#include "parser.h"
#include "renderer.h"
#include "runtime.h"
#include "tag.h"
#include "unit.h"
#include "utf8.h"
// ============================================================================
//
// Generic complex operations
//
// ============================================================================
//
// The generic operations optimize for the most efficient operation
// if there is a difference between rectangular and polar
//
SIZE_BODY(complex)
// ----------------------------------------------------------------------------
// Size of a complex number
// ----------------------------------------------------------------------------
{
object_p p = object_p(payload(o));
object_p e = p->skip()->skip();
return byte_p(e) - byte_p(o);
}
HELP_BODY(complex)
// ----------------------------------------------------------------------------
// Help topic for complex numbers
// ----------------------------------------------------------------------------
{
return utf8("Complex numbers");
}
algebraic_g complex::re() const
// ----------------------------------------------------------------------------
// Return real part in a format-independent way
// ----------------------------------------------------------------------------
{
if (type() == ID_rectangular)
return rectangular_p(this)->re();
return polar_p(this)->re();
}
algebraic_g complex::im() const
// ----------------------------------------------------------------------------
// Return imaginary part in a format-independent way
// ----------------------------------------------------------------------------
{
if (type() == ID_rectangular)
return rectangular_p(this)->im();
return polar_p(this)->im();
}
algebraic_g complex::mod() const
// ----------------------------------------------------------------------------
// Return modulus in a format-independant way
// ----------------------------------------------------------------------------
{
if (type() == ID_polar)
return polar_p(this)->mod();
return rectangular_p(this)->mod();
}
algebraic_g complex::arg(complex::angle_unit unit) const
// ----------------------------------------------------------------------------
// Return argument in a format-independant way
// ----------------------------------------------------------------------------
{
if (type() == ID_polar)
return polar_p(this)->arg(unit);
return rectangular_p(this)->arg(unit);
}
algebraic_g complex::pifrac() const
// ----------------------------------------------------------------------------
// Return argument as pi fraction in a format-independant way
// ----------------------------------------------------------------------------
{
if (type() == ID_polar)
return polar_p(this)->pifrac();
return rectangular_p(this)->pifrac();
}
algebraic_p complex::is_real() const
// ----------------------------------------------------------------------------
// Check if the complex is a purely real value
// ----------------------------------------------------------------------------
{
if (type() == ID_polar)
return polar_p(this)->is_real();
return rectangular_p(this)->is_real();
}
complex_g complex::conjugate() const
// ----------------------------------------------------------------------------
// Return complex conjugate in a format-independent way
// ----------------------------------------------------------------------------
{
return make(type(), x(), -y(), ID_PiRadians);
}
complex_p complex::make(id type, algebraic_r x, algebraic_r y, angle_unit aunit)
// ----------------------------------------------------------------------------
// Build a complex of the right type
// ----------------------------------------------------------------------------
{
if (!x|| !y)
return nullptr;
if (type == ID_polar)
return polar::make(x, y, aunit);
else if (type == ID_unit)
return unit::make(x, y);
return rectangular::make(x, y);
}
rectangular_p complex::make(int re, int im)
// ----------------------------------------------------------------------------
// Build a simple complex constant
// ----------------------------------------------------------------------------
{
return rectangular_p(make(ID_rectangular,
integer::make(re), integer::make(im),
ID_PiRadians));
}
complex_g operator-(complex_r x)
// ----------------------------------------------------------------------------
// Unary minus
// ----------------------------------------------------------------------------
{
if (!x)
return nullptr;
if (x->type() == object::ID_polar)
{
polar_r p = polar_r(x);
return polar::make(-p->mod(), p->pifrac(), object::ID_PiRadians);
}
rectangular_r r = rectangular_r(x);
return rectangular::make(-r->re(), -r->im());
}
complex_g operator+(complex_r x, complex_r y)
// ----------------------------------------------------------------------------
// Complex addition - In rectangular form, unless polar args are aligned
// ----------------------------------------------------------------------------
{
if (!x|| !y)
return nullptr;
if (x->type() == object::ID_polar &&
y->type() == object::ID_polar)
{
algebraic_g two = integer::make(2);
algebraic_g angle_diff = (x->y() - y->y()) % two;
if (angle_diff->is_zero(false))
return polar::make(x->x() + y->x(), x->y(), object::ID_PiRadians);
if (angle_diff->is_one(false))
return polar::make(x->x() - y->x(), x->y(), object::ID_PiRadians);
}
return rectangular::make(x->re() + y->re(), x->im() + y->im());
}
complex_g operator-(complex_r x, complex_r y)
// ----------------------------------------------------------------------------
// Complex subtraction - In rectangular form, unless polar args are aligned
// ----------------------------------------------------------------------------
{
if (!x|| !y)
return nullptr;
if (x->is_zero())
return -y;
if (y->is_zero())
return x;
if (x->type() == object::ID_polar &&
y->type() == object::ID_polar)
{
algebraic_g two = integer::make(2);
algebraic_g angle_diff = (x->y() - y->y()) % two;
if (angle_diff->is_zero(false))
return polar::make(x->x() - y->x(), x->y(), object::ID_PiRadians);
if (angle_diff->is_one(false))
return polar::make(x->x() + y->x(), x->y(), object::ID_PiRadians);
}
return rectangular::make(x->re() - y->re(), x->im() - y->im());
}
complex_g operator*(complex_r x, complex_r y)
// ----------------------------------------------------------------------------
// If both are in rectangular form, rectangular, otherwise polar
// ----------------------------------------------------------------------------
{
if (!x|| !y)
return nullptr;
object::id xt = x->type();
object::id yt = y->type();
if (xt != object::ID_rectangular || yt != object::ID_rectangular)
return polar::make(x->mod() * y->mod(),
x->pifrac() + y->pifrac(),
object::ID_PiRadians);
rectangular_p xx = rectangular_p(complex_p(x));
rectangular_p yy = rectangular_p(complex_p(y));
algebraic_g xr = xx->re();
algebraic_g xi = xx->im();
algebraic_g yr = yy->re();
algebraic_g yi = yy->im();
return rectangular::make(xr*yr-xi*yi, xr*yi+xi*yr);
}
complex_g operator/(complex_r x, complex_r y)
// ----------------------------------------------------------------------------
// Like for multiplication, it's slighly cheaper in polar form
// ----------------------------------------------------------------------------
{
if (!x|| !y)
return nullptr;
object::id xt = x->type();
object::id yt = y->type();
if (xt != object::ID_rectangular || yt != object::ID_rectangular)
return polar::make(x->mod() / y->mod(),
x->pifrac() - y->pifrac(),
object::ID_PiRadians);
rectangular_p xx = rectangular_p(complex_p(x));
rectangular_p yy = rectangular_p(complex_p(y));
algebraic_g a = xx->re();
algebraic_g b = xx->im();
algebraic_g c = yy->re();
algebraic_g d = yy->im();
algebraic_g r = sq::run(c) + sq::run(d);
return rectangular::make((a*c+b*d)/r, (b*c-a*d)/r);
}
polar_g complex::as_polar() const
// ----------------------------------------------------------------------------
// Switch to polar form if preferred for computation
// ----------------------------------------------------------------------------
{
if (type() == ID_rectangular)
{
rectangular_g r = rectangular_p(this);
return polar::make(r->mod(), r->pifrac(), object::ID_PiRadians);
}
return polar_p(this);
}
rectangular_g complex::as_rectangular() const
// ----------------------------------------------------------------------------
// Switch to rectangular form if preferred for computation
// ----------------------------------------------------------------------------
{
if (type() == ID_polar)
{
polar_g r = polar_p(this);
return rectangular::make(r->re(), r->im());
}
return rectangular_p(this);
}
// ============================================================================
//
// Specific code for rectangular form
//
// ============================================================================
PARSE_BODY(rectangular)
// ----------------------------------------------------------------------------
// Parse the rectangular forms of complex number
// ----------------------------------------------------------------------------
// We accept the following formats:
// a. (1;3) Classic RPL
// b. (1 3) Classic RPL
// c. 1ⅈ3 ⅈ as a separator
// d. 1+ⅈ3 ⅈ as a prefix
// e. 1-ⅈ3
// f. 1+3ⅈ ⅈ as a postfix
// g. 1-3ⅈ
// h. 1∡30 ∡ as a separator
// i. ⅈ Imaginary unit by itself
// j. 3.5ⅈ
// u. 1_km _ as a separator for unit objects
//
// Cases a-g generate a rectangular form, case i generates a polar form
// Cases c-h can be surrounded by parentheses as well
//
// In case (a), we do not accept (1,3) which classic RPL would accept,
// because in DB48X 1,000.000 is a valid real number with thousands separator
{
// Check if we have a real part
algebraic_g re = algebraic_p(+p.out);
if (re && re->is_complex())
return ERROR;
size_t max = p.length;
if (!max)
return SKIP;
// First character must be compatible with a rectangular complex value
size_t offs = 0;
unicode cp = utf8_codepoint(p.source + offs);
bool neg = false;
// Cases 'a' and 'b'
if (!re && cp == '(')
{
offs++;
// Parse the real part
size_t resz = max - offs;
object_p reobj = parse(p.source + offs, resz, p.precedence);
if (!reobj)
return SKIP;
if (!reobj->is_algebraic())
return SKIP;
re = algebraic_p(reobj);
offs += resz;
// Skip ';' if it's there. Also accept pure whitespace (case b)
size_t spaces = utf8_skip_whitespace(p.source + offs, max - offs);
offs += spaces;
cp = utf8_codepoint(p.source + offs);
if (cp == ';')
{
offs++;
offs += utf8_skip_whitespace(p.source + offs, max - offs);
cp = utf8_codepoint(p.source + offs);
spaces = 1;
}
// If we have a parenthese here, we are done, return that
if (cp == ')' || !spaces)
{
if (!p.precedence)
return SKIP;
p.out = +re;
p.length = offs+1;
return OK;
}
// Parse the imaginary part
size_t imsz = max - offs;
object_p imobj = parse(p.source + offs, imsz);
if (!imobj)
return ERROR;
if (!imobj->is_algebraic())
return SKIP;
offs += imsz;
// Check that we end up on a closing parenthese
offs += utf8_skip_whitespace(p.source + offs, max - offs);
cp = utf8_codepoint(p.source + offs);
if (cp != ')')
return SKIP;
algebraic_g im = algebraic_p(imobj);
p.out = rectangular::make(re, im);
p.length = offs+1;
return p.out ? OK : ERROR;
}
// Cases c, d, e or f
bool hadsign = re && (cp == '+' || cp == '-');
if (hadsign)
{
neg = cp == '-';
if (max <= 1)
return SKIP;
offs = utf8_next(p.source, offs, max);
cp = utf8_codepoint(p.source + offs);
}
bool imark = cp == I_MARK;
if (imark)
{
offs = utf8_next(p.source, offs, max);
cp = utf8_codepoint(p.source + offs);
}
bool sp = utf8_whitespace(cp);
size_t imsz = max - offs;
object_p imobj = sp ? nullptr : parse(p.source + offs, imsz, PARENTHESES);
if (!imobj)
{
rt.clear_error();
if (!imark)
return SKIP;
// Case i or 3+i: We only got the imaginary mark
algebraic_g im = re ? +re : algebraic_p(integer::make(neg ? -1 : 1));
re = integer::make(0);
p.out = rectangular::make(re, im);
p.length = offs;
return p.out ? OK : ERROR;
}
algebraic_g im = imobj->as_algebraic();
if (!im)
return SKIP; // Case of 3+"Hello"
offs += imsz;
if (!imark)
{
// Check case f and g
cp = utf8_codepoint(p.source + offs);
if (cp != I_MARK)
return SKIP; // Case of something like 3+3
offs = utf8_next(p.source, offs, max);
}
// Check cases like `i3` or `3i`
if (!re)
re = integer::make(0);
if (neg)
im = neg::run(im);
p.out = rectangular::make(re, im);
p.length = offs;
return p.out ? OK : ERROR;
}
algebraic_g rectangular::mod() const
// ----------------------------------------------------------------------------
// Compute the modulus in rectangular form
// ----------------------------------------------------------------------------
{
rectangular_g o = this;
algebraic_g r = o->re();
algebraic_g i = o->im();
return hypot::evaluate(r, i);
}
algebraic_g rectangular::arg(angle_unit unit) const
// ----------------------------------------------------------------------------
// Compute the argument in rectangular form
// ----------------------------------------------------------------------------
{
algebraic_g a = pifrac(); // Compute "exact" angle
a = convert_angle(a, object::ID_PiRadians, unit);
return a;
}
algebraic_g rectangular::pifrac() const
// ----------------------------------------------------------------------------
// Compute the argument as a fraction of pi
// ----------------------------------------------------------------------------
{
rectangular_g o = this;
algebraic_g r = o->re();
algebraic_g i = o->im();
if (!r || !i)
return nullptr;
settings::SaveAngleMode sam(ID_PiRadians); // Enable 'exact' optimizations
settings::SaveSetAngleUnits ssau(false); // Do not add angle to result
algebraic_g a = atan2::evaluate(i, r);
return a;
}
bool rectangular::is_zero() const
// ----------------------------------------------------------------------------
// A complex in rectangular form is zero iff both re and im are zero
// ----------------------------------------------------------------------------
{
rectangular_g o = this;
return o->re()->is_zero(false) && o->im()->is_zero(false);
}
bool rectangular::is_one() const
// ----------------------------------------------------------------------------
// A complex in rectangular form is zero iff both re and im are zero
// ----------------------------------------------------------------------------
{
rectangular_g o = this;
return o->re()->is_one(false) && o->im()->is_zero(false);
}
algebraic_p rectangular::is_real() const
// ----------------------------------------------------------------------------
// Check if the complex is a purely real value
// ----------------------------------------------------------------------------
{
return y()->is_zero(false) ? x() : nullptr;
}
RENDER_BODY(rectangular)
// ----------------------------------------------------------------------------
// Render a complex number in rectangular form
// ----------------------------------------------------------------------------
{
rectangular_g go = o;
algebraic_g re = go->re();
algebraic_g im = go->im();
if (!re || !im)
return r.printf("Invalid rectangular");
bool ifirst = r.editing() || Settings.ComplexIBeforeImaginary();
bool neg = im->is_negative(false);
if (neg)
im = -im;
re->render(r);
r.put(neg ? '-' : '+');
if (ifirst)
r.put(unicode(I_MARK));
im->render(r);
if (!ifirst)
r.put(unicode(I_MARK));
return r.size();
}
// ============================================================================
//
// Polar-specific code
//
// ============================================================================
//
// In the polar representation, the unit is always stored as a ratio of π.
// For example, the internal representation of the imaginary unit is (1;1),
// where the second 1 represents the angle π in radians.
// This makes it possible to have an exact and compact representation of
// common angles, like 1/4π, etc.
// When the argument is symbolic, it is not transformed. The assumption is
// that it represents an angle, irrespective of the angular unit.
PARSE_BODY(polar)
// ----------------------------------------------------------------------------
// Parsing
// ----------------------------------------------------------------------------
// We accept the following formats:
// a. (1;3) Classic RPL
// b. (1 3) Classic RPL
// c. 1ⅈ3 ⅈ as a separator
// d. 1+ⅈ3 ⅈ as a prefix
// e. 1-ⅈ3
// f. 1+3ⅈ ⅈ as a postfix
// g. 1-3ⅈ
// h. 1∡30 ∡ as a separator
// i. ⅈ Imaginary unit by itself
// j. ∡30 ∡ to indicate unit complex number
// u. 1_km _ as a separator for unit objects
//
// Cases a-g generate a rectangular form, case i generates a polar form
// Cases c-h can be surrounded by parentheses as well
//
// In case (a), we do not accept (1,3) which classic RPL would accept,
// because in DB48X 1,000.000 is a valid real number with thousands separator
{
// Check if we have a real part
algebraic_g mod = algebraic_p(+p.out);
if (mod && mod->is_complex())
return ERROR;
size_t max = p.length;
if (!max)
return SKIP;
// First character must be compatible with a rectangular complex value
size_t offs = 0;
unicode cp = p.separator;
bool amark = cp == ANGLE_MARK;
if (!amark)
return SKIP;
offs = utf8_next(p.source, offs, max);
size_t imsz = max - offs;
object_p imobj = parse(p.source + offs, imsz, 0, p.separator);
if (!imobj)
{
if (!rt.error())
rt.syntax_error().source(p.source+offs, imsz);
return ERROR; // Case of ∡ by itself
}
algebraic_g im = imobj->as_algebraic();
if (!im)
return SKIP; // Case of 3∡"Hello"
offs += imsz;
// Check cases like `∡3`, which is interpreted as 1∡3
if (!mod)
mod = integer::make(1);
angle_unit unit = Settings.AngleMode();
bool has_unit = true;
cp = offs < max ? utf8_codepoint(p.source + offs) : 0;
switch(cp)
{
case Settings.DEGREES_SYMBOL: unit = ID_Deg; break;
case Settings.RADIANS_SYMBOL: unit = ID_Rad; break;
case Settings.GRAD_SYMBOL: unit = ID_Grad; break;
case Settings.PI_RADIANS_SYMBOL: unit = ID_PiRadians; break;
default: has_unit = false; break;
}
if (has_unit)
offs = utf8_next(p.source, offs, max);
p.out = polar::make(mod, im, unit);
p.length = offs;
return p.out ? OK : ERROR;
}
algebraic_g polar::re() const
// ----------------------------------------------------------------------------
// Compute the real part in polar form
// ----------------------------------------------------------------------------
{
polar_g o = this;
algebraic_g m = o->mod();
algebraic_g a = o->arg(Settings.AngleMode());
return m * cos::run(a);
}
algebraic_g polar::im() const
// ----------------------------------------------------------------------------
// Compute the imaginary part in polar form
// ----------------------------------------------------------------------------
{
polar_g o = this;
algebraic_g m = o->mod();
algebraic_g a = o->arg(Settings.AngleMode());
return m * sin::run(a);
}
bool polar::is_zero() const
// ----------------------------------------------------------------------------
// A complex in polar form is zero iff modulus is zero
// ----------------------------------------------------------------------------
{
polar_g o = this;
return o->mod()->is_zero(false);
}
bool polar::is_one() const
// ----------------------------------------------------------------------------
// A complex in rectangular form is zero iff both re and im are zero
// ----------------------------------------------------------------------------
{
polar_g o = this;
return o->mod()->is_one(false) && o->pifrac()->is_zero();
}
algebraic_p polar::is_real() const
// ----------------------------------------------------------------------------
// Check if the complex is a purely real value
// ----------------------------------------------------------------------------
{
polar_g o = this;
algebraic_g pifrac = o->pifrac();
if (pifrac->is_zero(false))
return o->mod();
if (pifrac->is_one(false))
return -o->mod();
return nullptr;
}
polar_p polar::make(algebraic_r mr, algebraic_r ar, angle_unit unit)
// ----------------------------------------------------------------------------
// Build a normalized polar from given modulus and argument
// ----------------------------------------------------------------------------
{
if (!mr|| !ar)
return nullptr;
algebraic_g m = mr;
algebraic_g a = ar;
bool negmod = m->is_negative(false);
a = convert_angle(a, unit, object::ID_PiRadians, negmod);
if (negmod)
m = neg::run(m);
if (!a|| !m)
return nullptr;
return rt.make<polar>(m, a);
}
algebraic_g polar::mod() const
// ----------------------------------------------------------------------------
// The modulus of a polar complex is always its first item
// ----------------------------------------------------------------------------
{
return x();
}
algebraic_g polar::arg(angle_unit unit) const
// ----------------------------------------------------------------------------
// Convert the argument to the current angle setting
// ----------------------------------------------------------------------------
{
algebraic_g a = y();
a = convert_angle(a, object::ID_PiRadians, unit);
return a;
}
RENDER_BODY(polar)
// ----------------------------------------------------------------------------
// Render a complex number in rectangular form
// ----------------------------------------------------------------------------
{
angle_unit unit = Settings.AngleMode();
polar_g go = o;
algebraic_g m = go->mod();
algebraic_g a = go->arg(unit);
if (!m || !a)
return r.printf("Invalid polar");
m->render(r);
r.put(unicode(ANGLE_MARK));
a->render(r);
if (!a->is_symbolic())
{
switch(unit)
{
case ID_Deg:
r.put(unicode(Settings.DEGREES_SYMBOL));
break;
case ID_Grad:
r.put(unicode(Settings.GRAD_SYMBOL));
break;
case ID_PiRadians:
r.put(unicode(Settings.PI_RADIANS_SYMBOL));
break;
default:
case ID_Rad:
r.put(unicode(Settings.RADIANS_SYMBOL));
break;
}
}
return r.size();
}
COMMAND_BODY(RealToRectangular)
// ----------------------------------------------------------------------------
// Take two values in x and y and turn them into a rectangular complex
// ----------------------------------------------------------------------------
{
object_g re = strip(rt.stack(1));
object_g im = strip(rt.stack(0));
if (!re || !im)
return ERROR;
if (!(re->is_real() || re->is_symbolic()) ||
!(im->is_real() || im->is_symbolic()))
{
rt.type_error();
return ERROR;
}
complex_g z = rectangular::make(algebraic_p(+re),
algebraic_p(+im));
if (!z|| !rt.drop())
return ERROR;
if (!rt.top(z))
return ERROR;
return OK;
}
COMMAND_BODY(RealToPolar)
// ----------------------------------------------------------------------------
// Take two values in x and y and turn them into a polar complex
// ----------------------------------------------------------------------------
{
object_g mod = strip(rt.stack(1));
object_g arg = strip(rt.stack(0));
if (!mod || !arg)
return ERROR;
algebraic::angle_unit amode = Settings.AngleMode();
if (algebraic_g arga = arg->as_algebraic())
{
if (algebraic::angle_unit givenmode = algebraic::adjust_angle(arga))
{
amode = givenmode;
arg = +arga;
}
}
if (!(mod->is_real() || mod->is_symbolic()) ||
!(arg->is_real() || arg->is_symbolic()))
{
rt.type_error();
return ERROR;
}
complex_g z = polar::make(algebraic_p(+mod), algebraic_p(+arg), amode);
if (!z|| !rt.drop())
return ERROR;
if (!rt.top(z))
return ERROR;
return OK;
}
COMMAND_BODY(RectangularToReal)
// ----------------------------------------------------------------------------
// Take a complex value and convert it into two real values
// ----------------------------------------------------------------------------
{
object_g z = strip(rt.top());
if (!z)
return ERROR;
if (!z->is_complex())
{
rt.type_error();
return ERROR;
}
complex_g zz = complex_p(+z);
object_g re = +zz->re();
object_g im = +zz->im();
if (!re || !im)
return ERROR;
re = +tag::make("re", re);
im = +tag::make("im", im);
if (!re || !im || !rt.top(re) || !rt.push(im))
return ERROR;
return OK;
}
COMMAND_BODY(PolarToReal)
// ----------------------------------------------------------------------------
// Take a complex value in polar form and convert it into two real values
// ----------------------------------------------------------------------------
{
object_g z = strip(rt.top());
if (!z)
return ERROR;
if (!z->is_complex())
{
rt.type_error();
return ERROR;
}
complex_g zz = complex_p(+z);
algebraic_g mod = +zz->mod();
algebraic_g arg = +zz->arg(Settings.AngleMode());
if (!mod || !arg)
return ERROR;
if (!complex::add_angle(arg))
return ERROR;
object_g modobj = +tag::make("mod", +mod);
object_g argobj = +tag::make("arg", +arg);
if (!modobj || !argobj || !rt.top(modobj) || !rt.push(argobj))
return ERROR;
return OK;
}
COMMAND_BODY(ToRectangular)
// ----------------------------------------------------------------------------
// Convert the top-level complex to rectangular form
// ----------------------------------------------------------------------------
{
object_g x = strip(rt.top());
if (!x)
return ERROR;
if (array_p v = x->as<array>())
{
if (array_p r = v->to_rectangular())
if (rt.top(r))
return OK;
if (!rt.error())
rt.type_error();
return ERROR;
}
else if (!x->is_complex())
{
rt.type_error();
return ERROR;
}
if (polar_p zp = x->as<polar>())
{
x = +zp->as_rectangular();
if (!x|| !rt.top(x))
return ERROR;
}
return OK;
}
COMMAND_BODY(ToPolar)
// ----------------------------------------------------------------------------
// Convert the top-level complex to polar form
// ----------------------------------------------------------------------------
{
object_g x = strip(rt.top());
if (!x)
return ERROR;
if (array_p v = x->as<array>())
{
if (array_p polar = v->to_polar())
if (rt.top(polar))
return OK;
rt.type_error();
return ERROR;
}
else if (!x->is_complex())
{
rt.type_error();
return ERROR;
}
if (rectangular_p zr = x->as<rectangular>())
{
x = +zr->as_polar();
if (!x|| !rt.top(x))
return ERROR;
}
return OK;
}
// ============================================================================
//
// Implementation of complex functions
//
// ============================================================================
COMPLEX_BODY(sqrt)
// ----------------------------------------------------------------------------
// Complex implementation of sqrt
// ----------------------------------------------------------------------------
{
id zt = z->type();
if (zt == ID_polar)
{
// Computation is a bit easier in polar form
polar_r p = (polar_r) z;
algebraic_g mod = p->mod();
algebraic_g arg = p->pifrac(); // Want it in original form here
algebraic_g two = integer::make(2);
return polar::make(sqrt::run(mod), arg / two, object::ID_PiRadians);
}
rectangular_r r = (rectangular_r) z;
algebraic_g a = r->re();
algebraic_g b = r->im();
if (b->is_zero(false))
{
if (!a->is_symbolic())
{