Class: MPC

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

Overview

GMP Multiple Precision Complex numbers

Instances of this class can store variables of the type mpc_t. This class also contains many methods that act as the functions for mpc_t variables, as well as a few methods that attempt to make this library more Ruby-ish.

Defined Under Namespace

Classes: Rnd

Constant Summary collapse

MPC_VERSION =
rb_str_new2(MPC_VERSION_STRING)
MPC_RNDNN =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(0))
MPC_RNDNZ =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(16))
MPC_RNDNU =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(32))
MPC_RNDND =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(48))
MPC_RNDZN =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(1))
MPC_RNDZZ =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(17))
MPC_RNDZU =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(33))
MPC_RNDZD =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(49))
MPC_RNDUN =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(2))
MPC_RNDUZ =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(18))
MPC_RNDUU =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(34))
MPC_RNDUD =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(50))
MPC_RNDDN =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(3))
MPC_RNDDZ =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(19))
MPC_RNDDU =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(35))
MPC_RNDDD =
rb_funcall (cMPC_Rnd, new_id, 1, INT2FIX(51))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



244
245
246
247
248
249
250
251
252
253
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/mpc.c', line 244

VALUE r_mpc_initialize(int argc, VALUE *argv, VALUE self)
{
  MP_COMPLEX *self_val;
  MP_FLOAT *arg_val_f;
  MP_COMPLEX *arg_val_c;
  unsigned long prec = 0;
  mpfr_prec_t prec_re = 0;
  mpfr_prec_t prec_im = 0;
  mpc_rnd_t rnd_mode_val = r_mpc_default_rounding_mode;
  VALUE arg;

  mpc_get_struct (self, self_val);

  if (argc==0) {
    mpc_init2 (self_val, mpfr_get_default_prec());
    mpc_set_si (self_val, 0, rnd_mode_val);
    return Qnil;
  }

  arg = argv[0];

  // argc = 2 ==> argv[0] is value, argv[1] is prec
  //           OR argv[0] is value, argv[1] is rnd
  if (argc >= 2) {
    if (FIXNUM_P (argv[1])) {
      if (FIX2INT (argv[1]) >= 0)
        prec = FIX2INT (argv[1]);
      else {
        mpc_init2 (self_val, mpfr_get_default_prec());
        rb_raise (rb_eRangeError, "prec must be non-negative");
      }
    } else if (MPCRND_P (argv[1])) {
      rnd_mode_val = r_get_mpc_rounding_mode(argv[1]);
    } else {
      mpc_init2 (self_val, mpfr_get_default_prec());
      rb_raise (rb_eTypeError, "don't know how to interpret argument 1, a %s", rb_class2name (rb_class_of (argv[1])));
    }
  // if no precision provided, but an mpfr_t is passed as value, use its prec
  } else if (GMPF_P (arg)) {
    mpf_get_struct (arg, arg_val_f);
    prec = mpf_get_prec (arg_val_f);
  // if no precision provided, but an mpc_t is passed as value, use its prec
  } else if (MPC_P (arg)) {
    mpc_get_struct (arg, arg_val_c);
    mpc_get_prec2 (&prec_re, &prec_im, arg_val_c);
  }

  // argc = 3 ==> argv[0] is value, argv[1] is prec_r, argv[2] is prec_i
  //           OR argv[0] is value, argv[1] is prec,   argv[2] is rnd
  if (argc == 3) {
    if (MPCRND_P (argv[1])) {
      mpc_init2 (self_val, mpfr_get_default_prec());
      rb_raise (rb_eArgError, "the rounding mode should be the last argument");
    } else if (FIXNUM_P (argv[2])) {
      if (FIX2INT (argv[2]) >= 0) {
        // argv[1] was actually prec_r and //argv[2] is prec_i
        prec_re = (mpfr_prec_t) prec;
        prec_im = FIX2INT (argv[2]);
        prec = 0;
      } else {
        mpc_init2 (self_val, mpfr_get_default_prec());
        rb_raise (rb_eRangeError, "prec_im must be non-negative");
      }
    } else if (MPCRND_P (argv[2])) {
      rnd_mode_val = r_get_mpc_rounding_mode(argv[2]);
    } else {
      mpc_init2 (self_val, mpfr_get_default_prec());
      rb_raise (rb_eTypeError, "don't know how to interpret argument 2, a %s", rb_class2name (rb_class_of (argv[2])));
    }
  }

  // argc = 4 ==> argv[0] is value, argv[1] is prec_r, argv[2] is prec_i, argv[3] is rnd
  // TODO

  if (prec == 0)
    mpc_init2 (self_val, mpfr_get_default_prec());
  else
    mpc_init2 (self_val, prec);

  if (STRING_P (argv[0])) {
    // unfortunately, we cannot accept an explicit base, as we do in r_gmpf_initialize.
    // #new(c, prec, base) would be indistinguishable from #new(c, prec_r, prec_i).
    // TODO allow this behavior via something like #new_str or String#to_mpc
    mpc_set_str (self_val, StringValuePtr(arg), 0, rnd_mode_val);
    return Qnil;
  }

  //if (MPC_P(arg)) {
  //  mpc_get_struct (arg, arg_val_c);
  //  mpc_set (self_val, arg_val_c, rnd_mode_val);
  //} else {
    mpc_set_value (self_val, arg, rnd_mode_val);
  //}

  return Qnil;
}

