Class: Calc::C

Inherits:
Numeric show all
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

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.

Examples:

Calc::C(1,1) * Calc::C(1,1) #=> Calc::C(2i)

Parameters:

Returns:



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.

Examples:

Calc::C(1,1) + Calc::C(2,-2) #=> Calc::C(3-1i)

Parameters:

Returns:



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.

Examples:

Calc::C(1,1) - Calc::C(2,2) #=> Calc::C(-1-1i)

Parameters:

Returns:



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.

Examples:

-Calc::C(1,-1) #=> Calc::C(-1,1)

Returns:



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.

Examples:

Calc::C(1,1) / Calc::C(0,1) #=> Calc::C(1-1i)

Parameters:

Returns:



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.

Examples:

Calc::C(1,2) == Complex(1,2) #=> true
Calc::C(1,2) == Calc::C(1,2) #=> true
Calc::C(4,0) == 4            #=> true
Calc::C(4,1) == 4            #=> false

Returns:

  • (Boolean)


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.

Examples:

Calc::C(-1).abs          #=> Calc::Q(1)
Calc::C(3,-4).abs        #=> Calc::Q(5)
Calc::C(4,5).abs("1e-5") #=> Calc::Q(6.40312)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acos #=> Calc::C(1.00014354247379721852-1.98338702991653543235i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acosh #=> Calc::C(1.98338702991653543235+1.00014354247379721852i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acot #=> Calc::C(0.1608752771983210967-~0.22907268296853876630i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acoth #=> Calc::C(~0.14694666622552975204-~0.23182380450040305810i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acsc #=> Calc::C(0.15038560432786196325-0.23133469857397331455i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acsch #=> Calc::C(0.15735549884498542878-0.22996290237720785451i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(1,2).agd #=> Calc::C(0.22751065843194319695+1.422911462459226797i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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.

Examples:

Calc::C(1,0).arg  #=> 0
Calc::C(-1,0).arg #=> -pi
Calc::C(1,1).arg  #=> Calc::Q(0.78539816339744830962)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).asec #=> Calc::C(1.42041072246703465598+0.23133469857397331455i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).asech #=> Calc::C(0.23133469857397331455-1.42041072246703465598i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).asin #=> Calc::C(0.57065278432109940071+1.98338702991653543235i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).asinh #=> Calc::C(1.96863792579309629179+0.96465850440760279204i

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).atan #=> Calc::C(1.40992104959657552253+~0.22907268296853876630i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).atanh #=> Calc::C(~0.14694666622552975204+~1.33897252229449356112i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C("7/32","-7/32").bround(3) #=> Calc::C(0.25-0.25i)

Parameters:

  • places (Integer)

    number of binary places to round to (default 0)

  • rns (Integer)

    rounding flags (default Calc.config(:round))

Returns:



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

#conjCalc::C Also known as: conjugate

Complex conjugate

Returns the complex conjugate of self (same real part and same imaginary part but with opposite sign)

Examples:

Calc::C(3,3).conj #=> Calc::C(3-3i)

Returns:



73
74
75
# File 'lib/calc/c.rb', line 73

def conj
  C.new(re, -im)
end

#cos(*args) ⇒ Calc::C

Cosine

Examples:

Calc::C(2,3).cos #=> Calc::C(-4.18962569096880723013-9.10922789375533659798i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).cosh #=> Calc::C(~-3.72454550491532256548+~0.51182256998738460884i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).cot #=> Calc::C(~-0.00373971037633695666-~0.99675779656935831046i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).coth #=> Calc::C(~1.03574663776499539611+~0.01060478347033710175i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).csc #=> Calc::C(~0.09047320975320743981+~0.04120098628857412646i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).csch #=> Calc::C(~-0.27254866146294019951-~0.04030057885689152187i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



117
118
119
120
# File 'lib/calc/c.rb', line 117

def csch(*args)
  # see f_csch() in func.c
  sinh(*args).inverse
end

#denominatorCalc::Q

Denominator of a complex number

The denominator is the lowest common denominator of the real and imaginary parts

Examples:

Calc::C("1/2", "2/3").denominator #=> Calc::Q(6)

Returns:



130
131
132
# File 'lib/calc/c.rb', line 130

def denominator
  re.den.lcm(im.den)
end

#estrString

Returns a string which if evaluated creates a new object with the original value

Examples:

Calc::C(0.5,-2).estr #=> "Calc::C(Calc::Q(1,2),-2)"

Returns:

  • (String)


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

Examples:

Calc::C(2,0).even? #=> true
Calc::C(2,2).even? #=> false

Returns:

  • (Boolean)


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

Examples:

Calc::C(1,2).exp #=> Calc::C(-1.13120438375681363843+2.47172667200481892762i)

Parameters:

  • eps (Numeric)

    (optional) calculation accuracy

Returns:



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

#fracCalc::C

Return the fractional part of self

Examples:

Calc::C("2.15", "-3.25").frac #=> Calc::C(0.15-0.25i)

Returns:



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

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

#imCalc::Q Also known as: imaginary, imag

Returns the imaginary part of a complex number

Examples:

Calc::C(1,2).im #=> Calc::Q(2)

Returns:



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)

Examples:

Calc::C(0,1).imag? #=> true
Calc::C(1,1).imag? #=> false

Returns:

  • (Boolean)


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

#inspectObject



