diff options
-rw-r--r-- | ChangeLog | 19 | ||||
-rw-r--r-- | complex.c | 198 | ||||
-rw-r--r-- | gc.c | 2 | ||||
-rw-r--r-- | include/ruby/ruby.h | 2 | ||||
-rw-r--r-- | lib/cmath.rb | 32 | ||||
-rw-r--r-- | lib/complex.rb | 6 | ||||
-rw-r--r-- | lib/mathn.rb | 6 | ||||
-rw-r--r-- | rational.c | 4 | ||||
-rw-r--r-- | test/ruby/test_complex.rb | 181 |
9 files changed, 223 insertions, 227 deletions
@@ -1,3 +1,22 @@ +Sun Sep 21 07:43:16 2008 Tadayoshi Funaba <tadf@dotrb.org> + + * complex.c: an instance method image has been removed and + uses "imag" instead of "image". + + * complex.c: two instance method re and im are removed. + + * rational.c: follows the above changes. + + * include/ruby/ruby.h: ditto. + + * gc.c: ditto. + + * lib/cmath.rb: ditto. + + * lib/mathn.rb: ditto. + + * lib/complex.rb: ditto. + Sun Sep 21 02:48:45 2008 NARUSE, Yui <naruse@ruby-lang.org> * ext/json/ext/parser/parse.c: use ruby_xfree(). @@ -260,13 +260,13 @@ k_complex_p(VALUE x) bdat = ((struct RComplex *)(y)) inline static VALUE -nucomp_s_new_internal(VALUE klass, VALUE real, VALUE image) +nucomp_s_new_internal(VALUE klass, VALUE real, VALUE imag) { NEWOBJ(obj, struct RComplex); OBJSETUP(obj, klass, T_COMPLEX); obj->real = real; - obj->image = image; + obj->imag = imag; return (VALUE)obj; } @@ -280,23 +280,23 @@ nucomp_s_alloc(VALUE klass) static VALUE nucomp_s_new_bang(int argc, VALUE *argv, VALUE klass) { - VALUE real, image; + VALUE real, imag; - switch (rb_scan_args(argc, argv, "11", &real, &image)) { + switch (rb_scan_args(argc, argv, "11", &real, &imag)) { case 1: if (!k_numeric_p(real)) real = f_to_i(real); - image = ZERO; + imag = ZERO; break; default: if (!k_numeric_p(real)) real = f_to_i(real); - if (!k_numeric_p(image)) - image = f_to_i(image); + if (!k_numeric_p(imag)) + imag = f_to_i(imag); break; } - return nucomp_s_new_internal(klass, real, image); + return nucomp_s_new_internal(klass, real, imag); } inline static VALUE @@ -332,39 +332,39 @@ nucomp_real_check(VALUE num) } inline static VALUE -nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE image) +nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag) { #define CL_CANON #ifdef CL_CANON - if (f_zero_p(image) && f_unify_p(klass) && - k_exact_p(real) && k_exact_p(image)) + if (f_zero_p(imag) && f_unify_p(klass) && + k_exact_p(real) && k_exact_p(imag)) return real; #else - if (f_zero_p(image) && f_unify_p(klass)) + if (f_zero_p(imag) && f_unify_p(klass)) return real; #endif - else if (f_real_p(real) && f_real_p(image)) - return nucomp_s_new_internal(klass, real, image); + else 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(image); + get_dat1(imag); return nucomp_s_new_internal(klass, - f_sub(real, dat->image), + f_sub(real, dat->imag), f_add(ZERO, dat->real)); } - else if (f_real_p(image)) { + else if (f_real_p(imag)) { get_dat1(real); return nucomp_s_new_internal(klass, dat->real, - f_add(dat->image, image)); + f_add(dat->imag, imag)); } else { - get_dat2(real, image); + get_dat2(real, imag); return nucomp_s_new_internal(klass, - f_sub(adat->real, bdat->image), - f_add(adat->image, bdat->real)); + f_sub(adat->real, bdat->imag), + f_add(adat->imag, bdat->real)); } } @@ -372,40 +372,40 @@ nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE image) static VALUE nucomp_s_canonicalize(int argc, VALUE *argv, VALUE klass) { - VALUE real, image; + VALUE real, imag; - switch (rb_scan_args(argc, argv, "11", &real, &image)) { + switch (rb_scan_args(argc, argv, "11", &real, &imag)) { case 1: nucomp_real_check(real); - image = ZERO; + imag = ZERO; break; default: nucomp_real_check(real); - nucomp_real_check(image); + nucomp_real_check(imag); break; } - return nucomp_s_canonicalize_internal(klass, real, image); + return nucomp_s_canonicalize_internal(klass, real, imag); } #endif static VALUE nucomp_s_new(int argc, VALUE *argv, VALUE klass) { - VALUE real, image; + VALUE real, imag; - switch (rb_scan_args(argc, argv, "11", &real, &image)) { + switch (rb_scan_args(argc, argv, "11", &real, &imag)) { case 1: nucomp_real_check(real); - image = ZERO; + imag = ZERO; break; default: nucomp_real_check(real); - nucomp_real_check(image); + nucomp_real_check(imag); break; } - return nucomp_s_canonicalize_internal(klass, real, image); + return nucomp_s_canonicalize_internal(klass, real, imag); } inline static VALUE @@ -463,9 +463,9 @@ m_cos(VALUE x) return m_cos_bang(x); return f_complex_new2(rb_cComplex, f_mul(m_cos_bang(dat->real), - m_cosh_bang(dat->image)), + m_cosh_bang(dat->imag)), f_mul(f_negate(m_sin_bang(dat->real)), - m_sinh_bang(dat->image))); + m_sinh_bang(dat->imag))); } static VALUE @@ -477,9 +477,9 @@ m_sin(VALUE x) return m_sin_bang(x); return f_complex_new2(rb_cComplex, f_mul(m_sin_bang(dat->real), - m_cosh_bang(dat->image)), + m_cosh_bang(dat->imag)), f_mul(m_cos_bang(dat->real), - m_sinh_bang(dat->image))); + m_sinh_bang(dat->imag))); } static VALUE @@ -493,7 +493,7 @@ m_sqrt(VALUE x) else { get_dat1(x); - if (f_negative_p(dat->image)) + if (f_negative_p(dat->imag)) return f_conj(m_sqrt(f_conj(x))); else { VALUE a = f_abs(x); @@ -528,10 +528,10 @@ nucomp_real(VALUE self) } static VALUE -nucomp_image(VALUE self) +nucomp_imag(VALUE self) { get_dat1(self); - return dat->image; + return dat->imag; } static VALUE @@ -539,27 +539,27 @@ nucomp_negate(VALUE self) { get_dat1(self); return f_complex_new2(CLASS_OF(self), - f_negate(dat->real), f_negate(dat->image)); + f_negate(dat->real), f_negate(dat->imag)); } static VALUE nucomp_add(VALUE self, VALUE other) { if (k_complex_p(other)) { - VALUE real, image; + VALUE real, imag; get_dat2(self, other); real = f_add(adat->real, bdat->real); - image = f_add(adat->image, bdat->image); + imag = f_add(adat->imag, bdat->imag); - return f_complex_new2(CLASS_OF(self), real, image); + 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->image); + f_add(dat->real, other), dat->imag); } return rb_num_coerce_bin(self, other, '+'); } @@ -568,20 +568,20 @@ static VALUE nucomp_sub(VALUE self, VALUE other) { if (k_complex_p(other)) { - VALUE real, image; + VALUE real, imag; get_dat2(self, other); real = f_sub(adat->real, bdat->real); - image = f_sub(adat->image, bdat->image); + imag = f_sub(adat->imag, bdat->imag); - return f_complex_new2(CLASS_OF(self), real, image); + 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->image); + f_sub(dat->real, other), dat->imag); } return rb_num_coerce_bin(self, other, '-'); } @@ -590,23 +590,23 @@ static VALUE nucomp_mul(VALUE self, VALUE other) { if (k_complex_p(other)) { - VALUE real, image; + VALUE real, imag; get_dat2(self, other); real = f_sub(f_mul(adat->real, bdat->real), - f_mul(adat->image, bdat->image)); - image = f_add(f_mul(adat->real, bdat->image), - f_mul(adat->image, 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, image); + 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->image, other)); + f_mul(dat->imag, other)); } return rb_num_coerce_bin(self, other, '*'); } @@ -620,13 +620,13 @@ nucomp_div(VALUE self, VALUE other) get_dat2(self, other); if (TYPE(adat->real) == T_FLOAT || - TYPE(adat->image) == T_FLOAT || + TYPE(adat->imag) == T_FLOAT || TYPE(bdat->real) == T_FLOAT || - TYPE(bdat->image) == T_FLOAT) { - VALUE magn = m_hypot(bdat->real, bdat->image); + TYPE(bdat->imag) == T_FLOAT) { + VALUE magn = m_hypot(bdat->real, bdat->imag); VALUE tmp = f_complex_new_bang2(CLASS_OF(self), f_div(bdat->real, magn), - f_div(bdat->image, magn)); + f_div(bdat->imag, magn)); return f_div(f_mul(self, f_conj(tmp)), magn); } return f_div(f_mul(self, f_conj(other)), f_abs2(other)); @@ -636,7 +636,7 @@ nucomp_div(VALUE self, VALUE other) return f_complex_new2(CLASS_OF(self), f_div(dat->real, other), - f_div(dat->image, other)); + f_div(dat->imag, other)); } return rb_num_coerce_bin(self, other, '/'); } @@ -651,7 +651,7 @@ nucomp_fdiv(VALUE self, VALUE other) return f_div(f_complex_new2(CLASS_OF(self), f_to_f(dat->real), - f_to_f(dat->image)), other); + f_to_f(dat->imag)), other); } static VALUE @@ -673,7 +673,7 @@ nucomp_expt(VALUE self, VALUE other) theta = RARRAY_PTR(a)[1]; ore = dat->real; - oim = dat->image; + oim = dat->imag; nr = m_exp_bang(f_sub(f_mul(ore, m_log_bang(r)), f_mul(oim, theta))); ntheta = f_add(f_mul(theta, ore), f_mul(oim, m_log_bang(r))); @@ -696,8 +696,8 @@ nucomp_expt(VALUE self, VALUE other) x = f_complex_new2(CLASS_OF(self), f_sub(f_mul(dat->real, dat->real), - f_mul(dat->image, dat->image)), - f_mul(f_mul(TWO, dat->real), dat->image)); + f_mul(dat->imag, dat->imag)), + f_mul(f_mul(TWO, dat->real), dat->imag)); n = RARRAY_PTR(a)[0]; } z = f_mul(z, x); @@ -726,12 +726,12 @@ nucomp_equal_p(VALUE self, VALUE other) get_dat2(self, other); return f_boolcast(f_equal_p(adat->real, bdat->real) && - f_equal_p(adat->image, bdat->image)); + f_equal_p(adat->imag, bdat->imag)); } if (k_numeric_p(other) && f_real_p(other)) { get_dat1(self); - return f_boolcast(f_equal_p(dat->real, other) && f_zero_p(dat->image)); + return f_boolcast(f_equal_p(dat->real, other) && f_zero_p(dat->imag)); } return f_equal_p(other, self); } @@ -751,7 +751,7 @@ static VALUE nucomp_abs(VALUE self) { get_dat1(self); - return m_hypot(dat->real, dat->image); + return m_hypot(dat->real, dat->imag); } static VALUE @@ -759,21 +759,21 @@ nucomp_abs2(VALUE self) { get_dat1(self); return f_add(f_mul(dat->real, dat->real), - f_mul(dat->image, dat->image)); + f_mul(dat->imag, dat->imag)); } static VALUE nucomp_arg(VALUE self) { get_dat1(self); - return m_atan2_bang(dat->image, dat->real); + return m_atan2_bang(dat->imag, dat->real); } static VALUE nucomp_rect(VALUE self) { get_dat1(self); - return rb_assoc_new(dat->real, dat->image); + return rb_assoc_new(dat->real, dat->imag); } static VALUE @@ -786,7 +786,7 @@ static VALUE nucomp_conj(VALUE self) { get_dat1(self); - return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->image)); + return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag)); } #if 0 @@ -808,7 +808,7 @@ static VALUE nucomp_exact_p(VALUE self) { get_dat1(self); - return f_boolcast(f_exact_p(dat->real) && f_exact_p(dat->image)); + return f_boolcast(f_exact_p(dat->real) && f_exact_p(dat->imag)); } static VALUE @@ -824,7 +824,7 @@ static VALUE nucomp_denominator(VALUE self) { get_dat1(self); - return rb_lcm(f_denominator(dat->real), f_denominator(dat->image)); + return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag)); } static VALUE @@ -838,15 +838,15 @@ nucomp_numerator(VALUE self) return f_complex_new2(CLASS_OF(self), f_mul(f_numerator(dat->real), f_div(cd, f_denominator(dat->real))), - f_mul(f_numerator(dat->image), - f_div(cd, f_denominator(dat->image)))); + f_mul(f_numerator(dat->imag), + f_div(cd, f_denominator(dat->imag)))); } static VALUE nucomp_hash(VALUE self) { get_dat1(self); - return f_xor(f_hash(dat->real), f_hash(dat->image)); + return f_xor(f_hash(dat->real), f_hash(dat->imag)); } static VALUE @@ -856,7 +856,7 @@ nucomp_eql_p(VALUE self, VALUE other) get_dat2(self, other); return f_boolcast((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) && - (CLASS_OF(adat->image) == CLASS_OF(bdat->image)) && + (CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) && f_equal_p(self, other)); } @@ -902,12 +902,12 @@ nucomp_to_s(VALUE self) get_dat1(self); - impos = f_tpositive_p(dat->image); + impos = f_tpositive_p(dat->imag); s = f_to_s(dat->real); rb_str_cat2(s, !impos ? "-" : "+"); - rb_str_concat(s, f_to_s(f_abs(dat->image))); + rb_str_concat(s, f_to_s(f_abs(dat->imag))); rb_str_cat2(s, "i"); return s; @@ -920,13 +920,13 @@ nucomp_inspect(VALUE self) get_dat1(self); - impos = f_tpositive_p(dat->image); + impos = f_tpositive_p(dat->imag); s = rb_str_new2("("); rb_str_concat(s, f_inspect(dat->real)); rb_str_cat2(s, !impos ? "-" : "+"); - rb_str_concat(s, f_inspect(f_abs(dat->image))); + rb_str_concat(s, f_inspect(f_abs(dat->imag))); rb_str_cat2(s, "i)"); return s; @@ -938,7 +938,7 @@ nucomp_marshal_dump(VALUE self) VALUE a; get_dat1(self); - a = rb_assoc_new(dat->real, dat->image); + a = rb_assoc_new(dat->real, dat->imag); rb_copy_generic_ivar(a, self); return a; } @@ -948,7 +948,7 @@ nucomp_marshal_load(VALUE self, VALUE a) { get_dat1(self); dat->real = RARRAY_PTR(a)[0]; - dat->image = RARRAY_PTR(a)[1]; + dat->imag = RARRAY_PTR(a)[1]; rb_copy_generic_ivar(self, a); return self; } @@ -989,7 +989,7 @@ nucomp_to_i(VALUE self) { get_dat1(self); - if (k_inexact_p(dat->image) || !f_zero_p(dat->image)) { + if (k_inexact_p(dat->imag) || !f_zero_p(dat->imag)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Integer", StringValuePtr(s)); @@ -1002,7 +1002,7 @@ nucomp_to_f(VALUE self) { get_dat1(self); - if (k_inexact_p(dat->image) || !f_zero_p(dat->image)) { + if (k_inexact_p(dat->imag) || !f_zero_p(dat->imag)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Float", StringValuePtr(s)); @@ -1015,7 +1015,7 @@ nucomp_to_r(VALUE self) { get_dat1(self); - if (k_inexact_p(dat->image) || !f_zero_p(dat->image)) { + if (k_inexact_p(dat->imag) || !f_zero_p(dat->imag)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Rational", StringValuePtr(s)); @@ -1259,7 +1259,7 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass) { get_dat1(a1); - if (k_exact_p(dat->image) && f_zero_p(dat->image)) + if (k_exact_p(dat->imag) && f_zero_p(dat->imag)) a1 = dat->real; } } @@ -1269,7 +1269,7 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass) { get_dat1(a2); - if (k_exact_p(dat->image) && f_zero_p(dat->image)) + if (k_exact_p(dat->imag) && f_zero_p(dat->imag)) a2 = dat->real; } } @@ -1295,20 +1295,6 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass) /* --- */ -#define id_Complex rb_intern("Complex") - -static VALUE -numeric_re(VALUE self) -{ - return rb_Complex1(self); -} - -static VALUE -numeric_im(VALUE self) -{ - return rb_Complex2(ZERO, self); -} - static VALUE numeric_real(VALUE self) { @@ -1316,7 +1302,7 @@ numeric_real(VALUE self) } static VALUE -numeric_image(VALUE self) +numeric_imag(VALUE self) { return INT2FIX(0); } @@ -1427,8 +1413,8 @@ Init_Complex(void) #endif rb_define_method(rb_cComplex, "real", nucomp_real, 0); - rb_define_method(rb_cComplex, "image", nucomp_image, 0); - rb_define_method(rb_cComplex, "imag", nucomp_image, 0); + rb_define_method(rb_cComplex, "imaginary", nucomp_imag, 0); + rb_define_method(rb_cComplex, "imag", nucomp_imag, 0); rb_define_method(rb_cComplex, "-@", nucomp_negate, 0); rb_define_method(rb_cComplex, "+", nucomp_add, 1); @@ -1494,11 +1480,9 @@ Init_Complex(void) /* --- */ - rb_define_method(rb_cNumeric, "re", numeric_re, 0); - rb_define_method(rb_cNumeric, "im", numeric_im, 0); rb_define_method(rb_cNumeric, "real", numeric_real, 0); - rb_define_method(rb_cNumeric, "image", numeric_image, 0); - rb_define_method(rb_cNumeric, "imag", numeric_image, 0); + rb_define_method(rb_cNumeric, "imaginary", numeric_imag, 0); + rb_define_method(rb_cNumeric, "imag", numeric_imag, 0); rb_define_method(rb_cNumeric, "abs2", numeric_abs2, 0); rb_define_method(rb_cNumeric, "arg", numeric_arg, 0); rb_define_method(rb_cNumeric, "angle", numeric_arg, 0); @@ -1539,7 +1539,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev) case T_COMPLEX: gc_mark(objspace, obj->as.complex.real, lev); - gc_mark(objspace, obj->as.complex.image, lev); + gc_mark(objspace, obj->as.complex.imag, lev); break; case T_STRUCT: diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 8a4e603971..eb80bce4c2 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -650,7 +650,7 @@ struct RRational { struct RComplex { struct RBasic basic; VALUE real; - VALUE image; + VALUE imag; }; struct RData { diff --git a/lib/cmath.rb b/lib/cmath.rb index 795558c59b..d293401804 100644 --- a/lib/cmath.rb +++ b/lib/cmath.rb @@ -28,8 +28,8 @@ module CMath if z.real? exp!(z) else - Complex(exp!(z.real) * cos!(z.image), - exp!(z.real) * sin!(z.image)) + Complex(exp!(z.real) * cos!(z.imag), + exp!(z.real) * sin!(z.imag)) end end @@ -60,10 +60,10 @@ module CMath if z >= 0 sqrt!(z) else - Complex(0,sqrt!(-z)) + Complex(0, sqrt!(-z)) end else - if z.image < 0 + if z.imag < 0 sqrt(z.conjugate).conjugate else r = z.abs @@ -77,8 +77,8 @@ module CMath if z.real? sin!(z) else - Complex(sin!(z.real) * cosh!(z.image), - cos!(z.real) * sinh!(z.image)) + Complex(sin!(z.real) * cosh!(z.imag), + cos!(z.real) * sinh!(z.imag)) end end @@ -86,8 +86,8 @@ module CMath if z.real? cos!(z) else - Complex(cos!(z.real) * cosh!(z.image), - -sin!(z.real) * sinh!(z.image)) + Complex(cos!(z.real) * cosh!(z.imag), + -sin!(z.real) * sinh!(z.imag)) end end @@ -103,8 +103,8 @@ module CMath if z.real? sinh!(z) else - Complex(sinh!(z.real) * cos!(z.image), - cosh!(z.real) * sin!(z.image)) + Complex(sinh!(z.real) * cos!(z.imag), + cosh!(z.real) * sin!(z.imag)) end end @@ -112,8 +112,8 @@ module CMath if z.real? cosh!(z) else - Complex(cosh!(z.real) * cos!(z.image), - sinh!(z.real) * sin!(z.image)) + Complex(cosh!(z.real) * cos!(z.imag), + sinh!(z.real) * sin!(z.imag)) end end @@ -129,7 +129,7 @@ module CMath if z.real? and z >= -1 and z <= 1 asin!(z) else - -1.0.im * log(1.0.im * z + sqrt(1.0 - z * z)) + Complex(0, -1.0) * log(Complex(0, 1.0) * z + sqrt(1.0 - z * z)) end end @@ -137,7 +137,7 @@ module CMath if z.real? and z >= -1 and z <= 1 acos!(z) else - -1.0.im * log(z + 1.0.im * sqrt(1.0 - z * z)) + Complex(0, -1.0) * log(z + Complex(0, 1.0) * sqrt(1.0 - z * z)) end end @@ -145,7 +145,7 @@ module CMath if z.real? atan!(z) else - 1.0.im * log((1.0.im + z) / (1.0.im - z)) / 2.0 + Complex(0, 1.0) * log((Complex(0, 1.0) + z) / (Complex(0, 1.0) - z)) / 2.0 end end @@ -153,7 +153,7 @@ module CMath if y.real? and x.real? atan2!(y,x) else - -1.0.im * log((x + 1.0.im * y) / sqrt(x * x + y * y)) + Complex(0, -1.0) * log((x + Complex(0, 1.0) * y) / sqrt(x * x + y * y)) end end diff --git a/lib/complex.rb b/lib/complex.rb index 70e168e912..c03ee63c3e 100644 --- a/lib/complex.rb +++ b/lib/complex.rb @@ -10,3 +10,9 @@ def Complex.generic? (other) other.kind_of?(Float) || other.kind_of?(Rational) end + +class Complex + + alias image imag + +end diff --git a/lib/mathn.rb b/lib/mathn.rb index b29f994ac9..e918608b0d 100644 --- a/lib/mathn.rb +++ b/lib/mathn.rb @@ -37,7 +37,7 @@ class Rational if other.kind_of?(Rational) other2 = other if self < 0 - return Complex.__send__(:new!, self, 0) ** other + return Complex(self, 0.0) ** other elsif other == 0 return Rational(1,1) elsif self == 0 @@ -99,7 +99,7 @@ module Math remove_method(:sqrt) def sqrt(a) if a.kind_of?(Complex) - abs = sqrt(a.real*a.real + a.image*a.image) + abs = sqrt(a.real*a.real + a.imag*a.imag) # if not abs.kind_of?(Rational) # return a**Rational(1,2) # end @@ -108,7 +108,7 @@ module Math # if !(x.kind_of?(Rational) and y.kind_of?(Rational)) # return a**Rational(1,2) # end - if a.image >= 0 + if a.imag >= 0 Complex(x, y) else Complex(x, -y) diff --git a/rational.c b/rational.c index d8af04c61b..3ae00e8902 100644 --- a/rational.c +++ b/rational.c @@ -1406,7 +1406,7 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass) switch (TYPE(a1)) { case T_COMPLEX: - if (k_inexact_p(RCOMPLEX(a1)->image) || !f_zero_p(RCOMPLEX(a1)->image)) { + if (k_inexact_p(RCOMPLEX(a1)->imag) || !f_zero_p(RCOMPLEX(a1)->imag)) { VALUE s = f_to_s(a1); rb_raise(rb_eRangeError, "can't accept %s", StringValuePtr(s)); @@ -1416,7 +1416,7 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass) switch (TYPE(a2)) { case T_COMPLEX: - if (k_inexact_p(RCOMPLEX(a2)->image) || !f_zero_p(RCOMPLEX(a2)->image)) { + if (k_inexact_p(RCOMPLEX(a2)->imag) || !f_zero_p(RCOMPLEX(a2)->imag)) { VALUE s = f_to_s(a2); rb_raise(rb_eRangeError, "can't accept %s", StringValuePtr(s)); diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 7c29920b46..e6248a15cf 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -81,31 +81,31 @@ class Complex_Test < Test::Unit::TestCase def test_new_bang # no unify assert_instance_of(Complex, Complex.__send__(:new!, 2,0)) assert_equal([2,0], Complex.__send__(:new!, 2,0). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,4], Complex.__send__(:new!, 2,4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([-2,4], Complex.__send__(:new!, -2,4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,-4], Complex.__send__(:new!, 2,-4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([-2,-4], Complex.__send__(:new!, -2,-4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,0], Complex.__send__(:new!, Complex(2)). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,3], Complex.__send__(:new!, Complex(2), Complex(3)). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,3], Complex.__send__(:new!, 2, Complex(3)). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([1.1,0], Complex.__send__(:new!, 1.1). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([-1.1,0], Complex.__send__(:new!, -1.1). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([1,0], Complex.__send__(:new!, '1'). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([0,0], Complex.__send__(:new!, nil). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) end def test_new @@ -114,12 +114,12 @@ class Complex_Test < Test::Unit::TestCase assert_instance_of(Fixnum, Complex.__send__(:new, 2,0)) else assert_instance_of(Complex, Complex.__send__(:new, 2,0)) - assert_equal([2,0], Complex.__send__(:new, 2,0). instance_eval{[real, image]}) + assert_equal([2,0], Complex.__send__(:new, 2,0). instance_eval{[real, imag]}) end - assert_equal([2,4], Complex.__send__(:new, 2,4).instance_eval{[real, image]}) - assert_equal([-2,4], Complex.__send__(:new, -2,4).instance_eval{[real, image]}) - assert_equal([2,-4], Complex.__send__(:new, 2,-4).instance_eval{[real, image]}) - assert_equal([-2,-4], Complex.__send__(:new, -2,-4).instance_eval{[real, image]}) + assert_equal([2,4], Complex.__send__(:new, 2,4).instance_eval{[real, imag]}) + assert_equal([-2,4], Complex.__send__(:new, -2,4).instance_eval{[real, imag]}) + assert_equal([2,-4], Complex.__send__(:new, 2,-4).instance_eval{[real, imag]}) + assert_equal([-2,-4], Complex.__send__(:new, -2,-4).instance_eval{[real, imag]}) assert_raise(ArgumentError){Complex.__send__(:new, Complex(1,2),2)} assert_raise(ArgumentError){Complex.__send__(:new, 2,Complex(1,2))} @@ -138,19 +138,19 @@ class Complex_Test < Test::Unit::TestCase c = Complex(2**32, 2**32) assert_equal(Complex.__send__(:new, 2**32,2**32), c) - assert_equal([2**32,2**32], [c.real,c.image]) + assert_equal([2**32,2**32], [c.real,c.imag]) c = Complex(-2**32, 2**32) assert_equal(Complex.__send__(:new, -2**32,2**32), c) - assert_equal([-2**32,2**32], [c.real,c.image]) + assert_equal([-2**32,2**32], [c.real,c.imag]) c = Complex(2**32, -2**32) assert_equal(Complex.__send__(:new, 2**32,-2**32), c) - assert_equal([2**32,-2**32], [c.real,c.image]) + assert_equal([2**32,-2**32], [c.real,c.imag]) c = Complex(-2**32, -2**32) assert_equal(Complex.__send__(:new, -2**32,-2**32), c) - assert_equal([-2**32,-2**32], [c.real,c.image]) + assert_equal([-2**32,-2**32], [c.real,c.imag]) c = Complex(Complex(1),0) assert_equal(Complex.__send__(:new, 1,0), c) @@ -161,19 +161,6 @@ class Complex_Test < Test::Unit::TestCase c = Complex(Complex(1,1),Complex(1)) assert_equal(Complex.__send__(:new, 1,2), c) - c = 5.re - assert_equal(Complex.__send__(:new, 5,0), c) - - c = Complex(1,2).re - assert_equal(Complex.__send__(:new, 1,2), c) - - c = 5.im - assert_equal(Complex.__send__(:new, 0,5), c) - - c = Complex(2,0).im - assert_equal(Complex.__send__(:new, 0,2), c) - assert_equal(Complex.__send__(:new, -2,1), Complex(1,2).im) - c = Complex::I assert_equal(Complex.__send__(:new, 0,1), c) @@ -189,57 +176,57 @@ class Complex_Test < Test::Unit::TestCase c = Complex(4) assert_equal(4, c.real) - assert_equal(0, c.image) + assert_equal(0, c.imag) c = Complex(4,5) assert_equal(4, c.real) - assert_equal(5, c.image) + assert_equal(5, c.imag) if -0.0.to_s == '-0.0' c = Complex(-0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) + assert_equal('-0.0', c.imag.to_s) end c = Complex.__send__(:new, 4) assert_equal(4, c.real) - assert_equal(0, c.image) - assert_equal(c.imag, c.image) + assert_equal(0, c.imag) + assert_equal(c.imag, c.imaginary) c = Complex.__send__(:new, 4,5) assert_equal(4, c.real) - assert_equal(5, c.image) - assert_equal(c.imag, c.image) + assert_equal(5, c.imag) + assert_equal(c.imag, c.imaginary) if -0.0.to_s == '-0.0' c = Complex.__send__(:new, -0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) - assert_equal(c.imag.to_s, c.image.to_s) + assert_equal('-0.0', c.imag.to_s) + assert_equal(c.imag.to_s, c.imaginary.to_s) end c = Complex.__send__(:new!, 4) assert_equal(4, c.real) - assert_equal(c.imag, c.image) - assert_equal(0, c.image) + assert_equal(c.imag, c.imaginary) + assert_equal(0, c.imag) c = Complex.__send__(:new!, 4,5) assert_equal(4, c.real) - assert_equal(5, c.image) - assert_equal(c.imag, c.image) + assert_equal(5, c.imag) + assert_equal(c.imag, c.imaginary) c = Complex.__send__(:new!, -0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) - assert_equal(c.imag.to_s, c.image.to_s) + assert_equal('-0.0', c.imag.to_s) + assert_equal(c.imag.to_s, c.imaginary.to_s) end def test_attr2 @@ -316,11 +303,11 @@ class Complex_Test < Test::Unit::TestCase if -0.0.to_s == '-0.0' c = +Complex(0.0,0.0) assert_equal('0.0', c.real.to_s) - assert_equal('0.0', c.image.to_s) + assert_equal('0.0', c.imag.to_s) c = +Complex(-0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) + assert_equal('-0.0', c.imag.to_s) end end @@ -335,11 +322,11 @@ class Complex_Test < Test::Unit::TestCase if -0.0.to_s == '-0.0' c = -Complex(0.0,0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) + assert_equal('-0.0', c.imag.to_s) c = -Complex(-0.0,-0.0) assert_equal('0.0', c.real.to_s) - assert_equal('0.0', c.image.to_s) + assert_equal('0.0', c.imag.to_s) end =begin @@ -404,7 +391,7 @@ class Complex_Test < Test::Unit::TestCase else r = c / c2 assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) end c = Complex(1.0,2.0) @@ -412,7 +399,7 @@ class Complex_Test < Test::Unit::TestCase r = c / c2 assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1,2) c2 = Complex(2,3) @@ -439,7 +426,7 @@ class Complex_Test < Test::Unit::TestCase else r = c.quo(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) end c = Complex(1.0,2.0) @@ -447,7 +434,7 @@ class Complex_Test < Test::Unit::TestCase r = c.quo(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1,2) c2 = Complex(2,3) @@ -471,14 +458,14 @@ class Complex_Test < Test::Unit::TestCase r = c.fdiv(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1.0,2.0) c2 = Complex(2.0,3.0) r = c.fdiv(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1,2) c2 = Complex(2,3) @@ -493,7 +480,7 @@ class Complex_Test < Test::Unit::TestCase r = c ** c2 assert_in_delta(-0.015, r.real, 0.001) - assert_in_delta(-0.179, r.image, 0.001) + assert_in_delta(-0.179, r.imag, 0.001) assert_equal(Complex(-3,4), c ** 2) if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') @@ -501,15 +488,15 @@ class Complex_Test < Test::Unit::TestCase else r = c ** -2 assert_in_delta(-0.12, r.real, 0.001) - assert_in_delta(-0.16, r.image, 0.001) + assert_in_delta(-0.16, r.imag, 0.001) end r = c ** 2.0 assert_in_delta(-3.0, r.real, 0.001) - assert_in_delta(4.0, r.image, 0.001) + assert_in_delta(4.0, r.imag, 0.001) r = c ** -2.0 assert_in_delta(-0.12, r.real, 0.001) - assert_in_delta(-0.16, r.image, 0.001) + assert_in_delta(-0.16, r.imag, 0.001) if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') assert_equal(Complex(-3,4), c ** Rational(2)) @@ -520,11 +507,11 @@ class Complex_Test < Test::Unit::TestCase r = c ** Rational(2,3) assert_in_delta(1.264, r.real, 0.001) - assert_in_delta(1.150, r.image, 0.001) + assert_in_delta(1.150, r.imag, 0.001) r = c ** Rational(-2,3) assert_in_delta(0.432, r.real, 0.001) - assert_in_delta(-0.393, r.image, 0.001) + assert_in_delta(-0.393, r.imag, 0.001) end end @@ -573,9 +560,9 @@ class Complex_Test < Test::Unit::TestCase assert_equal(5, c.abs2) assert_equal(c.abs, Math.sqrt(c * c.conj)) - assert_equal(c.abs, Math.sqrt(c.real**2 + c.image**2)) + assert_equal(c.abs, Math.sqrt(c.real**2 + c.imag**2)) assert_equal(c.abs2, c * c.conj) - assert_equal(c.abs2, c.real**2 + c.image**2) + assert_equal(c.abs2, c.real**2 + c.imag**2) assert_in_delta(1.107, c.arg, 0.001) assert_in_delta(1.107, c.angle, 0.001) @@ -862,24 +849,24 @@ class Complex_Test < Test::Unit::TestCase def test_to_c c = nil.to_c - assert_equal([0,0] , [c.real, c.image]) + assert_equal([0,0] , [c.real, c.imag]) c = 0.to_c - assert_equal([0,0] , [c.real, c.image]) + assert_equal([0,0] , [c.real, c.imag]) c = 1.to_c - assert_equal([1,0] , [c.real, c.image]) + assert_equal([1,0] , [c.real, c.imag]) c = 1.1.to_c - assert_equal([1.1, 0], [c.real, c.image]) + assert_equal([1.1, 0], [c.real, c.imag]) if defined?(Rational) c = Rational(1,2).to_c - assert_equal([Rational(1,2), 0], [c.real, c.image]) + assert_equal([Rational(1,2), 0], [c.real, c.imag]) end c = Complex(1,2).to_c - assert_equal([1, 2], [c.real, c.image]) + assert_equal([1, 2], [c.real, c.imag]) end def test_supp @@ -887,12 +874,12 @@ class Complex_Test < Test::Unit::TestCase assert_equal(true, 1.1.real?) assert_equal(1, 1.real) - assert_equal(0, 1.image) assert_equal(0, 1.imag) + assert_equal(0, 1.imaginary) assert_equal(1.1, 1.1.real) - assert_equal(0, 1.1.image) assert_equal(0, 1.1.imag) + assert_equal(0, 1.1.imaginary) assert_equal(1, 1.magnitude) assert_equal(1, -1.magnitude) @@ -987,91 +974,91 @@ class Complex_Test < Test::Unit::TestCase c = Math.sqrt(Complex(1, 2)) assert_in_delta(1.272, c.real, 0.001) - assert_in_delta(0.786, c.image, 0.001) + assert_in_delta(0.786, c.imag, 0.001) c = Math.sqrt(-9) assert_in_delta(0.0, c.real, 0.001) - assert_in_delta(3.0, c.image, 0.001) + assert_in_delta(3.0, c.imag, 0.001) c = Math.exp(Complex(1, 2)) assert_in_delta(-1.131, c.real, 0.001) - assert_in_delta(2.471, c.image, 0.001) + assert_in_delta(2.471, c.imag, 0.001) c = Math.sin(Complex(1, 2)) assert_in_delta(3.165, c.real, 0.001) - assert_in_delta(1.959, c.image, 0.001) + assert_in_delta(1.959, c.imag, 0.001) c = Math.cos(Complex(1, 2)) assert_in_delta(2.032, c.real, 0.001) - assert_in_delta(-3.051, c.image, 0.001) + assert_in_delta(-3.051, c.imag, 0.001) c = Math.tan(Complex(1, 2)) assert_in_delta(0.033, c.real, 0.001) - assert_in_delta(1.014, c.image, 0.001) + assert_in_delta(1.014, c.imag, 0.001) c = Math.sinh(Complex(1, 2)) assert_in_delta(-0.489, c.real, 0.001) - assert_in_delta(1.403, c.image, 0.001) + assert_in_delta(1.403, c.imag, 0.001) c = Math.cosh(Complex(1, 2)) assert_in_delta(-0.642, c.real, 0.001) - assert_in_delta(1.068, c.image, 0.001) + assert_in_delta(1.068, c.imag, 0.001) c = Math.tanh(Complex(1, 2)) assert_in_delta(1.166, c.real, 0.001) - assert_in_delta(-0.243, c.image, 0.001) + assert_in_delta(-0.243, c.imag, 0.001) c = Math.log(Complex(1, 2)) assert_in_delta(0.804, c.real, 0.001) - assert_in_delta(1.107, c.image, 0.001) + assert_in_delta(1.107, c.imag, 0.001) c = Math.log(Complex(1, 2), Math::E) assert_in_delta(0.804, c.real, 0.001) - assert_in_delta(1.107, c.image, 0.001) + assert_in_delta(1.107, c.imag, 0.001) c = Math.log(-1) assert_in_delta(0.0, c.real, 0.001) - assert_in_delta(Math::PI, c.image, 0.001) + assert_in_delta(Math::PI, c.imag, 0.001) c = Math.log(8, 2) assert_in_delta(3.0, c.real, 0.001) - assert_in_delta(0.0, c.image, 0.001) + assert_in_delta(0.0, c.imag, 0.001) c = Math.log(-8, -2) assert_in_delta(1.092, c.real, 0.001) - assert_in_delta(-0.420, c.image, 0.001) + assert_in_delta(-0.420, c.imag, 0.001) c = Math.log10(Complex(1, 2)) assert_in_delta(0.349, c.real, 0.001) - assert_in_delta(0.480, c.image, 0.001) + assert_in_delta(0.480, c.imag, 0.001) c = Math.asin(Complex(1, 2)) assert_in_delta(0.427, c.real, 0.001) - assert_in_delta(1.528, c.image, 0.001) + assert_in_delta(1.528, c.imag, 0.001) c = Math.acos(Complex(1, 2)) assert_in_delta(1.143, c.real, 0.001) - assert_in_delta(-1.528, c.image, 0.001) + assert_in_delta(-1.528, c.imag, 0.001) c = Math.atan(Complex(1, 2)) assert_in_delta(1.338, c.real, 0.001) - assert_in_delta(0.402, c.image, 0.001) + assert_in_delta(0.402, c.imag, 0.001) c = Math.atan2(Complex(1, 2), 1) assert_in_delta(1.338, c.real, 0.001) - assert_in_delta(0.402, c.image, 0.001) + assert_in_delta(0.402, c.imag, 0.001) c = Math.asinh(Complex(1, 2)) assert_in_delta(1.469, c.real, 0.001) - assert_in_delta(1.063, c.image, 0.001) + assert_in_delta(1.063, c.imag, 0.001) c = Math.acosh(Complex(1, 2)) assert_in_delta(1.528, c.real, 0.001) - assert_in_delta(1.143, c.image, 0.001) + assert_in_delta(1.143, c.imag, 0.001) c = Math.atanh(Complex(1, 2)) assert_in_delta(0.173, c.real, 0.001) - assert_in_delta(1.178, c.image, 0.001) + assert_in_delta(1.178, c.imag, 0.001) end end |