在线文档教程
Ruby 2.4

Complex

Complex类

Parent:Numeric

一个复数可以用虚数单位表示为一个配对实数; a+bi。a是实部,b是虚部,i是虚部。在数学上等于实现一个复数a + 0i。

复杂对象可以创建为文字,也可以使用Kernel#Complex,:: rect,:: polar或#to_c方法。

2+1i #=> (2+1i) Complex(1) #=> (1+0i) Complex(2, 3) #=> (2+3i) Complex.polar(2, 3) #=> (-1.9799849932008908+0.2822400161197344i) 3.to_c #=> (3+0i)

您也可以使用浮点数字或字符串创建复杂的对象。

Complex(0.3) #=> (0.3+0i) Complex('0.3-0.5i') #=> (0.3-0.5i) Complex('2/3+3/4i') #=> ((2/3)+(3/4)*i) Complex('1@2') #=> (-0.4161468365471424+0.9092974268256817i) 0.3.to_c #=> (0.3+0i) '0.3-0.5i'.to_c #=> (0.3-0.5i) '2/3+3/4i'.to_c #=> ((2/3)+(3/4)*i) '1@2'.to_c #=> (-0.4161468365471424+0.9092974268256817i)

一个复杂的对象是一个确切的或不精确的数字。

Complex(1, 1) / 2 #=> ((1/2)+(1/2)*i) Complex(1, 1) / 2.0 #=> (0.5+0.5i)

常量

I

虚构的单位。

公共类方法

json_create(object)

通过将实数值r,虚数值i转换为复杂对象来反序列化JSON字符串。

# File ext/json/lib/json/add/complex.rb, line 11 def self.json_create(object) Complex(object['r'], object['i']) end

polar(abs, arg) → complex 显示源文件

返回表示给定极坐标形式的复杂对象。

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 if (canonicalization) return abs; return nucomp_s_new_internal(klass, abs, ZERO default: nucomp_real_check(abs nucomp_real_check(arg break; } return f_complex_polar(klass, abs, arg }

rect(real, imag) → complex 显示源文件

rectangular(real, imag) → complex

返回表示给定矩形形式的复杂对象。

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 }

rectangular(real, imag) → complex 显示源文件

返回表示给定矩形形式的复杂对象。

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 }

公共实例方法

cmp * numeric → complex 显示源文件

执行乘法。

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; VALUE areal, aimag, breal, bimag; int arzero, aizero, brzero, bizero; get_dat2(self, other arzero = f_zero_p(areal = adat->real aizero = f_zero_p(aimag = adat->imag brzero = f_zero_p(breal = bdat->real bizero = f_zero_p(bimag = bdat->imag 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) 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, '*' }

cmp ** numeric → complex 显示源文件

执行取幂。

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 (RB_TYPE_P(other, T_RATIONAL) && RRATIONAL(other)->den == LONG2FIX(1)) other = RRATIONAL(other)->num; /* c14n */ if (RB_TYPE_P(other, T_COMPLEX)) { get_dat1(other if (k_exact_zero_p(dat->imag)) other = dat->real; /* c14n */ } if (RB_TYPE_P(other, T_COMPLEX)) { 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 (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), rb_int_uminus(other) } if (k_numeric_p(other) && f_real_p(other)) { VALUE r, theta; if (RB_TYPE_P(other, T_BIGNUM)) 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 }

cmp + numeric → complex 显示源文件

执行添加。

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, '+' }

cmp - numeric → complex 显示源文件

执行减法。

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) { 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, '-' }

-cmp → complex 显示源文件

返回值的否定。

-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) }

cmp / numeric → complex 显示源文件

quo(numeric) → complex

执行划分。

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 }

cmp == object → true or false 显示源文件

