-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.c
2425 lines (2151 loc) · 57.2 KB
/
complex.c
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.c: Coded by Tadayoshi Funaba 2008-2012
This implementation is based on Keiju Ishitsuka's Complex library
which is written in ruby.
*/
#include "ruby/internal/config.h"
#if defined _MSC_VER
/* Microsoft Visual C does not define M_PI and others by default */
# define _USE_MATH_DEFINES 1
#endif
#include <ctype.h>
#include <math.h>
#include "id.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/class.h"
#include "internal/complex.h"
#include "internal/math.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/rational.h"
#include "ruby_assert.h"
#define ZERO INT2FIX(0)
#define ONE INT2FIX(1)
#define TWO INT2FIX(2)
#if USE_FLONUM
#define RFLOAT_0 DBL2NUM(0)
#else
static VALUE RFLOAT_0;
#endif
VALUE rb_cComplex;
static ID id_abs, id_arg,
id_denominator, id_numerator,
id_real_p, id_i_real, id_i_imag,
id_finite_p, id_infinite_p, id_rationalize,
id_PI;
#define id_to_i idTo_i
#define id_to_r idTo_r
#define id_negate idUMinus
#define id_expt idPow
#define id_to_f idTo_f
#define id_quo idQuo
#define id_fdiv idFdiv
#define fun1(n) \
inline static VALUE \
f_##n(VALUE x)\
{\
return rb_funcall(x, id_##n, 0);\
}
#define fun2(n) \
inline static VALUE \
f_##n(VALUE x, VALUE y)\
{\
return rb_funcall(x, id_##n, 1, y);\
}
#define PRESERVE_SIGNEDZERO
inline static VALUE
f_add(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x) &&
LIKELY(rb_method_basic_definition_p(rb_cInteger, idPLUS))) {
if (FIXNUM_ZERO_P(x))
return y;
if (FIXNUM_ZERO_P(y))
return x;
return rb_int_plus(x, y);
}
else if (RB_FLOAT_TYPE_P(x) &&
LIKELY(rb_method_basic_definition_p(rb_cFloat, idPLUS))) {
if (FIXNUM_ZERO_P(y))
return x;
return rb_float_plus(x, y);
}
else if (RB_TYPE_P(x, T_RATIONAL) &&
LIKELY(rb_method_basic_definition_p(rb_cRational, idPLUS))) {
if (FIXNUM_ZERO_P(y))
return x;
return rb_rational_plus(x, y);
}
return rb_funcall(x, '+', 1, y);
}
inline static VALUE
f_div(VALUE x, VALUE y)
{
if (FIXNUM_P(y) && FIX2LONG(y) == 1)
return x;
return rb_funcall(x, '/', 1, y);
}
inline static int
f_gt_p(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x)) {
if (FIXNUM_P(x) && FIXNUM_P(y))
return (SIGNED_VALUE)x > (SIGNED_VALUE)y;
return RTEST(rb_int_gt(x, y));
}
else if (RB_FLOAT_TYPE_P(x))
return RTEST(rb_float_gt(x, y));
else if (RB_TYPE_P(x, T_RATIONAL)) {
int const cmp = rb_cmpint(rb_rational_cmp(x, y), x, y);
return cmp > 0;
}
return RTEST(rb_funcall(x, '>', 1, y));
}
inline static VALUE
f_mul(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x) &&
LIKELY(rb_method_basic_definition_p(rb_cInteger, idMULT))) {
if (FIXNUM_ZERO_P(y))
return ZERO;
if (FIXNUM_ZERO_P(x) && RB_INTEGER_TYPE_P(y))
return ZERO;
if (x == ONE) return y;
if (y == ONE) return x;
return rb_int_mul(x, y);
}
else if (RB_FLOAT_TYPE_P(x) &&
LIKELY(rb_method_basic_definition_p(rb_cFloat, idMULT))) {
if (y == ONE) return x;
return rb_float_mul(x, y);
}
else if (RB_TYPE_P(x, T_RATIONAL) &&
LIKELY(rb_method_basic_definition_p(rb_cRational, idMULT))) {
if (y == ONE) return x;
return rb_rational_mul(x, y);
}
else if (LIKELY(rb_method_basic_definition_p(CLASS_OF(x), idMULT))) {
if (y == ONE) return x;
}
return rb_funcall(x, '*', 1, y);
}
inline static VALUE
f_sub(VALUE x, VALUE y)
{
if (FIXNUM_ZERO_P(y) &&
LIKELY(rb_method_basic_definition_p(CLASS_OF(x), idMINUS))) {
return x;
}
return rb_funcall(x, '-', 1, y);
}
inline static VALUE
f_abs(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return rb_int_abs(x);
}
else if (RB_FLOAT_TYPE_P(x)) {
return rb_float_abs(x);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
return rb_rational_abs(x);
}
else if (RB_TYPE_P(x, T_COMPLEX)) {
return rb_complex_abs(x);
}
return rb_funcall(x, id_abs, 0);
}
static VALUE numeric_arg(VALUE self);
static VALUE float_arg(VALUE self);
inline static VALUE
f_arg(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return numeric_arg(x);
}
else if (RB_FLOAT_TYPE_P(x)) {
return float_arg(x);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
return numeric_arg(x);
}
else if (RB_TYPE_P(x, T_COMPLEX)) {
return rb_complex_arg(x);
}
return rb_funcall(x, id_arg, 0);
}
inline static VALUE
f_numerator(VALUE x)
{
if (RB_TYPE_P(x, T_RATIONAL)) {
return RRATIONAL(x)->num;
}
if (RB_FLOAT_TYPE_P(x)) {
return rb_float_numerator(x);
}
return x;
}
inline static VALUE
f_denominator(VALUE x)
{
if (RB_TYPE_P(x, T_RATIONAL)) {
return RRATIONAL(x)->den;
}
if (RB_FLOAT_TYPE_P(x)) {
return rb_float_denominator(x);
}
return INT2FIX(1);
}
inline static VALUE
f_negate(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return rb_int_uminus(x);
}
else if (RB_FLOAT_TYPE_P(x)) {
return rb_float_uminus(x);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
return rb_rational_uminus(x);
}
else if (RB_TYPE_P(x, T_COMPLEX)) {
return rb_complex_uminus(x);
}
return rb_funcall(x, id_negate, 0);
}
static bool nucomp_real_p(VALUE self);
static inline bool
f_real_p(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return true;
}
else if (RB_FLOAT_TYPE_P(x)) {
return true;
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
return true;
}
else if (RB_TYPE_P(x, T_COMPLEX)) {
return nucomp_real_p(x);
}
return rb_funcall(x, id_real_p, 0);
}
inline static VALUE
f_to_i(VALUE x)
{
if (RB_TYPE_P(x, T_STRING))
return rb_str_to_inum(x, 10, 0);
return rb_funcall(x, id_to_i, 0);
}
inline static VALUE
f_to_f(VALUE x)
{
if (RB_TYPE_P(x, T_STRING))
return DBL2NUM(rb_str_to_dbl(x, 0));
return rb_funcall(x, id_to_f, 0);
}
fun1(to_r)
inline static int
f_eqeq_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return x == y;
else if (RB_FLOAT_TYPE_P(x) || RB_FLOAT_TYPE_P(y))
return NUM2DBL(x) == NUM2DBL(y);
return (int)rb_equal(x, y);
}
fun2(expt)
fun2(fdiv)
static VALUE
f_quo(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x))
return rb_numeric_quo(x, y);
if (RB_FLOAT_TYPE_P(x))
return rb_float_div(x, y);
if (RB_TYPE_P(x, T_RATIONAL))
return rb_numeric_quo(x, y);
return rb_funcallv(x, id_quo, 1, &y);
}
inline static int
f_negative_p(VALUE x)
{
if (RB_INTEGER_TYPE_P(x))
return INT_NEGATIVE_P(x);
else if (RB_FLOAT_TYPE_P(x))
return RFLOAT_VALUE(x) < 0.0;
else if (RB_TYPE_P(x, T_RATIONAL))
return INT_NEGATIVE_P(RRATIONAL(x)->num);
return rb_num_negative_p(x);
}
#define f_positive_p(x) (!f_negative_p(x))
inline static bool
f_zero_p(VALUE x)
{
if (RB_FLOAT_TYPE_P(x)) {
return FLOAT_ZERO_P(x);
}
else if (RB_INTEGER_TYPE_P(x)) {
return FIXNUM_ZERO_P(x);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
const VALUE num = RRATIONAL(x)->num;
return FIXNUM_ZERO_P(num);
}
return rb_equal(x, ZERO) != 0;
}
#define f_nonzero_p(x) (!f_zero_p(x))
static inline bool
always_finite_type_p(VALUE x)
{
if (FIXNUM_P(x)) return true;
if (FLONUM_P(x)) return true; /* Infinity can't be a flonum */
return (RB_INTEGER_TYPE_P(x) || RB_TYPE_P(x, T_RATIONAL));
}
inline static int
f_finite_p(VALUE x)
{
if (always_finite_type_p(x)) {
return TRUE;
}
else if (RB_FLOAT_TYPE_P(x)) {
return isfinite(RFLOAT_VALUE(x));
}
return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
}
inline static int
f_infinite_p(VALUE x)
{
if (always_finite_type_p(x)) {
return FALSE;
}
else if (RB_FLOAT_TYPE_P(x)) {
return isinf(RFLOAT_VALUE(x));
}
return RTEST(rb_funcallv(x, id_infinite_p, 0, 0));
}
inline static int
f_kind_of_p(VALUE x, VALUE c)
{
return (int)rb_obj_is_kind_of(x, c);
}
inline static int
k_numeric_p(VALUE x)
{
return f_kind_of_p(x, rb_cNumeric);
}
#define k_exact_p(x) (!RB_FLOAT_TYPE_P(x))
#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
#define get_dat1(x) \
struct RComplex *dat = RCOMPLEX(x)
#define get_dat2(x,y) \
struct RComplex *adat = RCOMPLEX(x), *bdat = RCOMPLEX(y)
inline static VALUE
nucomp_s_new_internal(VALUE klass, VALUE real, VALUE imag)
{
NEWOBJ_OF(obj, struct RComplex, klass,
T_COMPLEX | (RGENGC_WB_PROTECTED_COMPLEX ? FL_WB_PROTECTED : 0), sizeof(struct RComplex), 0);
RCOMPLEX_SET_REAL(obj, real);
RCOMPLEX_SET_IMAG(obj, imag);
OBJ_FREEZE_RAW((VALUE)obj);
return (VALUE)obj;
}
static VALUE
nucomp_s_alloc(VALUE klass)
{
return nucomp_s_new_internal(klass, ZERO, ZERO);
}
inline static VALUE
f_complex_new_bang1(VALUE klass, VALUE x)
{
assert(!RB_TYPE_P(x, T_COMPLEX));
return nucomp_s_new_internal(klass, x, ZERO);
}
inline static VALUE
f_complex_new_bang2(VALUE klass, VALUE x, VALUE y)
{
assert(!RB_TYPE_P(x, T_COMPLEX));
assert(!RB_TYPE_P(y, T_COMPLEX));
return nucomp_s_new_internal(klass, x, y);
}
WARN_UNUSED_RESULT(inline static VALUE nucomp_real_check(VALUE num));
inline static VALUE
nucomp_real_check(VALUE num)
{
if (!RB_INTEGER_TYPE_P(num) &&
!RB_FLOAT_TYPE_P(num) &&
!RB_TYPE_P(num, T_RATIONAL)) {
if (RB_TYPE_P(num, T_COMPLEX) && nucomp_real_p(num)) {
VALUE real = RCOMPLEX(num)->real;
assert(!RB_TYPE_P(real, T_COMPLEX));
return real;
}
if (!k_numeric_p(num) || !f_real_p(num))
rb_raise(rb_eTypeError, "not a real");
}
return num;
}
inline static VALUE
nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
{
int complex_r, complex_i;
complex_r = RB_TYPE_P(real, T_COMPLEX);
complex_i = RB_TYPE_P(imag, T_COMPLEX);
if (!complex_r && !complex_i) {
return nucomp_s_new_internal(klass, real, imag);
}
else if (!complex_r) {
get_dat1(imag);
return nucomp_s_new_internal(klass,
f_sub(real, dat->imag),
f_add(ZERO, dat->real));
}
else if (!complex_i) {
get_dat1(real);
return nucomp_s_new_internal(klass,
dat->real,
f_add(dat->imag, imag));
}
else {
get_dat2(real, imag);
return nucomp_s_new_internal(klass,
f_sub(adat->real, bdat->imag),
f_add(adat->imag, bdat->real));
}
}
/*
* call-seq:
* Complex.rect(real[, imag]) -> complex
* Complex.rectangular(real[, imag]) -> complex
*
* Returns a complex object which denotes the given rectangular form.
*
* Complex.rectangular(1, 2) #=> (1+2i)
*/
static VALUE
nucomp_s_new(int argc, VALUE *argv, VALUE klass)
{
VALUE real, imag;
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
real = nucomp_real_check(real);
imag = ZERO;
break;
default:
real = nucomp_real_check(real);
imag = nucomp_real_check(imag);
break;
}
return nucomp_s_new_internal(klass, real, imag);
}
inline static VALUE
f_complex_new2(VALUE klass, VALUE x, VALUE y)
{
if (RB_TYPE_P(x, T_COMPLEX)) {
get_dat1(x);
x = dat->real;
y = f_add(dat->imag, y);
}
return nucomp_s_canonicalize_internal(klass, x, y);
}
static VALUE nucomp_convert(VALUE klass, VALUE a1, VALUE a2, int raise);
static VALUE nucomp_s_convert(int argc, VALUE *argv, VALUE klass);
/*
* call-seq:
* Complex(x[, y], exception: true) -> numeric or nil
*
* Returns x+i*y;
*
* Complex(1, 2) #=> (1+2i)
* Complex('1+2i') #=> (1+2i)
* Complex(nil) #=> TypeError
* Complex(1, nil) #=> TypeError
*
* Complex(1, nil, exception: false) #=> nil
* Complex('1+2', exception: false) #=> nil
*
* Syntax of string form:
*
* string form = extra spaces , complex , extra spaces ;
* complex = real part | [ sign ] , imaginary part
* | real part , sign , imaginary part
* | rational , "@" , rational ;
* real part = rational ;
* imaginary part = imaginary unit | unsigned rational , imaginary unit ;
* rational = [ sign ] , unsigned rational ;
* unsigned rational = numerator | numerator , "/" , denominator ;
* numerator = integer part | fractional part | integer part , fractional part ;
* denominator = digits ;
* integer part = digits ;
* fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
* imaginary unit = "i" | "I" | "j" | "J" ;
* sign = "-" | "+" ;
* digits = digit , { digit | "_" , digit };
* digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
* extra spaces = ? \s* ? ;
*
* See String#to_c.
*/
static VALUE
nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
if (argc > 0 && CLASS_OF(a1) == rb_cComplex && UNDEF_P(a2)) {
return a1;
}
return nucomp_convert(rb_cComplex, a1, a2, raise);
}
#define imp1(n) \
inline static VALUE \
m_##n##_bang(VALUE x)\
{\
return rb_math_##n(x);\
}
imp1(cos)
imp1(cosh)
imp1(exp)
static VALUE
m_log_bang(VALUE x)
{
return rb_math_log(1, &x);
}
imp1(sin)
imp1(sinh)
static VALUE
m_cos(VALUE x)
{
if (!RB_TYPE_P(x, T_COMPLEX))
return m_cos_bang(x);
{
get_dat1(x);
return f_complex_new2(rb_cComplex,
f_mul(m_cos_bang(dat->real),
m_cosh_bang(dat->imag)),
f_mul(f_negate(m_sin_bang(dat->real)),
m_sinh_bang(dat->imag)));
}
}
static VALUE
m_sin(VALUE x)
{
if (!RB_TYPE_P(x, T_COMPLEX))
return m_sin_bang(x);
{
get_dat1(x);
return f_complex_new2(rb_cComplex,
f_mul(m_sin_bang(dat->real),
m_cosh_bang(dat->imag)),
f_mul(m_cos_bang(dat->real),
m_sinh_bang(dat->imag)));
}
}
static VALUE
f_complex_polar_real(VALUE klass, VALUE x, VALUE y)
{
if (f_zero_p(x) || f_zero_p(y)) {
return nucomp_s_new_internal(klass, x, RFLOAT_0);
}
if (RB_FLOAT_TYPE_P(y)) {
const double arg = RFLOAT_VALUE(y);
if (arg == M_PI) {
x = f_negate(x);
y = RFLOAT_0;
}
else if (arg == M_PI_2) {
y = x;
x = RFLOAT_0;
}
else if (arg == M_PI_2+M_PI) {
y = f_negate(x);
x = RFLOAT_0;
}
else if (RB_FLOAT_TYPE_P(x)) {
const double abs = RFLOAT_VALUE(x);
const double real = abs * cos(arg), imag = abs * sin(arg);
x = DBL2NUM(real);
y = DBL2NUM(imag);
}
else {
const double ax = sin(arg), ay = cos(arg);
y = f_mul(x, DBL2NUM(ax));
x = f_mul(x, DBL2NUM(ay));
}
return nucomp_s_new_internal(klass, x, y);
}
return nucomp_s_canonicalize_internal(klass,
f_mul(x, m_cos(y)),
f_mul(x, m_sin(y)));
}
static VALUE
f_complex_polar(VALUE klass, VALUE x, VALUE y)
{
x = nucomp_real_check(x);
y = nucomp_real_check(y);
return f_complex_polar_real(klass, x, y);
}
#ifdef HAVE___COSPI
# define cospi(x) __cospi(x)
#else
# define cospi(x) cos((x) * M_PI)
#endif
#ifdef HAVE___SINPI
# define sinpi(x) __sinpi(x)
#else
# define sinpi(x) sin((x) * M_PI)
#endif
/* returns a Complex or Float of ang*PI-rotated abs */
VALUE
rb_dbl_complex_new_polar_pi(double abs, double ang)
{
double fi;
const double fr = modf(ang, &fi);
int pos = fr == +0.5;
if (pos || fr == -0.5) {
if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
return rb_complex_new(RFLOAT_0, DBL2NUM(abs));
}
else if (fr == 0.0) {
if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
return DBL2NUM(abs);
}
else {
const double real = abs * cospi(ang), imag = abs * sinpi(ang);
return rb_complex_new(DBL2NUM(real), DBL2NUM(imag));
}
}
/*
* call-seq:
* Complex.polar(abs[, arg]) -> complex
*
* Returns a complex object which denotes the given polar form.
*
* Complex.polar(3, 0) #=> (3.0+0.0i)
* Complex.polar(3, Math::PI/2) #=> (1.836909530733566e-16+3.0i)
* Complex.polar(3, Math::PI) #=> (-3.0+3.673819061467132e-16i)
* Complex.polar(3, -Math::PI/2) #=> (1.836909530733566e-16-3.0i)
*/
static VALUE
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
{
VALUE abs, arg;
argc = rb_scan_args(argc, argv, "11", &abs, &arg);
abs = nucomp_real_check(abs);
if (argc == 2) {
arg = nucomp_real_check(arg);
}
else {
arg = ZERO;
}
return f_complex_polar_real(klass, abs, arg);
}
/*
* call-seq:
* cmp.real -> real
*
* Returns the real part.
*
* Complex(7).real #=> 7
* Complex(9, -4).real #=> 9
*/
VALUE
rb_complex_real(VALUE self)
{
get_dat1(self);
return dat->real;
}
/*
* call-seq:
* cmp.imag -> real
* cmp.imaginary -> real
*
* Returns the imaginary part.
*
* Complex(7).imaginary #=> 0
* Complex(9, -4).imaginary #=> -4
*/
VALUE
rb_complex_imag(VALUE self)
{
get_dat1(self);
return dat->imag;
}
/*
* call-seq:
* -cmp -> complex
*
* Returns negation of the value.
*
* -Complex(1, 2) #=> (-1-2i)
*/
VALUE
rb_complex_uminus(VALUE self)
{
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_negate(dat->real), f_negate(dat->imag));
}
/*
* call-seq:
* cmp + numeric -> complex
*
* Performs addition.
*
* Complex(2, 3) + Complex(2, 3) #=> (4+6i)
* Complex(900) + Complex(1) #=> (901+0i)
* Complex(-2, 9) + Complex(-9, 2) #=> (-11+11i)
* Complex(9, 8) + 4 #=> (13+8i)
* Complex(20, 9) + 9.8 #=> (29.8+9i)
*/
VALUE
rb_complex_plus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
real = f_add(adat->real, bdat->real);
imag = f_add(adat->imag, bdat->imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_add(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '+');
}
/*
* call-seq:
* cmp - numeric -> complex
*
* Performs subtraction.
*
* Complex(2, 3) - Complex(2, 3) #=> (0+0i)
* Complex(900) - Complex(1) #=> (899+0i)
* Complex(-2, 9) - Complex(-9, 2) #=> (7+7i)
* Complex(9, 8) - 4 #=> (5+8i)
* Complex(20, 9) - 9.8 #=> (10.2+9i)
*/
VALUE
rb_complex_minus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
real = f_sub(adat->real, bdat->real);
imag = f_sub(adat->imag, bdat->imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_sub(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '-');
}
static VALUE
safe_mul(VALUE a, VALUE b, bool az, bool bz)
{
double v;
if (!az && bz && RB_FLOAT_TYPE_P(a) && (v = RFLOAT_VALUE(a), !isnan(v))) {
a = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
}
if (!bz && az && RB_FLOAT_TYPE_P(b) && (v = RFLOAT_VALUE(b), !isnan(v))) {
b = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
}
return f_mul(a, b);
}
static void
comp_mul(VALUE areal, VALUE aimag, VALUE breal, VALUE bimag, VALUE *real, VALUE *imag)
{
bool arzero = f_zero_p(areal);
bool aizero = f_zero_p(aimag);
bool brzero = f_zero_p(breal);
bool bizero = f_zero_p(bimag);
*real = f_sub(safe_mul(areal, breal, arzero, brzero),
safe_mul(aimag, bimag, aizero, bizero));
*imag = f_add(safe_mul(areal, bimag, arzero, bizero),
safe_mul(aimag, breal, aizero, brzero));
}
/*
* call-seq:
* cmp * numeric -> complex
*
* Performs multiplication.
*
* Complex(2, 3) * Complex(2, 3) #=> (-5+12i)
* Complex(900) * Complex(1) #=> (900+0i)
* Complex(-2, 9) * Complex(-9, 2) #=> (0-85i)
* Complex(9, 8) * 4 #=> (36+32i)
* Complex(20, 9) * 9.8 #=> (196.0+88.2i)
*/
VALUE
rb_complex_mul(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_mul(dat->real, other),
f_mul(dat->imag, other));
}
return rb_num_coerce_bin(self, other, '*');
}
inline static VALUE
f_divide(VALUE self, VALUE other,
VALUE (*func)(VALUE, VALUE), ID id)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE r, n, x, y;
int flo;
get_dat2(self, other);
flo = (RB_FLOAT_TYPE_P(adat->real) || RB_FLOAT_TYPE_P(adat->imag) ||
RB_FLOAT_TYPE_P(bdat->real) || RB_FLOAT_TYPE_P(bdat->imag));
if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
r = (*func)(bdat->imag, bdat->real);
n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
}
else {
r = (*func)(bdat->real, bdat->imag);
n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
}
if (!flo) {
x = rb_rational_canonicalize(x);
y = rb_rational_canonicalize(y);
}
return f_complex_new2(CLASS_OF(self), x, y);
}
if (k_numeric_p(other) && f_real_p(other)) {
VALUE x, y;
get_dat1(self);
x = rb_rational_canonicalize((*func)(dat->real, other));
y = rb_rational_canonicalize((*func)(dat->imag, other));
return f_complex_new2(CLASS_OF(self), x, y);
}
return rb_num_coerce_bin(self, other, id);
}
#define rb_raise_zerodiv() rb_raise(rb_eZeroDivError, "divided by 0")
/*
* call-seq:
* cmp / numeric -> complex
* cmp.quo(numeric) -> complex
*
* Performs division.
*
* Complex(2, 3) / Complex(2, 3) #=> ((1/1)+(0/1)*i)
* Complex(900) / Complex(1) #=> ((900/1)+(0/1)*i)
* Complex(-2, 9) / Complex(-9, 2) #=> ((36/85)-(77/85)*i)
* Complex(9, 8) / 4 #=> ((9/4)+(2/1)*i)
* Complex(20, 9) / 9.8 #=> (2.0408163265306123+0.9183673469387754i)
*/
VALUE
rb_complex_div(VALUE self, VALUE other)
{
return f_divide(self, other, f_quo, id_quo);
}
#define nucomp_quo rb_complex_div
/*
* call-seq:
* cmp.fdiv(numeric) -> complex
*
* Performs division as each part is a float, never returns a float.
*
* Complex(11, 22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
*/
static VALUE
nucomp_fdiv(VALUE self, VALUE other)
{
return f_divide(self, other, f_fdiv, id_fdiv);
}
inline static VALUE
f_reciprocal(VALUE x)
{
return f_quo(ONE, x);
}
/*
* call-seq:
* cmp ** numeric -> complex
*
* Performs exponentiation.
*
* Complex('i') ** 2 #=> (-1+0i)
* Complex(-8) ** Rational(1, 3) #=> (1.0000000000000002+1.7320508075688772i)
*/
VALUE
rb_complex_pow(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
return f_complex_new_bang1(CLASS_OF(self), ONE);
if (RB_TYPE_P(other, T_RATIONAL) && RRATIONAL(other)->den == LONG2FIX(1))