ruby-mpfr 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -2
- data/Manifest.txt +33 -7
- data/README.rdoc +10 -8
- data/Rakefile +4 -3
- data/ext/{extconf.rb → mpfr/extconf.rb} +4 -0
- data/ext/{ruby_mpfr.c → mpfr/ruby_mpfr.c} +466 -199
- data/ext/{ruby_mpfr.h → mpfr/ruby_mpfr.h} +2 -0
- data/ext/mpfr_matrix/mpfr/extconf.rb +7 -0
- data/ext/mpfr_matrix/mpfr/func_mpfr_matrix.c +524 -0
- data/ext/mpfr_matrix/mpfr/func_mpfr_matrix.h +72 -0
- data/ext/mpfr_matrix/mpfr/ruby_mpfr.h +40 -0
- data/ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c +1056 -0
- data/ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.h +13 -0
- data/lib/mpfr/matrix.rb +145 -0
- data/lib/mpfr/version.rb +3 -0
- data/ruby-mpfr.gemspec +36 -0
- data/spec/mpfr/allocate_spec.rb +60 -0
- data/spec/mpfr/arithmetic_spec.rb +64 -0
- data/spec/mpfr/comparison_spec.rb +21 -0
- data/spec/mpfr/constant_spec.rb +23 -0
- data/spec/mpfr/conversion_spec.rb +14 -0
- data/spec/mpfr/exception_spec.rb +60 -0
- data/spec/mpfr/functions_spec.rb +25 -0
- data/spec/mpfr/generate_number_modulue.rb +44 -0
- data/spec/mpfr/precision_roundmode_spec.rb +65 -0
- data/spec/mpfr/rounding_spec.rb +51 -0
- data/spec/mpfr/set_value_spec.rb +77 -0
- data/spec/mpfr/spec_helper.rb +13 -0
- data/spec/mpfr/string_spec.rb +58 -0
- data/spec/mpfr_matrix/generate_matrix_arguments.rb +55 -0
- data/spec/mpfr_matrix/mpfr_matrix_alloc_spec.rb +126 -0
- data/spec/mpfr_matrix/mpfr_matrix_arithmetic_spec.rb +93 -0
- data/spec/mpfr_matrix/mpfr_matrix_set_element_spec.rb +55 -0
- data/spec/mpfr_matrix/mpfr_matrix_string_spec.rb +31 -0
- data/spec/mpfr_matrix/mpfr_square_matrix_spec.rb +75 -0
- data/spec/mpfr_matrix/spec_helper.rb +16 -0
- data/tasks/extconf.rake +36 -0
- metadata +48 -16
- data/lib/ruby-mpfr.rb +0 -6
- data/spec/ruby-mpfr_spec.rb +0 -11
- data/spec/spec_helper.rb +0 -10
- data/tasks/rspec.rake +0 -21
@@ -0,0 +1,1056 @@
|
|
1
|
+
#include "ruby_mpfr_matrix.h"
|
2
|
+
|
3
|
+
static ID eqq;
|
4
|
+
|
5
|
+
void r_mpfr_matrix_free(void *ptr)
|
6
|
+
{
|
7
|
+
mpfr_matrix_clear(ptr);
|
8
|
+
free(ptr);
|
9
|
+
}
|
10
|
+
|
11
|
+
void r_mpfr_matrix_suitable_matrix_init (VALUE *other, MPFRMatrix **ptr_other, int row, int column)
|
12
|
+
{
|
13
|
+
if(column == 1){
|
14
|
+
r_mpfr_make_col_vector_struct(*other, *ptr_other);
|
15
|
+
mpfr_col_vector_init(*ptr_other, row);
|
16
|
+
}else if (row == 1){
|
17
|
+
r_mpfr_make_row_vector_struct(*other, *ptr_other);
|
18
|
+
mpfr_row_vector_init(*ptr_other, column);
|
19
|
+
}else{
|
20
|
+
if (row == column){
|
21
|
+
r_mpfr_make_square_matrix_struct(*other, *ptr_other);
|
22
|
+
}else{
|
23
|
+
r_mpfr_make_matrix_struct(*other, *ptr_other);
|
24
|
+
}
|
25
|
+
mpfr_matrix_init(*ptr_other, row, column);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
/* VALUE r_mpfr_matrix_copied_robj(VALUE old){ */
|
30
|
+
/* MPFRMatrix *ptr_old, *ptr_ret; */
|
31
|
+
/* r_mpfr_get_matrix_struct(ptr_old, old); */
|
32
|
+
/* VALUE ret; */
|
33
|
+
/* r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_old->row, ptr_old->column); */
|
34
|
+
/* mpfr_matrix_set(ptr_ret, ptr_old); */
|
35
|
+
/* return ret; */
|
36
|
+
/* } */
|
37
|
+
|
38
|
+
VALUE r_mpfr_matrix_robj(MPFRMatrix *x)
|
39
|
+
{
|
40
|
+
VALUE ret;
|
41
|
+
MPFRMatrix *ptr_ret;
|
42
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, x->row, x->column);
|
43
|
+
mpfr_matrix_set(ptr_ret, x);
|
44
|
+
return ret;
|
45
|
+
}
|
46
|
+
|
47
|
+
/* Allocation function for MPF::Matrix. */
|
48
|
+
static VALUE r_mpfr_matrix_alloc (VALUE self)
|
49
|
+
{
|
50
|
+
MPFRMatrix *ptr;
|
51
|
+
r_mpfr_make_matrix_struct(self, ptr);
|
52
|
+
return self;
|
53
|
+
}
|
54
|
+
|
55
|
+
/* static void r_mpfr_matrix_init_set_from_rmatrix(MPFRMatrix *x, VALUE src){ */
|
56
|
+
/* MPFRMatrix *ptr_src; */
|
57
|
+
/* r_mpfr_get_matrix_struct(ptr_src, src); */
|
58
|
+
/* mpfr_matrix_init(x, ptr_src->row, ptr_src->column); */
|
59
|
+
/* mpfr_matrix_set(x, ptr_src); */
|
60
|
+
/* } */
|
61
|
+
|
62
|
+
/*
|
63
|
+
Initialization function for MPF::Matrix.
|
64
|
+
If this method gets one argument which is two dimensional Array,
|
65
|
+
it returns MPFR::Matrix instance of which size is same as the array.
|
66
|
+
Getting two argument integers (i, j), it returns MPFR::Matrix instance of which
|
67
|
+
size is (i x j).
|
68
|
+
*/
|
69
|
+
static VALUE r_mpfr_matrix_initialize (int argc, VALUE *argv, VALUE self)
|
70
|
+
{
|
71
|
+
MPFRMatrix *ptr;
|
72
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
73
|
+
int row, column, i, j;
|
74
|
+
VALUE row_ary;
|
75
|
+
switch (argc) {
|
76
|
+
case 1:
|
77
|
+
row = RARRAY_LEN(argv[0]);
|
78
|
+
column = RARRAY_LEN(rb_ary_entry(argv[0], 0));
|
79
|
+
mpfr_matrix_init(ptr, row, column);
|
80
|
+
for (i = 0; i < row; i++) {
|
81
|
+
row_ary = rb_ary_entry(argv[0], i);
|
82
|
+
if (column != RARRAY_LEN(row_ary)) {
|
83
|
+
rb_raise(rb_eArgError, "MPF::Matrix.new needs Array which has arrays of same sizes.");
|
84
|
+
}
|
85
|
+
for (j = 0; j < column; j++) {
|
86
|
+
r_mpfr_set_robj(mpfr_matrix_get_element(ptr, i, j), rb_ary_entry(row_ary, j), GMP_RNDN);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
break;
|
90
|
+
case 2:
|
91
|
+
mpfr_matrix_init(ptr, NUM2INT(argv[0]), NUM2INT(argv[1]));
|
92
|
+
mpfr_matrix_set_zeros(ptr);
|
93
|
+
break;
|
94
|
+
default:
|
95
|
+
rb_raise(rb_eArgError, "MPF::Matrix.new needs one or two arguments.");
|
96
|
+
break;
|
97
|
+
}
|
98
|
+
return Qtrue;
|
99
|
+
}
|
100
|
+
|
101
|
+
/* Allocation function for MPF::SquareMatrix. */
|
102
|
+
static VALUE r_mpfr_square_matrix_alloc (VALUE self)
|
103
|
+
{
|
104
|
+
MPFRMatrix *ptr;
|
105
|
+
r_mpfr_make_square_matrix_struct(self, ptr);
|
106
|
+
return self;
|
107
|
+
}
|
108
|
+
|
109
|
+
/* Initialization function for MPF::SquareMatrix. */
|
110
|
+
static VALUE r_mpfr_square_matrix_initialize (VALUE self, VALUE arg)
|
111
|
+
{
|
112
|
+
MPFRMatrix *ptr;
|
113
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
114
|
+
int row, column, i, j;
|
115
|
+
VALUE row_ary;
|
116
|
+
switch(TYPE(arg)){
|
117
|
+
case T_ARRAY:
|
118
|
+
row = RARRAY_LEN(arg);
|
119
|
+
column = RARRAY_LEN(rb_ary_entry(arg, 0));
|
120
|
+
if(row == column){
|
121
|
+
mpfr_matrix_init(ptr, row, column);
|
122
|
+
for (i = 0; i < row; i++) {
|
123
|
+
row_ary = rb_ary_entry(arg, i);
|
124
|
+
if (column != RARRAY_LEN(row_ary)) {
|
125
|
+
rb_raise(rb_eArgError, "MPF::Matrix.new needs Array which has arrays of same sizes.");
|
126
|
+
}
|
127
|
+
for (j = 0; j < column; j++) {
|
128
|
+
r_mpfr_set_robj(ptr->data + i + j * row, rb_ary_entry(row_ary, j), GMP_RNDN);
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}else{
|
132
|
+
rb_raise(rb_eArgError, "MPF::SquareMatrix.new needs two dimensinal Array which has the same sizes of row and column.");
|
133
|
+
}
|
134
|
+
break;
|
135
|
+
case T_FIXNUM:
|
136
|
+
row = NUM2INT(arg);
|
137
|
+
mpfr_matrix_init(ptr, row, row);
|
138
|
+
mpfr_matrix_set_zeros(ptr);
|
139
|
+
break;
|
140
|
+
default:
|
141
|
+
rb_raise(rb_eArgError, "MPF::SquareMatrix.new needs Array or Fixnum.");
|
142
|
+
break;
|
143
|
+
}
|
144
|
+
return Qtrue;
|
145
|
+
}
|
146
|
+
|
147
|
+
/* initialize_copy */
|
148
|
+
static VALUE r_mpfr_matrix_initialize_copy (VALUE self, VALUE other)
|
149
|
+
{
|
150
|
+
MPFRMatrix *ptr_self, *ptr_other;
|
151
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
152
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
153
|
+
mpfr_matrix_init(ptr_self, ptr_other->row, ptr_other->column);
|
154
|
+
mpfr_matrix_set(ptr_self, ptr_other);
|
155
|
+
return Qtrue;
|
156
|
+
}
|
157
|
+
|
158
|
+
/* Allocation function for MPF::ColumnVector. */
|
159
|
+
static VALUE r_mpfr_col_vector_alloc (VALUE self)
|
160
|
+
{
|
161
|
+
MPFRMatrix *ptr;
|
162
|
+
r_mpfr_make_col_vector_struct(self, ptr);
|
163
|
+
return self;
|
164
|
+
}
|
165
|
+
|
166
|
+
/* Initialization function for MPF::ColumnVector. */
|
167
|
+
static VALUE r_mpfr_col_vector_initialize (VALUE self, VALUE arg)
|
168
|
+
{
|
169
|
+
MPFRMatrix *ptr;
|
170
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
171
|
+
int row, i;
|
172
|
+
switch(TYPE(arg)){
|
173
|
+
case T_ARRAY:
|
174
|
+
row = RARRAY_LEN(arg);
|
175
|
+
mpfr_col_vector_init(ptr, row);
|
176
|
+
for (i = 0; i < row; i++) {
|
177
|
+
r_mpfr_set_robj(ptr->data + i, rb_ary_entry(arg, i), GMP_RNDN);
|
178
|
+
}
|
179
|
+
break;
|
180
|
+
case T_FIXNUM:
|
181
|
+
row = NUM2INT(arg);
|
182
|
+
mpfr_col_vector_init(ptr, row);
|
183
|
+
for (i = 0; i < row; i++) {
|
184
|
+
mpfr_set_si(ptr->data + i, 0, GMP_RNDN);
|
185
|
+
}
|
186
|
+
break;
|
187
|
+
default:
|
188
|
+
rb_raise(rb_eArgError, "MPF::ColumnVector.new needs Array or Fixnum.");
|
189
|
+
break;
|
190
|
+
}
|
191
|
+
return Qtrue;
|
192
|
+
}
|
193
|
+
|
194
|
+
/* Allocation function for MPF::RowVector. */
|
195
|
+
static VALUE r_mpfr_row_vector_alloc (VALUE self)
|
196
|
+
{
|
197
|
+
MPFRMatrix *ptr;
|
198
|
+
r_mpfr_make_row_vector_struct(self, ptr);
|
199
|
+
return self;
|
200
|
+
}
|
201
|
+
|
202
|
+
/* Initialization function for MPF::RowVector. */
|
203
|
+
static VALUE r_mpfr_row_vector_initialize (VALUE self, VALUE arg)
|
204
|
+
{
|
205
|
+
MPFRMatrix *ptr;
|
206
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
207
|
+
int column, i;
|
208
|
+
switch(TYPE(arg)){
|
209
|
+
case T_ARRAY:
|
210
|
+
column = RARRAY_LEN(arg);
|
211
|
+
mpfr_row_vector_init(ptr, column);
|
212
|
+
for (i = 0; i < column; i++) {
|
213
|
+
r_mpfr_set_robj(ptr->data + i, rb_ary_entry(arg, i), GMP_RNDN);
|
214
|
+
}
|
215
|
+
break;
|
216
|
+
case T_FIXNUM:
|
217
|
+
column = NUM2INT(arg);
|
218
|
+
mpfr_row_vector_init(ptr, column);
|
219
|
+
for (i = 0; i < column; i++) {
|
220
|
+
mpfr_set_si(ptr->data + i, 0, GMP_RNDN);
|
221
|
+
}
|
222
|
+
break;
|
223
|
+
default:
|
224
|
+
rb_raise(rb_eArgError, "MPF::RowVector.new needs Array or Fixnum.");
|
225
|
+
break;
|
226
|
+
}
|
227
|
+
return Qtrue;
|
228
|
+
}
|
229
|
+
|
230
|
+
/* Return true if all elements of _self_ equal to corresponding elements of _other_. */
|
231
|
+
static VALUE r_mpfr_matrix_equal_p (VALUE self, VALUE other)
|
232
|
+
{
|
233
|
+
MPFRMatrix *ptr_self, *ptr_other;
|
234
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
235
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
236
|
+
VALUE ret = Qtrue;
|
237
|
+
if (mpfr_matrix_equal_p(ptr_self, ptr_other) != 0) {
|
238
|
+
ret = Qnil;
|
239
|
+
}
|
240
|
+
return ret;
|
241
|
+
}
|
242
|
+
|
243
|
+
/* Return size of data array which equals to column * row. */
|
244
|
+
static VALUE r_mpfr_matrix_size (VALUE self)
|
245
|
+
{
|
246
|
+
MPFRMatrix *ptr_self;
|
247
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
248
|
+
return INT2FIX(ptr_self->size);
|
249
|
+
}
|
250
|
+
|
251
|
+
/* Return column size of matrix. */
|
252
|
+
static VALUE r_mpfr_matrix_column_size (VALUE self)
|
253
|
+
{
|
254
|
+
MPFRMatrix *ptr_self;
|
255
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
256
|
+
return INT2FIX(ptr_self->column);
|
257
|
+
}
|
258
|
+
|
259
|
+
/* Return row size of matrix. */
|
260
|
+
static VALUE r_mpfr_matrix_row_size (VALUE self)
|
261
|
+
{
|
262
|
+
MPFRMatrix *ptr_self;
|
263
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
264
|
+
return INT2FIX(ptr_self->row);
|
265
|
+
}
|
266
|
+
|
267
|
+
/* Return element with _row_ and _column_.*/
|
268
|
+
static VALUE r_mpfr_matrix_element (VALUE self, VALUE row, VALUE column)
|
269
|
+
{
|
270
|
+
MPFRMatrix *ptr_self;
|
271
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
272
|
+
int i = NUM2INT(row), j = NUM2INT(column);
|
273
|
+
if (i < ptr_self->row && j < ptr_self->column) {
|
274
|
+
VALUE ret;
|
275
|
+
MPFR *ptr_ret;
|
276
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
277
|
+
mpfr_set(ptr_ret, ptr_self->data + i + j * ptr_self->row, GMP_RNDN);
|
278
|
+
return ret;
|
279
|
+
}else{
|
280
|
+
return Qnil;
|
281
|
+
}
|
282
|
+
}
|
283
|
+
|
284
|
+
/* Return element at _arg_.*/
|
285
|
+
static VALUE r_mpfr_matrix_at (VALUE self, VALUE arg)
|
286
|
+
{
|
287
|
+
MPFRMatrix *ptr_self;
|
288
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
289
|
+
int i = NUM2INT(arg);
|
290
|
+
if (i < ptr_self->size) {
|
291
|
+
VALUE ret;
|
292
|
+
MPFR *ptr_ret;
|
293
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
294
|
+
mpfr_set(ptr_ret, ptr_self->data + i, GMP_RNDN);
|
295
|
+
return ret;
|
296
|
+
}else{
|
297
|
+
return Qnil;
|
298
|
+
}
|
299
|
+
}
|
300
|
+
|
301
|
+
/* Set _robj_ to element of which row is _row_ and column is _column_. */
|
302
|
+
static VALUE r_mpfr_matrix_set_element (VALUE self, VALUE row, VALUE column, VALUE robj)
|
303
|
+
{
|
304
|
+
MPFRMatrix *ptr_self;
|
305
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
306
|
+
int i = NUM2INT(row), j = NUM2INT(column);
|
307
|
+
if (i < ptr_self->row && j < ptr_self->column) {
|
308
|
+
r_mpfr_set_robj(mpfr_matrix_get_element(ptr_self, i, j), robj, GMP_RNDN);
|
309
|
+
}
|
310
|
+
return Qnil;
|
311
|
+
}
|
312
|
+
|
313
|
+
/* Evaluate block with each element of matrix. */
|
314
|
+
static VALUE r_mpfr_matrix_each_element (VALUE self)
|
315
|
+
{
|
316
|
+
MPFRMatrix *ptr_self;
|
317
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
318
|
+
VALUE ret = Qnil;
|
319
|
+
int i, j;
|
320
|
+
for (i = 0; i < ptr_self->row; i++) {
|
321
|
+
for (j = 0; j < ptr_self->column; j++) {
|
322
|
+
volatile VALUE arg = r_mpfr_make_new_fr_obj(mpfr_matrix_get_element(ptr_self, i, j));
|
323
|
+
ret = rb_yield(arg);
|
324
|
+
}
|
325
|
+
}
|
326
|
+
return ret;
|
327
|
+
}
|
328
|
+
|
329
|
+
/* Evaluate block with each element and its index. */
|
330
|
+
static VALUE r_mpfr_matrix_each_element_with_index (VALUE self)
|
331
|
+
{
|
332
|
+
MPFRMatrix *ptr_self;
|
333
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
334
|
+
VALUE ret = Qnil, tmp_i;
|
335
|
+
int i, j;
|
336
|
+
for (i = 0; i < ptr_self->row; i++) {
|
337
|
+
tmp_i = INT2NUM(i);
|
338
|
+
for (j = 0; j < ptr_self->column; j++) {
|
339
|
+
volatile VALUE arg = r_mpfr_make_new_fr_obj(mpfr_matrix_get_element(ptr_self, i, j));
|
340
|
+
ret = rb_yield(rb_ary_new3(3, arg, tmp_i, INT2NUM(j)));
|
341
|
+
}
|
342
|
+
}
|
343
|
+
return ret;
|
344
|
+
}
|
345
|
+
|
346
|
+
/* Return array which has strings converted elements to. */
|
347
|
+
static VALUE r_mpfr_matrix_str_ary(VALUE self, VALUE format_str)
|
348
|
+
{
|
349
|
+
MPFRMatrix *ptr_self;
|
350
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
351
|
+
char *format = StringValuePtr(format_str), *tmp_str;
|
352
|
+
VALUE ret_val[ptr_self->size];
|
353
|
+
int i;
|
354
|
+
for (i = 0; i < ptr_self->size; i++) {
|
355
|
+
mpfr_asprintf(&tmp_str, format, ptr_self->data + i);
|
356
|
+
ret_val[i] = rb_str_new2(tmp_str);
|
357
|
+
mpfr_free_str(tmp_str);
|
358
|
+
}
|
359
|
+
return rb_ary_new4(ptr_self->size, ret_val);
|
360
|
+
}
|
361
|
+
|
362
|
+
static VALUE r_mpfr_matrix_str_ary2(VALUE self, VALUE format_str)
|
363
|
+
{
|
364
|
+
MPFRMatrix *ptr_self;
|
365
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
366
|
+
char *format = StringValuePtr(format_str), *tmp_str;
|
367
|
+
VALUE ary[ptr_self->row];
|
368
|
+
int i, j;
|
369
|
+
for(i = 0; i < ptr_self->row; i++){
|
370
|
+
ary[i] = rb_ary_new();
|
371
|
+
}
|
372
|
+
for (i = 0; i < ptr_self->size; i += ptr_self->row) {
|
373
|
+
for (j = 0; j < ptr_self->row; j++) {
|
374
|
+
mpfr_asprintf(&tmp_str, format, ptr_self->data + i + j);
|
375
|
+
rb_ary_push(ary[j], rb_str_new2(tmp_str));
|
376
|
+
mpfr_free_str(tmp_str);
|
377
|
+
}
|
378
|
+
}
|
379
|
+
return rb_ary_new4(ptr_self->row, ary);
|
380
|
+
}
|
381
|
+
|
382
|
+
static VALUE r_mpfr_matrix_to_array(VALUE self)
|
383
|
+
{
|
384
|
+
MPFRMatrix *ptr_self;
|
385
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
386
|
+
VALUE ret_val[ptr_self->size];
|
387
|
+
int i;
|
388
|
+
for (i = 0; i < ptr_self->size; i++) {
|
389
|
+
ret_val[i] = r_mpfr_make_new_fr_obj(ptr_self->data + i);
|
390
|
+
}
|
391
|
+
return rb_ary_new4(ptr_self->size, ret_val);
|
392
|
+
}
|
393
|
+
|
394
|
+
static VALUE r_mpfr_matrix_to_array2(VALUE self)
|
395
|
+
{
|
396
|
+
MPFRMatrix *ptr_self;
|
397
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
398
|
+
VALUE ary[ptr_self->row];
|
399
|
+
int i, j;
|
400
|
+
for(i = 0; i < ptr_self->row; i++){
|
401
|
+
ary[i] = rb_ary_new();
|
402
|
+
}
|
403
|
+
for (i = 0; i < ptr_self->size; i += ptr_self->row) {
|
404
|
+
for (j = 0; j < ptr_self->row; j++) {
|
405
|
+
rb_ary_push(ary[j], r_mpfr_make_new_fr_obj(ptr_self->data + i + j));
|
406
|
+
}
|
407
|
+
}
|
408
|
+
return rb_ary_new4(ptr_self->row, ary);
|
409
|
+
}
|
410
|
+
|
411
|
+
static VALUE r_mpfr_matrix_row (VALUE self, VALUE arg)
|
412
|
+
{
|
413
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
414
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
415
|
+
int num = NUM2INT(arg);
|
416
|
+
VALUE ret;
|
417
|
+
if(num < ptr_self->row){
|
418
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, 1, ptr_self->column);
|
419
|
+
mpfr_matrix_row(ptr_ret, ptr_self, num);
|
420
|
+
return ret;
|
421
|
+
}else{
|
422
|
+
return Qnil;
|
423
|
+
}
|
424
|
+
}
|
425
|
+
|
426
|
+
static VALUE r_mpfr_matrix_column (VALUE self, VALUE arg)
|
427
|
+
{
|
428
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
429
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
430
|
+
int num = NUM2INT(arg);
|
431
|
+
VALUE ret;
|
432
|
+
if(num < ptr_self->column){
|
433
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, 1);
|
434
|
+
mpfr_matrix_column(ptr_ret, ptr_self, num);
|
435
|
+
return ret;
|
436
|
+
}else{
|
437
|
+
return Qnil;
|
438
|
+
}
|
439
|
+
}
|
440
|
+
|
441
|
+
/* Return transposed matrix which is new object. */
|
442
|
+
static VALUE r_mpfr_matrix_transpose (VALUE self)
|
443
|
+
{
|
444
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
445
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
446
|
+
VALUE ret;
|
447
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->column, ptr_self->row);
|
448
|
+
mpfr_matrix_transpose(ptr_ret, ptr_self);
|
449
|
+
return ret;
|
450
|
+
}
|
451
|
+
|
452
|
+
/* Transpose self. This method is destroy method. */
|
453
|
+
static VALUE r_mpfr_matrix_transpose2 (VALUE self)
|
454
|
+
{
|
455
|
+
MPFRMatrix *ptr_self, tmp;
|
456
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
457
|
+
if (ptr_self->column > 1 && ptr_self->row > 1) {
|
458
|
+
mpfr_matrix_init(&tmp, ptr_self->column, ptr_self->row);
|
459
|
+
mpfr_matrix_transpose(&tmp, ptr_self);
|
460
|
+
mpfr_matrix_set(ptr_self, &tmp);
|
461
|
+
mpfr_matrix_clear(&tmp);
|
462
|
+
}
|
463
|
+
int i;
|
464
|
+
i = ptr_self->column;
|
465
|
+
ptr_self->column = ptr_self->row;
|
466
|
+
ptr_self->row = i;
|
467
|
+
return self;
|
468
|
+
}
|
469
|
+
|
470
|
+
static VALUE r_mpfr_matrix_neg (VALUE self)
|
471
|
+
{
|
472
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
473
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
474
|
+
VALUE ret;
|
475
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
476
|
+
mpfr_matrix_neg(ptr_ret, ptr_self);
|
477
|
+
return ret;
|
478
|
+
}
|
479
|
+
|
480
|
+
/* Return _self_ + _other_. */
|
481
|
+
static VALUE r_mpfr_matrix_add (VALUE self, VALUE other)
|
482
|
+
{
|
483
|
+
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
484
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
485
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
486
|
+
VALUE ret;
|
487
|
+
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
|
488
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
489
|
+
mpfr_matrix_add(ptr_ret, ptr_self, ptr_other);
|
490
|
+
}else{
|
491
|
+
rb_raise(rb_eArgError, "Matrixes must have same size.");
|
492
|
+
}
|
493
|
+
return ret;
|
494
|
+
}
|
495
|
+
|
496
|
+
/* Return _self_ + _other_ which is MPF::Matrix. */
|
497
|
+
static VALUE r_mpfr_matrix_add2 (VALUE self, VALUE other)
|
498
|
+
{
|
499
|
+
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
500
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
501
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
502
|
+
VALUE ret;
|
503
|
+
r_mpfr_make_matrix_struct(ret, ptr_ret);
|
504
|
+
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
|
505
|
+
mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_self->column);
|
506
|
+
mpfr_matrix_add(ptr_ret, ptr_self, ptr_other);
|
507
|
+
}else{
|
508
|
+
rb_raise(rb_eArgError, "Matrixes must have same size.");
|
509
|
+
}
|
510
|
+
return ret;
|
511
|
+
}
|
512
|
+
|
513
|
+
/* Return _self_ - _other_. */
|
514
|
+
static VALUE r_mpfr_matrix_sub (VALUE self, VALUE other)
|
515
|
+
{
|
516
|
+
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
517
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
518
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
519
|
+
VALUE ret;
|
520
|
+
r_mpfr_make_matrix_struct(ret, ptr_ret);
|
521
|
+
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
|
522
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
523
|
+
mpfr_matrix_sub(ptr_ret, ptr_self, ptr_other);
|
524
|
+
}else{
|
525
|
+
rb_raise(rb_eArgError, "Matrixes must have same size.");
|
526
|
+
}
|
527
|
+
return ret;
|
528
|
+
}
|
529
|
+
|
530
|
+
/* Return _self_ - _other_ which is MPF::Matrix. */
|
531
|
+
static VALUE r_mpfr_matrix_sub2 (VALUE self, VALUE other)
|
532
|
+
{
|
533
|
+
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
534
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
535
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
536
|
+
VALUE ret;
|
537
|
+
r_mpfr_make_matrix_struct(ret, ptr_ret);
|
538
|
+
if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
|
539
|
+
mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_self->column);
|
540
|
+
mpfr_matrix_sub(ptr_ret, ptr_self, ptr_other);
|
541
|
+
}else{
|
542
|
+
rb_raise(rb_eArgError, "Matrixes must have same size.");
|
543
|
+
}
|
544
|
+
return ret;
|
545
|
+
}
|
546
|
+
|
547
|
+
static VALUE r_mpfr_vector_inner_product (VALUE self, VALUE arg)
|
548
|
+
{
|
549
|
+
MPFRMatrix *ptr_self, *ptr_arg;
|
550
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
551
|
+
r_mpfr_get_matrix_struct(ptr_arg, arg);
|
552
|
+
VALUE ret;
|
553
|
+
MPFR *ptr_ret;
|
554
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
555
|
+
if(ptr_self->size == ptr_arg->size){
|
556
|
+
mpfr_matrix_inner_product(ptr_ret, ptr_self, ptr_arg);
|
557
|
+
}else{
|
558
|
+
rb_raise(rb_eArgError, "Vectors must have same size.");
|
559
|
+
}
|
560
|
+
return ret;
|
561
|
+
}
|
562
|
+
|
563
|
+
/* Return _self_ * _other_. */
|
564
|
+
static VALUE r_mpfr_matrix_mul_matrix (VALUE self, VALUE other)
|
565
|
+
{
|
566
|
+
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
567
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
568
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
569
|
+
VALUE ret;
|
570
|
+
r_mpfr_make_matrix_struct(ret, ptr_ret);
|
571
|
+
if (ptr_self->column == ptr_other->row) {
|
572
|
+
if((ptr_other->column == 1) && (ptr_self->row == 1)){
|
573
|
+
ret = r_mpfr_vector_inner_product(self, other);
|
574
|
+
}else{
|
575
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
|
576
|
+
mpfr_matrix_mul(ptr_ret, ptr_self, ptr_other);
|
577
|
+
}
|
578
|
+
}else{
|
579
|
+
rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
|
580
|
+
}
|
581
|
+
return ret;
|
582
|
+
}
|
583
|
+
|
584
|
+
/* Return _self_ * _other_ which is MPF::Matrix. */
|
585
|
+
static VALUE r_mpfr_matrix_mul_matrix2 (VALUE self, VALUE other)
|
586
|
+
{
|
587
|
+
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
588
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
589
|
+
r_mpfr_get_matrix_struct(ptr_other, other);
|
590
|
+
VALUE ret;
|
591
|
+
r_mpfr_make_matrix_struct(ret, ptr_ret);
|
592
|
+
if (ptr_self->column == ptr_other->row) {
|
593
|
+
mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_other->column);
|
594
|
+
mpfr_matrix_mul(ptr_ret, ptr_self, ptr_other);
|
595
|
+
}else{
|
596
|
+
rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
|
597
|
+
}
|
598
|
+
return ret;
|
599
|
+
}
|
600
|
+
|
601
|
+
/* Return _scalar_ * _self_, where _scalar_ is scalar and _self_ is matrix. */
|
602
|
+
static VALUE r_mpfr_matrix_mul_scalar (VALUE self, VALUE scalar)
|
603
|
+
{
|
604
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
605
|
+
MPFR *ptr_scalar;
|
606
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
607
|
+
VALUE ret;
|
608
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
609
|
+
if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, scalar))){
|
610
|
+
r_mpfr_get_struct(ptr_scalar, scalar);
|
611
|
+
mpfr_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
|
612
|
+
}else{
|
613
|
+
r_mpfr_temp_alloc_init(ptr_scalar);
|
614
|
+
r_mpfr_set_robj(ptr_scalar, scalar, GMP_RNDN);
|
615
|
+
mpfr_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
|
616
|
+
r_mpfr_temp_free(ptr_scalar);
|
617
|
+
}
|
618
|
+
return ret;
|
619
|
+
}
|
620
|
+
|
621
|
+
/* Return _scalar_ * _self_, where _scalar_ is scalar and _self_ is matrix. */
|
622
|
+
static VALUE r_mpfr_matrix_div_scalar (VALUE self, VALUE scalar)
|
623
|
+
{
|
624
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
625
|
+
MPFR *ptr_scalar;
|
626
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
627
|
+
VALUE ret;
|
628
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
629
|
+
if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, scalar))){
|
630
|
+
r_mpfr_get_struct(ptr_scalar, scalar);
|
631
|
+
mpfr_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
|
632
|
+
}else{
|
633
|
+
r_mpfr_temp_alloc_init(ptr_scalar);
|
634
|
+
r_mpfr_set_robj(ptr_scalar, scalar, GMP_RNDN);
|
635
|
+
mpfr_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
|
636
|
+
r_mpfr_temp_free(ptr_scalar);
|
637
|
+
}
|
638
|
+
return ret;
|
639
|
+
}
|
640
|
+
|
641
|
+
/* Take matrix for vector and return length of the vector. */
|
642
|
+
static VALUE r_mpfr_matrix_vector_norm (VALUE self)
|
643
|
+
{
|
644
|
+
MPFRMatrix *ptr_self;
|
645
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
646
|
+
VALUE ret;
|
647
|
+
MPFR *ptr_ret;
|
648
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
649
|
+
mpfr_matrix_vector_norm(ptr_ret, ptr_self);
|
650
|
+
return ret;
|
651
|
+
}
|
652
|
+
|
653
|
+
/* Return maximum value of absolute value of element. */
|
654
|
+
static VALUE r_mpfr_matrix_max_norm (VALUE self)
|
655
|
+
{
|
656
|
+
MPFRMatrix *ptr_self;
|
657
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
658
|
+
VALUE ret;
|
659
|
+
MPFR *ptr_ret;
|
660
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
661
|
+
mpfr_matrix_max_norm(ptr_ret, ptr_self);
|
662
|
+
return ret;
|
663
|
+
}
|
664
|
+
|
665
|
+
/* Return row size of matrix. */
|
666
|
+
static VALUE r_mpfr_square_matrix_dim (VALUE self)
|
667
|
+
{
|
668
|
+
MPFRMatrix *ptr_self;
|
669
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
670
|
+
return INT2FIX(ptr_self->row);
|
671
|
+
}
|
672
|
+
|
673
|
+
/* Return [matrix_l, matrix_r, indx]. */
|
674
|
+
static VALUE r_mpfr_square_matrix_lu_decomp (VALUE self)
|
675
|
+
{
|
676
|
+
MPFRMatrix *ptr_self, *ptr_ret_l, *ptr_ret_u;
|
677
|
+
VALUE ret_l, ret_u;
|
678
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
679
|
+
r_mpfr_matrix_suitable_matrix_init (&ret_l, &ptr_ret_l, ptr_self->row, ptr_self->column);
|
680
|
+
r_mpfr_matrix_suitable_matrix_init (&ret_u, &ptr_ret_u, ptr_self->row, ptr_self->column);
|
681
|
+
VALUE ret_indx[ptr_self->row];
|
682
|
+
int indx[ptr_self->row];
|
683
|
+
if(mpfr_square_matrix_lu_decomp (ptr_ret_u, indx, ptr_self) >= 0){
|
684
|
+
int i, j;
|
685
|
+
for(i = 1; i < ptr_ret_u->row; i++){
|
686
|
+
for(j = 0; j < i; j++){
|
687
|
+
mpfr_set(mpfr_matrix_get_element(ptr_ret_l, i, j), mpfr_matrix_get_element(ptr_ret_u, i, j), GMP_RNDN);
|
688
|
+
mpfr_set_si(mpfr_matrix_get_element(ptr_ret_u, i, j), 0, GMP_RNDN);
|
689
|
+
}
|
690
|
+
}
|
691
|
+
for(i = 0; i < ptr_ret_u->row; i++){
|
692
|
+
mpfr_set_si(mpfr_matrix_get_element(ptr_ret_l, i, i), 1, GMP_RNDN);
|
693
|
+
}
|
694
|
+
for(i = 0; i < ptr_ret_u->row; i++){
|
695
|
+
for(j = i + 1; j < ptr_ret_u->column; j++){
|
696
|
+
mpfr_set_si(mpfr_matrix_get_element(ptr_ret_l, i, j), 0, GMP_RNDN);
|
697
|
+
}
|
698
|
+
}
|
699
|
+
for(i = 0; i < ptr_ret_u->row; i++){
|
700
|
+
ret_indx[i] = INT2NUM(indx[i]);
|
701
|
+
}
|
702
|
+
return rb_ary_new3(3, ret_l, ret_u, rb_ary_new4(ptr_ret_u->row, ret_indx));
|
703
|
+
}else{
|
704
|
+
return Qnil;
|
705
|
+
}
|
706
|
+
}
|
707
|
+
|
708
|
+
static VALUE r_mpfr_square_matrix_determinant (VALUE self)
|
709
|
+
{
|
710
|
+
MPFRMatrix *ptr_self;
|
711
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
712
|
+
MPFR *ptr_ret;
|
713
|
+
VALUE ret;
|
714
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
715
|
+
mpfr_square_matrix_determinant(ptr_ret, ptr_self);
|
716
|
+
return ret;
|
717
|
+
}
|
718
|
+
|
719
|
+
static VALUE r_mpfr_square_matrix_qr_decomp (VALUE self)
|
720
|
+
{
|
721
|
+
MPFRMatrix *ptr_self, *ptr_q, *ptr_r;
|
722
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
723
|
+
VALUE q, r;
|
724
|
+
r_mpfr_matrix_suitable_matrix_init (&q, &ptr_q, ptr_self->row, ptr_self->column);
|
725
|
+
r_mpfr_matrix_suitable_matrix_init (&r, &ptr_r, ptr_self->column, ptr_self->column);
|
726
|
+
mpfr_square_matrix_qr_decomp(ptr_q, ptr_r, ptr_self);
|
727
|
+
return rb_ary_new3(2, q, r);
|
728
|
+
}
|
729
|
+
|
730
|
+
static VALUE r_mpfr_square_matrix_identity (VALUE self, VALUE size)
|
731
|
+
{
|
732
|
+
VALUE ret;
|
733
|
+
MPFRMatrix *ptr_ret;
|
734
|
+
int s = NUM2INT(size);
|
735
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, s, s);
|
736
|
+
mpfr_square_matrix_identity(ptr_ret);
|
737
|
+
return ret;
|
738
|
+
}
|
739
|
+
|
740
|
+
/* Return size of data array which equals to column * row. */
|
741
|
+
static VALUE r_mpfr_vector_dim (VALUE self)
|
742
|
+
{
|
743
|
+
MPFRMatrix *ptr_self;
|
744
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
745
|
+
return INT2FIX(ptr_self->size);
|
746
|
+
}
|
747
|
+
|
748
|
+
/* Take matrix for vector and return length of the vector. */
|
749
|
+
static VALUE r_mpfr_vector_abs (VALUE self)
|
750
|
+
{
|
751
|
+
MPFRMatrix *ptr_self;
|
752
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
753
|
+
VALUE ret;
|
754
|
+
MPFR *ptr_ret;
|
755
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
756
|
+
mpfr_matrix_vector_norm(ptr_ret, ptr_self);
|
757
|
+
return ret;
|
758
|
+
}
|
759
|
+
|
760
|
+
/* Return element at _arg_.*/
|
761
|
+
static VALUE r_mpfr_vector_element (VALUE self, VALUE arg)
|
762
|
+
{
|
763
|
+
MPFRMatrix *ptr_self;
|
764
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
765
|
+
int i = NUM2INT(arg);
|
766
|
+
if (i < ptr_self->size) {
|
767
|
+
VALUE ret;
|
768
|
+
MPFR *ptr_ret;
|
769
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
770
|
+
mpfr_set(ptr_ret, ptr_self->data + i, GMP_RNDN);
|
771
|
+
return ret;
|
772
|
+
}else{
|
773
|
+
return Qnil;
|
774
|
+
}
|
775
|
+
}
|
776
|
+
|
777
|
+
/* Set _robj_ to _arg_ th element. */
|
778
|
+
static VALUE r_mpfr_vector_set_element (VALUE self, VALUE arg, VALUE robj)
|
779
|
+
{
|
780
|
+
MPFRMatrix *ptr_self;
|
781
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
782
|
+
int i = NUM2INT(arg);
|
783
|
+
if (i < ptr_self->size) {
|
784
|
+
r_mpfr_set_robj(ptr_self->data + i, robj, GMP_RNDN);
|
785
|
+
}
|
786
|
+
return Qnil;
|
787
|
+
}
|
788
|
+
|
789
|
+
/* Evaluate block with each element of vector. */
|
790
|
+
static VALUE r_mpfr_vector_each_element (VALUE self)
|
791
|
+
{
|
792
|
+
MPFRMatrix *ptr_self;
|
793
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
794
|
+
VALUE ret = Qnil, tmp;
|
795
|
+
MPFR *tmp_ptr;
|
796
|
+
r_mpfr_make_struct_init(tmp, tmp_ptr);
|
797
|
+
int i;
|
798
|
+
for (i = 0; i < ptr_self->size; i++) {
|
799
|
+
mpfr_set(tmp_ptr, ptr_self->data + i, GMP_RNDN);
|
800
|
+
ret = rb_yield(tmp);
|
801
|
+
}
|
802
|
+
return ret;
|
803
|
+
}
|
804
|
+
|
805
|
+
/* Evaluate block with each element and its index. */
|
806
|
+
static VALUE r_mpfr_vector_each_element_with_index (VALUE self)
|
807
|
+
{
|
808
|
+
MPFRMatrix *ptr_self;
|
809
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
810
|
+
VALUE ret = Qnil, tmp;
|
811
|
+
MPFR *tmp_ptr;
|
812
|
+
r_mpfr_make_struct_init(tmp, tmp_ptr);
|
813
|
+
int i;
|
814
|
+
for (i = 0; i < ptr_self->size; i++) {
|
815
|
+
mpfr_set(tmp_ptr, ptr_self->data + i, GMP_RNDN);
|
816
|
+
ret = rb_yield(rb_ary_new3(2, tmp, INT2NUM(i)));
|
817
|
+
}
|
818
|
+
return ret;
|
819
|
+
}
|
820
|
+
|
821
|
+
static VALUE r_mpfr_vector_distance_from (VALUE self, VALUE arg)
|
822
|
+
{
|
823
|
+
MPFRMatrix *ptr_self, *ptr_arg;
|
824
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
825
|
+
r_mpfr_get_matrix_struct(ptr_arg, arg);
|
826
|
+
VALUE ret;
|
827
|
+
MPFR *ptr_ret;
|
828
|
+
r_mpfr_make_struct_init(ret, ptr_ret);
|
829
|
+
if(ptr_self->size == ptr_arg->size){
|
830
|
+
mpfr_matrix_vector_distance(ptr_ret, ptr_self, ptr_arg);
|
831
|
+
}else{
|
832
|
+
rb_raise(rb_eArgError, "Vectors must have same size.");
|
833
|
+
}
|
834
|
+
return ret;
|
835
|
+
}
|
836
|
+
|
837
|
+
static VALUE r_mpfr_vector_midpoint (VALUE self, VALUE arg)
|
838
|
+
{
|
839
|
+
MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
|
840
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
841
|
+
r_mpfr_get_matrix_struct(ptr_arg, arg);
|
842
|
+
VALUE ret;
|
843
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
844
|
+
mpfr_vector_midpoint(ptr_ret, ptr_self, ptr_arg);
|
845
|
+
return ret;
|
846
|
+
}
|
847
|
+
|
848
|
+
static VALUE r_mpfr_vector_dividing_point (VALUE self, VALUE arg, VALUE div)
|
849
|
+
{
|
850
|
+
MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
|
851
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
852
|
+
r_mpfr_get_matrix_struct(ptr_arg, arg);
|
853
|
+
MPFR *ptr_div;
|
854
|
+
r_mpfr_get_struct(ptr_div, div);
|
855
|
+
VALUE ret;
|
856
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
857
|
+
mpfr_vector_dividing_point(ptr_ret, ptr_self, ptr_arg, ptr_div);
|
858
|
+
return ret;
|
859
|
+
}
|
860
|
+
|
861
|
+
static VALUE r_mpfr_vector_normalize (VALUE self)
|
862
|
+
{
|
863
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
864
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
865
|
+
VALUE ret;
|
866
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
867
|
+
if(mpfr_vector_normalize(ptr_ret, ptr_self) == 0){
|
868
|
+
return ret;
|
869
|
+
}else{
|
870
|
+
return Qnil;
|
871
|
+
}
|
872
|
+
}
|
873
|
+
|
874
|
+
/* Return normalized vector of _self_. This method is destroy method. */
|
875
|
+
static VALUE r_mpfr_vector_normalize2 (VALUE self)
|
876
|
+
{
|
877
|
+
MPFRMatrix *ptr_self, tmp;
|
878
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
879
|
+
VALUE ret = self;
|
880
|
+
mpfr_matrix_init(&tmp, ptr_self->column, ptr_self->row);
|
881
|
+
if(mpfr_vector_normalize(&tmp, ptr_self) == 0){
|
882
|
+
mpfr_matrix_set(ptr_self, &tmp);
|
883
|
+
}else{
|
884
|
+
ret = Qnil;
|
885
|
+
}
|
886
|
+
mpfr_matrix_clear(&tmp);
|
887
|
+
return ret;
|
888
|
+
}
|
889
|
+
|
890
|
+
static VALUE r_mpfr_vector_set_length (VALUE self, VALUE arg)
|
891
|
+
{
|
892
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
893
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
894
|
+
VALUE ret, returned_value = Qnil;
|
895
|
+
MPFR *ptr_l;
|
896
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
897
|
+
if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
|
898
|
+
r_mpfr_get_struct(ptr_l, arg);
|
899
|
+
if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
|
900
|
+
returned_value = ret;
|
901
|
+
}
|
902
|
+
}else{
|
903
|
+
r_mpfr_temp_alloc_init(ptr_l);
|
904
|
+
r_mpfr_set_robj(ptr_l, arg, GMP_RNDN);
|
905
|
+
if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
|
906
|
+
returned_value = ret;
|
907
|
+
}
|
908
|
+
r_mpfr_temp_free(ptr_l);
|
909
|
+
}
|
910
|
+
return returned_value;
|
911
|
+
}
|
912
|
+
|
913
|
+
static VALUE r_mpfr_vector_set_length2 (VALUE self, VALUE arg)
|
914
|
+
{
|
915
|
+
MPFRMatrix *ptr_self, *ptr_ret;
|
916
|
+
r_mpfr_get_matrix_struct(ptr_self, self);
|
917
|
+
VALUE ret, returned_value = Qnil;
|
918
|
+
MPFR *ptr_l;
|
919
|
+
r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
|
920
|
+
if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
|
921
|
+
r_mpfr_get_struct(ptr_l, arg);
|
922
|
+
if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
|
923
|
+
mpfr_matrix_set(ptr_self, ptr_ret);
|
924
|
+
returned_value = self;
|
925
|
+
}
|
926
|
+
}else{
|
927
|
+
r_mpfr_temp_alloc_init(ptr_l);
|
928
|
+
r_mpfr_set_robj(ptr_l, arg, GMP_RNDN);
|
929
|
+
if(mpfr_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
|
930
|
+
mpfr_matrix_set(ptr_self, ptr_ret);
|
931
|
+
returned_value = self;
|
932
|
+
}
|
933
|
+
r_mpfr_temp_free(ptr_l);
|
934
|
+
}
|
935
|
+
return returned_value;
|
936
|
+
}
|
937
|
+
|
938
|
+
void Init_matrix()
|
939
|
+
{
|
940
|
+
|
941
|
+
VALUE tmp_mpfr_class = rb_define_class("MPFR", rb_cNumeric);
|
942
|
+
|
943
|
+
/*
|
944
|
+
MPFR::Matrix is class in which instances means matrix and
|
945
|
+
have MPFR instances as elements.
|
946
|
+
*/
|
947
|
+
|
948
|
+
/* Initialization of MPFR::Matrix, MPFR::SquareMatrix, MPFR::ColumnVector and MPFR::RowVector */
|
949
|
+
r_mpfr_matrix = rb_define_class_under(tmp_mpfr_class, "Matrix", rb_cObject);
|
950
|
+
r_mpfr_square_matrix = rb_define_class_under(tmp_mpfr_class, "SquareMatrix", r_mpfr_matrix);
|
951
|
+
r_mpfr_col_vector = rb_define_class_under(tmp_mpfr_class, "ColumnVector", r_mpfr_matrix);
|
952
|
+
r_mpfr_row_vector = rb_define_class_under(tmp_mpfr_class, "RowVector", r_mpfr_matrix);
|
953
|
+
|
954
|
+
rb_define_alloc_func(r_mpfr_matrix, r_mpfr_matrix_alloc);
|
955
|
+
rb_define_private_method(r_mpfr_matrix, "initialize", r_mpfr_matrix_initialize, -1);
|
956
|
+
rb_define_private_method(r_mpfr_matrix, "initialize_copy", r_mpfr_matrix_initialize_copy, 1);
|
957
|
+
|
958
|
+
rb_define_alloc_func(r_mpfr_square_matrix, r_mpfr_square_matrix_alloc);
|
959
|
+
rb_define_private_method(r_mpfr_square_matrix, "initialize", r_mpfr_square_matrix_initialize, 1);
|
960
|
+
|
961
|
+
rb_define_alloc_func(r_mpfr_col_vector, r_mpfr_col_vector_alloc);
|
962
|
+
rb_define_private_method(r_mpfr_col_vector, "initialize", r_mpfr_col_vector_initialize, 1);
|
963
|
+
|
964
|
+
rb_define_alloc_func(r_mpfr_row_vector, r_mpfr_row_vector_alloc);
|
965
|
+
rb_define_private_method(r_mpfr_row_vector, "initialize", r_mpfr_row_vector_initialize, 1);
|
966
|
+
|
967
|
+
|
968
|
+
rb_define_method(r_mpfr_matrix, "size", r_mpfr_matrix_size, 0);
|
969
|
+
rb_define_method(r_mpfr_matrix, "column_size", r_mpfr_matrix_column_size, 0);
|
970
|
+
rb_define_method(r_mpfr_matrix, "row_size", r_mpfr_matrix_row_size, 0);
|
971
|
+
|
972
|
+
rb_define_method(r_mpfr_matrix, "set_element", r_mpfr_matrix_set_element, 3);
|
973
|
+
rb_define_method(r_mpfr_matrix, "at", r_mpfr_matrix_at, 1);
|
974
|
+
rb_define_method(r_mpfr_matrix, "element", r_mpfr_matrix_element, 2);
|
975
|
+
rb_define_method(r_mpfr_matrix, "each_element", r_mpfr_matrix_each_element, 0);
|
976
|
+
rb_define_method(r_mpfr_matrix, "each_element_with_index", r_mpfr_matrix_each_element_with_index, 0);
|
977
|
+
|
978
|
+
rb_define_alias(r_mpfr_matrix, "[]=", "set_element");
|
979
|
+
rb_define_alias(r_mpfr_matrix, "[]", "element");
|
980
|
+
rb_define_alias(r_mpfr_matrix, "each", "each_element");
|
981
|
+
|
982
|
+
rb_include_module(r_mpfr_matrix, rb_mEnumerable);
|
983
|
+
|
984
|
+
|
985
|
+
rb_define_method(r_mpfr_matrix, "str_ary", r_mpfr_matrix_str_ary, 1);
|
986
|
+
rb_define_method(r_mpfr_matrix, "str_ary2", r_mpfr_matrix_str_ary2, 1);
|
987
|
+
|
988
|
+
|
989
|
+
rb_define_method(r_mpfr_matrix, "to_a", r_mpfr_matrix_to_array2, 0);
|
990
|
+
rb_define_method(r_mpfr_matrix, "to_a2", r_mpfr_matrix_to_array, 0);
|
991
|
+
|
992
|
+
rb_define_method(r_mpfr_matrix, "row", r_mpfr_matrix_row, 1);
|
993
|
+
rb_define_method(r_mpfr_matrix, "column", r_mpfr_matrix_column, 1);
|
994
|
+
rb_define_method(r_mpfr_matrix, "transpose", r_mpfr_matrix_transpose, 0);
|
995
|
+
rb_define_method(r_mpfr_matrix, "transpose!", r_mpfr_matrix_transpose2, 0);
|
996
|
+
rb_define_method(r_mpfr_matrix, "neg", r_mpfr_matrix_neg, 0);
|
997
|
+
|
998
|
+
|
999
|
+
rb_define_method(r_mpfr_matrix, "==", r_mpfr_matrix_equal_p, 1);
|
1000
|
+
rb_define_method(r_mpfr_matrix, "add", r_mpfr_matrix_add, 1);
|
1001
|
+
rb_define_method(r_mpfr_matrix, "add2", r_mpfr_matrix_add2, 1);
|
1002
|
+
rb_define_method(r_mpfr_matrix, "sub", r_mpfr_matrix_sub, 1);
|
1003
|
+
rb_define_method(r_mpfr_matrix, "sub2", r_mpfr_matrix_sub2, 1);
|
1004
|
+
rb_define_method(r_mpfr_matrix, "mul_matrix", r_mpfr_matrix_mul_matrix, 1);
|
1005
|
+
rb_define_method(r_mpfr_matrix, "mul_matrix2", r_mpfr_matrix_mul_matrix2, 1);
|
1006
|
+
rb_define_method(r_mpfr_matrix, "mul_scalar", r_mpfr_matrix_mul_scalar, 1);
|
1007
|
+
rb_define_method(r_mpfr_matrix, "div_scalar", r_mpfr_matrix_div_scalar, 1);
|
1008
|
+
|
1009
|
+
rb_define_alias(r_mpfr_matrix, "+", "add");
|
1010
|
+
rb_define_alias(r_mpfr_matrix, "-", "sub");
|
1011
|
+
rb_define_alias(r_mpfr_matrix, "*", "mul_matrix");
|
1012
|
+
|
1013
|
+
rb_define_method(r_mpfr_matrix, "vector_norm", r_mpfr_matrix_vector_norm, 0);
|
1014
|
+
rb_define_method(r_mpfr_matrix, "max_norm", r_mpfr_matrix_max_norm, 0);
|
1015
|
+
|
1016
|
+
|
1017
|
+
rb_define_method(r_mpfr_square_matrix, "dim", r_mpfr_square_matrix_dim, 0);
|
1018
|
+
rb_define_method(r_mpfr_square_matrix, "lu_decomp", r_mpfr_square_matrix_lu_decomp, 0);
|
1019
|
+
rb_define_method(r_mpfr_square_matrix, "determinant", r_mpfr_square_matrix_determinant, 0);
|
1020
|
+
rb_define_method(r_mpfr_square_matrix, "qr_decomp", r_mpfr_square_matrix_qr_decomp, 0);
|
1021
|
+
|
1022
|
+
rb_define_singleton_method(r_mpfr_square_matrix, "identity", r_mpfr_square_matrix_identity, 1);
|
1023
|
+
|
1024
|
+
/* Initialization of MPFR::Vector module */
|
1025
|
+
r_mpfr_vector_module = rb_define_module_under(tmp_mpfr_class, "Vector");
|
1026
|
+
|
1027
|
+
rb_define_method(r_mpfr_vector_module, "[]=", r_mpfr_vector_set_element, 2);
|
1028
|
+
rb_define_method(r_mpfr_vector_module, "[]", r_mpfr_vector_element, 1);
|
1029
|
+
rb_define_method(r_mpfr_vector_module, "each_element", r_mpfr_vector_each_element, 0);
|
1030
|
+
rb_define_method(r_mpfr_vector_module, "each_element_with_index", r_mpfr_vector_each_element_with_index, 0);
|
1031
|
+
|
1032
|
+
rb_define_alias(r_mpfr_vector_module, "each", "each_element");
|
1033
|
+
rb_define_alias(r_mpfr_vector_module, "each_with_index", "each_element_with_index");
|
1034
|
+
|
1035
|
+
rb_define_method(r_mpfr_vector_module, "dim", r_mpfr_vector_dim, 0);
|
1036
|
+
rb_define_method(r_mpfr_vector_module, "normalize", r_mpfr_vector_normalize, 0);
|
1037
|
+
rb_define_method(r_mpfr_vector_module, "normalize!", r_mpfr_vector_normalize2, 0);
|
1038
|
+
rb_define_method(r_mpfr_vector_module, "set_length", r_mpfr_vector_set_length, 1);
|
1039
|
+
rb_define_method(r_mpfr_vector_module, "set_length!", r_mpfr_vector_set_length2, 1);
|
1040
|
+
|
1041
|
+
|
1042
|
+
rb_define_method(r_mpfr_vector_module, "abs", r_mpfr_vector_abs, 0);
|
1043
|
+
rb_define_method(r_mpfr_vector_module, "inner_product", r_mpfr_vector_inner_product, 1);
|
1044
|
+
rb_define_method(r_mpfr_vector_module, "distance_from", r_mpfr_vector_distance_from, 1);
|
1045
|
+
rb_define_method(r_mpfr_vector_module, "midpoint", r_mpfr_vector_midpoint, 1);
|
1046
|
+
rb_define_method(r_mpfr_vector_module, "dividing_point", r_mpfr_vector_dividing_point, 2);
|
1047
|
+
|
1048
|
+
rb_include_module(r_mpfr_col_vector, r_mpfr_vector_module);
|
1049
|
+
rb_include_module(r_mpfr_row_vector, r_mpfr_vector_module);
|
1050
|
+
|
1051
|
+
|
1052
|
+
eqq = rb_intern("===");
|
1053
|
+
|
1054
|
+
|
1055
|
+
}
|
1056
|
+
|