* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / complex.c
blob707383ac3019ad4d29aedaf78d11e571014b9fa7
1 /*
2 complex.c: Coded by Tadayoshi Funaba 2008
4 This implementation is based on Keiju Ishitsuka's Complex library
5 which is written in ruby.
6 */
8 #include "ruby.h"
9 #include <math.h>
11 #define NDEBUG
12 #include <assert.h>
14 #ifndef COMPLEX_NAME
15 #define COMPLEX_NAME "Complex"
16 #endif
18 #define ZERO INT2FIX(0)
19 #define ONE INT2FIX(1)
20 #define TWO INT2FIX(2)
22 VALUE rb_cComplex;
24 static ID id_Unify, id_abs, id_abs2, id_arg, id_atan2_bang, id_cmp,
25 id_conjugate, id_convert, id_cos, id_denominator, id_divmod,
26 id_equal_p, id_exact_p, id_exp_bang, id_expt, id_floor, id_format,
27 id_hypot, id_idiv, id_inspect, id_log_bang, id_negate, id_new, id_new_bang,
28 id_numerator, id_polar, id_quo, id_scalar_p, id_sin, id_sqrt, id_to_f,
29 id_to_i, id_to_r, id_to_s, id_truncate;
31 #define f_boolcast(x) ((x) ? Qtrue : Qfalse)
33 #define binop(n,op) \
34 inline static VALUE \
35 f_##n(VALUE x, VALUE y)\
37 return rb_funcall(x, op, 1, y);\
40 #define fun1(n) \
41 inline static VALUE \
42 f_##n(VALUE x)\
44 return rb_funcall(x, id_##n, 0);\
47 #define fun2(n) \
48 inline static VALUE \
49 f_##n(VALUE x, VALUE y)\
51 return rb_funcall(x, id_##n, 1, y);\
54 #define math1(n) \
55 inline static VALUE \
56 m_##n(VALUE x)\
58 return rb_funcall(rb_mMath, id_##n, 1, x);\
61 #define math2(n) \
62 inline static VALUE \
63 m_##n(VALUE x, VALUE y)\
65 return rb_funcall(rb_mMath, id_##n, 2, x, y);\
68 inline static VALUE
69 f_add(VALUE x, VALUE y)
71 if (FIXNUM_P(y)) {
72 if (FIX2LONG(y) == 0)
73 return x;
75 else if (FIXNUM_P(x)) {
76 if (FIX2LONG(x) == 0)
77 return y;
79 return rb_funcall(x, '+', 1, y);
82 inline static VALUE
83 f_cmp(VALUE x, VALUE y)
85 if (FIXNUM_P(x) && FIXNUM_P(y)) {
86 long c = FIX2LONG(x) - FIX2LONG(y);
87 if (c > 0)
88 c = 1;
89 else if (c < 0)
90 c = -1;
91 return INT2FIX(c);
93 return rb_funcall(x, id_cmp, 1, y);
96 inline static VALUE
97 f_div(VALUE x, VALUE y)
99 if (FIXNUM_P(y) && FIX2LONG(y) == 1)
100 return x;
101 return rb_funcall(x, '/', 1, y);
104 inline static VALUE
105 f_gt_p(VALUE x, VALUE y)
107 if (FIXNUM_P(x) && FIXNUM_P(y))
108 return f_boolcast(FIX2LONG(x) > FIX2LONG(y));
109 return rb_funcall(x, '>', 1, y);
112 inline static VALUE
113 f_lt_p(VALUE x, VALUE y)
115 if (FIXNUM_P(x) && FIXNUM_P(y))
116 return f_boolcast(FIX2LONG(x) < FIX2LONG(y));
117 return rb_funcall(x, '<', 1, y);
120 binop(mod, '%')
122 inline static VALUE
123 f_mul(VALUE x, VALUE y)
125 if (FIXNUM_P(y)) {
126 long _iy = FIX2LONG(y);
127 if (_iy == 0) {
128 if (TYPE(x) == T_FLOAT)
129 return rb_float_new(0.0);
130 else
131 return ZERO;
133 else if (_iy == 1)
134 return x;
136 else if (FIXNUM_P(x)) {
137 long _ix = FIX2LONG(x);
138 if (_ix == 0) {
139 if (TYPE(y) == T_FLOAT)
140 return rb_float_new(0.0);
141 else
142 return ZERO;
144 else if (_ix == 1)
145 return y;
147 return rb_funcall(x, '*', 1, y);
150 inline static VALUE
151 f_sub(VALUE x, VALUE y)
153 if (FIXNUM_P(y))
154 if (FIX2LONG(y) == 0)
155 return x;
156 return rb_funcall(x, '-', 1, y);
159 binop(xor, '^')
161 fun1(abs)
162 fun1(abs2)
163 fun1(arg)
164 fun1(conjugate)
165 fun1(denominator)
166 fun1(exact_p)
167 fun1(floor)
168 fun1(inspect)
169 fun1(negate)
170 fun1(numerator)
171 fun1(polar)
172 fun1(scalar_p)
173 fun1(to_f)
174 fun1(to_i)
175 fun1(to_r)
176 fun1(to_s)
177 fun1(truncate)
179 fun2(divmod)
181 inline static VALUE
182 f_equal_p(VALUE x, VALUE y)
184 if (FIXNUM_P(x) && FIXNUM_P(y))
185 return f_boolcast(FIX2LONG(x) == FIX2LONG(y));
186 return rb_funcall(x, id_equal_p, 1, y);
189 fun2(expt)
190 fun2(idiv)
191 fun2(quo)
193 inline static VALUE
194 f_negative_p(VALUE x)
196 if (FIXNUM_P(x))
197 return f_boolcast(FIX2LONG(x) < 0);
198 return rb_funcall(x, '<', 1, ZERO);
201 inline static VALUE
202 f_zero_p(VALUE x)
204 if (FIXNUM_P(x))
205 return f_boolcast(FIX2LONG(x) == 0);
206 return rb_funcall(x, id_equal_p, 1, ZERO);
209 inline static VALUE
210 f_one_p(VALUE x)
212 if (FIXNUM_P(x))
213 return f_boolcast(FIX2LONG(x) == 1);
214 return rb_funcall(x, id_equal_p, 1, ONE);
217 inline static VALUE
218 f_kind_of_p(VALUE x, VALUE c)
220 return rb_obj_is_kind_of(x, c);
223 inline static VALUE
224 k_numeric_p(VALUE x)
226 return f_kind_of_p(x, rb_cNumeric);
229 inline static VALUE
230 k_integer_p(VALUE x)
232 return f_kind_of_p(x, rb_cInteger);
235 inline static VALUE
236 k_float_p(VALUE x)
238 return f_kind_of_p(x, rb_cFloat);
241 inline static VALUE
242 k_rational_p(VALUE x)
244 return f_kind_of_p(x, rb_cRational);
247 inline static VALUE
248 k_complex_p(VALUE x)
250 return f_kind_of_p(x, rb_cComplex);
253 inline static VALUE
254 f_generic_p(VALUE x)
256 switch (TYPE(x)) {
257 case T_FIXNUM:
258 case T_BIGNUM:
259 case T_FLOAT:
260 case T_RATIONAL:
261 return Qtrue;
262 default:
263 return Qfalse;
267 static VALUE
268 nucomp_s_generic_p(VALUE klass, VALUE x)
270 return f_generic_p(x);
273 #define get_dat1(x) \
274 struct RComplex *dat;\
275 dat = ((struct RComplex *)(x))
277 #define get_dat2(x,y) \
278 struct RComplex *adat, *bdat;\
279 adat = ((struct RComplex *)(x));\
280 bdat = ((struct RComplex *)(y))
282 inline static VALUE
283 nucomp_s_new_internal(VALUE klass, VALUE real, VALUE image)
285 NEWOBJ(obj, struct RComplex);
286 OBJSETUP(obj, klass, T_COMPLEX);
288 obj->real = real;
289 obj->image = image;
291 return (VALUE)obj;
294 static VALUE
295 nucomp_s_alloc(VALUE klass)
297 return nucomp_s_new_internal(klass, ZERO, ZERO);
300 static VALUE
301 nucomp_s_new_bang(int argc, VALUE *argv, VALUE klass)
303 VALUE real, image;
305 switch (rb_scan_args(argc, argv, "11", &real, &image)) {
306 case 1:
307 if (!k_numeric_p(real))
308 real = f_to_i(real);
309 image = ZERO;
310 break;
311 default:
312 if (!k_numeric_p(real))
313 real = f_to_i(real);
314 if (!k_numeric_p(image))
315 image = f_to_i(image);
316 break;
319 return nucomp_s_new_internal(klass, real, image);
322 inline static VALUE
323 f_complex_new_bang1(VALUE klass, VALUE x)
325 return nucomp_s_new_internal(klass, x, ZERO);
328 inline static VALUE
329 f_complex_new_bang2(VALUE klass, VALUE x, VALUE y)
331 return nucomp_s_new_internal(klass, x, y);
334 #define f_unify_p(klass) rb_const_defined(klass, id_Unify)
336 inline static void
337 nucomp_real_check(VALUE num)
339 switch (TYPE(num)) {
340 case T_FIXNUM:
341 case T_BIGNUM:
342 case T_FLOAT:
343 case T_RATIONAL:
344 break;
345 default:
346 rb_raise(rb_eArgError, "not a real");
350 inline static VALUE
351 nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE image)
353 #define CL_CANON
354 #ifdef CL_CANON
355 if (f_zero_p(image) && f_unify_p(klass) &&
356 !k_float_p(real) && !k_float_p(image))
357 return real;
358 #else
359 if (f_zero_p(image) && f_unify_p(klass))
360 return real;
361 #endif
362 else if (f_scalar_p(real) && f_scalar_p(image))
363 return nucomp_s_new_internal(klass, real, image);
364 else if (f_scalar_p(real)) {
365 get_dat1(image);
367 return nucomp_s_new_internal(klass,
368 f_sub(real, dat->image),
369 f_add(ZERO, dat->real));
371 else if (f_scalar_p(image)) {
372 get_dat1(real);
374 return nucomp_s_new_internal(klass,
375 dat->real,
376 f_add(dat->image, image));
378 else {
379 get_dat2(real, image);
381 return nucomp_s_new_internal(klass,
382 f_sub(adat->real, bdat->image),
383 f_add(adat->image, bdat->real));
387 #if 0
388 static VALUE
389 nucomp_s_canonicalize(int argc, VALUE *argv, VALUE klass)
391 VALUE real, image;
393 switch (rb_scan_args(argc, argv, "11", &real, &image)) {
394 case 1:
395 image = ZERO;
396 break;
399 nucomp_real_check(real);
400 nucomp_real_check(image);
402 return nucomp_s_canonicalize_internal(klass, real, image);
404 #endif
406 static VALUE
407 nucomp_s_new(int argc, VALUE *argv, VALUE klass)
409 VALUE real, image;
411 switch (rb_scan_args(argc, argv, "11", &real, &image)) {
412 case 1:
413 image = ZERO;
414 break;
417 nucomp_real_check(real);
418 nucomp_real_check(image);
420 return nucomp_s_canonicalize_internal(klass, real, image);
423 inline static VALUE
424 f_complex_new1(VALUE klass, VALUE x)
426 assert(!k_complex_p(x));
427 return nucomp_s_canonicalize_internal(klass, x, ZERO);
430 inline static VALUE
431 f_complex_new2(VALUE klass, VALUE x, VALUE y)
433 assert(!k_complex_p(x));
434 return nucomp_s_canonicalize_internal(klass, x, y);
437 static VALUE
438 nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
440 return rb_funcall2(rb_cComplex, id_convert, argc, argv);
443 extern VALUE math_atan2(VALUE obj, VALUE x, VALUE y);
444 extern VALUE math_cos(VALUE obj, VALUE x);
445 extern VALUE math_cosh(VALUE obj, VALUE x);
446 extern VALUE math_exp(VALUE obj, VALUE x);
447 extern VALUE math_hypot(VALUE obj, VALUE x, VALUE y);
448 extern VALUE math_log(int argc, VALUE *argv);
449 extern VALUE math_sin(VALUE obj, VALUE x);
450 extern VALUE math_sinh(VALUE obj, VALUE x);
451 extern VALUE math_sqrt(VALUE obj, VALUE x);
453 #define m_atan2_bang(x,y) math_atan2(Qnil,x,y)
454 #define m_cos_bang(x) math_cos(Qnil,x)
455 #define m_cosh_bang(x) math_cosh(Qnil,x)
456 #define m_exp_bang(x) math_exp(Qnil,x)
457 #define m_hypot(x,y) math_hypot(Qnil,x,y)
459 static VALUE
460 m_log_bang(VALUE x)
462 return math_log(1, &x);
465 #define m_sin_bang(x) math_sin(Qnil,x)
466 #define m_sinh_bang(x) math_sinh(Qnil,x)
467 #define m_sqrt_bang(x) math_sqrt(Qnil,x)
469 static VALUE
470 m_cos(VALUE x)
472 get_dat1(x);
474 if (f_generic_p(x))
475 return m_cos_bang(x);
476 return f_complex_new2(rb_cComplex,
477 f_mul(m_cos_bang(dat->real),
478 m_cosh_bang(dat->image)),
479 f_mul(f_negate(m_sin_bang(dat->real)),
480 m_sinh_bang(dat->image)));
483 static VALUE
484 m_sin(VALUE x)
486 get_dat1(x);
488 if (f_generic_p(x))
489 return m_sin_bang(x);
490 return f_complex_new2(rb_cComplex,
491 f_mul(m_sin_bang(dat->real),
492 m_cosh_bang(dat->image)),
493 f_mul(m_cos_bang(dat->real),
494 m_sinh_bang(dat->image)));
497 static VALUE
498 m_sqrt(VALUE x)
500 if (f_generic_p(x)) {
501 if (!f_negative_p(x))
502 return m_sqrt_bang(x);
503 return f_complex_new2(rb_cComplex, ZERO, m_sqrt_bang(f_negate(x)));
505 else {
506 get_dat1(x);
508 if (f_negative_p(dat->image))
509 return f_conjugate(m_sqrt(f_conjugate(x)));
510 else {
511 VALUE a = f_abs(x);
512 return f_complex_new2(rb_cComplex,
513 m_sqrt_bang(f_div(f_add(a, dat->real), TWO)),
514 m_sqrt_bang(f_div(f_sub(a, dat->real), TWO)));
519 static VALUE
520 nucomp_s_polar(VALUE klass, VALUE abs, VALUE arg)
522 return f_complex_new2(klass,
523 f_mul(abs, m_cos(arg)),
524 f_mul(abs, m_sin(arg)));
527 static VALUE
528 nucomp_real(VALUE self)
530 get_dat1(self);
531 return dat->real;
534 static VALUE
535 nucomp_image(VALUE self)
537 get_dat1(self);
538 return dat->image;
541 static VALUE
542 nucomp_add(VALUE self, VALUE other)
544 switch (TYPE(other)) {
545 case T_FIXNUM:
546 case T_BIGNUM:
547 case T_FLOAT:
548 case T_RATIONAL:
550 get_dat1(self);
552 return f_complex_new2(CLASS_OF(self),
553 f_add(dat->real, other), dat->image);
555 case T_COMPLEX:
557 VALUE real, image;
559 get_dat2(self, other);
561 real = f_add(adat->real, bdat->real);
562 image = f_add(adat->image, bdat->image);
564 return f_complex_new2(CLASS_OF(self), real, image);
566 default:
567 return rb_num_coerce_bin(self, other, '+');
571 static VALUE
572 nucomp_sub(VALUE self, VALUE other)
574 switch (TYPE(other)) {
575 case T_FIXNUM:
576 case T_BIGNUM:
577 case T_FLOAT:
578 case T_RATIONAL:
580 get_dat1(self);
582 return f_complex_new2(CLASS_OF(self),
583 f_sub(dat->real, other), dat->image);
585 case T_COMPLEX:
587 VALUE real, image;
589 get_dat2(self, other);
591 real = f_sub(adat->real, bdat->real);
592 image = f_sub(adat->image, bdat->image);
594 return f_complex_new2(CLASS_OF(self), real, image);
596 default:
597 return rb_num_coerce_bin(self, other, '-');
601 static VALUE
602 nucomp_mul(VALUE self, VALUE other)
604 switch (TYPE(other)) {
605 case T_FIXNUM:
606 case T_BIGNUM:
607 case T_FLOAT:
608 case T_RATIONAL:
610 get_dat1(self);
612 return f_complex_new2(CLASS_OF(self),
613 f_mul(dat->real, other),
614 f_mul(dat->image, other));
616 case T_COMPLEX:
618 VALUE real, image;
620 get_dat2(self, other);
622 real = f_sub(f_mul(adat->real, bdat->real),
623 f_mul(adat->image, bdat->image));
624 image = f_add(f_mul(adat->real, bdat->image),
625 f_mul(adat->image, bdat->real));
627 return f_complex_new2(CLASS_OF(self), real, image);
629 default:
630 return rb_num_coerce_bin(self, other, '*');
634 static VALUE
635 nucomp_div(VALUE self, VALUE other)
637 switch (TYPE(other)) {
638 case T_FIXNUM:
639 case T_BIGNUM:
640 case T_FLOAT:
641 case T_RATIONAL:
643 get_dat1(self);
645 return f_complex_new2(CLASS_OF(self),
646 f_div(dat->real, other),
647 f_div(dat->image, other));
649 case T_COMPLEX:
651 get_dat2(self, other);
653 if (TYPE(adat->real) == T_FLOAT ||
654 TYPE(adat->image) == T_FLOAT ||
655 TYPE(bdat->real) == T_FLOAT ||
656 TYPE(bdat->image) == T_FLOAT) {
657 VALUE magn = m_hypot(bdat->real, bdat->image);
658 VALUE tmp = f_complex_new_bang2(CLASS_OF(self),
659 f_div(bdat->real, magn),
660 f_div(bdat->image, magn));
661 return f_div(f_mul(self, f_conjugate(tmp)), magn);
663 return f_div(f_mul(self, f_conjugate(other)), f_abs2(other));
665 default:
666 return rb_num_coerce_bin(self, other, '/');
670 static VALUE
671 nucomp_quo(VALUE self, VALUE other)
673 get_dat1(self);
675 return f_div(f_complex_new2(CLASS_OF(self),
676 f_quo(dat->real, ONE),
677 f_quo(dat->image, ONE)), other);
680 static VALUE
681 nucomp_fdiv(VALUE self, VALUE other)
683 get_dat1(self);
685 return f_div(f_complex_new2(CLASS_OF(self),
686 f_to_f(dat->real),
687 f_to_f(dat->image)), other);
690 static VALUE
691 nucomp_expt(VALUE self, VALUE other)
693 if (f_zero_p(other))
694 return f_complex_new_bang1(CLASS_OF(self), ONE);
696 if (k_rational_p(other) && f_one_p(f_denominator(other)))
697 other = f_numerator(other); /* good? */
699 switch (TYPE(other)) {
700 case T_FIXNUM:
701 case T_BIGNUM:
702 if (f_gt_p(other, ZERO)) {
703 VALUE x, z, n;
705 x = self;
706 z = x;
707 n = f_sub(other, ONE);
709 while (!f_zero_p(n)) {
710 VALUE a;
712 while (a = f_divmod(n, TWO),
713 f_zero_p(RARRAY_PTR(a)[1])) {
714 get_dat1(x);
716 x = f_complex_new2(CLASS_OF(self),
717 f_sub(f_mul(dat->real, dat->real),
718 f_mul(dat->image, dat->image)),
719 f_mul(f_mul(TWO, dat->real), dat->image));
720 n = RARRAY_PTR(a)[0];
722 z = f_mul(z, x);
723 n = f_sub(n, ONE);
725 return z;
727 return f_expt(f_div(f_to_r(ONE), self), f_negate(other));
728 case T_FLOAT:
729 case T_RATIONAL:
731 VALUE a, r, theta;
733 a = f_polar(self);
734 r = RARRAY_PTR(a)[0];
735 theta = RARRAY_PTR(a)[1];
736 return nucomp_s_polar(CLASS_OF(self), f_expt(r, other),
737 f_mul(theta, other));
739 case T_COMPLEX:
741 VALUE a, r, theta, ore, oim, nr, ntheta;
743 get_dat1(other);
745 a = f_polar(self);
746 r = RARRAY_PTR(a)[0];
747 theta = RARRAY_PTR(a)[1];
749 ore = dat->real;
750 oim = dat->image;
751 nr = m_exp_bang(f_sub(f_mul(ore, m_log_bang(r)),
752 f_mul(oim, theta)));
753 ntheta = f_add(f_mul(theta, ore), f_mul(oim, m_log_bang(r)));
754 return nucomp_s_polar(CLASS_OF(self), nr, ntheta);
756 default:
757 return rb_num_coerce_bin(self, other, id_expt);
761 static VALUE
762 nucomp_equal_p(VALUE self, VALUE other)
764 switch (TYPE(other)) {
765 case T_FIXNUM:
766 case T_BIGNUM:
767 case T_FLOAT:
768 case T_RATIONAL:
770 get_dat1(self);
772 return f_boolcast(f_equal_p(dat->real, other) && f_zero_p(dat->image));
774 case T_COMPLEX:
776 get_dat2(self, other);
778 return f_boolcast(f_equal_p(adat->real, bdat->real) &&
779 f_equal_p(adat->image, bdat->image));
781 default:
782 return f_equal_p(other, self);
786 static VALUE
787 nucomp_coerce(VALUE self, VALUE other)
789 switch (TYPE(other)) {
790 case T_FIXNUM:
791 case T_BIGNUM:
792 case T_FLOAT:
793 case T_RATIONAL:
794 return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
797 rb_raise(rb_eTypeError, "%s can't be coerced into %s",
798 rb_obj_classname(other), rb_obj_classname(self));
799 return Qnil;
802 static VALUE
803 nucomp_abs(VALUE self)
805 get_dat1(self);
806 return m_hypot(dat->real, dat->image);
809 static VALUE
810 nucomp_abs2(VALUE self)
812 get_dat1(self);
813 return f_add(f_mul(dat->real, dat->real),
814 f_mul(dat->image, dat->image));
817 static VALUE
818 nucomp_arg(VALUE self)
820 get_dat1(self);
821 return m_atan2_bang(dat->image, dat->real);
824 static VALUE
825 nucomp_polar(VALUE self)
827 return rb_assoc_new(f_abs(self), f_arg(self));
830 static VALUE
831 nucomp_conjugate(VALUE self)
833 get_dat1(self);
834 return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->image));
837 #if 0
838 static VALUE
839 nucomp_real_p(VALUE self)
841 return Qfalse;
844 static VALUE
845 nucomp_complex_p(VALUE self)
847 return Qtrue;
850 static VALUE
851 nucomp_exact_p(VALUE self)
853 get_dat1(self);
854 return f_boolcast(f_exact_p(dat->real) && f_exact_p(dat->image));
857 static VALUE
858 nucomp_inexact_p(VALUE self)
860 return f_boolcast(!nucomp_exact_p(self));
862 #endif
864 extern VALUE rb_lcm(VALUE x, VALUE y);
866 static VALUE
867 nucomp_denominator(VALUE self)
869 get_dat1(self);
870 return rb_lcm(f_denominator(dat->real), f_denominator(dat->image));
873 static VALUE
874 nucomp_numerator(VALUE self)
876 VALUE cd;
878 get_dat1(self);
880 cd = f_denominator(self);
881 return f_complex_new2(CLASS_OF(self),
882 f_mul(f_numerator(dat->real),
883 f_div(cd, f_denominator(dat->real))),
884 f_mul(f_numerator(dat->image),
885 f_div(cd, f_denominator(dat->image))));
888 static VALUE
889 nucomp_hash(VALUE self)
891 get_dat1(self);
892 return f_xor(dat->real, dat->image);
895 #ifndef HAVE_SIGNBIT
896 #ifdef signbit
897 #define HAVE_SIGNBIT 1
898 #endif
899 #endif
901 inline static VALUE
902 f_signbit(VALUE x)
904 switch (TYPE(x)) {
905 case T_FLOAT:
906 #ifdef HAVE_SIGNBIT
907 return f_boolcast(signbit(RFLOAT_VALUE(x)));
908 #else
910 char s[2];
912 (void)snprintf(s, sizeof s, "%.0f", RFLOAT_VALUE(x));
914 return f_boolcast(s[0] == '-');
916 #endif
918 return f_negative_p(x);
921 inline static VALUE
922 f_tzero_p(VALUE x)
924 return f_boolcast(f_zero_p(x) && !f_signbit(x));
927 inline static VALUE
928 f_tpositive_p(VALUE x)
930 return f_boolcast(!f_signbit(x));
933 static VALUE
934 nucomp_to_s(VALUE self)
936 VALUE s, rezero, impos;
938 get_dat1(self);
940 rezero = f_tzero_p(dat->real);
941 impos = f_tpositive_p(dat->image);
943 if (rezero)
944 s = rb_str_new2("");
945 else {
946 s = f_to_s(dat->real);
947 rb_str_cat2(s, !impos ? "-" : "+");
950 if (k_rational_p(dat->image) &&
951 !f_one_p(f_denominator(dat->image))) {
952 rb_str_cat2(s, "(");
953 rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
954 rb_str_cat2(s, ")i");
956 else {
957 rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
958 rb_str_cat2(s, "i");
961 return s;
964 static VALUE
965 nucomp_inspect(VALUE self)
967 VALUE s;
969 get_dat1(self);
971 s = rb_str_new2("Complex(");
972 rb_str_concat(s, f_inspect(dat->real));
973 rb_str_cat2(s, ", ");
974 rb_str_concat(s, f_inspect(dat->image));
975 rb_str_cat2(s, ")");
977 return s;
980 static VALUE
981 nucomp_marshal_dump(VALUE self)
983 get_dat1(self);
984 return rb_assoc_new(dat->real, dat->image);
987 static VALUE
988 nucomp_marshal_load(VALUE self, VALUE a)
990 get_dat1(self);
991 dat->real = RARRAY_PTR(a)[0];
992 dat->image = RARRAY_PTR(a)[1];
993 return self;
996 /* --- */
998 VALUE
999 rb_complex_raw(VALUE x, VALUE y)
1001 return nucomp_s_new_internal(rb_cComplex, x, y);
1004 VALUE
1005 rb_complex_new(VALUE x, VALUE y)
1007 return nucomp_s_canonicalize_internal(rb_cComplex, x, y);
1010 static VALUE nucomp_s_convert(int argc, VALUE *argv, VALUE klass);
1012 VALUE
1013 rb_Complex(VALUE x, VALUE y)
1015 VALUE a[2];
1016 a[0] = x;
1017 a[1] = y;
1018 return nucomp_s_convert(2, a, rb_cComplex);
1021 static VALUE
1022 nucomp_scalar_p(VALUE self)
1024 return Qfalse;
1027 static VALUE
1028 nucomp_to_i(VALUE self)
1030 get_dat1(self);
1032 if (k_float_p(dat->image) || !f_zero_p(dat->image)) {
1033 VALUE s = f_to_s(self);
1034 rb_raise(rb_eRangeError, "can't convert %s into Integer",
1035 StringValuePtr(s));
1037 return f_to_i(dat->real);
1040 static VALUE
1041 nucomp_to_f(VALUE self)
1043 get_dat1(self);
1045 if (k_float_p(dat->image) || !f_zero_p(dat->image)) {
1046 VALUE s = f_to_s(self);
1047 rb_raise(rb_eRangeError, "can't convert %s into Float",
1048 StringValuePtr(s));
1050 return f_to_f(dat->real);
1053 static VALUE
1054 nucomp_to_r(VALUE self)
1056 get_dat1(self);
1058 if (k_float_p(dat->image) || !f_zero_p(dat->image)) {
1059 VALUE s = f_to_s(self);
1060 rb_raise(rb_eRangeError, "can't convert %s into Rational",
1061 StringValuePtr(s));
1063 return f_to_r(dat->real);
1066 static VALUE
1067 nilclass_to_c(VALUE self)
1069 return rb_complex_new1(INT2FIX(0));
1072 static VALUE
1073 numeric_to_c(VALUE self)
1075 return rb_complex_new1(self);
1078 static VALUE comp_pat1, comp_pat2, a_slash, a_dot_and_an_e,
1079 null_string, underscores_pat, an_underscore;
1081 #define DIGITS "(?:\\d(?:_\\d|\\d)*)"
1082 #define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?"
1083 #define DENOMINATOR "[-+]?" DIGITS
1084 #define NUMBER "[-+]?" NUMERATOR "(?:\\/" DENOMINATOR ")?"
1085 #define NUMBERNOS NUMERATOR "(?:\\/" DENOMINATOR ")?"
1086 #define PATTERN1 "\\A((" NUMBER ")|\\((" NUMBER ")\\))?[iIjJ]"
1087 #define PATTERN2 "\\A(" NUMBER ")(([-+])(?:(" NUMBERNOS ")|\\((" NUMBER ")\\))?[iIjJ])?"
1089 static void
1090 make_patterns(void)
1092 static const char comp_pat1_source[] = PATTERN1;
1093 static const char comp_pat2_source[] = PATTERN2;
1094 static const char underscores_pat_source[] = "_+";
1096 if (comp_pat1) return;
1098 comp_pat1 = rb_reg_new(comp_pat1_source, sizeof comp_pat1_source - 1, 0);
1099 rb_global_variable(&comp_pat1);
1101 comp_pat2 = rb_reg_new(comp_pat2_source, sizeof comp_pat2_source - 1, 0);
1102 rb_global_variable(&comp_pat2);
1104 a_slash = rb_str_new2("/");
1105 rb_global_variable(&a_slash);
1107 a_dot_and_an_e = rb_str_new2(".eE");
1108 rb_global_variable(&a_dot_and_an_e);
1110 null_string = rb_str_new2("");
1111 rb_global_variable(&null_string);
1113 underscores_pat = rb_reg_new(underscores_pat_source,
1114 sizeof underscores_pat_source - 1, 0);
1115 rb_global_variable(&underscores_pat);
1117 an_underscore = rb_str_new2("_");
1118 rb_global_variable(&an_underscore);
1121 #define id_strip rb_intern("strip")
1122 #define f_strip(x) rb_funcall(x, id_strip, 0)
1124 #define id_match rb_intern("match")
1125 #define f_match(x,y) rb_funcall(x, id_match, 1, y)
1127 #define id_aref rb_intern("[]")
1128 #define f_aref(x,y) rb_funcall(x, id_aref, 1, y)
1130 #define id_post_match rb_intern("post_match")
1131 #define f_post_match(x) rb_funcall(x, id_post_match, 0)
1133 #define id_split rb_intern("split")
1134 #define f_split(x,y) rb_funcall(x, id_split, 1, y)
1136 #define id_include_p rb_intern("include?")
1137 #define f_include_p(x,y) rb_funcall(x, id_include_p, 1, y)
1139 #define id_count rb_intern("count")
1140 #define f_count(x,y) rb_funcall(x, id_count, 1, y)
1142 #define id_gsub_bang rb_intern("gsub!")
1143 #define f_gsub_bang(x,y,z) rb_funcall(x, id_gsub_bang, 2, y, z)
1145 static VALUE
1146 string_to_c_internal(VALUE self)
1148 VALUE s;
1150 s = f_strip(self);
1152 if (RSTRING_LEN(s) == 0)
1153 return rb_assoc_new(Qnil, self);
1156 VALUE m, sr, si, re, r, i;
1158 m = f_match(comp_pat1, s);
1159 if (!NIL_P(m)) {
1160 sr = Qnil;
1161 si = f_aref(m, INT2FIX(1));
1162 if (NIL_P(si))
1163 si = rb_str_new2("1");
1164 else {
1165 si = f_aref(m, INT2FIX(2));
1166 if (NIL_P(si))
1167 si = f_aref(m, INT2FIX(3));
1169 re = f_post_match(m);
1171 if (NIL_P(m)) {
1172 m = f_match(comp_pat2, s);
1173 if (NIL_P(m)) {
1174 return rb_assoc_new(Qnil, self);
1176 sr = f_aref(m, INT2FIX(1));
1177 if (NIL_P(f_aref(m, INT2FIX(2))))
1178 si = Qnil;
1179 else {
1180 VALUE t;
1182 si = f_aref(m, INT2FIX(3));
1183 t = f_aref(m, INT2FIX(4));
1184 if (NIL_P(t))
1185 t = f_aref(m, INT2FIX(5));
1186 if (NIL_P(t))
1187 t = rb_str_new2("1");
1188 rb_str_concat(si, t);
1190 re = f_post_match(m);
1192 r = INT2FIX(0);
1193 i = INT2FIX(0);
1194 if (!NIL_P(sr)) {
1195 if (f_include_p(sr, a_slash))
1196 r = f_to_r(sr);
1197 else if (f_gt_p(f_count(sr, a_dot_and_an_e), INT2FIX(0)))
1198 r = f_to_f(sr);
1199 else
1200 r = f_to_i(sr);
1202 if (!NIL_P(si)) {
1203 if (f_include_p(si, a_slash))
1204 i = f_to_r(si);
1205 else if (f_gt_p(f_count(si, a_dot_and_an_e), INT2FIX(0)))
1206 i = f_to_f(si);
1207 else
1208 i = f_to_i(si);
1210 return rb_assoc_new(rb_complex_new2(r, i), re);
1214 static VALUE
1215 string_to_c_strict(VALUE self)
1217 VALUE a = string_to_c_internal(self);
1218 if (NIL_P(RARRAY_PTR(a)[0]) || RSTRING_LEN(RARRAY_PTR(a)[1]) > 0) {
1219 VALUE s = f_inspect(self);
1220 rb_raise(rb_eArgError, "invalid value for Complex: %s",
1221 StringValuePtr(s));
1223 return RARRAY_PTR(a)[0];
1226 #define id_gsub rb_intern("gsub")
1227 #define f_gsub(x,y,z) rb_funcall(x, id_gsub, 2, y, z)
1229 static VALUE
1230 string_to_c(VALUE self)
1232 VALUE s, a, backref;
1234 backref = rb_backref_get();
1235 rb_match_busy(backref);
1237 s = f_gsub(self, underscores_pat, an_underscore);
1238 a = string_to_c_internal(s);
1240 rb_backref_set(backref);
1242 if (!NIL_P(RARRAY_PTR(a)[0]))
1243 return RARRAY_PTR(a)[0];
1244 return rb_complex_new1(INT2FIX(0));
1247 static VALUE
1248 nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
1250 VALUE a1, a2, backref;
1252 rb_scan_args(argc, argv, "02", &a1, &a2);
1254 backref = rb_backref_get();
1255 rb_match_busy(backref);
1257 switch (TYPE(a1)) {
1258 case T_FIXNUM:
1259 case T_BIGNUM:
1260 case T_FLOAT:
1261 break;
1262 case T_STRING:
1263 a1 = string_to_c_strict(a1);
1264 break;
1267 switch (TYPE(a2)) {
1268 case T_FIXNUM:
1269 case T_BIGNUM:
1270 case T_FLOAT:
1271 break;
1272 case T_STRING:
1273 a2 = string_to_c_strict(a2);
1274 break;
1277 rb_backref_set(backref);
1279 switch (TYPE(a1)) {
1280 case T_COMPLEX:
1282 get_dat1(a1);
1284 if (!k_float_p(dat->image) && f_zero_p(dat->image))
1285 a1 = dat->real;
1289 switch (TYPE(a2)) {
1290 case T_COMPLEX:
1292 get_dat1(a2);
1294 if (!k_float_p(dat->image) && f_zero_p(dat->image))
1295 a2 = dat->real;
1299 switch (TYPE(a1)) {
1300 case T_COMPLEX:
1301 if (NIL_P(a2) || f_zero_p(a2))
1302 return a1;
1306 VALUE argv2[2];
1307 argv2[0] = a1;
1308 argv2[1] = a2;
1309 return nucomp_s_new(argc, argv2, klass);
1313 /* --- */
1315 #define id_Complex rb_intern("Complex")
1317 static VALUE
1318 numeric_re(VALUE self)
1320 return rb_Complex1(self);
1323 static VALUE
1324 numeric_im(VALUE self)
1326 return rb_Complex2(ZERO, self);
1329 static VALUE
1330 numeric_real(VALUE self)
1332 return self;
1335 static VALUE
1336 numeric_image(VALUE self)
1338 return INT2FIX(0);
1341 #define id_PI rb_intern("PI")
1343 static VALUE
1344 numeric_arg(VALUE self)
1346 if (!f_negative_p(self))
1347 return INT2FIX(0);
1348 return rb_const_get(rb_mMath, id_PI);
1351 static VALUE
1352 numeric_polar(VALUE self)
1354 return rb_assoc_new(f_abs(self), f_arg(self));
1357 static VALUE
1358 numeric_conjugate(VALUE self)
1360 return self;
1363 void
1364 Init_Complex(void)
1366 #undef rb_intern
1367 #define rb_intern(str) rb_intern_const(str)
1369 assert(fprintf(stderr, "assert() is now active\n"));
1371 id_Unify = rb_intern("Unify");
1372 id_abs = rb_intern("abs");
1373 id_abs2 = rb_intern("abs2");
1374 id_arg = rb_intern("arg");
1375 id_atan2_bang = rb_intern("atan2!");
1376 id_cmp = rb_intern("<=>");
1377 id_conjugate = rb_intern("conjugate");
1378 id_convert = rb_intern("convert");
1379 id_cos = rb_intern("cos");
1380 id_denominator = rb_intern("denominator");
1381 id_divmod = rb_intern("divmod");
1382 id_equal_p = rb_intern("==");
1383 id_exact_p = rb_intern("exact?");
1384 id_exp_bang = rb_intern("exp!");
1385 id_expt = rb_intern("**");
1386 id_floor = rb_intern("floor");
1387 id_format = rb_intern("format");
1388 id_hypot = rb_intern("hypot");
1389 id_idiv = rb_intern("div");
1390 id_inspect = rb_intern("inspect");
1391 id_log_bang = rb_intern("log!");
1392 id_negate = rb_intern("-@");
1393 id_new = rb_intern("new");
1394 id_new_bang = rb_intern("new!");
1395 id_numerator = rb_intern("numerator");
1396 id_polar = rb_intern("polar");
1397 id_quo = rb_intern("quo");
1398 id_scalar_p = rb_intern("scalar?");
1399 id_sin = rb_intern("sin");
1400 id_sqrt = rb_intern("sqrt");
1401 id_to_f = rb_intern("to_f");
1402 id_to_i = rb_intern("to_i");
1403 id_to_r = rb_intern("to_r");
1404 id_to_s = rb_intern("to_s");
1405 id_truncate = rb_intern("truncate");
1407 rb_cComplex = rb_define_class(COMPLEX_NAME, rb_cNumeric);
1409 rb_define_alloc_func(rb_cComplex, nucomp_s_alloc);
1410 rb_funcall(rb_cComplex, rb_intern("private_class_method"), 1,
1411 ID2SYM(rb_intern("allocate")));
1413 rb_define_singleton_method(rb_cComplex, "generic?", nucomp_s_generic_p, 1);
1415 rb_define_singleton_method(rb_cComplex, "new!", nucomp_s_new_bang, -1);
1416 rb_funcall(rb_cComplex, rb_intern("private_class_method"), 1,
1417 ID2SYM(rb_intern("new!")));
1419 rb_define_singleton_method(rb_cComplex, "new", nucomp_s_new, -1);
1420 rb_funcall(rb_cComplex, rb_intern("private_class_method"), 1,
1421 ID2SYM(rb_intern("new")));
1423 #if 0
1424 rb_define_singleton_method(rb_cComplex, "rect", nucomp_s_new, -1);
1425 rb_define_singleton_method(rb_cComplex, "rectangular", nucomp_s_new, -1);
1426 #endif
1427 rb_define_singleton_method(rb_cComplex, "polar", nucomp_s_polar, 2);
1429 rb_define_global_function(COMPLEX_NAME, nucomp_f_complex, -1);
1431 rb_undef_method(rb_cComplex, "<");
1432 rb_undef_method(rb_cComplex, "<=");
1433 rb_undef_method(rb_cComplex, "<=>");
1434 rb_undef_method(rb_cComplex, ">");
1435 rb_undef_method(rb_cComplex, ">=");
1436 rb_undef_method(rb_cComplex, "between?");
1437 rb_undef_method(rb_cComplex, "divmod");
1438 rb_undef_method(rb_cComplex, "floor");
1439 rb_undef_method(rb_cComplex, "ceil");
1440 rb_undef_method(rb_cComplex, "modulo");
1441 rb_undef_method(rb_cComplex, "round");
1442 rb_undef_method(rb_cComplex, "step");
1443 rb_undef_method(rb_cComplex, "truncate");
1445 #if NUBY
1446 rb_undef_method(rb_cComplex, "//");
1447 #endif
1449 rb_define_method(rb_cComplex, "real", nucomp_real, 0);
1450 rb_define_method(rb_cComplex, "image", nucomp_image, 0);
1451 rb_define_method(rb_cComplex, "imag", nucomp_image, 0);
1453 rb_define_method(rb_cComplex, "+", nucomp_add, 1);
1454 rb_define_method(rb_cComplex, "-", nucomp_sub, 1);
1455 rb_define_method(rb_cComplex, "*", nucomp_mul, 1);
1456 rb_define_method(rb_cComplex, "/", nucomp_div, 1);
1457 rb_define_method(rb_cComplex, "quo", nucomp_quo, 1);
1458 rb_define_method(rb_cComplex, "fdiv", nucomp_fdiv, 1);
1459 rb_define_method(rb_cComplex, "**", nucomp_expt, 1);
1461 rb_define_method(rb_cComplex, "==", nucomp_equal_p, 1);
1462 rb_define_method(rb_cComplex, "coerce", nucomp_coerce, 1);
1464 rb_define_method(rb_cComplex, "abs", nucomp_abs, 0);
1465 #if 0
1466 rb_define_method(rb_cComplex, "magnitude", nucomp_abs, 0);
1467 #endif
1468 rb_define_method(rb_cComplex, "abs2", nucomp_abs2, 0);
1469 rb_define_method(rb_cComplex, "arg", nucomp_arg, 0);
1470 rb_define_method(rb_cComplex, "angle", nucomp_arg, 0);
1471 rb_define_method(rb_cComplex, "polar", nucomp_polar, 0);
1472 rb_define_method(rb_cComplex, "conjugate", nucomp_conjugate, 0);
1473 rb_define_method(rb_cComplex, "conj", nucomp_conjugate, 0);
1474 #if 0
1475 rb_define_method(rb_cComplex, "~", nucomp_conjugate, 0); /* gcc */
1476 #endif
1478 #if 0
1479 rb_define_method(rb_cComplex, "real?", nucomp_real_p, 0);
1480 rb_define_method(rb_cComplex, "complex?", nucomp_complex_p, 0);
1481 rb_define_method(rb_cComplex, "exact?", nucomp_exact_p, 0);
1482 rb_define_method(rb_cComplex, "inexact?", nucomp_inexact_p, 0);
1483 #endif
1485 rb_define_method(rb_cComplex, "numerator", nucomp_numerator, 0);
1486 rb_define_method(rb_cComplex, "denominator", nucomp_denominator, 0);
1488 rb_define_method(rb_cComplex, "hash", nucomp_hash, 0);
1490 rb_define_method(rb_cComplex, "to_s", nucomp_to_s, 0);
1491 rb_define_method(rb_cComplex, "inspect", nucomp_inspect, 0);
1493 rb_define_method(rb_cComplex, "marshal_dump", nucomp_marshal_dump, 0);
1494 rb_define_method(rb_cComplex, "marshal_load", nucomp_marshal_load, 1);
1496 /* --- */
1498 rb_define_method(rb_cComplex, "scalar?", nucomp_scalar_p, 0);
1499 rb_define_method(rb_cComplex, "to_i", nucomp_to_i, 0);
1500 rb_define_method(rb_cComplex, "to_f", nucomp_to_f, 0);
1501 rb_define_method(rb_cComplex, "to_r", nucomp_to_r, 0);
1502 rb_define_method(rb_cNilClass, "to_c", nilclass_to_c, 0);
1503 rb_define_method(rb_cNumeric, "to_c", numeric_to_c, 0);
1505 make_patterns();
1507 rb_define_method(rb_cString, "to_c", string_to_c, 0);
1509 rb_define_singleton_method(rb_cComplex, "convert", nucomp_s_convert, -1);
1510 rb_funcall(rb_cComplex, rb_intern("private_class_method"), 1,
1511 ID2SYM(rb_intern("convert")));
1513 /* --- */
1515 rb_define_method(rb_cNumeric, "re", numeric_re, 0);
1516 rb_define_method(rb_cNumeric, "im", numeric_im, 0);
1517 rb_define_method(rb_cNumeric, "real", numeric_real, 0);
1518 rb_define_method(rb_cNumeric, "image", numeric_image, 0);
1519 rb_define_method(rb_cNumeric, "imag", numeric_image, 0);
1520 rb_define_method(rb_cNumeric, "arg", numeric_arg, 0);
1521 rb_define_method(rb_cNumeric, "angle", numeric_arg, 0);
1522 rb_define_method(rb_cNumeric, "polar", numeric_polar, 0);
1523 rb_define_method(rb_cNumeric, "conjugate", numeric_conjugate, 0);
1524 rb_define_method(rb_cNumeric, "conj", numeric_conjugate, 0);
1526 rb_define_const(rb_cComplex, "I",
1527 f_complex_new_bang2(rb_cComplex, ZERO, ONE));