355
356
357
# File 'lib/calc/c.rb', line 355

def inspect
  "Calc::C(#{ self })"
end

#intCalc::C

Integer parts of the number

Examples:

Calc::C("2.15", "-3.25").int #=> Calc::C(2-3i)

Returns:



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

Examples:

Calc::C(1,1).int?  #=> false
Calc::C(1,0).int?  #=> true

Returns:

  • (Boolean)


154
155
156
# File 'lib/calc/c.rb', line 154

def int?
  im.zero? ? re.int? : false
end

#inverseCalc::C

Inverse of a complex number

Examples:

Calc::C(2+2i).inverse #=> Calc::C(0.25-0.25i)

Returns:

Raises:



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

#isevenObject



160
161
162
# File 'lib/calc/c.rb', line 160

def iseven
  even? ? Q::ONE : Q::ZERO
end

#isimagCalc::Q

Returns 1 if the number is imaginary (zero real part and non-zero imaginary part) otherwise returns 0. See also [imag?].

Examples:

Calc::C(0,1).isimag #=> Calc::Q(1)
Calc::C(1,1).isimag #=> Calc::Q(0)

Returns:



171
172
173
# File 'lib/calc/c.rb', line 171

def isimag
  imag? ? Q::ONE : Q::ZERO
end

#isoddObject



175
176
177
# File 'lib/calc/c.rb', line 175

def isodd
  odd? ? Q::ONE : Q::ZERO
end

#isrealCalc::Q

Returns 1 if the number has zero imaginary part, otherwise returns 0. See also [real?].

Examples:

Calc::C(1,1).isreal #=> Calc::Q(0)
Calc::C(1,0).isreal #=> Calc::Q(1)

Returns:



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.

Examples:

Calc::C(0, 11).mod(5) #=> Calc::C(1i)

Parameters:

  • y (Integer)
  • r (Integer)

    (optional) rounding mode (see “help mod”)

Returns:



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

#normCalc::Q

Norm of a value

For complex values, norm is the sum of re.norm and im.norm.

Examples:

Calc::C(3, 4).norm  #=> Calc::Q(25)
Calc::C(4, -5).norm #=> Calc::Q(41)

Returns:



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

#numeratorCalc::C

Numerator of a complex number

Examples:

Calc::C("1/2", "2/3").numerator #=> Calc::C(3+4i)

Returns:



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

Examples:

Calc::C(1,0).odd? #=> true
Calc::C(1,1).odd? #=> false

Returns:

  • (Boolean)


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

Examples:

Calc::C(1,1) ** 2 #=> Calc::C(2i)

Parameters:

Returns:



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`.

Examples:

Calc::C("1/3", 0).rationalize #=> Calc::Q(1/3)
Calc::C(1, 2).rationalize     # RangeError

Parameters:

  • eps (Float, Rational) (defaults to: nil)

Returns:

Raises:

  • (RangeError)


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

#reCalc::Q Also known as: real

Returns the real part of a complex number

Examples:

Calc::C(1,2).re #=> Calc::Q(1)

Returns:



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)

Examples:

Calc::C(1,1).real? #=> false
Calc::C(1,0).real? #=> true

Returns:

  • (Boolean)


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

Parameters:

  • places (Integer)

    number of decimal places to round to (default 0)

  • rns (Integer)

    rounding flags (default Calc.config(:round)) Calc::C(“7/32”,“-7/32”).round(3) #=> Calc::C(0.218-0.219i)

Returns:



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

Examples:

Calc::C(2,3).sec #=> Calc::C(~-0.04167496441114427005+~0.09061113719623759653i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).sech #=> Calc::C(~-0.26351297515838930964-~0.03621163655876852087i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).sin #=> Calc::C(9.15449914691142957347-4.16890695996656435076i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(2,3).acos #=>

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(1,2).tan #=> Calc::C(~-0.00376402564150424829+~1.00323862735360980145i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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

Examples:

Calc::C(1,2).tanh #=> Calc::C(~0.96538587902213312428-~0.00988437503832249372i)

Parameters:

  • eps (Calc::Q)

    (optional) calculation accuracy

Returns:



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_cComplex

Converts a Calc::C object into a ruby Complex object

Examples:

Calc::C(2, 3).to_c #=> ((2/1)+(3/1)*i)

Returns:

  • (Complex)


301
302
303
# File 'lib/calc/c.rb', line 301

def to_c
  Complex(re.to_r, im.to_r)
end

#to_fFloat

Convert a complex number with zero imaginary part into a ruby Float

Examples:

Calc::C("2/3", 0).to_f #=> 0.6666666666666666

Returns:

  • (Float)

Raises:

  • (RangeError)

    if imaginary part is non-zero



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_iInteger

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`.

Examples:

Calc::C(2, 0).to_i #=> 2
Calc::C(2, 2).to_i # RangeError

Returns:

  • (Integer)

Raises:

  • (RangeError)

    if imaginary part is non-zero



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_rRational

Convert a complex number with zero imaginary part into a ruby Rational

Examples:

Calc::C("2/3", 0).to_r #=> (2/3)

Returns:

  • (Rational)

Raises:

  • (RangeError)

    if imaginary part is non-zero



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

Examples:

Calc::C(1, 1).zero? #=> false
Calc::C(0, 0).zero? #=> true

Returns:

  • (Boolean)


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