Class: MPC

Inherits:
Numeric
  • Object
show all
Defined in:
lib/mpc/version.rb,
ext/mpc/ruby_mpc.c

Defined Under Namespace

Modules: Math

Constant Summary collapse

VERSION =
'0.0.9'
RNDNN =

Integer which is macro MPC_RNDNN.

INT2NUM(MPC_RNDNN)
RNDNZ =

Integer which is macro MPC_RNDNZ.

INT2NUM(MPC_RNDNZ)
RNDNU =

Integer which is macro MPC_RNDNU.

INT2NUM(MPC_RNDNU)
RNDND =

Integer which is macro MPC_RNDND.

INT2NUM(MPC_RNDND)
RNDZN =

Integer which is macro MPC_RNDZN.

INT2NUM(MPC_RNDZN)
RNDZZ =

Integer which is macro MPC_RNDZZ.

INT2NUM(MPC_RNDZZ)
RNDZU =

Integer which is macro MPC_RNDZU.

INT2NUM(MPC_RNDZU)
RNDZD =

Integer which is macro MPC_RNDZD.

INT2NUM(MPC_RNDZD)
RNDUN =

Integer which is macro MPC_RNDUN

INT2NUM(MPC_RNDUN)
RNDUZ =

Integer which is macro MPC_RNDUZ.

INT2NUM(MPC_RNDUZ)
RNDUU =

Integer which is macro MPC_RNDUU.

INT2NUM(MPC_RNDUU)
RNDUD =

Integer which is macro MPC_RNDUD.

INT2NUM(MPC_RNDUD)
RNDDN =

Integer which is macro MPC_RNDDN.

INT2NUM(MPC_RNDDN)
RNDDZ =

Integer which is macro MPC_RNDDZ.

INT2NUM(MPC_RNDDZ)
RNDDU =

Integer which is macro MPC_RNDDU.

INT2NUM(MPC_RNDDU)
RNDDD =

Integer which is macro MPC_RNDDD.

INT2NUM(MPC_RNDDD)
VERSION_MAJOR =

Integer which is macro MPC_VERSION_MAJOR.

INT2NUM(MPC_VERSION_MAJOR)
VERSION_MINOR =

Integer which is macro MPC_VERSION_MINOR.

INT2NUM(MPC_VERSION_MINOR)
VERSION_PATCHLEVEL =

Integer which is macro MPC_VERSION_PATCHLEVEL.

INT2NUM(MPC_VERSION_PATCHLEVEL)
VERSION_STRING =

Stirng which is macro MPC_VERSION_STRING.

rb_str_new2(MPC_VERSION_STRING)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_default_rounding_modeObject

Get the default rounding mode.



105
106
107
108
# File 'ext/mpc/ruby_mpc.c', line 105

static VALUE r_mpc_get_default_rounding_mode(VALUE self)
{
  return INT2NUM(rb_mpc_extended_get_default_rounding_mode());
}

.get_function_stateObject

Return function state.



111
112
113
114
# File 'ext/mpc/ruby_mpc.c', line 111

static VALUE r_mpc_get_function_state(VALUE self)
{
  return rb_cv_get(r_mpc_class, CLASS_VAL_FUNCTION_STATE);
}

.set_default_rounding_mode(rnd) ⇒ Object

Set the default rounding mode. The default rounding mode is MPC::RNDNN.



97
98
99
100
101
102
# File 'ext/mpc/ruby_mpc.c', line 97

static VALUE r_mpc_set_default_rounding_mode(VALUE self, VALUE rnd)
{
  mp_rnd_t a = NUM2INT(rnd);
  rb_mpc_extended_set_default_rounding_mode(a);
  return INT2FIX(rb_mpc_extended_get_default_rounding_mode());
}

Instance Method Details

#*(other) ⇒ Object

Return self * p1.



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'ext/mpc/ruby_mpc.c', line 375

static VALUE r_mpc_mul(VALUE self, VALUE other)
{
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init(val_ret, ptr_return);

  if(RTEST(rb_funcall(__mpc_class__, eqq, 1, other))){
    MPC *ptr_other;
    r_mpc_get_struct(ptr_other, other);
    mpc_mul(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, other);
    mpc_mul_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else{
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, rb_funcall(__mpc_class__, new, 1, other));
    mpc_mul_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }
  return val_ret;
}

#+(other) ⇒ Object

Return self + p1.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'ext/mpc/ruby_mpc.c', line 328

static VALUE r_mpc_add(VALUE self, VALUE other)
{
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init(val_ret, ptr_return);
  if(RTEST(rb_funcall(__mpc_class__, eqq, 1, other))){
    MPC *ptr_other;
    r_mpc_get_struct(ptr_other, other);
    mpc_add(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, other);
    mpc_add_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else{
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, rb_funcall(__mpc_class__, new, 1, other));
    mpc_add_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }
  return val_ret;
}

#-(other) ⇒ Object

Return self / p1.



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'ext/mpc/ruby_mpc.c', line 351

static VALUE r_mpc_sub(VALUE self, VALUE other)
{
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init(val_ret, ptr_return);

  if(RTEST(rb_funcall(__mpc_class__, eqq, 1, other))){
    MPC *ptr_other;
    r_mpc_get_struct(ptr_other, other);
    mpc_sub(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, other);
    mpc_sub_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else{
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, rb_funcall(__mpc_class__, new, 1, other));
    mpc_sub_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }
  return val_ret;
}

#/(other) ⇒ Object

Return self / p1.



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'ext/mpc/ruby_mpc.c', line 399

