Class: CComplex

Inherits:
Numeric show all
Defined in:
ext/ruby_ccomplex.c

Instance Method Summary collapse

Methods inherited from Numeric

#deg_180, #deg_360, #eq, #ne, #rad_2pi, #rad_pi, #to_cc

Constructor Details

#initializeObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'ext/ruby_ccomplex.c', line 168

static VALUE
rb_cc_initialize (int argc, VALUE *argv, VALUE self)
{
  volatile VALUE rre, rim;
  double complex *cp;

  Data_Get_Struct(self, double complex, cp);

  rb_scan_args(argc, argv, "11", (VALUE *)&rre, (VALUE *)&rim);

  if ( NIL_P(rim) ) {
    *cp = NUM2DBL(rre);
  }
  else {
    *cp = NUM2DBL(rre) + I * NUM2DBL(rim);
  }

  return Qnil;
}

Instance Method Details

#*(other) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'ext/ruby_ccomplex.c', line 356

static VALUE
rb_cc_asterisk(VALUE self, VALUE other)
{
  double complex a, b;

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    a = NUM2CC(self);
    b = NUM2CC(other);
    return CC2NUM( a * b );
  }
  else {
#if RUBY_VERSION_CODE >= 190
    return rb_num_coerce_bin(self, other, '*');
#else
    return rb_num_coerce_bin(self, other);
#endif
  }
}

#**(other) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/ruby_ccomplex.c', line 394

static VALUE
rb_cc_star2(VALUE self, VALUE other)
{
  double complex a, b;

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    a = NUM2CC(self);
    b = NUM2CC(other);
    return CC2NUM( cpow(a, b) );
  }
  else {
#if RUBY_VERSION_CODE >= 190
    return rb_num_coerce_bin(self, other, rb_intern("**"));
#else
    return rb_num_coerce_bin(self, other);
#endif
  }
}

#+(other) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'ext/ruby_ccomplex.c', line 318

static VALUE
rb_cc_plus(VALUE self, VALUE other)
{
  double complex a, b;

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    a = NUM2CC(self);
    b = NUM2CC(other);
    return CC2NUM( a + b );
  }
  else {
#if RUBY_VERSION_CODE >= 190
    return rb_num_coerce_bin(self, other, '+');
#else
    return rb_num_coerce_bin(self, other);
#endif
  }
}

#-(other) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'ext/ruby_ccomplex.c', line 337

static VALUE
rb_cc_minus(VALUE self, VALUE other)
{
  double complex a, b;

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    a = NUM2CC(self);
    b = NUM2CC(other);
    return CC2NUM( a - b );
  }
  else {
#if RUBY_VERSION_CODE >= 190
    return rb_num_coerce_bin(self, other, '-');
#else
    return rb_num_coerce_bin(self, other);
#endif
  }
}

#-@Object



312
313
314
315
316
# File 'ext/ruby_ccomplex.c', line 312

static VALUE
rb_cc_uminus(VALUE self)
{
  return CC2NUM( - NUM2CC(self) );
}

#/(other) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/ruby_ccomplex.c', line 375

static VALUE
rb_cc_slash(VALUE self, VALUE other)
{
  double complex a, b;

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    a = NUM2CC(self);
    b = NUM2CC(other);
    return CC2NUM( a / b );
  }
  else {
#if RUBY_VERSION_CODE >= 190
    return rb_num_coerce_bin(self, other, '/');
#else
    return rb_num_coerce_bin(self, other);
#endif
  }
}

#==(other) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'ext/ruby_ccomplex.c', line 297

static VALUE
rb_cc_equal(VALUE self, VALUE other)
{
  double complex a, b;

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    a = NUM2CC(self);
    b = NUM2CC(other);
    return ( a == b ) ? Qtrue : Qfalse;
  }
  else {
    return rb_funcall(other, rb_intern("=="), 1, self);
  }
}

#absObject



274
275
276
277
278
279
280
281
282
# File 'ext/ruby_ccomplex.c', line 274

static VALUE
rb_cc_abs(VALUE self)
{
  double complex *cp;

  Data_Get_Struct(self, double complex, cp);

  return rb_float_new(cabs(*cp));
}

#acosObject



417
# File 'ext/ruby_ccomplex.c', line 417

static VALUE rb_cc_acos  (VALUE self) { return CC2NUM(cacos(NUM2CC(self))); }

#acoshObject



425
# File 'ext/ruby_ccomplex.c', line 425

static VALUE rb_cc_acosh (VALUE self) { return CC2NUM(cacosh(NUM2CC(self))); }

#argObject



264
265
266
267
268
269
270
271
272
# File 'ext/ruby_ccomplex.c', line 264

static VALUE
rb_cc_arg(VALUE self)
{
  double complex *cp;

  Data_Get_Struct(self, double complex, cp);

  return rb_float_new(carg(*cp));
}

#asinObject



418
# File 'ext/ruby_ccomplex.c', line 418

static VALUE rb_cc_asin  (VALUE self) { return CC2NUM(casin(NUM2CC(self))); }

#asinhObject



426
# File 'ext/ruby_ccomplex.c', line 426

static VALUE rb_cc_asinh (VALUE self) { return CC2NUM(casinh(NUM2CC(self))); }

#atanObject