Class Method Details

.new(value = 0) ⇒ Object

TODO:

support #new(c, prec)

TODO:

support #new(c, prec_r, prec_i)

Creates a new MPC complex number, with value as its value, converting where necessary. value must be an instance of one of the following classes:

  • MPC

  • GMP::Z

  • GMP::F

  • Fixnum

  • String

  • Bignum

Examples:

MPC.new(5)  #=> 5


229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'ext/mpc.c', line 229

VALUE r_mpcsg_new(int argc, VALUE *argv, VALUE klass)
{
  MP_COMPLEX *res_val;
  VALUE res;
  (void)klass;

  if (argc > 3)
    rb_raise (rb_eArgError, "wrong # of arguments (%d for 0, 1, 2, or 3)", argc);

  mpc_make_struct (res, res_val);
  rb_obj_call_init (res, argc, argv);

  return res;
}

.sprintf(format, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mpc.rb', line 15

def self.sprintf(format, *args)
  first_pct = format.index '%'
  result = format[0...first_pct]
  format.gsub(/(?<!%)%[0#+ ']*[0-9]*.?[0-9]*[a-zA-Z][^%]*/) do |fragment|
    arg = args.shift
    if fragment =~ /%[0#+ ']*[0-9]*.?[0-9]*[ZQF]/
      result << sprintf2(fragment, arg)
    elsif fragment =~ /%[0#+ ']*[0-9]*.?[0-9]*[PR]/ && GMP.const_defined?(:MPFR_VERSION)
      result << GMP::F.sprintf2(fragment, arg)
    else
      result << (fragment % arg)
    end
  end
  result
end

Instance Method Details

#*(arg_val) ⇒ Object



799
800
801
802
803
804
805
806
807
808
809
810
# File 'ext/mpc.c', line 799

VALUE r_mpc_mul2(VALUE self_val, VALUE arg_val)
{
  MP_COMPLEX *self;

  mpfr_prec_t res_real_prec, res_imag_prec;

  mpc_get_struct(self_val, self);
  res_real_prec = mpfr_get_prec(mpc_realref(self));
  res_imag_prec = mpfr_get_prec(mpc_imagref(self));

  return r_mpc_mul_do_the_work(self_val, arg_val, MPC_RNDNN, res_real_prec, res_imag_prec);
}

#+(arg_val) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
# File 'ext/mpc.c', line 627

VALUE r_mpc_add2(VALUE self_val, VALUE arg_val)
{
  MP_COMPLEX *self;

  mpfr_prec_t res_real_prec, res_imag_prec;

  mpc_get_struct(self_val, self);
  res_real_prec = mpfr_get_prec(mpc_realref(self));
  res_imag_prec = mpfr_get_prec(mpc_imagref(self));

  return r_mpc_add_do_the_work(self_val, arg_val, MPC_RNDNN, res_real_prec, res_imag_prec);
}

#-(arg_val) ⇒ Object



713
714
715
716
717
718
719
720
721
722
723
724
# File 'ext/mpc.c', line 713

VALUE r_mpc_sub2(VALUE self_val, VALUE arg_val)
{
  MP_COMPLEX *self;

  mpfr_prec_t res_real_prec, res_imag_prec;

  mpc_get_struct(self_val, self);
  res_real_prec = mpfr_get_prec(mpc_realref(self));
  res_imag_prec = mpfr_get_prec(mpc_imagref(self));

  return r_mpc_sub_do_the_work(self_val, arg_val, MPC_RNDNN, res_real_prec, res_imag_prec);
}

#-@Object

#/(arg_val) ⇒ Object



885
886
887
888
889
890
891
892
893
894
895
896
# File 'ext/mpc.c', line 885

VALUE r_mpc_div2(VALUE self_val, VALUE arg_val)
{
  MP_COMPLEX *self;

  mpfr_prec_t res_real_prec, res_imag_prec;

  mpc_get_struct(self_val, self);
  res_real_prec = mpfr_get_prec(mpc_realref(self));
  res_imag_prec = mpfr_get_prec(mpc_imagref(self));

  return r_mpc_div_do_the_work(self_val, arg_val, MPC_RNDNN, res_real_prec, res_imag_prec);
}

#<=>(arg) ⇒ Object

Comparison Functions



481
482
483
484
485
486
487
488
# File 'ext/mpc.c', line 481

VALUE r_mpc_cmp(VALUE self, VALUE arg)
{
  MP_COMPLEX *self_val;
  int res;
  mpc_get_struct (self, self_val);
  res = mpc_cmp_value (self_val, arg);
  return rb_assoc_new(INT2FIX(MPC_INEX_RE(res)), INT2FIX (MPC_INEX_IM(res)));
}

#==(arg) ⇒ Object



474
475
476
477
478
479
# File 'ext/mpc.c', line 474

VALUE r_mpc_eq(VALUE self, VALUE arg)
{
  MP_COMPLEX *self_val;
  mpc_get_struct (self,self_val);
  return (mpc_cmp_value (self_val, arg) == 0) ? Qtrue : Qfalse;
}

#absObject

#acosObject

#add(*args) ⇒ Object

Basic Arithmetic Functions



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'ext/mpc.c', line 596

VALUE r_mpc_add(int argc, VALUE *argv, VALUE self_val)
{
  MP_COMPLEX *self;
  VALUE rnd_mode_val;
  VALUE  res_real_prec_val, res_imag_prec_val;
  VALUE arg_val;

  mpfr_prec_t real_prec, imag_prec;
  mpfr_prec_t res_real_prec, res_imag_prec;
  mpc_rnd_t rnd_mode;

  mpc_get_struct(self_val,self);
  real_prec = mpfr_get_prec(mpc_realref(self));
  imag_prec = mpfr_get_prec(mpc_imagref(self));

  //if (argc > 0 && TYPE(argv[0]) == T_HASH) {
  //  rb_mpc_get_hash_arguments (&rnd_mode, &real_prec, &imag_prec, argv[0]);
    //res_real_prec = real_prec;
    //res_imag_prec = imag_prec;
  //} else {
    rb_scan_args (argc, argv, "13", &arg_val, &rnd_mode_val, &res_real_prec_val, &res_imag_prec_val);

    r_mpc_set_default_args (rnd_mode_val, res_real_prec_val, res_imag_prec_val,
                           &rnd_mode,    &res_real_prec,    &res_imag_prec,
                                              real_prec,         imag_prec);
  //}

  //return res_val;
  return r_mpc_add_do_the_work(self_val, arg_val, rnd_mode, res_real_prec, res_imag_prec);
}

#asinObject

#atanObject

#conjObject

#cosObject

#coshObject

#div(*args) ⇒ Object

TODO rb_define_method (cMPC, “fma”, r_mpc_fma, 2);



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'ext/mpc.c', line 854

VALUE r_mpc_div(int argc, VALUE *argv, VALUE self_val)
{
  MP_COMPLEX *self;
  VALUE rnd_mode_val;
  VALUE  res_real_prec_val, res_imag_prec_val;
  VALUE arg_val;

  mpfr_prec_t real_prec, imag_prec;
  mpfr_prec_t res_real_prec, res_imag_prec;
  mpc_rnd_t rnd_mode;

  mpc_get_struct(self_val,self);
  real_prec = mpfr_get_prec(mpc_realref(self));
  imag_prec = mpfr_get_prec(mpc_imagref(self));

  //if (argc > 0 && TYPE(argv[0]) == T_HASH) {
  //  rb_mpc_get_hash_arguments (&rnd_mode, &real_prec, &imag_prec, argv[0]);
    //res_real_prec = real_prec;
    //res_imag_prec = imag_prec;
  //} else {
    rb_scan_args (argc, argv, "13", &arg_val, &rnd_mode_val, &res_real_prec_val, &res_imag_prec_val);

    r_mpc_set_default_args (rnd_mode_val, res_real_prec_val, res_imag_prec_val,
                           &rnd_mode,    &res_real_prec,    &res_imag_prec,
                                              real_prec,         imag_prec);
  //}

  //return res_val;
  return r_mpc_div_do_the_work(self_val, arg_val, rnd_mode, res_real_prec, res_imag_prec);
}

#expObject

TODO rb_define_method (cMPC, “**”, r_mpc_pow, 1);

#imagObject #imag(rounding_mode) ⇒ Object

Returns the imaginary part of c as a GMP_F float (an MPFR float, really).



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'ext/mpc.c', line 547

VALUE r_mpc_imag(int argc, VALUE *argv, VALUE self)
{
  MP_COMPLEX *self_val;
  MP_FLOAT *imag_val;
  VALUE rnd_mode, imag;
  mpfr_prec_t pr=0, pi=0;
  mpc_rnd_t rnd_mode_val;

  mpc_get_struct (self, self_val);

  rb_scan_args (argc, argv, "01", &rnd_mode);
  if (NIL_P (rnd_mode)) { rnd_mode_val = r_mpc_default_rounding_mode; }
  else { rnd_mode_val = r_get_mpc_rounding_mode(rnd_mode); }

  mpf_make_struct (imag, imag_val);
  mpc_get_prec2 (&pr, &pi, self_val);
  mpfr_init2 (imag_val, pr);
  mpc_imag (imag_val, self_val, rnd_mode_val);
  return imag;
}

#logObject

#log10Object

#mul(*args) ⇒ Object



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'ext/mpc.c', line 768

VALUE r_mpc_mul(int argc, VALUE *argv, VALUE self_val)
{
  MP_COMPLEX *self;
  VALUE rnd_mode_val;
  VALUE  res_real_prec_val, res_imag_prec_val;
  VALUE arg_val;

  mpfr_prec_t real_prec, imag_prec;
  mpfr_prec_t res_real_prec, res_imag_prec;
  mpc_rnd_t rnd_mode;

  mpc_get_struct(self_val,self);
  real_prec = mpfr_get_prec(mpc_realref(self));
  imag_prec = mpfr_get_prec(mpc_imagref(self));

  //if (argc > 0 && TYPE(argv[0]) == T_HASH) {
  //  rb_mpc_get_hash_arguments (&rnd_mode, &real_prec, &imag_prec, argv[0]);
    //res_real_prec = real_prec;
    //res_imag_prec = imag_prec;
  //} else {
    rb_scan_args (argc, argv, "13", &arg_val, &rnd_mode_val, &res_real_prec_val, &res_imag_prec_val);

    r_mpc_set_default_args (rnd_mode_val, res_real_prec_val, res_imag_prec_val,
                           &rnd_mode,    &res_real_prec,    &res_imag_prec,
                                              real_prec,         imag_prec);
  //}

  //return res_val;
  return r_mpc_mul_do_the_work(self_val, arg_val, rnd_mode, res_real_prec, res_imag_prec);
}

#negObject

#normObject #norm(rounding_mode) ⇒ Object

Returns the norm of c (i.e., the square of its absolute value), as a GMP_F float (an MPFR float, really).



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'ext/mpc.c', line 1002

VALUE r_mpc_norm(int argc, VALUE *argv, VALUE self)
{
  MP_COMPLEX *self_val;
  MP_FLOAT *norm_val;
  VALUE rnd_mode, norm;
  mpfr_prec_t pr=0, pi=0;
  mpc_rnd_t rnd_mode_val;

  mpc_get_struct (self, self_val);

  rb_scan_args (argc, argv, "01", &rnd_mode);
  if (NIL_P (rnd_mode)) { rnd_mode_val = r_mpc_default_rounding_mode; }
  else { rnd_mode_val = r_get_mpc_rounding_mode (rnd_mode); }

  mpf_make_struct (norm, norm_val);
  mpc_get_prec2 (&pr, &pi, self_val);
  mpfr_init2 (norm_val, pr);
  mpc_norm (norm_val, self_val, rnd_mode_val);
  return norm;
}

#precObject

If the real and imaginary part of c have the same precision, it is returned. Otherwise, 0 is returned.



405
406
407
408
409
410
# File 'ext/mpc.c', line 405

VALUE r_mpc_prec(VALUE self)
{
  MP_COMPLEX *self_val;
  mpc_get_struct (self, self_val);
  return INT2NUM (mpc_get_prec (self_val));
}

#projObject

TODO rb_define_method (cMPC, “arg”, r_mpc_arg, 0);

#realObject #real(rounding_mode) ⇒ Object

Returns the real part of c as a GMP_F float (an MPFR float, really).



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'ext/mpc.c', line 519

VALUE r_mpc_real(int argc, VALUE *argv, VALUE self)
{
  MP_COMPLEX *self_val;
  MP_FLOAT *real_val;
  VALUE rnd_mode, real;
  mpfr_prec_t pr=0, pi=0;
  mpc_rnd_t rnd_mode_val;

  mpc_get_struct (self, self_val);

  rb_scan_args (argc, argv, "01", &rnd_mode);
  if (NIL_P (rnd_mode)) { rnd_mode_val = r_mpc_default_rounding_mode; }
  else { rnd_mode_val = r_get_mpc_rounding_mode (rnd_mode); }

  mpf_make_struct (real, real_val);
  mpc_get_prec2 (&pr, &pi, self_val);
  mpfr_init2 (real_val, pr);
  mpc_real (real_val, self_val, rnd_mode_val);
  return real;
}

#sinObject

Trigonometric Functions

#sinhObject

#sqrObject

TODO rb_define_method (cMPC, “mul_i”, r_mpc_mul_i, -1);

#sqrtObject

Power Functions and Logarithm

#sub(*args) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'ext/mpc.c', line 682

VALUE r_mpc_sub(int argc, VALUE *argv, VALUE self_val)
{
  MP_COMPLEX *self;
  VALUE rnd_mode_val;
  VALUE  res_real_prec_val, res_imag_prec_val;
  VALUE arg_val;

  mpfr_prec_t real_prec, imag_prec;
  mpfr_prec_t res_real_prec, res_imag_prec;
  mpc_rnd_t rnd_mode;

  mpc_get_struct(self_val,self);
  real_prec = mpfr_get_prec(mpc_realref(self));
  imag_prec = mpfr_get_prec(mpc_imagref(self));

  //if (argc > 0 && TYPE(argv[0]) == T_HASH) {
  //  rb_mpc_get_hash_arguments (&rnd_mode, &real_prec, &imag_prec, argv[0]);
    //res_real_prec = real_prec;
    //res_imag_prec = imag_prec;
  //} else {
    rb_scan_args (argc, argv, "13", &arg_val, &rnd_mode_val, &res_real_prec_val, &res_imag_prec_val);

    r_mpc_set_default_args (rnd_mode_val, res_real_prec_val, res_imag_prec_val,
                           &rnd_mode,    &res_real_prec,    &res_imag_prec,
                                              real_prec,         imag_prec);
  //}

  //return res_val;
  return r_mpc_sub_do_the_work(self_val, arg_val, rnd_mode, res_real_prec, res_imag_prec);
}

#tanObject

TODO rb_define_method (cMPC, “sin_cos”, r_mpc_sin_cos, -1);

#tanhObject

#to_sObject

Returns the decimal representation of the real part and imaginary part of c, as a String.



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'ext/mpc.c', line 425

VALUE r_mpc_to_s(int argc, VALUE *argv, VALUE self)
{
  MP_COMPLEX *self_val;
  char *str;
  VALUE base, sig_figs, rnd_mode, res;
  int base_val;
  size_t sig_figs_val;
  mpc_rnd_t rnd_mode_val;
  //mp_exp_t exponent;

  mpc_get_struct (self, self_val)

  rb_scan_args (argc, argv, "03", &base, &sig_figs, &rnd_mode);
  base_val = rb_base_type_range_check (base);
  sig_figs_val = rb_sig_figs_type_range_check (sig_figs);
  //if (NIL_P (sig_figs)) { sig_figs_val = (size_t)(0); }
  //else {                  sig_figs_val = (size_t)(FIX2NUM(sig_figs)); }
  if (NIL_P (rnd_mode)) { rnd_mode_val = r_mpc_default_rounding_mode; }
  else {                  rnd_mode_val = r_get_mpc_rounding_mode(rnd_mode); }

  str = mpc_get_str (base_val, sig_figs_val, self_val, rnd_mode_val);
  res = rb_str_new2 (str);

  mpc_free_str (str);
  return res;
}