-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.c
2093 lines (1826 loc) · 45.3 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-2011
This implementation is based on Keiju Ishitsuka's Complex library
which is written in ruby.
*/
#include "ruby.h"
#include "internal.h"
#include <math.h>
#define NDEBUG
#include <assert.h>
#define ZERO INT2FIX(0)
#define ONE INT2FIX(1)
#define TWO INT2FIX(2)
VALUE rb_cComplex;
static ID id_abs, id_abs2, id_arg, id_cmp, id_conj, id_convert,
id_denominator, id_divmod, id_eqeq_p, id_expt, id_fdiv, id_floor,
id_idiv, id_imag, id_inspect, id_negate, id_numerator, id_quo,
id_real, id_real_p, id_to_f, id_to_i, id_to_r, id_to_s,
id_i_real, id_i_imag;
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
#define binop(n,op) \
inline static VALUE \
f_##n(VALUE x, VALUE y)\
{\
return rb_funcall(x, (op), 1, y);\
}
#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 math1(n) \
inline static VALUE \
m_##n(VALUE x)\
{\
return rb_funcall(rb_mMath, id_##n, 1, x);\
}
#define math2(n) \
inline static VALUE \
m_##n(VALUE x, VALUE y)\
{\
return rb_funcall(rb_mMath, id_##n, 2, x, y);\
}
#define PRESERVE_SIGNEDZERO
inline static VALUE
f_add(VALUE x, VALUE y)
{
#ifndef PRESERVE_SIGNEDZERO
if (FIXNUM_P(y) && FIX2LONG(y) == 0)
return x;
else if (FIXNUM_P(x) && FIX2LONG(x) == 0)
return y;
#endif
return rb_funcall(x, '+', 1, y);
}
inline static VALUE
f_cmp(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
long c = FIX2LONG(x) - FIX2LONG(y);
if (c > 0)
c = 1;
else if (c < 0)
c = -1;
return INT2FIX(c);
}
return rb_funcall(x, id_cmp, 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 VALUE
f_gt_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return f_boolcast(FIX2LONG(x) > FIX2LONG(y));
return rb_funcall(x, '>', 1, y);
}
inline static VALUE
f_lt_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return f_boolcast(FIX2LONG(x) < FIX2LONG(y));
return rb_funcall(x, '<', 1, y);
}
binop(mod, '%')
inline static VALUE
f_mul(VALUE x, VALUE y)
{
#ifndef PRESERVE_SIGNEDZERO
if (FIXNUM_P(y)) {
long iy = FIX2LONG(y);
if (iy == 0) {
if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM))
return ZERO;
}
else if (iy == 1)
return x;
}
else if (FIXNUM_P(x)) {
long ix = FIX2LONG(x);
if (ix == 0) {
if (FIXNUM_P(y) || RB_TYPE_P(y, T_BIGNUM))
return ZERO;
}
else if (ix == 1)
return y;
}
#endif
return rb_funcall(x, '*', 1, y);
}
inline static VALUE
f_sub(VALUE x, VALUE y)
{
#ifndef PRESERVE_SIGNEDZERO
if (FIXNUM_P(y) && FIX2LONG(y) == 0)
return x;
#endif
return rb_funcall(x, '-', 1, y);
}
fun1(abs)
fun1(abs2)
fun1(arg)
fun1(conj)
fun1(denominator)
fun1(floor)
fun1(imag)
fun1(inspect)
fun1(negate)
fun1(numerator)
fun1(real)
fun1(real_p)
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)
fun1(to_s)
fun2(divmod)
inline static VALUE
f_eqeq_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return f_boolcast(FIX2LONG(x) == FIX2LONG(y));
return rb_funcall(x, id_eqeq_p, 1, y);
}
fun2(expt)
fun2(fdiv)
fun2(idiv)
fun2(quo)
inline static VALUE
f_negative_p(VALUE x)
{
if (FIXNUM_P(x))
return f_boolcast(FIX2LONG(x) < 0);
return rb_funcall(x, '<', 1, ZERO);
}
#define f_positive_p(x) (!f_negative_p(x))
inline static VALUE
f_zero_p(VALUE x)
{
switch (TYPE(x)) {
case T_FIXNUM:
return f_boolcast(FIX2LONG(x) == 0);
case T_BIGNUM:
return Qfalse;
case T_RATIONAL:
{
VALUE num = RRATIONAL(x)->num;
return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 0);
}
}
return rb_funcall(x, id_eqeq_p, 1, ZERO);
}
#define f_nonzero_p(x) (!f_zero_p(x))
inline static VALUE
f_one_p(VALUE x)
{
switch (TYPE(x)) {
case T_FIXNUM:
return f_boolcast(FIX2LONG(x) == 1);
case T_BIGNUM:
return Qfalse;
case T_RATIONAL:
{
VALUE num = RRATIONAL(x)->num;
VALUE den = RRATIONAL(x)->den;
return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 1 &&
FIXNUM_P(den) && FIX2LONG(den) == 1);
}
}
return rb_funcall(x, id_eqeq_p, 1, ONE);
}
inline static VALUE
f_kind_of_p(VALUE x, VALUE c)
{
return rb_obj_is_kind_of(x, c);
}
inline static VALUE
k_numeric_p(VALUE x)
{
return f_kind_of_p(x, rb_cNumeric);
}
inline static VALUE
k_integer_p(VALUE x)
{
return f_kind_of_p(x, rb_cInteger);
}
inline static VALUE
k_fixnum_p(VALUE x)
{
return f_kind_of_p(x, rb_cFixnum);
}
inline static VALUE
k_bignum_p(VALUE x)
{
return f_kind_of_p(x, rb_cBignum);
}
inline static VALUE
k_float_p(VALUE x)
{
return f_kind_of_p(x, rb_cFloat);
}
inline static VALUE
k_rational_p(VALUE x)
{
return f_kind_of_p(x, rb_cRational);
}
inline static VALUE
k_complex_p(VALUE x)
{
return f_kind_of_p(x, rb_cComplex);
}
#define k_exact_p(x) (!k_float_p(x))
#define k_inexact_p(x) k_float_p(x)
#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
#define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
#define get_dat1(x) \
struct RComplex *dat;\
dat = ((struct RComplex *)(x))
#define get_dat2(x,y) \
struct RComplex *adat, *bdat;\
adat = ((struct RComplex *)(x));\
bdat = ((struct RComplex *)(y))
inline static VALUE
nucomp_s_new_internal(VALUE klass, VALUE real, VALUE imag)
{
NEWOBJ_OF(obj, struct RComplex, klass, T_COMPLEX);
obj->real = real;
obj->imag = imag;
return (VALUE)obj;
}
static VALUE
nucomp_s_alloc(VALUE klass)
{
return nucomp_s_new_internal(klass, ZERO, ZERO);
}
#if 0
static VALUE
nucomp_s_new_bang(int argc, VALUE *argv, VALUE klass)
{
VALUE real, imag;
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
if (!k_numeric_p(real))
real = f_to_i(real);
imag = ZERO;
break;
default:
if (!k_numeric_p(real))
real = f_to_i(real);
if (!k_numeric_p(imag))
imag = f_to_i(imag);
break;
}
return nucomp_s_new_internal(klass, real, imag);
}
#endif
inline static VALUE
f_complex_new_bang1(VALUE klass, VALUE x)
{
assert(!k_complex_p(x));
return nucomp_s_new_internal(klass, x, ZERO);
}
inline static VALUE
f_complex_new_bang2(VALUE klass, VALUE x, VALUE y)
{
assert(!k_complex_p(x));
assert(!k_complex_p(y));
return nucomp_s_new_internal(klass, x, y);
}
#ifdef CANONICALIZATION_FOR_MATHN
#define CANON
#endif
#ifdef CANON
static int canonicalization = 0;
RUBY_FUNC_EXPORTED void
nucomp_canonicalization(int f)
{
canonicalization = f;
}
#endif
inline static void
nucomp_real_check(VALUE num)
{
switch (TYPE(num)) {
case T_FIXNUM:
case T_BIGNUM:
case T_FLOAT:
case T_RATIONAL:
break;
default:
if (!k_numeric_p(num) || !f_real_p(num))
rb_raise(rb_eTypeError, "not a real");
}
}
inline static VALUE
nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
{
#ifdef CANON
#define CL_CANON
#ifdef CL_CANON
if (k_exact_zero_p(imag) && canonicalization)
return real;
#else
if (f_zero_p(imag) && canonicalization)
return real;
#endif
#endif
if (f_real_p(real) && f_real_p(imag))
return nucomp_s_new_internal(klass, real, imag);
else if (f_real_p(real)) {
get_dat1(imag);
return nucomp_s_new_internal(klass,
f_sub(real, dat->imag),
f_add(ZERO, dat->real));
}
else if (f_real_p(imag)) {
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:
nucomp_real_check(real);
imag = ZERO;
break;
default:
nucomp_real_check(real);
nucomp_real_check(imag);
break;
}
return nucomp_s_canonicalize_internal(klass, real, imag);
}
inline static VALUE
f_complex_new1(VALUE klass, VALUE x)
{
assert(!k_complex_p(x));
return nucomp_s_canonicalize_internal(klass, x, ZERO);
}
inline static VALUE
f_complex_new2(VALUE klass, VALUE x, VALUE y)
{
assert(!k_complex_p(x));
return nucomp_s_canonicalize_internal(klass, x, y);
}
/*
* call-seq:
* Complex(x[, y]) -> numeric
*
* Returns x+i*y;
*
* Complex(1, 2) #=> (1+2i)
* Complex('1+2i') #=> (1+2i)
*/
static VALUE
nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
{
return rb_funcall2(rb_cComplex, id_convert, argc, argv);
}
#define imp1(n) \
inline static VALUE \
m_##n##_bang(VALUE x)\
{\
return rb_math_##n(x);\
}
#define imp2(n) \
inline static VALUE \
m_##n##_bang(VALUE x, VALUE y)\
{\
return rb_math_##n(x, y);\
}
imp2(atan2)
imp1(cos)
imp1(cosh)
imp1(exp)
imp2(hypot)
#define m_hypot(x,y) m_hypot_bang((x),(y))
static VALUE
m_log_bang(VALUE x)
{
return rb_math_log(1, &x);
}
imp1(sin)
imp1(sinh)
imp1(sqrt)
static VALUE
m_cos(VALUE x)
{
if (f_real_p(x))
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 (f_real_p(x))
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)));
}
}
#if 0
static VALUE
m_sqrt(VALUE x)
{
if (f_real_p(x)) {
if (f_positive_p(x))
return m_sqrt_bang(x);
return f_complex_new2(rb_cComplex, ZERO, m_sqrt_bang(f_negate(x)));
}
else {
get_dat1(x);
if (f_negative_p(dat->imag))
return f_conj(m_sqrt(f_conj(x)));
else {
VALUE a = f_abs(x);
return f_complex_new2(rb_cComplex,
m_sqrt_bang(f_div(f_add(a, dat->real), TWO)),
m_sqrt_bang(f_div(f_sub(a, dat->real), TWO)));
}
}
}
#endif
inline static VALUE
f_complex_polar(VALUE klass, VALUE x, VALUE y)
{
assert(!k_complex_p(x));
assert(!k_complex_p(y));
return nucomp_s_canonicalize_internal(klass,
f_mul(x, m_cos(y)),
f_mul(x, m_sin(y)));
}
/*
* 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;
switch (rb_scan_args(argc, argv, "11", &abs, &arg)) {
case 1:
nucomp_real_check(abs);
arg = ZERO;
break;
default:
nucomp_real_check(abs);
nucomp_real_check(arg);
break;
}
return f_complex_polar(klass, abs, arg);
}
/*
* call-seq:
* cmp.real -> real
*
* Returns the real part.
*
* Complex(7).real #=> 7
* Complex(9, -4).real #=> 9
*/
static VALUE
nucomp_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
*/
static VALUE
nucomp_imag(VALUE self)
{
get_dat1(self);
return dat->imag;
}
/*
* call-seq:
* -cmp -> complex
*
* Returns negation of the value.
*
* -Complex(1, 2) #=> (-1-2i)
*/
static VALUE
nucomp_negate(VALUE self)
{
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_negate(dat->real), f_negate(dat->imag));
}
inline static VALUE
f_addsub(VALUE self, VALUE other,
VALUE (*func)(VALUE, VALUE), ID id)
{
if (k_complex_p(other)) {
VALUE real, imag;
get_dat2(self, other);
real = (*func)(adat->real, bdat->real);
imag = (*func)(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),
(*func)(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, id);
}
/*
* 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)
*/
static VALUE
nucomp_add(VALUE self, VALUE other)
{
return f_addsub(self, other, f_add, '+');
}
/*
* 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)
*/
static VALUE
nucomp_sub(VALUE self, VALUE other)
{
return f_addsub(self, other, f_sub, '-');
}
/*
* 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)
*/
static VALUE
nucomp_mul(VALUE self, VALUE other)
{
if (k_complex_p(other)) {
VALUE real, imag;
get_dat2(self, other);
real = f_sub(f_mul(adat->real, bdat->real),
f_mul(adat->imag, bdat->imag));
imag = f_add(f_mul(adat->real, bdat->imag),
f_mul(adat->imag, bdat->real));
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 (k_complex_p(other)) {
int flo;
get_dat2(self, other);
flo = (k_float_p(adat->real) || k_float_p(adat->imag) ||
k_float_p(bdat->real) || k_float_p(bdat->imag));
if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
VALUE r, n;
r = (*func)(bdat->imag, bdat->real);
n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
if (flo)
return f_complex_new2(CLASS_OF(self),
(*func)(self, n),
(*func)(f_negate(f_mul(self, r)), n));
return f_complex_new2(CLASS_OF(self),
(*func)(f_add(adat->real,
f_mul(adat->imag, r)), n),
(*func)(f_sub(adat->imag,
f_mul(adat->real, r)), n));
}
else {
VALUE r, n;
r = (*func)(bdat->real, bdat->imag);
n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
if (flo)
return f_complex_new2(CLASS_OF(self),
(*func)(f_mul(self, r), n),
(*func)(f_negate(self), n));
return f_complex_new2(CLASS_OF(self),
(*func)(f_add(f_mul(adat->real, r),
adat->imag), n),
(*func)(f_sub(f_mul(adat->imag, r),
adat->real), n));
}
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
(*func)(dat->real, other),
(*func)(dat->imag, other));
}
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)
*/
static VALUE
nucomp_div(VALUE self, VALUE other)
{
return f_divide(self, other, f_quo, id_quo);
}
#define nucomp_quo nucomp_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)
*/
static VALUE
nucomp_expt(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
return f_complex_new_bang1(CLASS_OF(self), ONE);
if (k_rational_p(other) && f_one_p(f_denominator(other)))
other = f_numerator(other); /* c14n */
if (k_complex_p(other)) {
get_dat1(other);
if (k_exact_zero_p(dat->imag))
other = dat->real; /* c14n */
}
if (k_complex_p(other)) {
VALUE r, theta, nr, ntheta;
get_dat1(other);
r = f_abs(self);
theta = f_arg(self);
nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
f_mul(dat->imag, theta)));
ntheta = f_add(f_mul(theta, dat->real),
f_mul(dat->imag, m_log_bang(r)));
return f_complex_polar(CLASS_OF(self), nr, ntheta);
}
if (k_fixnum_p(other)) {
if (f_gt_p(other, ZERO)) {
VALUE x, z;
long n;
x = self;
z = x;
n = FIX2LONG(other) - 1;
while (n) {
long q, r;
while (1) {
get_dat1(x);
q = n / 2;
r = n % 2;
if (r)
break;
x = nucomp_s_new_internal(CLASS_OF(self),
f_sub(f_mul(dat->real, dat->real),
f_mul(dat->imag, dat->imag)),
f_mul(f_mul(TWO, dat->real), dat->imag));
n = q;
}
z = f_mul(z, x);
n--;
}
return z;
}
return f_expt(f_reciprocal(self), f_negate(other));
}
if (k_numeric_p(other) && f_real_p(other)) {
VALUE r, theta;
if (k_bignum_p(other))
rb_warn("in a**b, b may be too big");
r = f_abs(self);
theta = f_arg(self);
return f_complex_polar(CLASS_OF(self), f_expt(r, other),
f_mul(theta, other));
}
return rb_num_coerce_bin(self, other, id_expt);
}
/*
* call-seq:
* cmp == object -> true or false
*
* Returns true if cmp equals object numerically.
*
* Complex(2, 3) == Complex(2, 3) #=> true
* Complex(5) == 5 #=> true
* Complex(0) == 0.0 #=> true
* Complex('1/3') == 0.33 #=> false
* Complex('1/2') == '1/2' #=> false
*/
static VALUE
nucomp_eqeq_p(VALUE self, VALUE other)
{
if (k_complex_p(other)) {
get_dat2(self, other);
return f_boolcast(f_eqeq_p(adat->real, bdat->real) &&
f_eqeq_p(adat->imag, bdat->imag));
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_boolcast(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
}
return f_eqeq_p(other, self);
}
/* :nodoc: */
static VALUE
nucomp_coerce(VALUE self, VALUE other)
{
if (k_numeric_p(other) && f_real_p(other))
return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
if (RB_TYPE_P(other, T_COMPLEX))
return rb_assoc_new(other, self);
rb_raise(rb_eTypeError, "%s can't be coerced into %s",
rb_obj_classname(other), rb_obj_classname(self));
return Qnil;
}
/*
* call-seq:
* cmp.abs -> real
* cmp.magnitude -> real
*
* Returns the absolute part of its polar form.
*
* Complex(-1).abs #=> 1
* Complex(3.0, -4.0).abs #=> 5.0
*/
static VALUE
nucomp_abs(VALUE self)