Class: MPFI::Complex

Inherits:
Object
  • Object
show all
Defined in:
ext/mpfi_complex/mpfi/ruby_mpfi_complex.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ComplexObject

Return new MPFI::Complex instance. The same arguments as MPFI::Complex.new is acceptable.



39
40
41
42
43
44
45
46
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 39

static VALUE r_mpfi_complex_global_new (int argc, VALUE *argv, VALUE self)
{
  MPFIComplex *ptr;
  VALUE val;
  r_mpfi_make_complex_struct(val, ptr);
  r_mpfi_complex_set_initial_value(ptr, argc, argv);
  return val;
}

Instance Method Details

#addObject Also known as: +

Return self + p1.



164
165
166
167
168
169
170
171
172
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 164

static VALUE r_mpfi_complex_add (VALUE self, VALUE other) {
  MPFIComplex *ptr_self, *ptr_other, *ptr_ret;
  VALUE ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_get_complex_struct(ptr_other, other);
  r_mpfi_make_complex_struct_init(ret, ptr_ret);
  mpfi_complex_add(ptr_ret, ptr_self, ptr_other);
  return ret;
}

#conjugateObject

Return conjugate complex number.



110
111
112
113
114
115
116
117
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 110

static VALUE r_mpfi_complex_conjugate (VALUE self) {
  MPFIComplex *ptr_self, *ptr_ret;
  VALUE ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_make_complex_struct_init(ret, ptr_ret);
  mpfi_complex_conjugate(ptr_ret, ptr_self);
  return ret;
}

#divObject Also known as: /

Return self / p1.



197
198
199
200
201
202
203
204
205
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 197

static VALUE r_mpfi_complex_div (VALUE self, VALUE other) {
  MPFIComplex *ptr_self, *ptr_other, *ptr_ret;
  VALUE ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_get_complex_struct(ptr_other, other);
  r_mpfi_make_complex_struct_init(ret, ptr_ret);
  mpfi_complex_div(ptr_ret, ptr_self, ptr_other);
  return ret;
}

#elementObject Also known as: []

If p1 is 0, return real part. If p1 is 1, return imaginary part.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 89

static VALUE r_mpfi_complex_element (VALUE self, VALUE arg) {
  MPFIComplex *ptr_self;
  VALUE ret;
  MPFI *ptr_ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_make_struct_init(ret, ptr_ret);
  switch (NUM2INT(arg)) {
  case 0:
    mpfi_set(ptr_ret, ptr_self->re);
    break;
  case 1:
    mpfi_set(ptr_ret, ptr_self->im);
    break;
  default:
    rb_raise(rb_eArgError, "Argument must be 0 or 1.");
    break;
  }
  return ret;
}

#imaginaryObject

Return imaginary part.



78
79
80
81
82
83
84
85
86
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 78

static VALUE r_mpfi_complex_imaginary (VALUE self) {
  MPFIComplex *ptr_self;
  VALUE ret;
  MPFI *ptr_ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_make_struct_init(ret, ptr_ret);
  mpfi_set(ptr_ret, ptr_self->im);
  return ret;
}

#inspectObject

String for inspect.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 131

static VALUE r_mpfi_complex_inspect (VALUE self) {
  MPFIComplex *ptr_s;
  char *ret_str;
  VALUE ret_val;
  r_mpfi_get_complex_struct(ptr_s, self);
  if (!mpfr_asprintf(&ret_str, "#<MPFI:%lx,['%.Re %.Re', '%.Re %.Re'], [%d, %d]>",
		     NUM2LONG(rb_funcall(self, object_id, 0)), r_mpfi_left_ptr(ptr_s->re),
		     r_mpfi_right_ptr(ptr_s->re), r_mpfi_left_ptr(ptr_s->im), r_mpfi_right_ptr(ptr_s->im),
		     mpfi_get_prec(ptr_s->re), mpfi_get_prec(ptr_s->im))) {
    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;
}

#mulObject Also known as: *

Return self * p1.



186
187
188
189
190
191
192
193
194
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 186

static VALUE r_mpfi_complex_mul (VALUE self, VALUE other) {
  MPFIComplex *ptr_self, *ptr_other, *ptr_ret;
  VALUE ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_get_complex_struct(ptr_other, other);
  r_mpfi_make_complex_struct_init(ret, ptr_ret);
  mpfi_complex_mul(ptr_ret, ptr_self, ptr_other);
  return ret;
}

#realObject

Return real part.



67
68
69
70
71
72
73
74
75
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 67

static VALUE r_mpfi_complex_real (VALUE self) {
  MPFIComplex *ptr_self;
  VALUE ret;
  MPFI *ptr_ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_make_struct_init(ret, ptr_ret);
  mpfi_set(ptr_ret, ptr_self->re);
  return ret;
}

#str_aryObject

Return array including strings which the elements is converted to by p1.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 148

static VALUE r_mpfi_complex_str_ary (VALUE self, VALUE format_str) {
  MPFIComplex *ptr_self;
  char *format, *tmp_str;
  VALUE ret_val[2];
  r_mpfi_get_complex_struct(ptr_self, self);
  format = StringValuePtr(format_str);
  gmp_asprintf(&tmp_str, format, ptr_self->re);
  ret_val[0] = rb_str_new2(tmp_str);
  free(tmp_str);
  gmp_asprintf(&tmp_str, format, ptr_self->im);
  ret_val[1] = rb_str_new2(tmp_str);
  free(tmp_str);
  return rb_ary_new4(2, ret_val);
}

#subObject Also known as: -

Return self - p1.



175
176
177
178
179
180
181
182
183
# File 'ext/mpfi_complex/mpfi/ruby_mpfi_complex.c', line 175

static VALUE r_mpfi_complex_sub (VALUE self, VALUE other) {
  MPFIComplex *ptr_self, *ptr_other, *ptr_ret;
  VALUE ret;
  r_mpfi_get_complex_struct(ptr_self, self);
  r_mpfi_get_complex_struct(ptr_other, other);
  r_mpfi_make_complex_struct_init(ret, ptr_ret);
  mpfi_complex_sub(ptr_ret, ptr_self, ptr_other);
  return ret;
}