如果cmp等于数字对象,则返回true。

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 (RB_TYPE_P(other, T_COMPLEX)) { 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_boolcast(f_eqeq_p(other, self) }

abs → real 显示源文件

返回其极坐标的绝对部分。

Complex(-1).abs #=> 1 Complex(3.0, -4.0).abs #=> 5.0

static VALUE nucomp_abs(VALUE self) { get_dat1(self if (f_zero_p(dat->real)) { VALUE a = f_abs(dat->imag if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag)) a = f_to_f(a return a; } if (f_zero_p(dat->imag)) { VALUE a = f_abs(dat->real if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag)) a = f_to_f(a return a; } return rb_math_hypot(dat->real, dat->imag }

abs2 → real 显示源文件

返回绝对值的平方。

Complex(-1).abs2 #=> 1 Complex(3.0, -4.0).abs2 #=> 25.0

static VALUE nucomp_abs2(VALUE self) { get_dat1(self return f_add(f_mul(dat->real, dat->real), f_mul(dat->imag, dat->imag) }

angle → float 显示源文件

返回其极坐标的角度部分。

Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966

static VALUE nucomp_arg(VALUE self) { get_dat1(self return rb_math_atan2(dat->imag, dat->real }

arg → float 显示源文件

返回其极坐标的角度部分。

Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966

static VALUE nucomp_arg(VALUE self) { get_dat1(self return rb_math_atan2(dat->imag, dat->real }

as_json(*) 显示源文件

返回一个散列,它将变成一个JSON对象并表示这个对象。

# File ext/json/lib/json/add/complex.rb, line 17 def as_json(*) { JSON.create_id => self.class.name, 'r' => real, 'i' => imag, } end

conj → complex 显示源文件

conjugate → complex

返回复共轭。

Complex(1, 2).conjugate #=> (1-2i)

static VALUE nucomp_conj(VALUE self) { get_dat1(self return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag) }

conjugate → complex 显示源文件

返回复共轭。

Complex(1, 2).conjugate #=> (1-2i)

static VALUE nucomp_conj(VALUE self) { get_dat1(self return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag) }

denominator → integer 显示源文件

返回分母(两个分母 - 真实和成像的lcm)。

见分子。

static VALUE nucomp_denominator(VALUE self) { get_dat1(self return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag) }

fdiv(numeric) → complex 显示源文件

由于每个部分都是一个浮点数,所以不会返回浮点数。

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 }

finite? → true or false 显示源文件

返回true如果cmp的幅度是有限的数量,oterwise回报false

static VALUE rb_complex_finite_p(VALUE self) { VALUE magnitude = nucomp_abs(self if (FINITE_TYPE_P(magnitude)) { return Qtrue; } else if (RB_FLOAT_TYPE_P(magnitude)) { const double f = RFLOAT_VALUE(magnitude return isinf(f) ? Qfalse : Qtrue; } else { return rb_funcall(magnitude, id_finite_p, 0 } }

imag → real 显示源文件

imaginary → real

返回虚部。

Complex(7).imaginary #=> 0 Complex(9, -4).imaginary #=> -4

static VALUE nucomp_imag(VALUE self) { get_dat1(self return dat->imag; }

imaginary → real Show source

返回虚部。

Complex(7).imaginary #=> 0 Complex(9, -4).imaginary #=> -4

static VALUE nucomp_imag(VALUE self) { get_dat1(self return dat->imag; }

infinite? → nil or 1 or -1 Show source

返回对应于cmp幅度值的值:

finite

nil

+Infinity

+1

For example: (1+1i).infinite? #=> nil (Float::INFINITY + 1i).infinite? #=> 1

static VALUE rb_complex_infinite_p(VALUE self) { VALUE magnitude = nucomp_abs(self if (FINITE_TYPE_P(magnitude)) { return Qnil; } if (RB_FLOAT_TYPE_P(magnitude)) { const double f = RFLOAT_VALUE(magnitude if (isinf(f)) { return INT2FIX(f < 0 ? -1 : 1 } return Qnil; } else { return rb_funcall(magnitude, id_infinite_p, 0 } }

inspect → string Show source

将该值作为字符串返回以进行检查。

Complex(2).inspect #=> "(2+0i)" Complex('-8/6').inspect #=> "((-4/3)+0i)" Complex('1/2i').inspect #=> "(0+(1/2)*i)" Complex(0, Float::INFINITY).inspect #=> "(0+Infinity*i)" Complex(Float::NAN, Float::NAN).inspect #=> "(NaN+NaN*i)"

static VALUE nucomp_inspect(VALUE self) { VALUE s; s = rb_usascii_str_new2("(" rb_str_concat(s, f_format(self, rb_inspect) rb_str_cat2(s, ")" return s; }

magnitude → real Show source

返回其极坐标的绝对部分。

Complex(-1).abs #=> 1 Complex(3.0, -4.0).abs #=> 5.0

static VALUE nucomp_abs(VALUE self) { get_dat1(self if (f_zero_p(dat->real)) { VALUE a = f_abs(dat->imag if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag)) a = f_to_f(a return a; } if (f_zero_p(dat->imag)) { VALUE a = f_abs(dat->real if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag)) a = f_to_f(a return a; } return rb_math_hypot(dat->real, dat->imag }

numerator → numeric Show source

返回分子。

1 2 3+4i <- numerator - + -i -> ---- 2 3 6 <- denominator c = Complex('1/2+2/3i') #=> ((1/2)+(2/3)*i) n = c.numerator #=> (3+4i) d = c.denominator #=> 6 n / d #=> ((1/2)+(2/3)*i) Complex(Rational(n.real, d), Rational(n.imag, d)) #=> ((1/2)+(2/3)*i)

请参阅分母。

static VALUE nucomp_numerator(VALUE self) { VALUE cd; get_dat1(self cd = f_denominator(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->imag), f_div(cd, f_denominator(dat->imag))) }

phase → float Show source

返回其极坐标的角度部分。

Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966

static VALUE nucomp_arg(VALUE self) { get_dat1(self return rb_math_atan2(dat->imag, dat->real }

polar → array Show source

返回一个数组; cmp.abs,cmp.arg。

Complex(1, 2).polar #=> [2.23606797749979, 1.1071487177940904]

static VALUE nucomp_polar(VALUE self) { return rb_assoc_new(f_abs(self), f_arg(self) }

cmp / numeric → complex Show source

quo(numeric) → complex

执行划分。

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 }

rationalize(eps) → rational Show source

如果可能,将值作为有理数返回(虚数部分应该完全为零)。

Complex(1.0/3, 0).rationalize #=> (1/3) Complex(1, 0.0).rationalize # RangeError Complex(1, 2).rationalize # RangeError

请参阅to_r。

static VALUE nucomp_rationalize(int argc, VALUE *argv, VALUE self) { get_dat1(self rb_scan_args(argc, argv, "01", NULL if (!k_exact_zero_p(dat->imag)) { rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational", self } return rb_funcallv(dat->real, id_rationalize, argc, argv }

real → real Show source

返回实部。

Complex(7).real #=> 7 Complex(9, -4).real #=> 9

static VALUE nucomp_real(VALUE self) { get_dat1(self return dat->real; }

real? → false Show source

返回false。

static VALUE nucomp_false(VALUE self) { return Qfalse; }

rect → array Show source

rectangular → array

返回一个数组; cmp.real,cmp.imag。

Complex(1, 2).rectangular #=> [1, 2]

static VALUE nucomp_rect(VALUE self) { get_dat1(self return rb_assoc_new(dat->real, dat->imag }

rect → array Show source

rectangular → array

返回一个数组; cmp.real,cmp.imag。

Complex(1, 2).rectangular #=> [1, 2]

static VALUE nucomp_rect(VALUE self) { get_dat1(self return rb_assoc_new(dat->real, dat->imag }

to_c → self Show source

返回自身。

Complex(2).to_c #=> (2+0i) Complex(-8, 6).to_c #=> (-8+6i)

static VALUE nucomp_to_c(VALUE self) { return self; }

to_f → float Show source

如果可能,以浮点形式返回值(虚部应该完全为零)。

Complex(1, 0).to_f #=> 1.0 Complex(1, 0.0).to_f # RangeError Complex(1, 2).to_f # RangeError

static VALUE nucomp_to_f(VALUE self) { get_dat1(self if (!k_exact_zero_p(dat->imag)) { rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Float", self } return f_to_f(dat->real }

to_i → integer Show source

如果可能,以整数形式返回值(虚部应完全为零)。

Complex(1, 0).to_i #=> 1 Complex(1, 0.0).to_i # RangeError Complex(1, 2).to_i # RangeError

static VALUE nucomp_to_i(VALUE self) { get_dat1(self if (!k_exact_zero_p(dat->imag)) { rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Integer", self } return f_to_i(dat->real }

to_json(*) Show source

以JSON字符串形式存储类名称(Complex)以及实际值r和虚数值i

# File ext/json/lib/json/add/complex.rb, line 26 def to_json(*) as_json.to_json end

to_r → rational Show source

如果可能,将值作为有理数返回(虚数部分应该完全为零)。

Complex(1, 0).to_r #=> (1/1) Complex(1, 0.0).to_r # RangeError Complex(1, 2).to_r # RangeError

参阅rationalize

static VALUE nucomp_to_r(VALUE self) { get_dat1(self if (!k_exact_zero_p(dat->imag)) { rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational", self } return f_to_r(dat->real }

to_s → string Show source

以字符串形式返回值。

Complex(2).to_s #=> "2+0i" Complex('-8/6').to_s #=> "-4/3+0i" Complex('1/2i').to_s #=> "0+1/2i" Complex(0, Float::INFINITY).to_s #=> "0+Infinity*i" Complex(Float::NAN, Float::NAN).to_s #=> "NaN+NaN*i"

static VALUE nucomp_to_s(VALUE self) { return f_format(self, rb_String }

conj → complex Show source

conjugate → complex

返回复共轭。

Complex(1, 2).conjugate #=> (1-2i)

static VALUE nucomp_conj(VALUE self) { get_dat1(self return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag) }