ruby-mpfi 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +62 -0
  3. data/PostInstall.txt +7 -0
  4. data/README.rdoc +48 -0
  5. data/Rakefile +26 -0
  6. data/ext/mpfi/extconf.rb +10 -0
  7. data/ext/mpfi/func_mpfi_extention.c +52 -0
  8. data/ext/mpfi/func_mpfi_extention.h +2 -0
  9. data/ext/mpfi/make_c_source.rb +115 -0
  10. data/ext/mpfi/ruby_mpfi.c +1452 -0
  11. data/ext/mpfi/ruby_mpfi.h +39 -0
  12. data/ext/mpfi/ruby_mpfr.h +38 -0
  13. data/ext/mpfi/yasnippet_mpfi.el +44 -0
  14. data/ext/mpfi_complex/mpfi/extconf.rb +10 -0
  15. data/ext/mpfi_complex/mpfi/func_mpfi_extention.h +2 -0
  16. data/ext/mpfi_complex/mpfi/func_ruby_mpfi_complex.c +130 -0
  17. data/ext/mpfi_complex/mpfi/func_ruby_mpfi_complex.h +35 -0
  18. data/ext/mpfi_complex/mpfi/ruby_mpfi.h +39 -0
  19. data/ext/mpfi_complex/mpfi/ruby_mpfi_complex.c +217 -0
  20. data/ext/mpfi_complex/mpfi/ruby_mpfi_complex.h +15 -0
  21. data/ext/mpfi_complex/mpfi/ruby_mpfr.h +38 -0
  22. data/ext/mpfi_matrix/mpfi/extconf.rb +9 -0
  23. data/ext/mpfi_matrix/mpfi/func_mpfi_extention.h +2 -0
  24. data/ext/mpfi_matrix/mpfi/func_mpfi_matrix.c +795 -0
  25. data/ext/mpfi_matrix/mpfi/func_mpfi_matrix.h +103 -0
  26. data/ext/mpfi_matrix/mpfi/func_mpfr_matrix.h +72 -0
  27. data/ext/mpfi_matrix/mpfi/func_ruby_mpfi_complex.h +35 -0
  28. data/ext/mpfi_matrix/mpfi/ruby_mpfi.h +39 -0
  29. data/ext/mpfi_matrix/mpfi/ruby_mpfi_complex.h +15 -0
  30. data/ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c +1200 -0
  31. data/ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.h +13 -0
  32. data/ext/mpfi_matrix/mpfi/ruby_mpfr.h +38 -0
  33. data/ext/mpfi_matrix/mpfi/ruby_mpfr_matrix.h +13 -0
  34. data/lib/mpfi/matrix.rb +188 -0
  35. data/lib/mpfi/version.rb +3 -0
  36. data/ruby-mpfi.gemspec +35 -0
  37. data/script/console +10 -0
  38. data/script/destroy +14 -0
  39. data/script/generate +14 -0
  40. data/spec/mpfi/generate_number_module.rb +48 -0
  41. data/spec/mpfi/mpfi_alloc_spec.rb +55 -0
  42. data/spec/mpfi/mpfi_diam_arithmetic_spec.rb +25 -0
  43. data/spec/mpfi/mpfi_interval_arithmetic_spec.rb +105 -0
  44. data/spec/mpfi/mpfi_interval_functions_spec.rb +95 -0
  45. data/spec/mpfi/mpfi_math_functions_spec.rb +16 -0
  46. data/spec/mpfi/mpfi_set_operation_spec.rb +102 -0
  47. data/spec/mpfi/ruby-mpfi_spec.rb +11 -0
  48. data/spec/mpfi/spec_helper.rb +10 -0
  49. data/spec/mpfi_complex/spec_helper.rb +10 -0
  50. data/spec/mpfi_matrix/generate_matrix_arguments.rb +65 -0
  51. data/spec/mpfi_matrix/mpfi_matrix_alloc_spec.rb +134 -0
  52. data/spec/mpfi_matrix/mpfi_matrix_arithmetic_spec.rb +156 -0
  53. data/spec/mpfi_matrix/mpfi_matrix_interval_func_spec.rb +30 -0
  54. data/spec/mpfi_matrix/mpfi_matrix_set_element_spec.rb +55 -0
  55. data/spec/mpfi_matrix/mpfi_matrix_set_operation_spec.rb +71 -0
  56. data/spec/mpfi_matrix/mpfi_matrix_string_spec.rb +32 -0
  57. data/spec/mpfi_matrix/mpfi_matrix_subdivision_spec.rb +14 -0
  58. data/spec/mpfi_matrix/mpfi_square_matrix_spec.rb +37 -0
  59. data/spec/mpfi_matrix/mpfi_vector_spec.rb +15 -0
  60. data/spec/mpfi_matrix/spec_helper.rb +19 -0
  61. data/spec/spec.opts +1 -0
  62. data/tasks/extconf.rake +36 -0
  63. metadata +132 -0