419
# File 'ext/ruby_ccomplex.c', line 419

static VALUE rb_cc_atan  (VALUE self) { return CC2NUM(catan(NUM2CC(self))); }

#atanhObject



427
# File 'ext/ruby_ccomplex.c', line 427

static VALUE rb_cc_atanh (VALUE self) { return CC2NUM(catanh(NUM2CC(self))); }

#coerce(other) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/ruby_ccomplex.c', line 284

static VALUE
rb_cc_coerce(VALUE self, VALUE other)
{
  VALUE argv[1];

  if ( rb_obj_is_kind_of(other, rb_cNumeric) ) {
    return rb_assoc_new(CC2NUM(NUM2CC(other)), self);
  }

  argv[0] = other;
  return rb_call_super(1, argv);
}

#conjObject



254
255
256
257
258
259
260
261
262
# File 'ext/ruby_ccomplex.c', line 254

static VALUE
rb_cc_conj(VALUE self)
{
  double complex *cp;

  Data_Get_Struct(self, double complex, cp);

  return CC2NUM(conj(*cp));
}

#cosObject



413
# File 'ext/ruby_ccomplex.c', line 413

static VALUE rb_cc_cos   (VALUE self) { return CC2NUM(ccos(NUM2CC(self))); }

#coshObject



421
# File 'ext/ruby_ccomplex.c', line 421

static VALUE rb_cc_cosh  (VALUE self) { return CC2NUM(ccosh(NUM2CC(self))); }

#expObject



429
# File 'ext/ruby_ccomplex.c', line 429

static VALUE rb_cc_exp   (VALUE self) { return CC2NUM(cexp(NUM2CC(self))); }

#imagObject



218
219
220
221
222
223
224
225
226
# File 'ext/ruby_ccomplex.c', line 218

static VALUE
rb_cc_imag(VALUE self)
{
  double complex *cp;

  Data_Get_Struct(self, double complex, cp);

  return rb_float_new(cimag(*cp));
}

#inspectObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/ruby_ccomplex.c', line 228

static VALUE
rb_cc_inspect (VALUE self)
{
  volatile VALUE vary = rb_ary_new(), rim;
  double complex *cp;
  double re, im;

  Data_Get_Struct(self, double complex, cp);

  re = creal(*cp);
  im = cimag(*cp);

  rb_ary_push(vary, rb_inspect(rb_float_new(re)));

  rim = rb_inspect(rb_float_new(im));

  if ( StringValuePtr(rim)[0] != '-' ) {
    rb_ary_push(vary, rb_str_new2("+"));
  }

  rb_ary_push(vary, rim);
  rb_ary_push(vary, rb_str_new2("i"));

  return rb_ary_join(vary, Qnil);
}

#logObject



430
# File 'ext/ruby_ccomplex.c', line 430

static VALUE rb_cc_log   (VALUE self) { return CC2NUM(clog(NUM2CC(self))); }

#realObject



208
209
210
211
212
213
214
215
216
# File 'ext/ruby_ccomplex.c', line 208

static VALUE
rb_cc_real(VALUE self)
{
  double complex *cp;

  Data_Get_Struct(self, double complex, cp);

  return rb_float_new(creal(*cp));
}

#sinObject



414
# File 'ext/ruby_ccomplex.c', line 414

static VALUE rb_cc_sin   (VALUE self) { return CC2NUM(csin(NUM2CC(self))); }

#sinhObject



422
# File 'ext/ruby_ccomplex.c', line 422

static VALUE rb_cc_sinh  (VALUE self) { return CC2NUM(csinh(NUM2CC(self))); }

#sqrtObject



431
# File 'ext/ruby_ccomplex.c', line 431

static VALUE rb_cc_sqrt  (VALUE self) { return CC2NUM(csqrt(NUM2CC(self))); }

#tanObject



415
# File 'ext/ruby_ccomplex.c', line 415

static VALUE rb_cc_tan   (VALUE self) { return CC2NUM(ctan(NUM2CC(self))); }

#tanhObject



423
# File 'ext/ruby_ccomplex.c', line 423

static VALUE rb_cc_tanh  (VALUE self) { return CC2NUM(ctanh(NUM2CC(self))); }

#to_cObject



188
189
190
191
192
193
194
# File 'ext/ruby_ccomplex.c', line 188

static VALUE
rb_cc_to_c (VALUE self)
{
  double complex *cp;
  Data_Get_Struct(self, double complex, cp);
  return rb_Complex(rb_float_new(creal(*cp)), rb_float_new(cimag(*cp)));
}

#to_sObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/ruby_ccomplex.c', line 228

static VALUE
rb_cc_inspect (VALUE self)
{
  volatile VALUE vary = rb_ary_new(), rim;
  double complex *cp;
  double re, im;

  Data_Get_Struct(self, double complex, cp);

  re = creal(*cp);
  im = cimag(*cp);

  rb_ary_push(vary, rb_inspect(rb_float_new(re)));

  rim = rb_inspect(rb_float_new(im));

  if ( StringValuePtr(rim)[0] != '-' ) {
    rb_ary_push(vary, rb_str_new2("+"));
  }

  rb_ary_push(vary, rim);
  rb_ary_push(vary, rb_str_new2("i"));

  return rb_ary_join(vary, Qnil);
}