Class: Calc::C
- Defined in:
- ext/calc/c.c,
lib/calc/c.rb,
ext/calc/c.c
Overview
Calc complex number.
A complex number consists of a real and an imaginary part, both of which are Calc::Q objects.
Wraps the libcalc C type ‘COMPLEX*`.
Instance Method Summary collapse
-
#*(y) ⇒ Calc::C
Performs complex multiplication.
-
#+(y) ⇒ Calc::C
Performs complex addition.
-
#-(y) ⇒ Calc::C
Performs complex subtraction.
-
#-@ ⇒ Calc::C
Unary minus.
-
#/(y) ⇒ Calc::C
Performs complex division.
-
#==(other) ⇒ Boolean
Test for equality.
-
#abs(*args) ⇒ Calc::Q
(also: #magnitude)
Returns the absolute value of a complex number.
-
#acos(*args) ⇒ Calc::C
Inverse trigonometric cosine.
-
#acosh(*args) ⇒ Calc::C
Inverse hyperbolic cosine.
-
#acot(*args) ⇒ Calc::C
Inverse trigonometric cotangent.
-
#acoth(*args) ⇒ Calc::C
Inverse hyperbolic cotangent.
-
#acsc(*args) ⇒ Calc::C
Inverse trigonometric cosecant.
-
#acsch(*args) ⇒ Calc::C
Inverse hyperbolic cosecant.
-
#agd(*args) ⇒ Calc::C
Inverse gudermannian function.
-
#appr(*args) ⇒ Object
Approximate numbers of multiples of a specific number.
-
#arg(*args) ⇒ Calc::Q
(also: #angle, #phase)
Returns the argument (the angle or phase) of a complex number in radians.
-
#asec(*args) ⇒ Calc::C
Inverse trigonometric secant.
-
#asech(*args) ⇒ Calc::C
Inverse hyperbolic secant.
-
#asin(*args) ⇒ Calc::C
Inverse trigonometric sine.
-
#asinh(*args) ⇒ Calc::C
Inverse hyperbolic sine.
-
#atan(*args) ⇒ Calc::C
Inverse trigonometric tangent.
-
#atanh(*args) ⇒ Calc::C
Inverse hyperbolic tangent.
-
#bround(*args) ⇒ Calc::C, Calc::Q
Round real and imaginary parts to the specified number of binary digits.
-
#conj ⇒ Calc::C
(also: #conjugate)
Complex conjugate.
-
#cos(*args) ⇒ Calc::C
Cosine.
-
#cosh(*args) ⇒ Calc::C
Hyperbolic cosine.
-
#cot(*args) ⇒ Calc::C
Trigonometric cotangent.
-
#coth(*args) ⇒ Calc::C
Hyperbolic cotangent.
-
#csc(*args) ⇒ Calc::C
Trigonometric cosecant.
-
#csch(*args) ⇒ Calc::C
Hyperbolic cosecant.
-
#denominator ⇒ Calc::Q
Denominator of a complex number.
-
#estr ⇒ String
Returns a string which if evaluated creates a new object with the original value.
-
#even? ⇒ Boolean
Returns true if the number is real and even.
-
#exp(*args) ⇒ Calc::C
Exponential function.
-
#frac ⇒ Calc::C
Return the fractional part of self.
-
#gd(*args) ⇒ Calc::C
Gudermannian function.
-
#im ⇒ Calc::Q
(also: #imaginary, #imag)
Returns the imaginary part of a complex number.
-
#imag? ⇒ Boolean
Returns true if the number is imaginary (ie, has zero real part and non-zero imaginary part).
-
#initialize(*args) ⇒ Object
constructor
Creates a new complex number.
- #initialize_copy(orig) ⇒ Object
- #inspect ⇒ Object
-
#int ⇒ Calc::C
Integer parts of the number.
-
#int? ⇒ Boolean
(also: #integer?)
Returns true if self has integer real part and zero imaginary part.
-
#inverse ⇒ Calc::C
Inverse of a complex number.
- #iseven ⇒ Object
-
#isimag ⇒ Calc::Q
Returns 1 if the number is imaginary (zero real part and non-zero imaginary part) otherwise returns 0.
- #isodd ⇒ Object
-
#isreal ⇒ Calc::Q
Returns 1 if the number has zero imaginary part, otherwise returns 0.
-
#mod(*args) ⇒ Calc::C
Computes the remainder for an integer quotient.
-
#norm ⇒ Calc::Q
Norm of a value.
-
#numerator ⇒ Calc::C
Numerator of a complex number.
-
#odd? ⇒ Boolean
Returns true if the number is real and odd.
-
#power(*args) ⇒ Calc::C
(also: #**)
Raise to a specified power.
-
#rationalize(eps = nil) ⇒ Calc::Q
Returns the real part of the value as a rational.
-
#re ⇒ Calc::Q
(also: #real)
Returns the real part of a complex number.
-
#real? ⇒ Boolean
Returns true if the number is real (ie, has zero imaginary part).
-
#round(*args) ⇒ Calc::C, Calc::Q
Round real and imaginary parts to the specified number of decimal digits.
-
#sec(*args) ⇒ Calc::C
Trigonometric secant.
-
#sech(*args) ⇒ Calc::C
Hyperbolic secant.
-
#sin(*args) ⇒ Calc::C
Trigonometric sine.
-
#sinh(*args) ⇒ Calc::C
Hyperbolic sine.
-
#tan(*args) ⇒ Calc::C
Trigonometric tangent.
-
#tanh(*args) ⇒ Calc::C
Hyperbolic tangent.
-
#to_c ⇒ Complex
Converts a Calc::C object into a ruby Complex object.
-
#to_f ⇒ Float
Convert a complex number with zero imaginary part into a ruby Float.
-
#to_i ⇒ Integer
Convert a wholly real number to an integer.
-
#to_r ⇒ Rational
Convert a complex number with zero imaginary part into a ruby Rational.
- #to_s(*args) ⇒ Object
-
#zero? ⇒ Boolean
Returns true if real and imaginary parts are both zero.
Methods inherited from Numeric
#%, #+@, #<<, #>>, #abs2, #ceil, #cmp, #coerce, #comb, #fdiv, #finite?, #floor, #ilog, #ilog10, #ilog2, #infinite?, #isint, #ln, #log, #log2, #mmin, #nonzero?, #polar, #quo, #rectangular, #root, #scale, #sgn, #sqrt, #to_int
Constructor Details
#initialize(*args) ⇒ Object
Creates a new complex number.
If a single param of type Complex or Calc::C, returns a new complex number with the same real and imaginary parts.
If a single param of other numeric types (Integer, Rational, Float, Calc::Q), returns a complex number with the specified real part and zero imaginary part.
If two params, returns a complex number with the specified real and imaginary parts; the parts can be any type allowed by Calc::Q.new.
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 |
# File 'ext/calc/c.c', line 47
static VALUE
cc_initialize(int argc, VALUE * argv, VALUE self)
{
COMPLEX *cself;
NUMBER *qre, *qim;
VALUE re, im;
setup_math_error();
if (rb_scan_args(argc, argv, "11", &re, &im) == 1) {
if (CALC_C_P(re)) {
cself = clink((COMPLEX *) DATA_PTR(re));
}
else if (RB_TYPE_P(re, T_COMPLEX)) {
cself = value_to_complex(re);
}
else {
qre = value_to_number(re, 1);
cself = qqtoc(qre, &_qzero_);
qfree(qre);
}
}
else {
qre = value_to_number(re, 1);
qim = value_to_number(im, 1);
cself = qqtoc(qre, qim);
qfree(qre);
qfree(qim);
}
DATA_PTR(self) = cself;
return self;
}
|
Instance Method Details
#*(y) ⇒ Calc::C
Performs complex multiplication.
178 179 180 181 182 |
# File 'ext/calc/c.c', line 178
static VALUE
cc_multiply(VALUE x, VALUE y)
{
return numeric_op(x, y, &c_mul, &c_mulq);
}
|
#+(y) ⇒ Calc::C
Performs complex addition.
191 192 193 194 195 |
# File 'ext/calc/c.c', line 191
static VALUE
cc_add(VALUE x, VALUE y)
{
return numeric_op(x, y, &c_add, &c_addq);
}
|
#-(y) ⇒ Calc::C
Performs complex subtraction.
204 205 206 207 208 |
# File 'ext/calc/c.c', line 204
static VALUE
cc_subtract(VALUE x, VALUE y)
{
return numeric_op(x, y, &c_sub, &c_subq);
}
|
#-@ ⇒ Calc::C
Unary minus. Returns the receiver’s value, negated.
216 217 218 219 220 221 |
# File 'ext/calc/c.c', line 216
static VALUE
cc_uminus(VALUE self)
{
setup_math_error();
return wrap_complex(c_sub(&_czero_, DATA_PTR(self)));
}
|
#/(y) ⇒ Calc::C
Performs complex division.
230 231 232 233 234 |
# File 'ext/calc/c.c', line 230
static VALUE
cc_divide(VALUE x, VALUE y)
{
return numeric_op(x, y, &c_div, &c_divq);
}
|
#==(other) ⇒ Boolean
Test for equality.
If the other value is complex (Calc::C or Complex), returns true if the real an imaginary parts of both numbers are the same.
The other value is some other numberic type (Integer, Calc::Q, Rational or Float) then returns true if the complex part of this number is zero and the real part is equal to the other.
For any other type, returns false.
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 |
# File 'ext/calc/c.c', line 254
static VALUE
cc_equal(VALUE self, VALUE other)
{
COMPLEX *cself, *cother;
int result;
setup_math_error();
cself = DATA_PTR(self);
if (CALC_C_P(other)) {
result = !c_cmp(cself, DATA_PTR(other));
}
else if (RB_TYPE_P(other, T_COMPLEX)) {
cother = value_to_complex(other);
result = !c_cmp(cself, cother);
comfree(cother);
}
else if (FIXNUM_P(other) || RB_TYPE_P(other, T_BIGNUM) || RB_TYPE_P(other, T_RATIONAL) ||
RB_TYPE_P(other, T_FLOAT) || CALC_Q_P(other)) {
cother = qqtoc(value_to_number(other, 0), &_qzero_);
result = !c_cmp(cself, cother);
comfree(cother);
}
else {
return Qfalse;
}
return result ? Qtrue : Qfalse;
}
|
#abs(*args) ⇒ Calc::Q Also known as: magnitude
Returns the absolute value of a complex number. For purely real or purely imaginary values, returns the absolute value of the non-zero part. Otherwise returns the absolute part of its complex form within the specified accuracy.
14 15 16 17 |
# File 'lib/calc/c.rb', line 14 def abs(*args) # see absvalue() in value.c re.hypot(im, *args) end |
#acos(*args) ⇒ Calc::C
Inverse trigonometric cosine
290 291 292 293 294 |
# File 'ext/calc/c.c', line 290
static VALUE
cc_acos(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_acos);
}
|
#acosh(*args) ⇒ Calc::C
Inverse hyperbolic cosine
303 304 305 306 307 |
# File 'ext/calc/c.c', line 303
static VALUE
cc_acosh(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_acosh);
}
|
#acot(*args) ⇒ Calc::C
Inverse trigonometric cotangent
316 317 318 319 320 |
# File 'ext/calc/c.c', line 316
static VALUE
cc_acot(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_acot);
}
|
#acoth(*args) ⇒ Calc::C
Inverse hyperbolic cotangent
329 330 331 332 333 |
# File 'ext/calc/c.c', line 329
static VALUE
cc_acoth(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_acoth);
}
|
#acsc(*args) ⇒ Calc::C
Inverse trigonometric cosecant
342 343 344 345 346 |
# File 'ext/calc/c.c', line 342
static VALUE
cc_acsc(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_acsc);
}
|
#acsch(*args) ⇒ Calc::C
Inverse hyperbolic cosecant
355 356 357 358 359 |
# File 'ext/calc/c.c', line 355
static VALUE
cc_acsch(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_acsch);
}
|
#agd(*args) ⇒ Calc::C
Inverse gudermannian function
368 369 370 371 372 |
# File 'ext/calc/c.c', line 368
static VALUE
cc_agd(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_agd);
}
|
#appr(*args) ⇒ Object
Approximate numbers of multiples of a specific number.
c.appr(y,z) is equivalent to c.re.appr(y,z) + c.im.appr(y,z) * Calc::C(0,1)
23 24 25 26 27 28 29 30 31 |
# File 'lib/calc/c.rb', line 23 def appr(*args) q1 = re.appr(*args) q2 = im.appr(*args) if q2.zero? q1 else C.new(q1, q2) end end |
#arg(*args) ⇒ Calc::Q Also known as: angle, phase
Returns the argument (the angle or phase) of a complex number in radians.
41 42 43 44 |
# File 'lib/calc/c.rb', line 41 def arg(*args) # see f_arg() in func.c im.atan2(re, *args) end |
#asec(*args) ⇒ Calc::C
Inverse trigonometric secant
381 382 383 384 385 |
# File 'ext/calc/c.c', line 381
static VALUE
cc_asec(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_asec);
}
|
#asech(*args) ⇒ Calc::C
Inverse hyperbolic secant
394 395 396 397 398 |
# File 'ext/calc/c.c', line 394
static VALUE
cc_asech(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_asech);
}
|
#asin(*args) ⇒ Calc::C
Inverse trigonometric sine
407 408 409 410 411 |
# File 'ext/calc/c.c', line 407
static VALUE
cc_asin(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_asin);
}
|
#asinh(*args) ⇒ Calc::C
Inverse hyperbolic sine
420 421 422 423 424 |
# File 'ext/calc/c.c', line 420
static VALUE
cc_asinh(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_asinh);
}
|
#atan(*args) ⇒ Calc::C
Inverse trigonometric tangent
433 434 435 436 437 |
# File 'ext/calc/c.c', line 433
static VALUE
cc_atan(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_atan);
}
|
#atanh(*args) ⇒ Calc::C
Inverse hyperbolic tangent
446 447 448 449 450 |
# File 'ext/calc/c.c', line 446
static VALUE
cc_atanh(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_atanh);
}
|
#bround(*args) ⇒ Calc::C, Calc::Q
Round real and imaginary parts to the specified number of binary digits
55 56 57 58 59 60 61 62 63 |
# File 'lib/calc/c.rb', line 55 def bround(*args) q1 = re.bround(*args) q2 = im.bround(*args) if q2.zero? q1 else C.new(q1, q2) end end |
#conj ⇒ Calc::C Also known as: conjugate
Complex conjugate
Returns the complex conjugate of self (same real part and same imaginary part but with opposite sign)
73 74 75 |
# File 'lib/calc/c.rb', line 73 def conj C.new(re, -im) end |
#cos(*args) ⇒ Calc::C
Cosine
459 460 461 462 463 |
# File 'ext/calc/c.c', line 459
static VALUE
cc_cos(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_cos);
}
|
#cosh(*args) ⇒ Calc::C
Hyperbolic cosine
472 473 474 475 476 |
# File 'ext/calc/c.c', line 472
static VALUE
cc_cosh(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_cosh);
}
|
#cot(*args) ⇒ Calc::C
Trigonometric cotangent
84 85 86 87 |
# File 'lib/calc/c.rb', line 84 def cot(*args) # see f_cot() in func.c cos(*args) / sin(*args) end |
#coth(*args) ⇒ Calc::C
Hyperbolic cotangent
95 96 97 98 |
# File 'lib/calc/c.rb', line 95 def coth(*args) # see f_coth() in func.c cosh(*args) / sinh(*args) end |
#csc(*args) ⇒ Calc::C
Trigonometric cosecant
106 107 108 109 |
# File 'lib/calc/c.rb', line 106 def csc(*args) # see f_csc() in func.c sin(*args).inverse end |
#csch(*args) ⇒ Calc::C
Hyperbolic cosecant
117 118 119 120 |
# File 'lib/calc/c.rb', line 117 def csch(*args) # see f_csch() in func.c sinh(*args).inverse end |
#denominator ⇒ Calc::Q
Denominator of a complex number
The denominator is the lowest common denominator of the real and imaginary parts
130 131 132 |
# File 'lib/calc/c.rb', line 130 def denominator re.den.lcm(im.den) end |
#estr ⇒ String
Returns a string which if evaluated creates a new object with the original value
139 140 141 142 143 144 145 146 |
# File 'lib/calc/c.rb', line 139 def estr s = self.class.name s << "(" s << (re.int? ? re.to_s : re.estr) s << "," + (im.int? ? im.to_s : im.estr) unless im.zero? s << ")" s end |
#even? ⇒ Boolean
Returns true if the number is real and even
485 486 487 488 489 490 491 492 493 494 |
# File 'ext/calc/c.c', line 485
static VALUE
cc_evenp(VALUE self)
{
/* note that macro ciseven() doesn't match calc's actual behaviour */
COMPLEX *cself = DATA_PTR(self);
if (cisreal(cself) && qiseven(cself->real)) {
return Qtrue;
}
return Qfalse;
}
|
#exp(*args) ⇒ Calc::C
Exponential function
503 504 505 506 507 |
# File 'ext/calc/c.c', line 503
static VALUE
cc_exp(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_exp);
}
|
#frac ⇒ Calc::C
Return the fractional part of self
515 516 517 518 519 520 |
# File 'ext/calc/c.c', line 515
static VALUE
cc_frac(VALUE self)
{
setup_math_error();
return wrap_complex(c_frac(DATA_PTR(self)));
}
|
#gd(*args) ⇒ Calc::C
Gudermannian function
528 529 530 531 532 |
# File 'ext/calc/c.c', line 528
static VALUE
cc_gd(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_gd);
}
|
#im ⇒ Calc::Q Also known as: imaginary, imag
Returns the imaginary part of a complex number
540 541 542 543 544 545 546 547 548 |
# File 'ext/calc/c.c', line 540
static VALUE
cc_im(VALUE self)
{
COMPLEX *cself;
setup_math_error();
cself = DATA_PTR(self);
return wrap_number(qlink(cself->imag));
}
|
#imag? ⇒ Boolean
Returns true if the number is imaginary (ie, has zero real part and non-zero imaginary part)
558 559 560 561 562 |
# File 'ext/calc/c.c', line 558
static VALUE
cc_imagp(VALUE self)
{
return cisimag((COMPLEX *) DATA_PTR(self)) ? Qtrue : Qfalse;
}
|
#initialize_copy(orig) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'ext/calc/c.c', line 80
static VALUE
cc_initialize_copy(VALUE obj, VALUE orig)
{
COMPLEX *corig;
if (obj == orig) {
return obj;
}
if (!CALC_C_P(orig)) {
rb_raise(rb_eTypeError, "wrong argument type");
}
corig = DATA_PTR(orig);
DATA_PTR(obj) = clink(corig);
return obj;
}
|
#inspect ⇒ Object
355 356 357 |
# File 'lib/calc/c.rb', line 355 def inspect "Calc::C(#{ self })" end |
#int ⇒ Calc::C
Integer parts of the number
570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'ext/calc/c.c', line 570
static VALUE
cc_int(VALUE self)
{
COMPLEX *cself;
setup_math_error();
cself = DATA_PTR(self);
if (cisint(cself)) {
return self;
}
return wrap_complex(c_int(cself));
}
|
#int? ⇒ Boolean Also known as: integer?
Returns true if self has integer real part and zero imaginary part
154 155 156 |
# File 'lib/calc/c.rb', line 154 def int? im.zero? ? re.int? : false end |
#inverse ⇒ Calc::C
Inverse of a complex number
590 591 592 593 594 595 |
# File 'ext/calc/c.c', line 590
static VALUE
cc_inverse(VALUE self)
{
setup_math_error();
return wrap_complex(c_inv(DATA_PTR(self)));
}
|
#iseven ⇒ Object
160 161 162 |
# File 'lib/calc/c.rb', line 160 def iseven even? ? Q::ONE : Q::ZERO end |
#isimag ⇒ Calc::Q
Returns 1 if the number is imaginary (zero real part and non-zero imaginary part) otherwise returns 0. See also [imag?].
171 172 173 |
# File 'lib/calc/c.rb', line 171 def isimag imag? ? Q::ONE : Q::ZERO end |
#isreal ⇒ Calc::Q
Returns 1 if the number has zero imaginary part, otherwise returns 0. See also [real?].
186 187 188 |
# File 'lib/calc/c.rb', line 186 def isreal real? ? Q::ONE : Q::ZERO end |
#mod(*args) ⇒ Calc::C
Computes the remainder for an integer quotient
Result is equivalent to applying the mod function separately to the real and imaginary parts.
200 201 202 203 204 205 206 207 208 |
# File 'lib/calc/c.rb', line 200 def mod(*args) q1 = re.mod(*args) q2 = im.mod(*args) if q2.zero? q1 else Calc::C(q1, q2) end end |
#norm ⇒ Calc::Q
Norm of a value
For complex values, norm is the sum of re.norm and im.norm.
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'ext/calc/c.c', line 606
static VALUE
cc_norm(VALUE self)
{
COMPLEX *cself;
NUMBER *q1, *q2, *qresult;
setup_math_error();
cself = DATA_PTR(self);
q1 = qsquare(cself->real);
q2 = qsquare(cself->imag);
qresult = qqadd(q1, q2);
qfree(q1);
qfree(q2);
return wrap_number(qresult);
}
|
#numerator ⇒ Calc::C
Numerator of a complex number
215 216 217 |
# File 'lib/calc/c.rb', line 215 def numerator C.new(re.num * denominator / re.den, im.num * denominator / im.den) end |
#odd? ⇒ Boolean
Returns true if the number is real and odd
629 630 631 632 633 634 635 636 637 638 |
# File 'ext/calc/c.c', line 629
static VALUE
cc_oddp(VALUE self)
{
/* note that macro cisodd() doesn't match calc's actual behaviour */
COMPLEX *cself = DATA_PTR(self);
if (cisreal(cself) && qisodd(cself->real)) {
return Qtrue;
}
return Qfalse;
}
|
#power(*args) ⇒ Calc::C Also known as: **
Raise to a specified power
648 649 650 651 652 653 654 |
# File 'ext/calc/c.c', line 648
static VALUE
cc_power(int argc, VALUE * argv, VALUE self)
{
/* todo: if y is integer, converting to NUMBER* and using c_powi might
* be faster */
return trans_function2(argc, argv, self, &c_power);
}
|
#rationalize(eps = nil) ⇒ Calc::Q
Returns the real part of the value as a rational
If this number is non-imaginary, equivalent to re.rationalize(eps).
Note that this method exists for ruby Numeric compatibility. Libcalc has an alternative approximation method with different semantics, see ‘appr`.
231 232 233 234 |
# File 'lib/calc/c.rb', line 231 def rationalize(eps = nil) raise RangeError, "Can't convert #{ self } into Rational" if im.nonzero? re.rationalize(eps) end |
#re ⇒ Calc::Q Also known as: real
Returns the real part of a complex number
662 663 664 665 666 667 668 669 670 |
# File 'ext/calc/c.c', line 662
static VALUE
cc_re(VALUE self)
{
COMPLEX *cself;
setup_math_error();
cself = DATA_PTR(self);
return wrap_number(qlink(cself->real));
}
|
#real? ⇒ Boolean
Returns true if the number is real (ie, has zero imaginary part)
679 680 681 682 683 |
# File 'ext/calc/c.c', line 679
static VALUE
cc_realp(VALUE self)
{
return cisreal((COMPLEX *) DATA_PTR(self)) ? Qtrue : Qfalse;
}
|
#round(*args) ⇒ Calc::C, Calc::Q
Round real and imaginary parts to the specified number of decimal digits
242 243 244 245 246 247 248 249 250 |
# File 'lib/calc/c.rb', line 242 def round(*args) q1 = re.round(*args) q2 = im.round(*args) if q2.zero? q1 else C.new(q1, q2) end end |
#sec(*args) ⇒ Calc::C
Trigonometric secant
258 259 260 261 |
# File 'lib/calc/c.rb', line 258 def sec(*args) # see f_sec() in func.c cos(*args).inverse end |
#sech(*args) ⇒ Calc::C
Hyperbolic secant
269 270 271 272 |
# File 'lib/calc/c.rb', line 269 def sech(*args) # see f_sech() in func.c cosh(*args).inverse end |
#sin(*args) ⇒ Calc::C
Trigonometric sine
692 693 694 695 696 |
# File 'ext/calc/c.c', line 692
static VALUE
cc_sin(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_sin);
}
|
#sinh(*args) ⇒ Calc::C
Hyperbolic sine
705 706 707 708 709 |
# File 'ext/calc/c.c', line 705
static VALUE
cc_sinh(int argc, VALUE * argv, VALUE self)
{
return trans_function(argc, argv, self, &c_sinh);
}
|
#tan(*args) ⇒ Calc::C
Trigonometric tangent
280 281 282 283 |
# File 'lib/calc/c.rb', line 280 def tan(*args) # see f_tan() in func.c sin(*args) / cos(*args) end |
#tanh(*args) ⇒ Calc::C
Hyperbolic tangent
291 292 293 294 |
# File 'lib/calc/c.rb', line 291 def tanh(*args) # see f_tanh() in func.c sinh(*args) / cosh(*args) end |
#to_c ⇒ Complex
Converts a Calc::C object into a ruby Complex object
301 302 303 |
# File 'lib/calc/c.rb', line 301 def to_c Complex(re.to_r, im.to_r) end |
#to_f ⇒ Float
Convert a complex number with zero imaginary part into a ruby Float
311 312 313 314 |
# File 'lib/calc/c.rb', line 311 def to_f raise RangeError, "can't convert #{ self } into Float" if im.nonzero? re.to_f end |
#to_i ⇒ Integer
Convert a wholly real number to an integer.
Note that the return value is a ruby Integer. If you want to convert to an integer but have the result be a ‘Calc::Q` object, use `trunc` or `round`.
327 328 329 330 |
# File 'lib/calc/c.rb', line 327 def to_i raise RangeError, "can't convert #{ self } into Integer" if im.nonzero? re.to_i end |
#to_r ⇒ Rational
Convert a complex number with zero imaginary part into a ruby Rational
338 339 340 341 |
# File 'lib/calc/c.rb', line 338 def to_r raise RangeError, "can't convert #{ self } into Rational" if im.nonzero? re.to_r end |
#to_s(*args) ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/calc/c.rb', line 343 def to_s(*args) if im.zero? re.to_s(*args) elsif re.zero? imag_part(im, *args) elsif im > 0 re.to_s(*args) + "+" + imag_part(im, *args) else re.to_s(*args) + "-" + imag_part(im.abs, *args) end end |
#zero? ⇒ Boolean
Returns true if real and imaginary parts are both zero
718 719 720 721 722 |
# File 'ext/calc/c.c', line 718
static VALUE
cc_zerop(VALUE self)
{
return ciszero((COMPLEX *) DATA_PTR(self)) ? Qtrue : Qfalse;
}
|