ruby-mpfr 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,12 @@
1
1
  #include "func_mpfr_matrix.h"
2
2
 
3
3
  void mpfr_matrix_init(MPFRMatrix *mat, int row, int column){
4
+ int i;
4
5
  mat->row = row;
5
6
  mat->column = column;
6
7
  mat->size = row * column;
7
8
  /* mat->data = (MPFR *)malloc(sizeof(MPF) * mat->size); */
8
9
  mat->data = ALLOC_N(MPFR, mat->size);
9
- int i;
10
10
  for(i = 0; i < mat->size; i++){
11
11
  mpfr_init(mat->data + i);
12
12
  }
@@ -76,7 +76,6 @@ void mpfr_matrix_neg(MPFRMatrix *new, MPFRMatrix *x){
76
76
  }
77
77
  }
78
78
 
79
-
80
79
  /* Return 0 if *x and *y has the same elements. Otherwise return 1. */
81
80
  int mpfr_matrix_equal_p(MPFRMatrix *x, MPFRMatrix *y){
82
81
  int i, ret = 0;
@@ -150,8 +149,8 @@ void mpfr_matrix_div_scalar(MPFRMatrix *new, MPFRMatrix *x, MPFR *scalar){
150
149
 
151
150
  void mpfr_matrix_inner_product(MPFR *pr, MPFRMatrix *x, MPFRMatrix *y){
152
151
  MPFR *tmp;
153
- r_mpfr_temp_alloc_init(tmp);
154
152
  int i;
153
+ r_mpfr_temp_alloc_init(tmp);
155
154
  mpfr_set_si(pr, 0, GMP_RNDN);
156
155
  for(i = 0; i < x->size; i++){
157
156
  mpfr_mul(tmp, x->data + i, y->data + i, GMP_RNDN);
@@ -162,8 +161,8 @@ void mpfr_matrix_inner_product(MPFR *pr, MPFRMatrix *x, MPFRMatrix *y){
162
161
 
163
162
  void mpfr_matrix_vector_distance(MPFR *distance, MPFRMatrix *x, MPFRMatrix *y){
164
163
  MPFR *tmp;
165
- r_mpfr_temp_alloc_init(tmp);
166
164
  int i;
165
+ r_mpfr_temp_alloc_init(tmp);
167
166
  mpfr_set_si(distance, 0, GMP_RNDN);
168
167
  for(i = 0; i < x->size; i++){
169
168
  mpfr_sub(tmp, x->data + i, y->data + i, GMP_RNDN);
@@ -176,8 +175,8 @@ void mpfr_matrix_vector_distance(MPFR *distance, MPFRMatrix *x, MPFRMatrix *y){
176
175
 
177
176
  void mpfr_matrix_vector_norm(MPFR *norm, MPFRMatrix *x){
178
177
  MPFR *tmp;
179
- r_mpfr_temp_alloc_init(tmp);
180
178
  int i;
179
+ r_mpfr_temp_alloc_init(tmp);
181
180
  mpfr_set_si(norm, 0, GMP_RNDN);
182
181
  for(i = 0; i < x->size; i++){
183
182
  mpfr_mul(tmp, x->data + i, x->data + i, GMP_RNDN);
@@ -189,8 +188,8 @@ void mpfr_matrix_vector_norm(MPFR *norm, MPFRMatrix *x){
189
188
 
190
189
  void mpfr_matrix_max_norm(MPFR *norm, MPFRMatrix *x){
191
190
  MPFR *tmp;
192
- r_mpfr_temp_alloc_init(tmp);
193
191
  int i;
192
+ r_mpfr_temp_alloc_init(tmp);
194
193
  mpfr_set_si(norm, 0, GMP_RNDN);
195
194
  for(i = 0; i < x->size; i++){
196
195
  mpfr_abs(tmp, x->data + i, GMP_RNDN);
@@ -202,24 +201,24 @@ void mpfr_matrix_max_norm(MPFR *norm, MPFRMatrix *x){
202
201
  }
203
202
 
204
203
  void mpfr_col_vector_init(MPFRMatrix *mat, int row){
204
+ int i;
205
205
  mat->row = row;
206
206
  mat->column = 1;
207
207
  mat->size = row;
208
208
  /* mat->data = (MPFR *)malloc(sizeof(MPF) * mat->size); */
209
209
  mat->data = ALLOC_N(MPFR, mat->size);
210
- int i;
211
210
  for(i = 0; i < mat->size; i++){
212
211
  mpfr_init(mat->data + i);
213
212
  }
214
213
  }
215
214
 
216
215
  void mpfr_row_vector_init(MPFRMatrix *mat, int column){
216
+ int i;
217
217
  mat->row = 1;
218
218
  mat->column = column;
219
219
  mat->size = column;
220
220
  /* mat->data = (MPFR *)malloc(sizeof(MPF) * mat->size); */
221
221
  mat->data = ALLOC_N(MPFR, mat->size);
222
- int i;
223
222
  for(i = 0; i < mat->size; i++){
224
223
  mpfr_init(mat->data + i);
225
224
  }
@@ -228,9 +227,9 @@ void mpfr_row_vector_init(MPFRMatrix *mat, int column){
228
227
  /* If length of MPFRMatrix *x is zero, return 1. Otherwise return 0. */
229
228
  int mpfr_vector_normalize(MPFRMatrix *new, MPFRMatrix *x){
230
229
  MPFR *norm;
230
+ int i, j, index, ret = 0;
231
231
  r_mpfr_temp_alloc_init(norm);
232
232
  mpfr_matrix_vector_norm(norm, x);
233
- int i, j, index, ret = 0;
234
233
  if(mpfr_cmp_ui(norm, 0) > 0){
235
234
  for(j = 0; j < x->column; j++){
236
235
  index = j * x->row;
@@ -248,9 +247,9 @@ int mpfr_vector_normalize(MPFRMatrix *new, MPFRMatrix *x){
248
247
  /* If length of MPFRMatrix *x is zero, return 1. Otherwise return 0. */
249
248
  int mpfr_vector_set_length(MPFRMatrix *new, MPFRMatrix *x, MPFR *length){
250
249
  MPFR *factor;
250
+ int i, j, index, ret = 0;
251
251
  r_mpfr_temp_alloc_init(factor);
252
252
  mpfr_matrix_vector_norm(factor, x);
253
- int i, j, index, ret = 0;
254
253
  if(mpfr_cmp_ui(factor, 0) > 0){
255
254
  mpfr_ui_div(factor, 1, factor, GMP_RNDN);
256
255
  mpfr_mul(factor, factor, length, GMP_RNDN);
@@ -278,10 +277,10 @@ void mpfr_vector_midpoint(MPFRMatrix *new, MPFRMatrix *x, MPFRMatrix *y){
278
277
  /* "distance between *new and *x" / "distance between *y and *x" = *div */
279
278
  void mpfr_vector_dividing_point(MPFRMatrix *new, MPFRMatrix *x, MPFRMatrix *y, MPFR *div){
280
279
  MPFRMatrix *tmp_x, *tmp_y;
280
+ MPFR *ratio;
281
281
  r_mpfr_matrix_temp_alloc_init(tmp_x, new->row, new->column);
282
282
  r_mpfr_matrix_temp_alloc_init(tmp_y, new->row, new->column);
283
283
 
284
- MPFR *ratio;
285
284
  r_mpfr_temp_alloc_init(ratio);
286
285
  mpfr_ui_sub(ratio, 1, div, GMP_RNDN);
287
286
  mpfr_matrix_mul_scalar(tmp_y, y, ratio);
@@ -441,8 +440,9 @@ void mpfr_square_matrix_determinant(MPFR *det, MPFRMatrix *x){
441
440
  mpfr_3d_square_matrix_determinant(det, x);
442
441
  }else{
443
442
  MPFRMatrix *ptr_lu;
443
+ int i, *indx;
444
444
  r_mpfr_matrix_temp_alloc_init(ptr_lu, x->row, x->column);
445
- int indx[x->row], i;
445
+ indx = (int *)malloc(sizeof(int) * x->row);
446
446
  if((i = mpfr_square_matrix_lu_decomp (ptr_lu, indx, x)) >= 0){
447
447
  if (i == 0) {
448
448
  mpfr_set_si(det, 1, GMP_RNDN);
@@ -456,22 +456,21 @@ void mpfr_square_matrix_determinant(MPFR *det, MPFRMatrix *x){
456
456
  mpfr_set_ui(det, 0, GMP_RNDN);
457
457
  }
458
458
  r_mpfr_matrix_temp_free(ptr_lu);
459
+ free(indx);
459
460
  }
460
-
461
461
  }
462
462
 
463
463
  void mpfr_square_matrix_qr_decomp(MPFRMatrix *q, MPFRMatrix *r, MPFRMatrix *x){
464
- MPFRMatrix *q_mat, *r_mat;
464
+ MPFRMatrix *q_mat, *r_mat, *ary;
465
+ int i, j, k, ind1, ind2, ind3, size;
466
+ MPFR *tmp;
465
467
  r_mpfr_matrix_temp_alloc_init(q_mat, q->row, q->column);
466
468
  r_mpfr_matrix_temp_alloc_init(r_mat, r->row, r->column);
467
469
 
468
- int size = x->row;
469
- MPFRMatrix *ary;
470
+ size = x->row;
470
471
  r_mpfr_matrix_temp_alloc_init(ary, size, size);
471
472
  mpfr_matrix_set(ary, x);
472
- MPFR *tmp;
473
473
  r_mpfr_temp_alloc_init(tmp);
474
- int i, j, k, ind1, ind2, ind3;
475
474
  for (i = 0; i < size; i++) {
476
475
  ind1 = i * size;
477
476
  ind2 = i + ind1;
@@ -518,7 +517,5 @@ void mpfr_square_matrix_identity(MPFRMatrix *id){
518
517
  mpfr_set_si(id->data + i + index, 0, GMP_RNDN);
519
518
  }
520
519
  }
521
- }
522
-
520
+ }
523
521
  }
524
-
@@ -195,10 +195,10 @@ static VALUE r_mpfr_matrix_initialize_copy (VALUE self, VALUE other)
195
195
  static VALUE r_mpfr_matrix_marshal_dump(VALUE self)
196
196
  {
197
197
  MPFRMatrix *ptr;
198
- r_mpfr_get_matrix_struct(ptr, self);
199
198
  int i;
200
199
  char *tmp_str;
201
200
  VALUE ret_ary;
201
+ r_mpfr_get_matrix_struct(ptr, self);
202
202
  ret_ary = rb_ary_new();
203
203
  rb_ary_push(ret_ary, INT2FIX(ptr->row));
204
204
  rb_ary_push(ret_ary, INT2FIX(ptr->column));
@@ -215,15 +215,15 @@ static VALUE r_mpfr_matrix_marshal_dump(VALUE self)
215
215
  static VALUE r_mpfr_matrix_marshal_load(VALUE self, VALUE dump_ary)
216
216
  {
217
217
  MPFRMatrix *ptr;
218
+ int i;
219
+ char *dump;
220
+ VALUE dump_element;
218
221
  r_mpfr_get_matrix_struct(ptr, self);
219
222
 
220
223
  ptr->row = NUM2INT(rb_ary_entry(dump_ary, 0));
221
224
  ptr->column = NUM2INT(rb_ary_entry(dump_ary, 1));
222
225
  ptr->size = ptr->row * ptr->column;
223
226
  ptr->data = ALLOC_N(MPFR, ptr->size);
224
- int i;
225
- char *dump;
226
- VALUE dump_element;
227
227
 
228
228
  for(i = 0; i < ptr->size; i++){
229
229
  dump_element = rb_ary_entry(dump_ary, i + 2);
@@ -342,11 +342,11 @@ static VALUE r_mpfr_matrix_equal_p (VALUE self, VALUE other)
342
342
  MPFRMatrix *ptr_self, *ptr_other;
343
343
  r_mpfr_get_matrix_struct(ptr_self, self);
344
344
  r_mpfr_get_matrix_struct(ptr_other, other);
345
- VALUE ret = Qtrue;
346
345
  if (mpfr_matrix_equal_p(ptr_self, ptr_other) != 0) {
347
- ret = Qnil;
346
+ return Qnil;
347
+ } else {
348
+ return Qtrue;
348
349
  }
349
- return ret;
350
350
  }
351
351
 
352
352
  /* Return size of data array which equals to column * row. */
@@ -377,8 +377,9 @@ static VALUE r_mpfr_matrix_row_size (VALUE self)
377
377
  static VALUE r_mpfr_matrix_element (VALUE self, VALUE row, VALUE column)
378
378
  {
379
379
  MPFRMatrix *ptr_self;
380
+ int i, j;
380
381
  r_mpfr_get_matrix_struct(ptr_self, self);
381
- int i = NUM2INT(row), j = NUM2INT(column);
382
+ i = NUM2INT(row), j = NUM2INT(column);
382
383
  if (i < ptr_self->row && j < ptr_self->column) {
383
384
  VALUE ret;
384
385
  MPFR *ptr_ret;
@@ -394,8 +395,9 @@ static VALUE r_mpfr_matrix_element (VALUE self, VALUE row, VALUE column)
394
395
  static VALUE r_mpfr_matrix_at (VALUE self, VALUE arg)
395
396
  {
396
397
  MPFRMatrix *ptr_self;
398
+ int i;
397
399
  r_mpfr_get_matrix_struct(ptr_self, self);
398
- int i = NUM2INT(arg);
400
+ i = NUM2INT(arg);
399
401
  if (i < ptr_self->size) {
400
402
  VALUE ret;
401
403
  MPFR *ptr_ret;
@@ -411,8 +413,9 @@ static VALUE r_mpfr_matrix_at (VALUE self, VALUE arg)
411
413
  static VALUE r_mpfr_matrix_set_element (VALUE self, VALUE row, VALUE column, VALUE robj)
412
414
  {
413
415
  MPFRMatrix *ptr_self;
416
+ int i, j;
414
417
  r_mpfr_get_matrix_struct(ptr_self, self);
415
- int i = NUM2INT(row), j = NUM2INT(column);
418
+ i = NUM2INT(row), j = NUM2INT(column);
416
419
  if (i < ptr_self->row && j < ptr_self->column) {
417
420
  r_mpfr_set_robj(mpfr_matrix_get_element(ptr_self, i, j), robj, GMP_RNDN);
418
421
  }
@@ -423,9 +426,10 @@ static VALUE r_mpfr_matrix_set_element (VALUE self, VALUE row, VALUE column, VAL
423
426
  static VALUE r_mpfr_matrix_each_element (VALUE self)
424
427
  {
425
428
  MPFRMatrix *ptr_self;
426
- r_mpfr_get_matrix_struct(ptr_self, self);
427
- VALUE ret = Qnil;
428
429
  int i, j;
430
+ VALUE ret;
431
+ r_mpfr_get_matrix_struct(ptr_self, self);
432
+ ret = Qnil;
429
433
  for (i = 0; i < ptr_self->row; i++) {
430
434
  for (j = 0; j < ptr_self->column; j++) {
431
435
  volatile VALUE arg = r_mpfr_make_new_fr_obj(mpfr_matrix_get_element(ptr_self, i, j));
@@ -439,9 +443,10 @@ static VALUE r_mpfr_matrix_each_element (VALUE self)
439
443
  static VALUE r_mpfr_matrix_each_element_with_index (VALUE self)
440
444
  {
441
445
  MPFRMatrix *ptr_self;
442
- r_mpfr_get_matrix_struct(ptr_self, self);
443
- VALUE ret = Qnil, tmp_i;
446
+ VALUE ret, tmp_i;
444
447
  int i, j;
448
+ r_mpfr_get_matrix_struct(ptr_self, self);
449
+ ret = Qnil;
445
450
  for (i = 0; i < ptr_self->row; i++) {
446
451
  tmp_i = INT2NUM(i);
447
452
  for (j = 0; j < ptr_self->column; j++) {
@@ -456,10 +461,11 @@ static VALUE r_mpfr_matrix_each_element_with_index (VALUE self)
456
461
  static VALUE r_mpfr_matrix_str_ary(VALUE self, VALUE format_str)
457
462
  {
458
463
  MPFRMatrix *ptr_self;
464
+ char *format, *tmp_str;
465
+ int i;
459
466
  r_mpfr_get_matrix_struct(ptr_self, self);
460
- char *format = StringValuePtr(format_str), *tmp_str;
461
467
  VALUE ret_val[ptr_self->size];
462
- int i;
468
+ format = StringValuePtr(format_str);
463
469
  for (i = 0; i < ptr_self->size; i++) {
464
470
  if (!mpfr_asprintf(&tmp_str, format, ptr_self->data + i)) {
465
471
  rb_raise(rb_eFatal, "Can not allocate a string by mpfr_asprintf.");
@@ -474,10 +480,11 @@ static VALUE r_mpfr_matrix_str_ary(VALUE self, VALUE format_str)
474
480
  static VALUE r_mpfr_matrix_str_ary2(VALUE self, VALUE format_str)
475
481
  {
476
482
  MPFRMatrix *ptr_self;
483
+ char *format, *tmp_str;
484
+ int i, j;
477
485
  r_mpfr_get_matrix_struct(ptr_self, self);
478
- char *format = StringValuePtr(format_str), *tmp_str;
486
+ format = StringValuePtr(format_str);
479
487
  VALUE ary[ptr_self->row];
480
- int i, j;
481
488
  for(i = 0; i < ptr_self->row; i++){
482
489
  ary[i] = rb_ary_new();
483
490
  }
@@ -497,9 +504,9 @@ static VALUE r_mpfr_matrix_str_ary2(VALUE self, VALUE format_str)
497
504
  static VALUE r_mpfr_matrix_to_array(VALUE self)
498
505
  {
499
506
  MPFRMatrix *ptr_self;
507
+ int i;
500
508
  r_mpfr_get_matrix_struct(ptr_self, self);
501
509
  VALUE ret_val[ptr_self->size];
502
- int i;
503
510
  for (i = 0; i < ptr_self->size; i++) {
504
511
  ret_val[i] = r_mpfr_make_new_fr_obj(ptr_self->data + i);
505
512
  }
@@ -510,9 +517,9 @@ static VALUE r_mpfr_matrix_to_array(VALUE self)
510
517
  static VALUE r_mpfr_matrix_to_array2(VALUE self)
511
518
  {
512
519
  MPFRMatrix *ptr_self;
520
+ int i, j;
513
521
  r_mpfr_get_matrix_struct(ptr_self, self);
514
522
  VALUE ary[ptr_self->row];
515
- int i, j;
516
523
  for(i = 0; i < ptr_self->row; i++){
517
524
  ary[i] = rb_ary_new();
518
525
  }
@@ -528,9 +535,10 @@ static VALUE r_mpfr_matrix_to_array2(VALUE self)
528
535
  static VALUE r_mpfr_matrix_row (VALUE self, VALUE arg)
529
536
  {
530
537
  MPFRMatrix *ptr_self, *ptr_ret;
531
- r_mpfr_get_matrix_struct(ptr_self, self);
532
- int num = NUM2INT(arg);
538
+ int num;
533
539
  VALUE ret;
540
+ r_mpfr_get_matrix_struct(ptr_self, self);
541
+ num = NUM2INT(arg);
534
542
  if(num < ptr_self->row){
535
543
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, 1, ptr_self->column);
536
544
  mpfr_matrix_row(ptr_ret, ptr_self, num);
@@ -544,9 +552,10 @@ static VALUE r_mpfr_matrix_row (VALUE self, VALUE arg)
544
552
  static VALUE r_mpfr_matrix_column (VALUE self, VALUE arg)
545
553
  {
546
554
  MPFRMatrix *ptr_self, *ptr_ret;
547
- r_mpfr_get_matrix_struct(ptr_self, self);
548
- int num = NUM2INT(arg);
555
+ int num;
549
556
  VALUE ret;
557
+ r_mpfr_get_matrix_struct(ptr_self, self);
558
+ num = NUM2INT(arg);
550
559
  if(num < ptr_self->column){
551
560
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, 1);
552
561
  mpfr_matrix_column(ptr_ret, ptr_self, num);
@@ -560,8 +569,8 @@ static VALUE r_mpfr_matrix_column (VALUE self, VALUE arg)
560
569
  static VALUE r_mpfr_matrix_transpose (VALUE self)
561
570
  {
562
571
  MPFRMatrix *ptr_self, *ptr_ret;
563
- r_mpfr_get_matrix_struct(ptr_self, self);
564
572
  VALUE ret;
573
+ r_mpfr_get_matrix_struct(ptr_self, self);
565
574
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->column, ptr_self->row);
566
575
  mpfr_matrix_transpose(ptr_ret, ptr_self);
567
576
  return ret;
@@ -571,6 +580,7 @@ static VALUE r_mpfr_matrix_transpose (VALUE self)
571
580
  static VALUE r_mpfr_matrix_transpose2 (VALUE self)
572
581
  {
573
582
  MPFRMatrix *ptr_self, tmp;
583
+ int i;
574
584
  r_mpfr_get_matrix_struct(ptr_self, self);
575
585
  if (ptr_self->column > 1 && ptr_self->row > 1) {
576
586
  mpfr_matrix_init(&tmp, ptr_self->column, ptr_self->row);
@@ -578,7 +588,6 @@ static VALUE r_mpfr_matrix_transpose2 (VALUE self)
578
588
  mpfr_matrix_set(ptr_self, &tmp);
579
589
  mpfr_matrix_clear(&tmp);
580
590
  }
581
- int i;
582
591
  i = ptr_self->column;
583
592
  ptr_self->column = ptr_self->row;
584
593
  ptr_self->row = i;
@@ -589,8 +598,8 @@ static VALUE r_mpfr_matrix_transpose2 (VALUE self)
589
598
  static VALUE r_mpfr_matrix_neg (VALUE self)
590
599
  {
591
600
  MPFRMatrix *ptr_self, *ptr_ret;
592
- r_mpfr_get_matrix_struct(ptr_self, self);
593
601
  VALUE ret;
602
+ r_mpfr_get_matrix_struct(ptr_self, self);
594
603
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
595
604
  mpfr_matrix_neg(ptr_ret, ptr_self);
596
605
  return ret;
@@ -600,9 +609,9 @@ static VALUE r_mpfr_matrix_neg (VALUE self)
600
609
  static VALUE r_mpfr_matrix_add (VALUE self, VALUE other)
601
610
  {
602
611
  MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
612
+ VALUE ret;
603
613
  r_mpfr_get_matrix_struct(ptr_self, self);
604
614
  r_mpfr_get_matrix_struct(ptr_other, other);
605
- VALUE ret;
606
615
  if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
607
616
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
608
617
  mpfr_matrix_add(ptr_ret, ptr_self, ptr_other);
@@ -616,9 +625,9 @@ static VALUE r_mpfr_matrix_add (VALUE self, VALUE other)
616
625
  static VALUE r_mpfr_matrix_add2 (VALUE self, VALUE other)
617
626
  {
618
627
  MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
628
+ VALUE ret;
619
629
  r_mpfr_get_matrix_struct(ptr_self, self);
620
630
  r_mpfr_get_matrix_struct(ptr_other, other);
621
- VALUE ret;
622
631
  r_mpfr_make_matrix_struct(ret, ptr_ret);
623
632
  if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
624
633
  mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_self->column);
@@ -633,9 +642,9 @@ static VALUE r_mpfr_matrix_add2 (VALUE self, VALUE other)
633
642
  static VALUE r_mpfr_matrix_sub (VALUE self, VALUE other)
634
643
  {
635
644
  MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
645
+ VALUE ret;
636
646
  r_mpfr_get_matrix_struct(ptr_self, self);
637
647
  r_mpfr_get_matrix_struct(ptr_other, other);
638
- VALUE ret;
639
648
  r_mpfr_make_matrix_struct(ret, ptr_ret);
640
649
  if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
641
650
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
@@ -650,9 +659,9 @@ static VALUE r_mpfr_matrix_sub (VALUE self, VALUE other)
650
659
  static VALUE r_mpfr_matrix_sub2 (VALUE self, VALUE other)
651
660
  {
652
661
  MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
662
+ VALUE ret;
653
663
  r_mpfr_get_matrix_struct(ptr_self, self);
654
664
  r_mpfr_get_matrix_struct(ptr_other, other);
655
- VALUE ret;
656
665
  r_mpfr_make_matrix_struct(ret, ptr_ret);
657
666
  if (ptr_self->column == ptr_other->column && ptr_self->row == ptr_other->row) {
658
667
  mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_self->column);
@@ -667,10 +676,10 @@ static VALUE r_mpfr_matrix_sub2 (VALUE self, VALUE other)
667
676
  static VALUE r_mpfr_vector_inner_product (VALUE self, VALUE arg)
668
677
  {
669
678
  MPFRMatrix *ptr_self, *ptr_arg;
670
- r_mpfr_get_matrix_struct(ptr_self, self);
671
- r_mpfr_get_matrix_struct(ptr_arg, arg);
672
679
  VALUE ret;
673
680
  MPFR *ptr_ret;
681
+ r_mpfr_get_matrix_struct(ptr_self, self);
682
+ r_mpfr_get_matrix_struct(ptr_arg, arg);
674
683
  r_mpfr_make_struct_init(ret, ptr_ret);
675
684
  if(ptr_self->size == ptr_arg->size){
676
685
  mpfr_matrix_inner_product(ptr_ret, ptr_self, ptr_arg);
@@ -684,9 +693,9 @@ static VALUE r_mpfr_vector_inner_product (VALUE self, VALUE arg)
684
693
  static VALUE r_mpfr_matrix_mul_matrix (VALUE self, VALUE other)
685
694
  {
686
695
  MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
696
+ VALUE ret;
687
697
  r_mpfr_get_matrix_struct(ptr_self, self);
688
698
  r_mpfr_get_matrix_struct(ptr_other, other);
689
- VALUE ret;
690
699
  r_mpfr_make_matrix_struct(ret, ptr_ret);
691
700
  if (ptr_self->column == ptr_other->row) {
692
701
  if((ptr_other->column == 1) && (ptr_self->row == 1)){
@@ -705,9 +714,9 @@ static VALUE r_mpfr_matrix_mul_matrix (VALUE self, VALUE other)
705
714
  static VALUE r_mpfr_matrix_mul_matrix2 (VALUE self, VALUE other)
706
715
  {
707
716
  MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
717
+ VALUE ret;
708
718
  r_mpfr_get_matrix_struct(ptr_self, self);
709
719
  r_mpfr_get_matrix_struct(ptr_other, other);
710
- VALUE ret;
711
720
  r_mpfr_make_matrix_struct(ret, ptr_ret);
712
721
  if (ptr_self->column == ptr_other->row) {
713
722
  mpfr_matrix_init(ptr_ret, ptr_self->row, ptr_other->column);
@@ -723,8 +732,8 @@ static VALUE r_mpfr_matrix_mul_scalar (VALUE self, VALUE scalar)
723
732
  {
724
733
  MPFRMatrix *ptr_self, *ptr_ret;
725
734
  MPFR *ptr_scalar;
726
- r_mpfr_get_matrix_struct(ptr_self, self);
727
735
  VALUE ret;
736
+ r_mpfr_get_matrix_struct(ptr_self, self);
728
737
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
729
738
  if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, scalar))){
730
739
  r_mpfr_get_struct(ptr_scalar, scalar);
@@ -743,8 +752,8 @@ static VALUE r_mpfr_matrix_div_scalar (VALUE self, VALUE scalar)
743
752
  {
744
753
  MPFRMatrix *ptr_self, *ptr_ret;
745
754
  MPFR *ptr_scalar;
746
- r_mpfr_get_matrix_struct(ptr_self, self);
747
755
  VALUE ret;
756
+ r_mpfr_get_matrix_struct(ptr_self, self);
748
757
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
749
758
  if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, scalar))){
750
759
  r_mpfr_get_struct(ptr_scalar, scalar);
@@ -762,9 +771,9 @@ static VALUE r_mpfr_matrix_div_scalar (VALUE self, VALUE scalar)
762
771
  static VALUE r_mpfr_matrix_vector_norm (VALUE self)
763
772
  {
764
773
  MPFRMatrix *ptr_self;
765
- r_mpfr_get_matrix_struct(ptr_self, self);
766
774
  VALUE ret;
767
775
  MPFR *ptr_ret;
776
+ r_mpfr_get_matrix_struct(ptr_self, self);
768
777
  r_mpfr_make_struct_init(ret, ptr_ret);
769
778
  mpfr_matrix_vector_norm(ptr_ret, ptr_self);
770
779
  return ret;
@@ -774,9 +783,9 @@ static VALUE r_mpfr_matrix_vector_norm (VALUE self)
774
783
  static VALUE r_mpfr_matrix_max_norm (VALUE self)
775
784
  {
776
785
  MPFRMatrix *ptr_self;
777
- r_mpfr_get_matrix_struct(ptr_self, self);
778
786
  VALUE ret;
779
787
  MPFR *ptr_ret;
788
+ r_mpfr_get_matrix_struct(ptr_self, self);
780
789
  r_mpfr_make_struct_init(ret, ptr_ret);
781
790
  mpfr_matrix_max_norm(ptr_ret, ptr_self);
782
791
  return ret;
@@ -795,13 +804,13 @@ static VALUE r_mpfr_square_matrix_lu_decomp (VALUE self)
795
804
  {
796
805
  MPFRMatrix *ptr_self, *ptr_ret_l, *ptr_ret_u;
797
806
  VALUE ret_l, ret_u;
807
+ int i, j;
798
808
  r_mpfr_get_matrix_struct(ptr_self, self);
799
809
  r_mpfr_matrix_suitable_matrix_init (&ret_l, &ptr_ret_l, ptr_self->row, ptr_self->column);
800
810
  r_mpfr_matrix_suitable_matrix_init (&ret_u, &ptr_ret_u, ptr_self->row, ptr_self->column);
801
811
  VALUE ret_indx[ptr_self->row];
802
812
  int indx[ptr_self->row];
803
813
  if(mpfr_square_matrix_lu_decomp (ptr_ret_u, indx, ptr_self) >= 0){
804
- int i, j;
805
814
  for(i = 1; i < ptr_ret_u->row; i++){
806
815
  for(j = 0; j < i; j++){
807
816
  mpfr_set(mpfr_matrix_get_element(ptr_ret_l, i, j), mpfr_matrix_get_element(ptr_ret_u, i, j), GMP_RNDN);
@@ -829,9 +838,9 @@ static VALUE r_mpfr_square_matrix_lu_decomp (VALUE self)
829
838
  static VALUE r_mpfr_square_matrix_determinant (VALUE self)
830
839
  {
831
840
  MPFRMatrix *ptr_self;
832
- r_mpfr_get_matrix_struct(ptr_self, self);
833
841
  MPFR *ptr_ret;
834
842
  VALUE ret;
843
+ r_mpfr_get_matrix_struct(ptr_self, self);
835
844
  r_mpfr_make_struct_init(ret, ptr_ret);
836
845
  mpfr_square_matrix_determinant(ptr_ret, ptr_self);
837
846
  return ret;
@@ -841,8 +850,8 @@ static VALUE r_mpfr_square_matrix_determinant (VALUE self)
841
850
  static VALUE r_mpfr_square_matrix_qr_decomp (VALUE self)
842
851
  {
843
852
  MPFRMatrix *ptr_self, *ptr_q, *ptr_r;
844
- r_mpfr_get_matrix_struct(ptr_self, self);
845
853
  VALUE q, r;
854
+ r_mpfr_get_matrix_struct(ptr_self, self);
846
855
  r_mpfr_matrix_suitable_matrix_init (&q, &ptr_q, ptr_self->row, ptr_self->column);
847
856
  r_mpfr_matrix_suitable_matrix_init (&r, &ptr_r, ptr_self->column, ptr_self->column);
848
857
  mpfr_square_matrix_qr_decomp(ptr_q, ptr_r, ptr_self);
@@ -872,9 +881,9 @@ static VALUE r_mpfr_vector_dim (VALUE self)
872
881
  static VALUE r_mpfr_vector_abs (VALUE self)
873
882
  {
874
883
  MPFRMatrix *ptr_self;
875
- r_mpfr_get_matrix_struct(ptr_self, self);
876
884
  VALUE ret;
877
885
  MPFR *ptr_ret;
886
+ r_mpfr_get_matrix_struct(ptr_self, self);
878
887
  r_mpfr_make_struct_init(ret, ptr_ret);
879
888
  mpfr_matrix_vector_norm(ptr_ret, ptr_self);
880
889
  return ret;
@@ -884,8 +893,9 @@ static VALUE r_mpfr_vector_abs (VALUE self)
884
893
  static VALUE r_mpfr_vector_element (VALUE self, VALUE arg)
885
894
  {
886
895
  MPFRMatrix *ptr_self;
896
+ int i;
887
897
  r_mpfr_get_matrix_struct(ptr_self, self);
888
- int i = NUM2INT(arg);
898
+ i = NUM2INT(arg);
889
899
  if (i < ptr_self->size) {
890
900
  VALUE ret;
891
901
  MPFR *ptr_ret;
@@ -901,8 +911,9 @@ static VALUE r_mpfr_vector_element (VALUE self, VALUE arg)
901
911
  static VALUE r_mpfr_vector_set_element (VALUE self, VALUE arg, VALUE robj)
902
912
  {
903
913
  MPFRMatrix *ptr_self;
914
+ int i;
904
915
  r_mpfr_get_matrix_struct(ptr_self, self);
905
- int i = NUM2INT(arg);
916
+ i = NUM2INT(arg);
906
917
  if (i < ptr_self->size) {
907
918
  r_mpfr_set_robj(ptr_self->data + i, robj, GMP_RNDN);
908
919
  }
@@ -913,11 +924,12 @@ static VALUE r_mpfr_vector_set_element (VALUE self, VALUE arg, VALUE robj)
913
924
  static VALUE r_mpfr_vector_each_element (VALUE self)
914
925
  {
915
926
  MPFRMatrix *ptr_self;
916
- r_mpfr_get_matrix_struct(ptr_self, self);
917
- VALUE ret = Qnil, tmp;
927
+ VALUE ret, tmp;
918
928
  MPFR *tmp_ptr;
919
- r_mpfr_make_struct_init(tmp, tmp_ptr);
920
929
  int i;
930
+ r_mpfr_get_matrix_struct(ptr_self, self);
931
+ ret = Qnil;
932
+ r_mpfr_make_struct_init(tmp, tmp_ptr);
921
933
  for (i = 0; i < ptr_self->size; i++) {
922
934
  mpfr_set(tmp_ptr, ptr_self->data + i, GMP_RNDN);
923
935
  ret = rb_yield(tmp);
@@ -929,11 +941,12 @@ static VALUE r_mpfr_vector_each_element (VALUE self)
929
941
  static VALUE r_mpfr_vector_each_element_with_index (VALUE self)
930
942
  {
931
943
  MPFRMatrix *ptr_self;
932
- r_mpfr_get_matrix_struct(ptr_self, self);
933
- VALUE ret = Qnil, tmp;
944
+ VALUE ret, tmp;
934
945
  MPFR *tmp_ptr;
935
- r_mpfr_make_struct_init(tmp, tmp_ptr);
936
946
  int i;
947
+ r_mpfr_get_matrix_struct(ptr_self, self);
948
+ ret = Qnil;
949
+ r_mpfr_make_struct_init(tmp, tmp_ptr);
937
950
  for (i = 0; i < ptr_self->size; i++) {
938
951
  mpfr_set(tmp_ptr, ptr_self->data + i, GMP_RNDN);
939
952
  ret = rb_yield(rb_ary_new3(2, tmp, INT2NUM(i)));
@@ -945,10 +958,10 @@ static VALUE r_mpfr_vector_each_element_with_index (VALUE self)
945
958
  static VALUE r_mpfr_vector_distance_from (VALUE self, VALUE arg)
946
959
  {
947
960
  MPFRMatrix *ptr_self, *ptr_arg;
948
- r_mpfr_get_matrix_struct(ptr_self, self);
949
- r_mpfr_get_matrix_struct(ptr_arg, arg);
950
961
  VALUE ret;
951
962
  MPFR *ptr_ret;
963
+ r_mpfr_get_matrix_struct(ptr_self, self);
964
+ r_mpfr_get_matrix_struct(ptr_arg, arg);
952
965
  r_mpfr_make_struct_init(ret, ptr_ret);
953
966
  if(ptr_self->size == ptr_arg->size){
954
967
  mpfr_matrix_vector_distance(ptr_ret, ptr_self, ptr_arg);
@@ -962,9 +975,9 @@ static VALUE r_mpfr_vector_distance_from (VALUE self, VALUE arg)
962
975
  static VALUE r_mpfr_vector_midpoint (VALUE self, VALUE arg)
963
976
  {
964
977
  MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
978
+ VALUE ret;
965
979
  r_mpfr_get_matrix_struct(ptr_self, self);
966
980
  r_mpfr_get_matrix_struct(ptr_arg, arg);
967
- VALUE ret;
968
981
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
969
982
  mpfr_vector_midpoint(ptr_ret, ptr_self, ptr_arg);
970
983
  return ret;
@@ -974,11 +987,11 @@ static VALUE r_mpfr_vector_midpoint (VALUE self, VALUE arg)
974
987
  static VALUE r_mpfr_vector_dividing_point (VALUE self, VALUE arg, VALUE div)
975
988
  {
976
989
  MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
990
+ MPFR *ptr_div;
991
+ VALUE ret;
977
992
  r_mpfr_get_matrix_struct(ptr_self, self);
978
993
  r_mpfr_get_matrix_struct(ptr_arg, arg);
979
- MPFR *ptr_div;
980
994
  r_mpfr_get_struct(ptr_div, div);
981
- VALUE ret;
982
995
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
983
996
  mpfr_vector_dividing_point(ptr_ret, ptr_self, ptr_arg, ptr_div);
984
997
  return ret;
@@ -988,8 +1001,8 @@ static VALUE r_mpfr_vector_dividing_point (VALUE self, VALUE arg, VALUE div)
988
1001
  static VALUE r_mpfr_vector_normalize (VALUE self)
989
1002
  {
990
1003
  MPFRMatrix *ptr_self, *ptr_ret;
991
- r_mpfr_get_matrix_struct(ptr_self, self);
992
1004
  VALUE ret;
1005
+ r_mpfr_get_matrix_struct(ptr_self, self);
993
1006
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
994
1007
  if(mpfr_vector_normalize(ptr_ret, ptr_self) == 0){
995
1008
  return ret;
@@ -1002,8 +1015,9 @@ static VALUE r_mpfr_vector_normalize (VALUE self)
1002
1015
  static VALUE r_mpfr_vector_normalize2 (VALUE self)
1003
1016
  {
1004
1017
  MPFRMatrix *ptr_self, tmp;
1018
+ VALUE ret;
1005
1019
  r_mpfr_get_matrix_struct(ptr_self, self);
1006
- VALUE ret = self;
1020
+ ret = self;
1007
1021
  mpfr_matrix_init(&tmp, ptr_self->column, ptr_self->row);
1008
1022
  if(mpfr_vector_normalize(&tmp, ptr_self) == 0){
1009
1023
  mpfr_matrix_set(ptr_self, &tmp);
@@ -1018,9 +1032,10 @@ static VALUE r_mpfr_vector_normalize2 (VALUE self)
1018
1032
  static VALUE r_mpfr_vector_set_length (VALUE self, VALUE arg)
1019
1033
  {
1020
1034
  MPFRMatrix *ptr_self, *ptr_ret;
1021
- r_mpfr_get_matrix_struct(ptr_self, self);
1022
- VALUE ret, returned_value = Qnil;
1035
+ VALUE ret, returned_value;
1023
1036
  MPFR *ptr_l;
1037
+ r_mpfr_get_matrix_struct(ptr_self, self);
1038
+ returned_value = Qnil;
1024
1039
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
1025
1040
  if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
1026
1041
  r_mpfr_get_struct(ptr_l, arg);
@@ -1042,9 +1057,10 @@ static VALUE r_mpfr_vector_set_length (VALUE self, VALUE arg)
1042
1057
  static VALUE r_mpfr_vector_set_length2 (VALUE self, VALUE arg)
1043
1058
  {
1044
1059
  MPFRMatrix *ptr_self, *ptr_ret;
1045
- r_mpfr_get_matrix_struct(ptr_self, self);
1046
- VALUE ret, returned_value = Qnil;
1060
+ VALUE ret, returned_value;
1047
1061
  MPFR *ptr_l;
1062
+ r_mpfr_get_matrix_struct(ptr_self, self);
1063
+ returned_value = Qnil;
1048
1064
  r_mpfr_matrix_suitable_matrix_init (&ret, &ptr_ret, ptr_self->row, ptr_self->column);
1049
1065
  if(RTEST(rb_funcall(r_mpfr_class, eqq, 1, arg))){
1050
1066
  r_mpfr_get_struct(ptr_l, arg);