@@ -0,0 +1,1200 @@
1
+ #include "ruby_mpfi_matrix.h"
2
+
3
+ static ID eqq;
4
+ static VALUE __mpfr_matrix_class__, __mpfi_matrix_class__;
5
+
6
+ void r_mpfi_matrix_free(void *ptr){
7
+ mpfi_matrix_clear(ptr);
8
+ free(ptr);
9
+ }
10
+
11
+ void r_mpfi_matrix_suitable_matrix_init (VALUE *other, MPFIMatrix **ptr_other, int row, int column){
12
+ if(column == 1){
13
+ r_mpfi_make_col_vector_struct(*other, *ptr_other);
14
+ mpfi_col_vector_init(*ptr_other, row);
15
+ }else if (row == 1){
16
+ r_mpfi_make_row_vector_struct(*other, *ptr_other);
17
+ mpfi_row_vector_init(*ptr_other, column);
18
+ }else{
19
+ if (row == column){
20
+ r_mpfi_make_square_matrix_struct(*other, *ptr_other);
21
+ }else{
22
+ r_mpfi_make_matrix_struct(*other, *ptr_other);
23
+ }
24
+ mpfi_matrix_init(*ptr_other, row, column);
25
+ }
26
+ }
27
+
28
+ /* VALUE r_mpfi_matrix_copied_robj(VALUE old){ */
29
+ /* MPFIMatrix *ptr_old, *ptr_ret; */
30
+ /* r_mpfi_get_matrix_struct(ptr_old, old); */
31
+ /* VALUE ret; */
32
+ /* r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_old->row, ptr_old->column); */
33
+ /* mpfi_matrix_set(ptr_ret, ptr_old); */
34
+ /* return ret; */
35
+ /* } */
36
+
37
+ VALUE r_mpfi_matrix_robj(MPFIMatrix *x){
38
+ VALUE ret;
39
+ MPFIMatrix *ptr_ret;
40
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, x->row, x->column);
41
+ mpfi_matrix_set(ptr_ret, x);
42
+ return ret;
43
+ }
44
+
45
+ /* Allocation function for MPF::Matrix. */
46
+ static VALUE r_mpfi_matrix_alloc (VALUE self){
47
+ MPFIMatrix *ptr;
48
+ r_mpfi_make_matrix_struct(self, ptr);
49
+ return self;
50
+ }
51
+
52
+ /* static void r_mpfi_matrix_init_set_from_rmatrix(MPFIMatrix *x, VALUE src){ */
53
+ /* MPFIMatrix *ptr_src; */
54
+ /* r_mpfi_get_matrix_struct(ptr_src, src); */
55
+ /* mpfi_matrix_init(x, ptr_src->row, ptr_src->column); */
56
+ /* mpfi_matrix_set(x, ptr_src); */
57
+ /* } */
58
+
59
+ /* Initialization function for MPF::Matrix. */
60
+ static VALUE r_mpfi_matrix_initialize (int argc, VALUE *argv, VALUE self){
61
+ MPFIMatrix *ptr;
62
+ r_mpfi_get_matrix_struct(ptr, self);
63
+ int row, column, i, j;
64
+ VALUE row_ary;
65
+ switch (argc) {
66
+ case 1:
67
+ row = RARRAY_LEN(argv[0]);
68
+ column = RARRAY_LEN(rb_ary_entry(argv[0], 0));
69
+ mpfi_matrix_init(ptr, row, column);
70
+ for (i = 0; i < row; i++) {
71
+ row_ary = rb_ary_entry(argv[0], i);
72
+ if (column != RARRAY_LEN(row_ary)) {
73
+ rb_raise(rb_eArgError, "MPF::Matrix.new needs Array which has arrays of same sizes.");
74
+ }
75
+ for (j = 0; j < column; j++) {
76
+ r_mpfi_set_robj(mpfi_matrix_get_element(ptr, i, j), rb_ary_entry(row_ary, j));
77
+ }
78
+ }
79
+ break;
80
+ case 2:
81
+ mpfi_matrix_init(ptr, NUM2INT(argv[0]), NUM2INT(argv[1]));
82
+ mpfi_matrix_set_zeros(ptr);
83
+ break;
84
+ default:
85
+ rb_raise(rb_eArgError, "MPF::Matrix.new needs one or two arguments.");
86
+ break;
87
+ }
88
+ return Qtrue;
89
+ }
90
+
91
+ /* Allocation function for MPF::SquareMatrix. */
92
+ static VALUE r_mpfi_square_matrix_alloc (VALUE self){
93
+ MPFIMatrix *ptr;
94
+ r_mpfi_make_square_matrix_struct(self, ptr);
95
+ return self;
96
+ }
97
+
98
+ /* Initialization function for MPF::SquareMatrix. */
99
+ static VALUE r_mpfi_square_matrix_initialize (VALUE self, VALUE arg){
100
+ MPFIMatrix *ptr;
101
+ r_mpfi_get_matrix_struct(ptr, self);
102
+ int row, column, i, j;
103
+ VALUE row_ary;
104
+ switch(TYPE(arg)){
105
+ case T_ARRAY:
106
+ row = RARRAY_LEN(arg);
107
+ column = RARRAY_LEN(rb_ary_entry(arg, 0));
108
+ if(row == column){
109
+ mpfi_matrix_init(ptr, row, column);
110
+ for (i = 0; i < row; i++) {
111
+ row_ary = rb_ary_entry(arg, i);
112
+ if (column != RARRAY_LEN(row_ary)) {
113
+ rb_raise(rb_eArgError, "MPF::Matrix.new needs Array which has arrays of same sizes.");
114
+ }
115
+ for (j = 0; j < column; j++) {
116
+ r_mpfi_set_robj(mpfi_matrix_get_element(ptr, i, j), rb_ary_entry(row_ary, j));
117
+ }
118
+ }
119
+ }else{
120
+ rb_raise(rb_eArgError, "MPF::SquareMatrix.new needs two dimensinal Array which has the same sizes of row and column.");
121
+ }
122
+ break;
123
+ case T_FIXNUM:
124
+ row = NUM2INT(arg);
125
+ mpfi_matrix_init(ptr, row, row);
126
+ mpfi_matrix_set_zeros(ptr);
127
+ break;
128
+ default:
129
+ rb_raise(rb_eArgError, "MPF::SquareMatrix.new needs Array or Fixnum.");
130
+ break;
131
+ }
132
+ return Qtrue;
133
+ }
134
+
135
+ /* initialize_copy */
136
+ static VALUE r_mpfi_matrix_initialize_copy (VALUE self, VALUE other){
137
+ MPFIMatrix *ptr_self, *ptr_other;
138
+ r_mpfi_get_matrix_struct(ptr_self, self);
139
+ r_mpfi_get_matrix_struct(ptr_other, other);
140
+ mpfi_matrix_init(ptr_self, ptr_other->row, ptr_other->column);
141
+ mpfi_matrix_set(ptr_self, ptr_other);
142
+ return Qtrue;
143
+ }
144
+
145
+ /* Allocation function for MPF::ColumnVector. */
146
+ static VALUE r_mpfi_col_vector_alloc (VALUE self){
147
+ MPFIMatrix *ptr;
148
+ r_mpfi_make_col_vector_struct(self, ptr);
149
+ return self;
150
+ }
151
+
152
+ /* Initialization function for MPF::ColumnVector. */
153
+ static VALUE r_mpfi_col_vector_initialize (VALUE self, VALUE arg){
154
+ MPFIMatrix *ptr;
155
+ r_mpfi_get_matrix_struct(ptr, self);
156
+ int row, i;
157
+ switch(TYPE(arg)){
158
+ case T_ARRAY:
159
+ row = RARRAY_LEN(arg);
160
+ mpfi_col_vector_init(ptr, row);
161
+ for (i = 0; i < row; i++) {
162
+ r_mpfi_set_robj(mpfi_matrix_get_ptr(ptr, i), rb_ary_entry(arg, i));
163
+ }
164
+ break;
165
+ case T_FIXNUM:
166
+ row = NUM2INT(arg);
167
+ mpfi_col_vector_init(ptr, row);
168
+ for (i = 0; i < row; i++) {
169
+ mpfi_set_si(mpfi_matrix_get_ptr(ptr, i), 0);
170
+ }
171
+ break;
172
+ default:
173
+ rb_raise(rb_eArgError, "MPF::ColumnVector.new needs Array or Fixnum.");
174
+ break;
175
+ }
176
+ return Qtrue;
177
+ }
178
+
179
+ /* Allocation function for MPF::RowVector. */
180
+ static VALUE r_mpfi_row_vector_alloc (VALUE self){
181
+ MPFIMatrix *ptr;
182
+ r_mpfi_make_row_vector_struct(self, ptr);
183
+ return self;
184
+ }
185
+
186
+ /* Initialization function for MPF::RowVector. */
187
+ static VALUE r_mpfi_row_vector_initialize (VALUE self, VALUE arg){
188
+ MPFIMatrix *ptr;
189
+ r_mpfi_get_matrix_struct(ptr, self);
190
+ int column, i;
191
+ switch(TYPE(arg)){
192
+ case T_ARRAY:
193
+ column = RARRAY_LEN(arg);
194
+ mpfi_row_vector_init(ptr, column);
195
+ for (i = 0; i < column; i++) {
196
+ r_mpfi_set_robj(mpfi_matrix_get_ptr(ptr, i), rb_ary_entry(arg, i));
197
+ }
198
+ break;
199
+ case T_FIXNUM:
200
+ column = NUM2INT(arg);
201
+ mpfi_row_vector_init(ptr, column);
202
+ for (i = 0; i < column; i++) {
203
+ mpfi_set_si(mpfi_matrix_get_ptr(ptr, i), 0);
204
+ }
205
+ break;
206
+ default:
207
+ rb_raise(rb_eArgError, "MPF::RowVector.new needs Array or Fixnum.");
208
+ break;
209
+ }
210
+ return Qtrue;
211
+ }
212
+
213
+ /* Return size of data array which equals to column * row. */
214
+ static VALUE r_mpfi_matrix_size (VALUE self){
215
+ MPFIMatrix *ptr_self;
216
+ r_mpfi_get_matrix_struct(ptr_self, self);
217
+ return INT2FIX(ptr_self->size);
218
+ }
219
+
220
+ /* Return column size of matrix. */
221
+ static VALUE r_mpfi_matrix_column_size (VALUE self){
222
+ MPFIMatrix *ptr_self;
223
+ r_mpfi_get_matrix_struct(ptr_self, self);
224
+ return INT2FIX(ptr_self->column);
225
+ }
226
+
227
+ /* Return row size of matrix. */
228
+ static VALUE r_mpfi_matrix_row_size (VALUE self){
229
+ MPFIMatrix *ptr_self;
230
+ r_mpfi_get_matrix_struct(ptr_self, self);
231
+ return INT2FIX(ptr_self->row);
232
+ }
233
+
234
+ /* Return element at _arg_.*/
235
+ static VALUE r_mpfi_matrix_at (VALUE self, VALUE arg){
236
+ MPFIMatrix *ptr_self;
237
+ r_mpfi_get_matrix_struct(ptr_self, self);
238
+ int i = NUM2INT(arg);
239
+ if (i < ptr_self->size) {
240
+ VALUE ret;
241
+ MPFI *ptr_ret;
242
+ r_mpfi_make_struct_init(ret, ptr_ret);
243
+ mpfi_set(ptr_ret, ptr_self->data + i);
244
+ return ret;
245
+ }else{
246
+ return Qnil;
247
+ }
248
+ }
249
+
250
+ /* Return element with _row_ and _column_.*/
251
+ static VALUE r_mpfi_matrix_element (VALUE self, VALUE row, VALUE column){
252
+ MPFIMatrix *ptr_self;
253
+ r_mpfi_get_matrix_struct(ptr_self, self);
254
+ int i = NUM2INT(row), j = NUM2INT(column);
255
+ if (i < ptr_self->row && j < ptr_self->column) {
256
+ VALUE ret;
257
+ MPFI*ptr_ret;
258
+ r_mpfi_make_struct_init(ret, ptr_ret);
259
+ mpfi_set(ptr_ret, ptr_self->data + i + j * ptr_self->row);
260
+ return ret;
261
+ }else{
262
+ return Qnil;
263
+ }
264
+ }
265
+
266
+ /* Set _robj_ to element of which row is _row_ and column is _column_. */
267
+ static VALUE r_mpfi_matrix_set_element (VALUE self, VALUE row, VALUE column, VALUE robj){
268
+ MPFIMatrix *ptr_self;
269
+ r_mpfi_get_matrix_struct(ptr_self, self);
270
+ int i = NUM2INT(row), j = NUM2INT(column);
271
+ if (i < ptr_self->row && j < ptr_self->column) {
272
+ r_mpfi_set_robj(ptr_self->data + i + j * ptr_self->row, robj);
273
+ }
274
+ return Qnil;
275
+ }
276
+
277
+ /* Evaluate block with each element of matrix. */
278
+ static VALUE r_mpfi_matrix_each_element (VALUE self){
279
+ MPFIMatrix *ptr_self;
280
+ r_mpfi_get_matrix_struct(ptr_self, self);
281
+ VALUE ret = Qnil;
282
+ int i, j;
283
+ for (i = 0; i < ptr_self->row; i++) {
284
+ for (j = 0; j < ptr_self->column; j++) {
285
+ volatile VALUE arg = r_mpfi_make_new_fi_obj(ptr_self->data + i + j * ptr_self->row);
286
+ ret = rb_yield(arg);
287
+ }
288
+ }
289
+ return ret;
290
+ }
291
+
292
+ /* Evaluate block with each element and its index. */
293
+ static VALUE r_mpfi_matrix_each_element_with_index (VALUE self){
294
+ MPFIMatrix *ptr_self;
295
+ r_mpfi_get_matrix_struct(ptr_self, self);
296
+ VALUE ret = Qnil, tmp_i;
297
+ int i, j;
298
+ for (i = 0; i < ptr_self->row; i++) {
299
+ tmp_i = INT2NUM(i);
300
+ for (j = 0; j < ptr_self->column; j++) {
301
+ volatile VALUE arg = r_mpfi_make_new_fi_obj(ptr_self->data + i + j * ptr_self->row);
302
+ ret = rb_yield(rb_ary_new3(3, arg, tmp_i, INT2NUM(j)));
303
+ }
304
+ }
305
+ return ret;
306
+ }
307
+
308
+ /* Return array which has strings converted elements to. */
309
+ static VALUE r_mpfi_matrix_str_ary_for_inspect(VALUE self){
310
+ MPFIMatrix *ptr_self;
311
+ r_mpfi_get_matrix_struct(ptr_self, self);
312
+ char *tmp_str;
313
+ VALUE ret_val[ptr_self->size];
314
+ int i;
315
+ for (i = 0; i < ptr_self->size; i++) {
316
+ mpfr_asprintf(&tmp_str, "%.Re %.Re",
317
+ r_mpfi_left_ptr((ptr_self->data + i)), r_mpfi_right_ptr(ptr_self->data + i));
318
+ ret_val[i] = rb_str_new2(tmp_str);
319
+ mpfr_free_str(tmp_str);
320
+ }
321
+ return rb_ary_new4(ptr_self->size, ret_val);
322
+ }
323
+
324
+ static VALUE r_mpfi_matrix_str_ary_for_inspect2(VALUE self){
325
+ MPFIMatrix *ptr_self;
326
+ r_mpfi_get_matrix_struct(ptr_self, self);
327
+ char *tmp_str;
328
+ VALUE ary[ptr_self->row];
329
+ int i, j;
330
+ for(i = 0; i < ptr_self->row; i++){
331
+ ary[i] = rb_ary_new();
332
+ }
333
+ for (i = 0; i < ptr_self->size; i += ptr_self->row) {
334
+ for (j = 0; j < ptr_self->row; j++) {
335
+ mpfr_asprintf(&tmp_str, "%.Re %.Re",
336
+ r_mpfi_left_ptr((ptr_self->data + i + j)), r_mpfi_right_ptr(ptr_self->data + i + j));
337
+ rb_ary_push(ary[j], rb_str_new2(tmp_str));
338
+ mpfr_free_str(tmp_str);
339
+ }
340
+ }
341
+ return rb_ary_new4(ptr_self->row, ary);
342
+ }
343
+
344
+ static VALUE r_mpfi_matrix_to_str_ary(VALUE self){
345
+ MPFIMatrix *ptr_self;
346
+ r_mpfi_get_matrix_struct(ptr_self, self);
347
+ char *tmp_str1, *tmp_str2;
348
+ VALUE ary[ptr_self->row];
349
+ int i, j;
350
+ for(i = 0; i < ptr_self->row; i++){
351
+ ary[i] = rb_ary_new();
352
+ }
353
+ for (i = 0; i < ptr_self->size; i += ptr_self->row) {
354
+ for (j = 0; j < ptr_self->row; j++) {
355
+ mpfr_asprintf(&tmp_str1, "%.Re", r_mpfi_left_ptr((ptr_self->data + i + j)));
356
+ mpfr_asprintf(&tmp_str2, "%.Re", r_mpfi_right_ptr(ptr_self->data + i + j));
357
+ rb_ary_push(ary[j], rb_ary_new3(2, rb_str_new2(tmp_str1), rb_str_new2(tmp_str2)));
358
+ mpfr_free_str(tmp_str1);
359
+ mpfr_free_str(tmp_str2);
360
+ }
361
+ }
362
+ return rb_ary_new4(ptr_self->row, ary);
363
+ }
364
+
365
+ static VALUE r_mpfi_matrix_to_strf_ary(VALUE self, VALUE format_str){
366
+ MPFIMatrix *ptr_self;
367
+ r_mpfi_get_matrix_struct(ptr_self, self);
368
+ char *tmp_str1, *tmp_str2;
369
+ char *format = StringValuePtr(format_str);
370
+ VALUE ary[ptr_self->row];
371
+ int i, j;
372
+ for(i = 0; i < ptr_self->row; i++){
373
+ ary[i] = rb_ary_new();
374
+ }
375
+ for (i = 0; i < ptr_self->size; i += ptr_self->row) {
376
+ for (j = 0; j < ptr_self->row; j++) {
377
+ mpfr_asprintf(&tmp_str1, format, r_mpfi_left_ptr((ptr_self->data + i + j)));
378
+ mpfr_asprintf(&tmp_str2, format, r_mpfi_right_ptr(ptr_self->data + i + j));
379
+ rb_ary_push(ary[j], rb_ary_new3(2, rb_str_new2(tmp_str1), rb_str_new2(tmp_str2)));
380
+ mpfr_free_str(tmp_str1);
381
+ mpfr_free_str(tmp_str2);
382
+ }
383
+ }
384
+ return rb_ary_new4(ptr_self->row, ary);
385
+ }
386
+
387
+ static VALUE r_mpfi_matrix_to_array(VALUE self){
388
+ MPFIMatrix *ptr_self;
389
+ r_mpfi_get_matrix_struct(ptr_self, self);
390
+ VALUE ret_val[ptr_self->size];
391
+ int i;
392
+ for (i = 0; i < ptr_self->size; i++) {
393
+ ret_val[i] = r_mpfi_make_new_fi_obj(ptr_self->data + i);
394
+ }
395
+ return rb_ary_new4(ptr_self->size, ret_val);
396
+ }
397
+
398
+ static VALUE r_mpfi_matrix_to_array2(VALUE self){
399
+ MPFIMatrix *ptr_self;
400
+ r_mpfi_get_matrix_struct(ptr_self, self);
401
+ VALUE ary[ptr_self->row];
402
+ int i, j;
403
+ for(i = 0; i < ptr_self->row; i++){
404
+ ary[i] = rb_ary_new();
405
+ }
406
+ for (i = 0; i < ptr_self->size; i += ptr_self->row) {
407
+ for (j = 0; j < ptr_self->row; j++) {
408
+ rb_ary_push(ary[j], r_mpfi_make_new_fi_obj(ptr_self->data + i + j));
409
+ }
410
+ }
411
+ return rb_ary_new4(ptr_self->row, ary);
412
+ }
413
+
414
+ static VALUE r_mpfi_matrix_row (VALUE self, VALUE arg) {
415
+ MPFIMatrix *ptr_self, *ptr_ret;
416
+ r_mpfi_get_matrix_struct(ptr_self, self);
417
+ int num = NUM2INT(arg);
418
+ VALUE ret;
419
+ if(num < ptr_self->row){
420
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, 1, ptr_self->column);
421
+ mpfi_matrix_row(ptr_ret, ptr_self, num);
422
+ return ret;
423
+ }else{
424
+ return Qnil;
425
+ }
426
+ }
427
+
428
+ static VALUE r_mpfi_matrix_column (VALUE self, VALUE arg) {
429
+ MPFIMatrix *ptr_self, *ptr_ret;
430
+ r_mpfi_get_matrix_struct(ptr_self, self);
431
+ int num = NUM2INT(arg);
432
+ VALUE ret;
433
+ if(num < ptr_self->column){
434
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, 1);
435
+ mpfi_matrix_column(ptr_ret, ptr_self, num);
436
+ return ret;
437
+ }else{
438
+ return Qnil;
439
+ }
440
+ }
441
+
442
+ /* Return transposed matrix which is new object. */
443
+ static VALUE r_mpfi_matrix_transpose (VALUE self){
444
+ MPFIMatrix *ptr_self, *ptr_ret;
445
+ r_mpfi_get_matrix_struct(ptr_self, self);
446
+ VALUE ret;
447
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->column, ptr_self->row);
448
+ mpfi_matrix_transpose(ptr_ret, ptr_self);
449
+ return ret;
450
+ }
451
+
452
+ /* Transpose self. This method is destroying method. */
453
+ static VALUE r_mpfi_matrix_transpose2 (VALUE self){
454
+ MPFIMatrix *ptr_self, tmp;
455
+ r_mpfi_get_matrix_struct(ptr_self, self);
456
+ if (ptr_self->column > 1 && ptr_self->row > 1) {
457
+ mpfi_matrix_init(&tmp, ptr_self->column, ptr_self->row);
458
+ mpfi_matrix_transpose(&tmp, ptr_self);
459
+ mpfi_matrix_set(ptr_self, &tmp);
460
+ mpfi_matrix_clear(&tmp);
461
+ }
462
+ int i;
463
+ i = ptr_self->column;
464
+ ptr_self->column = ptr_self->row;
465
+ ptr_self->row = i;
466
+ return self;
467
+ }
468
+
469
+ /* Return transposed matrix which is new object. */
470
+ static VALUE r_mpfi_matrix_neg (VALUE self){
471
+ MPFIMatrix *ptr_self, *ptr_ret;
472
+ r_mpfi_get_matrix_struct(ptr_self, self);
473
+ VALUE ret;
474
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
475
+ mpfi_matrix_neg(ptr_ret, ptr_self);
476
+ return ret;
477
+ }
478
+
479
+
480
+ /* Return true if all elements of _self_ equal to corresponding elements of _other_. */
481
+ static VALUE r_mpfi_matrix_equal_p (VALUE self, VALUE other){
482
+ MPFIMatrix *ptr_self, *ptr_other;
483
+ r_mpfi_get_matrix_struct(ptr_self, self);
484
+ r_mpfi_get_matrix_struct(ptr_other, other);
485
+ VALUE ret = Qtrue;
486
+ if (mpfi_matrix_equal_p(ptr_self, ptr_other) != 0) {
487
+ ret = Qnil;
488
+ }
489
+ return ret;
490
+ }
491
+
492
+ /* Return _self_ + _other_. */
493
+ static VALUE r_mpfi_matrix_add (VALUE self, VALUE other){
494
+ MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
495
+ r_mpfi_get_matrix_struct(ptr_self, self);
496
+ r_mpfi_get_matrix_struct(ptr_other, other);
497
+ VALUE ret;
498
+ if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
499
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
500
+ mpfi_matrix_add(ptr_ret, ptr_self, ptr_other);
501
+ }else{
502
+ rb_raise(rb_eArgError, "Matrixes must have same size.");
503
+ }
504
+ return ret;
505
+ }
506
+
507
+ /* Return _self_ + _other_ which is MPFI::Matrix. */
508
+ static VALUE r_mpfi_matrix_add2 (VALUE self, VALUE other){
509
+ MPFIMatrix *ptr_self, *ptr_ret;
510
+ r_mpfi_get_matrix_struct(ptr_self, self);
511
+ VALUE ret;
512
+ r_mpfi_make_matrix_struct(ret, ptr_ret);
513
+
514
+ if(RTEST(rb_funcall(__mpfi_matrix_class__, eqq, 1, other))){
515
+ MPFIMatrix *ptr_other;
516
+ r_mpfi_get_matrix_struct(ptr_other, other);
517
+ if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
518
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
519
+ mpfi_matrix_add(ptr_ret, ptr_self, ptr_other);
520
+ }else{
521
+ rb_raise(rb_eArgError, "Matrixes must have same size.");
522
+ }
523
+ }else if(RTEST(rb_funcall(__mpfr_matrix_class__, eqq, 1, other))){
524
+ MPFRMatrix *ptr_other;
525
+ r_mpfr_get_matrix_struct(ptr_other, other);
526
+ if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
527
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
528
+ mpfi_matrix_add_fr(ptr_ret, ptr_self, ptr_other);
529
+ }else{
530
+ rb_raise(rb_eArgError, "Matrixes must have same size.");
531
+ }
532
+ }else{
533
+ rb_raise(rb_eArgError, "Argument matrix must be an instance of MPFI::Matrix or MPFR::Matrix.");
534
+ }
535
+ return ret;
536
+ }
537
+
538
+ /* Return _self_ - _other_. */
539
+ static VALUE r_mpfi_matrix_sub (VALUE self, VALUE other){
540
+ MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
541
+ r_mpfi_get_matrix_struct(ptr_self, self);
542
+ r_mpfi_get_matrix_struct(ptr_other, other);
543
+ VALUE ret;
544
+ r_mpfi_make_matrix_struct(ret, ptr_ret);
545
+ if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
546
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
547
+ mpfi_matrix_sub(ptr_ret, ptr_self, ptr_other);
548
+ }else{
549
+ rb_raise(rb_eArgError, "Matrixes must have same size.");
550
+ }
551
+ return ret;
552
+ }
553
+
554
+ /* Return _self_ - _other_ which is MPFI::Matrix. */
555
+ static VALUE r_mpfi_matrix_sub2 (VALUE self, VALUE other){
556
+ MPFIMatrix *ptr_self, *ptr_ret;
557
+ r_mpfi_get_matrix_struct(ptr_self, self);
558
+ VALUE ret;
559
+ r_mpfi_make_matrix_struct(ret, ptr_ret);
560
+
561
+ if(RTEST(rb_funcall(__mpfi_matrix_class__, eqq, 1, other))){
562
+ MPFIMatrix *ptr_other;
563
+ r_mpfi_get_matrix_struct(ptr_other, other);
564
+ if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
565
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
566
+ mpfi_matrix_sub(ptr_ret, ptr_self, ptr_other);
567
+ }else{
568
+ rb_raise(rb_eArgError, "Matrixes must have same size.");
569
+ }
570
+ }else if(RTEST(rb_funcall(__mpfr_matrix_class__, eqq, 1, other))){
571
+ MPFRMatrix *ptr_other;
572
+ r_mpfr_get_matrix_struct(ptr_other, other);
573
+ if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
574
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
575
+ mpfi_matrix_sub_fr(ptr_ret, ptr_self, ptr_other);
576
+ }else{
577
+ rb_raise(rb_eArgError, "Matrixes must have same size.");
578
+ }
579
+ }else{
580
+ rb_raise(rb_eArgError, "Argument matrix must be an instance of MPFI::Matrix or MPFR::Matrix.");
581
+ }
582
+ return ret;
583
+ }
584
+
585
+ static VALUE r_mpfi_matrix_vector_inner_product (VALUE self, VALUE arg){
586
+ MPFIMatrix *ptr_self, *ptr_arg;
587
+ r_mpfi_get_matrix_struct(ptr_self, self);
588
+ r_mpfi_get_matrix_struct(ptr_arg, arg);
589
+ VALUE ret;
590
+ MPFI *ptr_ret;
591
+ r_mpfi_make_struct_init(ret, ptr_ret);
592
+ if(ptr_self->size == ptr_arg->size){
593
+ mpfi_matrix_inner_product(ptr_ret, ptr_self, ptr_arg);
594
+ }else{
595
+ rb_raise(rb_eArgError, "Vectors must have same size.");
596
+ }
597
+ return ret;
598
+ }
599
+
600
+ /* Return _self_ * _other_. */
601
+ static VALUE r_mpfi_matrix_mul_matrix (VALUE self, VALUE other){
602
+ MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
603
+ r_mpfi_get_matrix_struct(ptr_self, self);
604
+ r_mpfi_get_matrix_struct(ptr_other, other);
605
+ VALUE ret;
606
+ r_mpfi_make_matrix_struct(ret, ptr_ret);
607
+ if (ptr_self->column == ptr_other->row) {
608
+ if((ptr_other->column == 1) && (ptr_self->row == 1)){
609
+ ret = r_mpfi_matrix_vector_inner_product(self, other);
610
+ }else{
611
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
612
+ mpfi_matrix_mul(ptr_ret, ptr_self, ptr_other);
613
+ }
614
+ }else{
615
+ rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
616
+ }
617
+ return ret;
618
+ }
619
+
620
+ /* Return _self_ * _other_ which is MPFI::Matrix. */
621
+ static VALUE r_mpfi_matrix_mul_matrix2 (VALUE self, VALUE other){
622
+ MPFIMatrix *ptr_self, *ptr_ret;
623
+ r_mpfi_get_matrix_struct(ptr_self, self);
624
+ VALUE ret;
625
+ r_mpfi_make_matrix_struct(ret, ptr_ret);
626
+
627
+ if(RTEST(rb_funcall(__mpfi_matrix_class__, eqq, 1, other))){
628
+ MPFIMatrix *ptr_other;
629
+ r_mpfi_get_matrix_struct(ptr_other, other);
630
+ if (ptr_self->column == ptr_other->row) {
631
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
632
+ mpfi_matrix_mul(ptr_ret, ptr_self, ptr_other);
633
+ }else{
634
+ rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
635
+ }
636
+ }else if(RTEST(rb_funcall(__mpfr_matrix_class__, eqq, 1, other))){
637
+ MPFRMatrix *ptr_other;
638
+ r_mpfr_get_matrix_struct(ptr_other, other);
639
+ if (ptr_self->column == ptr_other->row) {
640
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_other->column);
641
+ mpfi_matrix_mul_fr(ptr_ret, ptr_self, ptr_other);
642
+ }else{
643
+ rb_raise(rb_eArgError, "Row size of first matrixes must be same as colmun size of second matrixes.");
644
+ }
645
+ }else{
646
+ rb_raise(rb_eArgError, "Argument matrix must be an instance of MPFI::Matrix or MPFR::Matrix.");
647
+ }
648
+ return ret;
649
+ }
650
+
651
+ /* Return _scalar_ * _self_, where _scalar_ is scalar and _self_ is matrix. */
652
+ static VALUE r_mpfi_matrix_mul_scalar (VALUE self, VALUE scalar){
653
+ MPFIMatrix *ptr_self, *ptr_ret;
654
+ MPFI *ptr_scalar;
655
+ r_mpfi_get_matrix_struct(ptr_self, self);
656
+ VALUE ret;
657
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
658
+ if(RTEST(rb_funcall(r_mpfi_class, eqq, 1, scalar))){
659
+ r_mpfi_get_struct(ptr_scalar, scalar);
660
+ mpfi_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
661
+ }else{
662
+ r_mpfi_temp_alloc_init(ptr_scalar);
663
+ r_mpfi_set_robj(ptr_scalar, scalar);
664
+ mpfi_matrix_mul_scalar(ptr_ret, ptr_self, ptr_scalar);
665
+ r_mpfi_temp_free(ptr_scalar);
666
+ }
667
+ return ret;
668
+ }
669
+
670
+ static VALUE r_mpfi_matrix_div_scalar (VALUE self, VALUE scalar){
671
+ MPFIMatrix *ptr_self, *ptr_ret;
672
+ MPFI *ptr_scalar;
673
+ r_mpfi_get_matrix_struct(ptr_self, self);
674
+ VALUE ret;
675
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
676
+ if(RTEST(rb_funcall(r_mpfi_class, eqq, 1, scalar))){
677
+ r_mpfi_get_struct(ptr_scalar, scalar);
678
+ mpfi_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
679
+ }else{
680
+ r_mpfi_temp_alloc_init(ptr_scalar);
681
+ r_mpfi_set_robj(ptr_scalar, scalar);
682
+ mpfi_matrix_div_scalar(ptr_ret, ptr_self, ptr_scalar);
683
+ r_mpfi_temp_free(ptr_scalar);
684
+ }
685
+ return ret;
686
+ }
687
+
688
+
689
+ static VALUE r_mpfi_matrix_include_p (VALUE self, VALUE other){
690
+ MPFIMatrix *ptr_self;
691
+ r_mpfi_get_matrix_struct(ptr_self, self);
692
+ VALUE ret = Qnil;
693
+ if(RTEST(rb_funcall(r_mpfi_matrix, eqq, 1, other))){
694
+ MPFIMatrix *ptr_other;
695
+ r_mpfi_get_matrix_struct(ptr_other, other);
696
+ if(mpfi_matrix_include_p(ptr_self, ptr_other) == 0){
697
+ ret = Qtrue;
698
+ }
699
+ }else if(RTEST(rb_funcall(r_mpfr_matrix, eqq, 1, other))){
700
+ MPFRMatrix *ptr_other;
701
+ r_mpfr_get_matrix_struct(ptr_other, other);
702
+ if(mpfi_matrix_include_fr_p(ptr_self, ptr_other) == 0){
703
+ ret = Qtrue;
704
+ }
705
+ }else{
706
+ rb_raise(rb_eArgError, "Argument must be MPFI::Matrix or MPFR::Matrix instance.");
707
+ }
708
+ return ret;
709
+ }
710
+
711
+ static VALUE r_mpfi_matrix_strictly_include_p (VALUE self, VALUE other){
712
+ MPFIMatrix *ptr_self;
713
+ r_mpfi_get_matrix_struct(ptr_self, self);
714
+ VALUE ret = Qnil;
715
+ if(RTEST(rb_funcall(r_mpfi_matrix, eqq, 1, other))){
716
+ MPFIMatrix *ptr_other;
717
+ r_mpfi_get_matrix_struct(ptr_other, other);
718
+ if(mpfi_matrix_strictly_include_p(ptr_self, ptr_other) == 0){
719
+ ret = Qtrue;
720
+ }
721
+ }else{
722
+ rb_raise(rb_eArgError, "Argument must be MPFI::Matrix instance.");
723
+ }
724
+ return ret;
725
+ }
726
+
727
+ static VALUE r_mpfi_matrix_bounded_p (VALUE self) {
728
+ MPFIMatrix *ptr_self;
729
+ r_mpfi_get_matrix_struct(ptr_self, self);
730
+ if(mpfi_matrix_bounded_p(ptr_self) == 0){
731
+ return Qtrue;
732
+ }else{
733
+ return Qnil;
734
+ }
735
+ }
736
+
737
+ static VALUE r_mpfi_matrix_mid (VALUE self){
738
+ MPFIMatrix *ptr_self;
739
+ r_mpfi_get_matrix_struct(ptr_self, self);
740
+ VALUE val_ret;
741
+ MPFRMatrix *ptr_ret;
742
+ r_mpfr_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
743
+ mpfi_matrix_mid(ptr_ret, ptr_self);
744
+ return val_ret;
745
+ }
746
+
747
+ static VALUE r_mpfi_matrix_mid_interval (VALUE self){
748
+ MPFIMatrix *ptr_self, *ptr_ret;
749
+ r_mpfi_get_matrix_struct(ptr_self, self);
750
+ VALUE val_ret;
751
+ r_mpfi_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
752
+ mpfi_matrix_mid_interval(ptr_ret, ptr_self);
753
+ return val_ret;
754
+ }
755
+
756
+ static VALUE r_mpfi_matrix_intersect (VALUE self, VALUE other){
757
+ MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
758
+ r_mpfi_get_matrix_struct(ptr_self, self);
759
+ r_mpfi_get_matrix_struct(ptr_other, other);
760
+ VALUE val_ret;
761
+ r_mpfi_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
762
+ if(mpfi_matrix_intersect(ptr_ret, ptr_self, ptr_other) == 0){
763
+ return val_ret;
764
+ }else{
765
+ return Qnil;
766
+ }
767
+ }
768
+
769
+ static VALUE r_mpfi_matrix_union (VALUE self, VALUE other){
770
+ MPFIMatrix *ptr_self, *ptr_other, *ptr_ret;
771
+ r_mpfi_get_matrix_struct(ptr_self, self);
772
+ r_mpfi_get_matrix_struct(ptr_other, other);
773
+ VALUE val_ret;
774
+ r_mpfi_matrix_suitable_matrix_init(&val_ret, &ptr_ret, ptr_self->row, ptr_self->column);
775
+ if(mpfi_matrix_union(ptr_ret, ptr_self, ptr_other) == 0){
776
+ return val_ret;
777
+ }else{
778
+ return Qnil;
779
+ }
780
+ }
781
+
782
+
783
+ static VALUE r_mpfi_matrix_max_diam_abs (VALUE self){
784
+ MPFIMatrix *ptr_self;
785
+ r_mpfi_get_matrix_struct(ptr_self, self);
786
+ MPFR *ptr_ret;
787
+ VALUE ret_val;
788
+ r_mpfr_make_struct_init(ret_val, ptr_ret);
789
+ mpfi_matrix_max_diam_abs(ptr_ret, ptr_self);
790
+ return ret_val;
791
+ }
792
+
793
+ /* Return row size of matrix. */
794
+ static VALUE r_mpfi_square_matrix_dim (VALUE self){
795
+ MPFIMatrix *ptr_self;
796
+ r_mpfi_get_matrix_struct(ptr_self, self);
797
+ return INT2FIX(ptr_self->row);
798
+ }
799
+
800
+ /* Return [matrix_l, matrix_r, indx]. */
801
+ static VALUE r_mpfi_square_matrix_lu_decomp (VALUE self){
802
+ MPFIMatrix *ptr_self, *ptr_ret_l, *ptr_ret_u;
803
+ VALUE ret_l, ret_u;
804
+ r_mpfi_get_matrix_struct(ptr_self, self);
805
+ r_mpfi_matrix_suitable_matrix_init (&ret_l, &ptr_ret_l, ptr_self->row, ptr_self->column);
806
+ r_mpfi_matrix_suitable_matrix_init (&ret_u, &ptr_ret_u, ptr_self->row, ptr_self->column);
807
+ VALUE ret_indx[ptr_self->row];
808
+ int indx[ptr_self->row];
809
+ if(mpfi_square_matrix_lu_decomp (ptr_ret_u, indx, ptr_self) >= 0){
810
+ int i, j;
811
+ for(i = 1; i < ptr_ret_u->row; i++){
812
+ for(j = 0; j < i; j++){
813
+ mpfi_set(mpfi_matrix_get_element(ptr_ret_l, i, j), mpfi_matrix_get_element(ptr_ret_u, i, j));
814
+ mpfi_set_si(mpfi_matrix_get_element(ptr_ret_u, i, j), 0);
815
+ }
816
+ }
817
+ for(i = 0; i < ptr_ret_u->row; i++){
818
+ mpfi_set_si(mpfi_matrix_get_element(ptr_ret_l, i, i), 1);
819
+ }
820
+ for(i = 0; i < ptr_ret_u->row; i++){
821
+ for(j = i + 1; j < ptr_ret_u->column; j++){
822
+ mpfi_set_si(mpfi_matrix_get_element(ptr_ret_l, i, j), 0);
823
+ }
824
+ }
825
+ for(i = 0; i < ptr_ret_u->row; i++){
826
+ ret_indx[i] = INT2NUM(indx[i]);
827
+ }
828
+ return rb_ary_new3(3, ret_l, ret_u, rb_ary_new4(ptr_ret_u->row, ret_indx));
829
+ }else{
830
+ return Qnil;
831
+ }
832
+ }
833
+
834
+ static VALUE r_mpfi_square_matrix_determinant (VALUE self){
835
+ MPFIMatrix *ptr_self;
836
+ r_mpfi_get_matrix_struct(ptr_self, self);
837
+ MPFI *ptr_ret;
838
+ VALUE ret;
839
+ r_mpfi_make_struct_init(ret, ptr_ret);
840
+ mpfi_square_matrix_determinant(ptr_ret, ptr_self);
841
+ return ret;
842
+ }
843
+
844
+ static VALUE r_mpfi_square_matrix_qr_decomp (VALUE self){
845
+ MPFIMatrix *ptr_self, *ptr_q, *ptr_r;
846
+ r_mpfi_get_matrix_struct(ptr_self, self);
847
+ VALUE q, r;
848
+ r_mpfi_matrix_suitable_matrix_init (&q, &ptr_q, ptr_self->row, ptr_self->column);
849
+ r_mpfi_matrix_suitable_matrix_init (&r, &ptr_r, ptr_self->column, ptr_self->column);
850
+ mpfi_square_matrix_qr_decomp(ptr_q, ptr_r, ptr_self);
851
+ return rb_ary_new3(2, q, r);
852
+ }
853
+
854
+ static VALUE r_mpfi_square_matrix_identity (VALUE self, VALUE size){
855
+ VALUE ret;
856
+ MPFIMatrix *ptr_ret;
857
+ int s = NUM2INT(size);
858
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, s, s);
859
+ mpfi_square_matrix_identity(ptr_ret);
860
+ return ret;
861
+ }
862
+
863
+
864
+ /* Return size of data array which equals to column * row. */
865
+ static VALUE r_mpfi_vector_dim (VALUE self){
866
+ MPFIMatrix *ptr_self;
867
+ r_mpfi_get_matrix_struct(ptr_self, self);
868
+ return INT2FIX(ptr_self->size);
869
+ }
870
+
871
+ /* Take matrix for vector and return length of the vector. */
872
+ static VALUE r_mpfi_vector_abs (VALUE self){
873
+ MPFIMatrix *ptr_self;
874
+ r_mpfi_get_matrix_struct(ptr_self, self);
875
+ VALUE ret;
876
+ MPFI *ptr_ret;
877
+ r_mpfi_make_struct_init(ret, ptr_ret);
878
+ mpfi_matrix_vector_norm(ptr_ret, ptr_self);
879
+ return ret;
880
+ }
881
+
882
+ /* Return element at _arg_.*/
883
+ static VALUE r_mpfi_vector_element (VALUE self, VALUE arg){
884
+ MPFIMatrix *ptr_self;
885
+ r_mpfi_get_matrix_struct(ptr_self, self);
886
+ int i = NUM2INT(arg);
887
+ if (i < ptr_self->size) {
888
+ VALUE ret;
889
+ MPFI *ptr_ret;
890
+ r_mpfi_make_struct_init(ret, ptr_ret);
891
+ mpfi_set(ptr_ret, ptr_self->data + i);
892
+ return ret;
893
+ }else{
894
+ return Qnil;
895
+ }
896
+ }
897
+
898
+ /* Set _robj_ to _arg_ th element. */
899
+ static VALUE r_mpfi_vector_set_element (VALUE self, VALUE arg, VALUE robj){
900
+ MPFIMatrix *ptr_self;
901
+ r_mpfi_get_matrix_struct(ptr_self, self);
902
+ int i = NUM2INT(arg);
903
+ if (i < ptr_self->size) {
904
+ r_mpfi_set_robj(ptr_self->data + i, robj);
905
+ }
906
+ return Qnil;
907
+ }
908
+
909
+ /* Evaluate block with each element of vector. */
910
+ static VALUE r_mpfi_vector_each_element (VALUE self){
911
+ MPFIMatrix *ptr_self;
912
+ r_mpfi_get_matrix_struct(ptr_self, self);
913
+ VALUE ret = Qnil, tmp;
914
+ MPFI *tmp_ptr;
915
+ r_mpfi_make_struct_init(tmp, tmp_ptr);
916
+ int i;
917
+ for (i = 0; i < ptr_self->size; i++) {
918
+ mpfi_set(tmp_ptr, ptr_self->data + i);
919
+ ret = rb_yield(tmp);
920
+ }
921
+ return ret;
922
+ }
923
+
924
+ /* Evaluate block with each element and its index. */
925
+ static VALUE r_mpfi_vector_each_element_with_index (VALUE self){
926
+ MPFIMatrix *ptr_self;
927
+ r_mpfi_get_matrix_struct(ptr_self, self);
928
+ VALUE ret = Qnil, tmp;
929
+ MPFI *tmp_ptr;
930
+ r_mpfi_make_struct_init(tmp, tmp_ptr);
931
+ int i;
932
+ for (i = 0; i < ptr_self->size; i++) {
933
+ mpfi_set(tmp_ptr, ptr_self->data + i);
934
+ ret = rb_yield(rb_ary_new3(2, tmp, INT2NUM(i)));
935
+ }
936
+ return ret;
937
+ }
938
+
939
+ static VALUE r_mpfi_vector_inner_product (VALUE self, VALUE arg){
940
+ MPFIMatrix *ptr_self, *ptr_arg;
941
+ r_mpfi_get_matrix_struct(ptr_self, self);
942
+ r_mpfi_get_matrix_struct(ptr_arg, arg);
943
+ VALUE ret;
944
+ MPFI *ptr_ret;
945
+ r_mpfi_make_struct_init(ret, ptr_ret);
946
+ if(ptr_self->size == ptr_arg->size){
947
+ mpfi_matrix_inner_product(ptr_ret, ptr_self, ptr_arg);
948
+ }else{
949
+ rb_raise(rb_eArgError, "Vectors must have same size.");
950
+ }
951
+ return ret;
952
+ }
953
+
954
+ static VALUE r_mpfi_vector_distance_from (VALUE self, VALUE arg){
955
+ MPFIMatrix *ptr_self, *ptr_arg;
956
+ r_mpfi_get_matrix_struct(ptr_self, self);
957
+ r_mpfi_get_matrix_struct(ptr_arg, arg);
958
+ VALUE ret;
959
+ MPFI *ptr_ret;
960
+ r_mpfi_make_struct_init(ret, ptr_ret);
961
+ if(ptr_self->size == ptr_arg->size){
962
+ mpfi_matrix_vector_distance(ptr_ret, ptr_self, ptr_arg);
963
+ }else{
964
+ rb_raise(rb_eArgError, "Vectors must have same size.");
965
+ }
966
+ return ret;
967
+ }
968
+
969
+ static VALUE r_mpfi_vector_distance_center_pts (VALUE self, VALUE arg){
970
+ MPFIMatrix *ptr_self, *ptr_arg;
971
+ r_mpfi_get_matrix_struct(ptr_self, self);
972
+ r_mpfi_get_matrix_struct(ptr_arg, arg);
973
+ VALUE ret;
974
+ MPFR *ptr_ret;
975
+ r_mpfr_make_struct_init(ret, ptr_ret);
976
+ if(ptr_self->size == ptr_arg->size){
977
+ mpfi_matrix_vector_distance_center_pts(ptr_ret, ptr_self, ptr_arg);
978
+ }else{
979
+ rb_raise(rb_eArgError, "Vectors must have same size.");
980
+ }
981
+ return ret;
982
+ }
983
+
984
+ static VALUE r_mpfi_vector_midpoint (VALUE self, VALUE arg){
985
+ MPFIMatrix *ptr_self, *ptr_arg, *ptr_ret;
986
+ r_mpfi_get_matrix_struct(ptr_self, self);
987
+ r_mpfi_get_matrix_struct(ptr_arg, arg);
988
+ VALUE ret;
989
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
990
+ mpfi_vector_midpoint(ptr_ret, ptr_self, ptr_arg);
991
+ return ret;
992
+ }
993
+
994
+ static VALUE r_mpfi_vector_normalize (VALUE self){
995
+ MPFIMatrix *ptr_self, *ptr_ret;
996
+ r_mpfi_get_matrix_struct(ptr_self, self);
997
+ VALUE ret;
998
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
999
+ if(mpfi_vector_normalize(ptr_ret, ptr_self) == 0){
1000
+ return ret;
1001
+ }else{
1002
+ return Qnil;
1003
+ }
1004
+ }
1005
+
1006
+ /* Return normalized vector of _self_. This method is destroy method. */
1007
+ static VALUE r_mpfi_vector_normalize2 (VALUE self){
1008
+ MPFIMatrix *ptr_self, tmp;
1009
+ r_mpfi_get_matrix_struct(ptr_self, self);
1010
+ VALUE ret = self;
1011
+ mpfi_matrix_init(&tmp, ptr_self->column, ptr_self->row);
1012
+ if(mpfi_vector_normalize(&tmp, ptr_self) == 0){
1013
+ mpfi_matrix_set(ptr_self, &tmp);
1014
+ }else{
1015
+ ret = Qnil;
1016
+ }
1017
+ mpfi_matrix_clear(&tmp);
1018
+ return ret;
1019
+ }
1020
+
1021
+ static VALUE r_mpfi_vector_set_length (VALUE self, VALUE arg){
1022
+ MPFIMatrix *ptr_self, *ptr_ret;
1023
+ r_mpfi_get_matrix_struct(ptr_self, self);
1024
+ VALUE ret, returned_value = Qnil;
1025
+ MPFR *ptr_l;
1026
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
1027
+ if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
1028
+ r_mpfr_get_struct(ptr_l, arg);
1029
+ if(mpfi_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
1030
+ returned_value = ret;
1031
+ }
1032
+ }else{
1033
+ r_mpfr_temp_alloc_init(ptr_l);
1034
+ r_mpfr_set_robj(ptr_l, arg, GMP_RNDN);
1035
+ if(mpfi_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
1036
+ returned_value = ret;
1037
+ }
1038
+ r_mpfr_temp_free(ptr_l);
1039
+ }
1040
+ return returned_value;
1041
+ }
1042
+
1043
+ static VALUE r_mpfi_vector_set_length2 (VALUE self, VALUE arg){
1044
+ MPFIMatrix *ptr_self, *ptr_ret;
1045
+ r_mpfi_get_matrix_struct(ptr_self, self);
1046
+ VALUE ret, returned_value = Qnil;
1047
+ MPFR *ptr_l;
1048
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
1049
+ if(RTEST(rb_funcall(r_mpfi_class, eqq, 1, arg))){
1050
+ r_mpfr_get_struct(ptr_l, arg);
1051
+ if(mpfi_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
1052
+ mpfi_matrix_set(ptr_self, ptr_ret);
1053
+ returned_value = self;
1054
+ }
1055
+ }else{
1056
+ r_mpfr_temp_alloc_init(ptr_l);
1057
+ r_mpfr_set_robj(ptr_l, arg, GMP_RNDN);
1058
+ if(mpfi_vector_set_length(ptr_ret, ptr_self, ptr_l) == 0){
1059
+ mpfi_matrix_set(ptr_self, ptr_ret);
1060
+ returned_value = self;
1061
+ }
1062
+ r_mpfr_temp_free(ptr_l);
1063
+ }
1064
+ return returned_value;
1065
+ }
1066
+
1067
+
1068
+
1069
+ static VALUE r_mpfr_matrix_to_fi_matrix(VALUE self){
1070
+ MPFRMatrix *ptr;
1071
+ r_mpfr_get_matrix_struct(ptr, self);
1072
+ VALUE ret;
1073
+ MPFIMatrix *ptr_ret;
1074
+ r_mpfi_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr->row, ptr->column);
1075
+ mpfi_matrix_from_mpfr_matrix(ptr_ret, ptr);
1076
+ return ret;
1077
+ }
1078
+
1079
+ void Init_matrix(){
1080
+
1081
+ /* Initialization of MPFI::Matrix, MPFI::SquareMatrix, MPFI::ColumnVector and MPFI::RowVector */
1082
+ VALUE tmp_mpfi_class = rb_define_class("MPFI", rb_cNumeric);
1083
+ r_mpfi_matrix = rb_define_class_under(tmp_mpfi_class, "Matrix", rb_cObject);
1084
+ r_mpfi_square_matrix = rb_define_class_under(tmp_mpfi_class, "SquareMatrix", r_mpfi_matrix);
1085
+ r_mpfi_col_vector = rb_define_class_under(tmp_mpfi_class, "ColumnVector", r_mpfi_matrix);
1086
+ r_mpfi_row_vector = rb_define_class_under(tmp_mpfi_class, "RowVector", r_mpfi_matrix);
1087
+
1088
+ rb_define_alloc_func(r_mpfi_matrix, r_mpfi_matrix_alloc);
1089
+ rb_define_private_method(r_mpfi_matrix, "initialize", r_mpfi_matrix_initialize, -1);
1090
+ rb_define_private_method(r_mpfi_matrix, "initialize_copy", r_mpfi_matrix_initialize_copy, 1);
1091
+
1092
+ rb_define_alloc_func(r_mpfi_square_matrix, r_mpfi_square_matrix_alloc);
1093
+ rb_define_private_method(r_mpfi_square_matrix, "initialize", r_mpfi_square_matrix_initialize, 1);
1094
+
1095
+ rb_define_alloc_func(r_mpfi_col_vector, r_mpfi_col_vector_alloc);
1096
+ rb_define_private_method(r_mpfi_col_vector, "initialize", r_mpfi_col_vector_initialize, 1);
1097
+
1098
+ rb_define_alloc_func(r_mpfi_row_vector, r_mpfi_row_vector_alloc);
1099
+ rb_define_private_method(r_mpfi_row_vector, "initialize", r_mpfi_row_vector_initialize, 1);
1100
+
1101
+
1102
+ rb_define_method(r_mpfi_matrix, "at", r_mpfi_matrix_at, 1);
1103
+ rb_define_method(r_mpfi_matrix, "set_element", r_mpfi_matrix_set_element, 3);
1104
+ rb_define_method(r_mpfi_matrix, "element", r_mpfi_matrix_element, 2);
1105
+ rb_define_method(r_mpfi_matrix, "each_element", r_mpfi_matrix_each_element, 0);
1106
+ rb_define_method(r_mpfi_matrix, "each_element_with_index", r_mpfi_matrix_each_element_with_index, 0);
1107
+
1108
+ rb_define_method(r_mpfi_matrix, "size", r_mpfi_matrix_size, 0);
1109
+ rb_define_method(r_mpfi_matrix, "column_size", r_mpfi_matrix_column_size, 0);
1110
+ rb_define_method(r_mpfi_matrix, "row_size", r_mpfi_matrix_row_size, 0);
1111
+
1112
+ rb_define_alias(r_mpfi_matrix, "[]=", "set_element");
1113
+ rb_define_alias(r_mpfi_matrix, "[]", "element");
1114
+ rb_define_alias(r_mpfi_matrix, "each", "each_element");
1115
+
1116
+ rb_include_module(r_mpfi_matrix, rb_mEnumerable);
1117
+
1118
+
1119
+ rb_define_method(r_mpfi_matrix, "str_ary_for_inspect", r_mpfi_matrix_str_ary_for_inspect, 0);
1120
+ rb_define_method(r_mpfi_matrix, "str_ary_for_inspect2", r_mpfi_matrix_str_ary_for_inspect2, 0);
1121
+ rb_define_method(r_mpfi_matrix, "to_str_ary", r_mpfi_matrix_to_str_ary, 0);
1122
+ rb_define_method(r_mpfi_matrix, "to_strf_ary", r_mpfi_matrix_to_strf_ary, 1);
1123
+ rb_define_method(r_mpfi_matrix, "to_a", r_mpfi_matrix_to_array2, 0);
1124
+ rb_define_method(r_mpfi_matrix, "to_a2", r_mpfi_matrix_to_array, 0);
1125
+
1126
+
1127
+ rb_define_method(r_mpfi_matrix, "row", r_mpfi_matrix_row, 1);
1128
+ rb_define_method(r_mpfi_matrix, "column", r_mpfi_matrix_column, 1);
1129
+ rb_define_method(r_mpfi_matrix, "transpose", r_mpfi_matrix_transpose, 0);
1130
+ rb_define_method(r_mpfi_matrix, "transpose!", r_mpfi_matrix_transpose2, 0);
1131
+ rb_define_method(r_mpfi_matrix, "neg", r_mpfi_matrix_neg, 0);
1132
+
1133
+
1134
+ rb_define_method(r_mpfi_matrix, "==", r_mpfi_matrix_equal_p, 1);
1135
+ rb_define_method(r_mpfi_matrix, "+", r_mpfi_matrix_add, 1);
1136
+ rb_define_method(r_mpfi_matrix, "add", r_mpfi_matrix_add2, 1);
1137
+ rb_define_method(r_mpfi_matrix, "-", r_mpfi_matrix_sub, 1);
1138
+ rb_define_method(r_mpfi_matrix, "sub", r_mpfi_matrix_sub2, 1);
1139
+ rb_define_method(r_mpfi_matrix, "*", r_mpfi_matrix_mul_matrix, 1);
1140
+ rb_define_method(r_mpfi_matrix, "mul", r_mpfi_matrix_mul_matrix2, 1);
1141
+ rb_define_method(r_mpfi_matrix, "mul_scalar", r_mpfi_matrix_mul_scalar, 1);
1142
+ rb_define_method(r_mpfi_matrix, "div_scalar", r_mpfi_matrix_div_scalar, 1);
1143
+
1144
+
1145
+ rb_define_method(r_mpfi_matrix, "include?", r_mpfi_matrix_include_p, 1);
1146
+ rb_define_method(r_mpfi_matrix, "strictly_include?", r_mpfi_matrix_strictly_include_p, 1);
1147
+ rb_define_method(r_mpfi_matrix, "bounded?", r_mpfi_matrix_bounded_p, 0);
1148
+ rb_define_method(r_mpfi_matrix, "mid", r_mpfi_matrix_mid, 0);
1149
+ rb_define_method(r_mpfi_matrix, "mid_interval", r_mpfi_matrix_mid_interval, 0);
1150
+ rb_define_method(r_mpfi_matrix, "intersect", r_mpfi_matrix_intersect, 1);
1151
+ rb_define_method(r_mpfi_matrix, "union", r_mpfi_matrix_union, 1);
1152
+
1153
+ rb_define_method(r_mpfi_matrix, "max_diam_abs", r_mpfi_matrix_max_diam_abs, 0);
1154
+
1155
+
1156
+ rb_define_method(r_mpfi_square_matrix, "dim", r_mpfi_square_matrix_dim, 0);
1157
+ rb_define_method(r_mpfi_square_matrix, "lu_decomp", r_mpfi_square_matrix_lu_decomp, 0);
1158
+ rb_define_method(r_mpfi_square_matrix, "determinant", r_mpfi_square_matrix_determinant, 0);
1159
+ rb_define_method(r_mpfi_square_matrix, "qr_decomp", r_mpfi_square_matrix_qr_decomp, 0);
1160
+
1161
+ rb_define_singleton_method(r_mpfi_square_matrix, "identity", r_mpfi_square_matrix_identity, 1);
1162
+
1163
+ /* Initialization of MPFI::Vector module */
1164
+ r_mpfi_vector_module = rb_define_module_under(tmp_mpfi_class, "Vector");
1165
+
1166
+ rb_include_module(r_mpfi_col_vector, r_mpfi_vector_module);
1167
+ rb_include_module(r_mpfi_row_vector, r_mpfi_vector_module);
1168
+
1169
+ rb_define_method(r_mpfi_vector_module, "[]=", r_mpfi_vector_set_element, 2);
1170
+ rb_define_method(r_mpfi_vector_module, "[]", r_mpfi_vector_element, 1);
1171
+ rb_define_method(r_mpfi_vector_module, "each_element", r_mpfi_vector_each_element, 0);
1172
+ rb_define_method(r_mpfi_vector_module, "each_element_with_index", r_mpfi_vector_each_element_with_index, 0);
1173
+
1174
+ rb_define_alias(r_mpfi_vector_module, "each", "each_element");
1175
+ rb_define_alias(r_mpfi_vector_module, "each_with_index", "each_element_with_index");
1176
+
1177
+ rb_define_method(r_mpfi_vector_module, "dim", r_mpfi_vector_dim, 0);
1178
+ rb_define_method(r_mpfi_vector_module, "abs", r_mpfi_vector_abs, 0);
1179
+ rb_define_method(r_mpfi_vector_module, "normalize", r_mpfi_vector_normalize, 0);
1180
+ rb_define_method(r_mpfi_vector_module, "normalize!", r_mpfi_vector_normalize2, 0);
1181
+ rb_define_method(r_mpfi_vector_module, "set_length", r_mpfi_vector_set_length, 1);
1182
+ rb_define_method(r_mpfi_vector_module, "set_length!", r_mpfi_vector_set_length2, 1);
1183
+
1184
+ rb_define_method(r_mpfi_vector_module, "inner_product", r_mpfi_vector_inner_product, 1);
1185
+ rb_define_method(r_mpfi_vector_module, "distance_from", r_mpfi_vector_distance_from, 1);
1186
+ rb_define_method(r_mpfi_vector_module, "distance_center_pts", r_mpfi_vector_distance_center_pts, 1);
1187
+ rb_define_method(r_mpfi_vector_module, "midpoint", r_mpfi_vector_midpoint, 1);
1188
+
1189
+ /* rb_define_method(r_mpfi_matrix, "vector_norm", r_mpfi_matrix_vector_norm, 0); */
1190
+ /* rb_define_method(r_mpfi_matrix, "max_norm", r_mpfi_matrix_max_norm, 0); */
1191
+
1192
+ rb_define_method(r_mpfr_matrix, "to_fi_matrix", r_mpfr_matrix_to_fi_matrix, 0);
1193
+
1194
+
1195
+ eqq = rb_intern("===");
1196
+ __mpfr_matrix_class__ = rb_eval_string("MPFR::Matrix");
1197
+ __mpfi_matrix_class__ = rb_eval_string("MPFI::Matrix");
1198
+
1199
+ }
1200
+