static VALUE r_mpc_div(VALUE self, VALUE other)
{
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init(val_ret, ptr_return);

  if(RTEST(rb_funcall(__mpc_class__, eqq, 1, other))){
    MPC *ptr_other;
    r_mpc_get_struct(ptr_other, other);
    mpc_div(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, other);
    mpc_div_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }else{
    MPFR *ptr_other;
    r_mpfr_get_struct(ptr_other, rb_funcall(__mpc_class__, new, 1, other));
    mpc_div_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
  }
  return val_ret;
}

#absObject

Return absolute value.



465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'ext/mpc/ruby_mpc.c', line 465

static VALUE r_mpc_abs (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self;
  MPFR *ptr_return;
  VALUE val_ret;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
  r_mpc_set_fr_function_state(mpc_abs(ptr_return, ptr_self, rnd));
  return val_ret;
}

#argObject

Return the argument.



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

static VALUE r_mpc_arg (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self;
  VALUE ret_val;
  MPFR *ptr_ret;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpfr_make_struct_init2(ret_val, ptr_ret, prec);
  r_mpc_set_fr_function_state(mpc_arg(ptr_ret, ptr_self, rnd));
  return ret_val;
}

#coerce(other) ⇒ Object

Return array which have MPC instance converted to from p1 and self.



240
241
242
243
244
245
246
247
248
# File 'ext/mpc/ruby_mpc.c', line 240

static VALUE r_mpc_coerce(VALUE self, VALUE other)
{
  VALUE val_other;
  MPC *ptr_self, *ptr_other;
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init2(val_other, ptr_other, rb_mpc_get_max_prec(ptr_self));
  r_mpc_set_from_one_object(ptr_other, other, rb_mpc_extended_get_default_rounding_mode());
  return rb_ary_new3(2, val_other, self);
}

#conjObject

Return conjugate of self.



451
452
453
454
455
456
457
458
459
460
461
462
# File 'ext/mpc/ruby_mpc.c', line 451

static VALUE r_mpc_conj (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init2(val_ret, ptr_return, prec);
  r_mpc_set_c_function_state(mpc_conj(ptr_return, ptr_self, rnd));
  return val_ret;
}

#imagObject

Return the imaginary part.



285
286
287
288
289
290
291
292
293
294
# File 'ext/mpc/ruby_mpc.c', line 285

static VALUE r_mpc_imag (VALUE self)
{
  MPC *ptr_self;
  VALUE ret_val;
  MPFR *ptr_ret;
  r_mpc_get_struct(ptr_self, self);
  r_mpfr_make_struct_init2(ret_val, ptr_ret, mpfr_get_prec(mpc_imagref(ptr_self)));
  r_mpc_set_fr_function_state(mpc_imag(ptr_ret, ptr_self, MPC_RNDNN));
  return ret_val;
}

#inspectObject

inspect



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'ext/mpc/ruby_mpc.c', line 253

static VALUE r_mpc_inspect(VALUE self)
{
  MPC *ptr_s;
  char *ret_str;
  VALUE ret_val;
  r_mpc_get_struct(ptr_s, self);
  if(!mpfr_asprintf(&ret_str, "#<MPC:%lx,['%.Re','%.Re'],[%d,%d]>",
		    NUM2LONG(rb_funcall(self, object_id, 0)), mpc_realref (ptr_s), mpc_imagref (ptr_s),
		    mpfr_get_prec(mpc_realref (ptr_s)), mpfr_get_prec(mpc_imagref (ptr_s)))) {
    rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
  }
  ret_val = rb_str_new2(ret_str);
  mpfr_free_str(ret_str);
  return ret_val;
}

#mul_iObject

Multiply self by i.



423
424
425
426
427
428
429
430
431
432
433
434
# File 'ext/mpc/ruby_mpc.c', line 423

static VALUE r_mpc_mul_i (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init2(val_ret, ptr_return, prec);
  r_mpc_set_c_function_state(mpc_mul_i(ptr_return, ptr_self, NUM2INT(argv[0]), rnd));
  return val_ret;
}

#negObject

Multiply self by -1.



437
438
439
440
441
442
443
444
445
446
447
448
# File 'ext/mpc/ruby_mpc.c', line 437

static VALUE r_mpc_neg (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self, *ptr_return;
  VALUE val_ret;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init2(val_ret, ptr_return, prec);
  r_mpc_set_c_function_state(mpc_neg(ptr_return, ptr_self, rnd));
  return val_ret;
}

#normObject

Return the norm (the square of absolute value).



480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'ext/mpc/ruby_mpc.c', line 480

static VALUE r_mpc_norm (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self;
  MPFR *ptr_return;
  VALUE val_ret;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
  r_mpc_set_fr_function_state(mpc_norm(ptr_return, ptr_self, rnd));
  return val_ret;
}

#projObject

Return the projection onto the Riemann sphere.



312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/mpc/ruby_mpc.c', line 312

static VALUE r_mpc_proj (int argc, VALUE *argv, VALUE self)
{
  mpc_rnd_t rnd;
  mp_prec_t prec;
  MPC *ptr_self, *ptr_ret;
  VALUE ret_val;
  r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
  r_mpc_get_struct(ptr_self, self);
  r_mpc_make_struct_init2(ret_val, ptr_ret, prec);
  r_mpc_set_c_function_state(mpc_proj(ptr_ret, ptr_self, rnd));
  return ret_val;
}

#realObject

Return the real part.



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

static VALUE r_mpc_real (VALUE self)
{
  MPC *ptr_self;
  VALUE ret_val;
  MPFR *ptr_ret;
  r_mpc_get_struct(ptr_self, self);
  r_mpfr_make_struct_init2(ret_val, ptr_ret, mpfr_get_prec(mpc_realref(ptr_self)));
  r_mpc_set_fr_function_state(mpc_real(ptr_ret, ptr_self, MPC_RNDNN));
  return ret_val;
}