ruby-mpfr 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -2
- data/README.rdoc +13 -7
- data/ext/mpfr/ruby_mpfr.c +34 -9
- data/ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c +138 -56
- data/lib/mpfr/version.rb +1 -3
- data/ruby-mpfr.gemspec +7 -5
- data/spec/mpfr/allocate_spec.rb +13 -0
- data/spec/mpfr_matrix/mpfr_matrix_alloc_spec.rb +6 -0
- metadata +5 -3
data/History.txt
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
|
1
|
+
=== 0.0.5 2009-12-18
|
2
|
+
* 1 major enhancement:
|
3
|
+
* Add methods MPFR, MPFR::Matrix, MPFR::SquareMatrix, MPFR::ColumnVector, and MPFR::RowVector.
|
4
|
+
* 1 minor enhancement:
|
5
|
+
* Maintenance of docments.
|
6
|
+
|
2
7
|
=== 0.0.4 2009-12-17
|
3
|
-
* 1 major enhancements
|
8
|
+
* 1 major enhancements:
|
4
9
|
* Add MPFR::Matrix
|
5
10
|
|
6
11
|
=== 0.0.2 2009-07-25
|
data/README.rdoc
CHANGED
@@ -1,17 +1,21 @@
|
|
1
1
|
= ruby-mpfr
|
2
2
|
|
3
3
|
* http://rubyforge.org/projects/ruby-mpfr/
|
4
|
+
* http://gemcutter.org/gems/ruby-mpfr/
|
4
5
|
|
5
|
-
==
|
6
|
+
== Description:
|
6
7
|
|
7
8
|
ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for
|
8
9
|
multiple-precision floating-point computations.
|
10
|
+
ruby-mpfr also includes Matrix class of MPFR; MPFR::Matrix, MPFR::SquareMatrix,
|
11
|
+
MPFR::ColumnVector, and MPFR::RowVector.
|
9
12
|
|
10
|
-
==
|
13
|
+
== Notice:
|
11
14
|
|
12
|
-
* Many methods have not been tested.
|
15
|
+
* Many methods have not been tested sufficiently.
|
16
|
+
* Documentation is not complete.
|
13
17
|
|
14
|
-
==
|
18
|
+
== Example:
|
15
19
|
|
16
20
|
require "mpfr"
|
17
21
|
MPFR.get_default_prec(100)
|
@@ -19,19 +23,21 @@ multiple-precision floating-point computations.
|
|
19
23
|
b = MPFR.new('2.2')
|
20
24
|
puts (a * b).to_strf("%.30Re")
|
21
25
|
|
22
|
-
|
26
|
+
== Requirements:
|
23
27
|
|
24
28
|
* MPFR[http://www.mpfr.org/] 2.4.1 or later
|
25
29
|
|
26
30
|
For Ubuntu 9.10, we can install MPFR with the following command.
|
27
31
|
$ sudo apt-get install libmpfr-dev
|
28
32
|
|
29
|
-
==
|
33
|
+
== Install:
|
30
34
|
The package of rubygems of ruby-mpfr is provided.
|
35
|
+
You can install ruby-mpfr with the following command
|
36
|
+
in the system satisfying the above requirements.
|
31
37
|
|
32
38
|
$ sudo gem install ruby-mpfr
|
33
39
|
|
34
|
-
==
|
40
|
+
== License:
|
35
41
|
|
36
42
|
ruby-mpfr
|
37
43
|
Copyright (C) 2009 Takayuki YAMAGUCHI
|
data/ext/mpfr/ruby_mpfr.c
CHANGED
@@ -373,16 +373,8 @@ static VALUE r_mpfr_alloc(VALUE self)
|
|
373
373
|
return self;
|
374
374
|
}
|
375
375
|
|
376
|
-
|
377
|
-
This method returns MPFR instance.
|
378
|
-
If there is not an argument, it is set NaN.
|
379
|
-
Possible arguments are value, rounding mode, and precesion.
|
380
|
-
All arguments are optional.
|
381
|
-
*/
|
382
|
-
static VALUE r_mpfr_initialize(int argc, VALUE *argv, VALUE self)
|
376
|
+
static void r_mpfr_set_initial_value(MPFR *ptr, int argc, VALUE *argv)
|
383
377
|
{
|
384
|
-
MPFR *ptr;
|
385
|
-
r_mpfr_get_struct(ptr, self);
|
386
378
|
switch(argc){
|
387
379
|
case 0:
|
388
380
|
mpfr_init(ptr);
|
@@ -405,6 +397,29 @@ static VALUE r_mpfr_initialize(int argc, VALUE *argv, VALUE self)
|
|
405
397
|
rb_raise(rb_eArgError, "Invalid number of arguments.");
|
406
398
|
break;
|
407
399
|
}
|
400
|
+
}
|
401
|
+
|
402
|
+
/* Return new MPFR instance. The same arguments as MPFR.new is acceptable. */
|
403
|
+
static VALUE r_mpfr_global_new(int argc, VALUE *argv, VALUE self)
|
404
|
+
{
|
405
|
+
MPFR *ptr;
|
406
|
+
VALUE val;
|
407
|
+
r_mpfr_make_struct(val, ptr);
|
408
|
+
r_mpfr_set_initial_value(ptr, argc, argv);
|
409
|
+
return val;
|
410
|
+
}
|
411
|
+
|
412
|
+
/*
|
413
|
+
This method returns MPFR instance.
|
414
|
+
If there is not an argument, it is set NaN.
|
415
|
+
Possible arguments are value, rounding mode, and precesion.
|
416
|
+
All arguments are optional.
|
417
|
+
*/
|
418
|
+
static VALUE r_mpfr_initialize(int argc, VALUE *argv, VALUE self)
|
419
|
+
{
|
420
|
+
MPFR *ptr;
|
421
|
+
r_mpfr_get_struct(ptr, self);
|
422
|
+
r_mpfr_set_initial_value(ptr, argc, argv);
|
408
423
|
return Qtrue;
|
409
424
|
}
|
410
425
|
|
@@ -2506,6 +2521,12 @@ void Init_mpfr()
|
|
2506
2521
|
|
2507
2522
|
When method have argument of MPFR and we set instance of other class to the argument,
|
2508
2523
|
the method tries to convert the argument to MPFR instance.
|
2524
|
+
|
2525
|
+
== Conversion to String
|
2526
|
+
|
2527
|
+
MPFR#to_s makes a string corresponding to mpfr_printf("%.Re", fr).
|
2528
|
+
If you want to adjust format of string, you can MPFR#to_strf.
|
2529
|
+
MPFR#to_strf(format) returns a string corresponding to mpfr_printf(format, fr).
|
2509
2530
|
*/
|
2510
2531
|
r_mpfr_class = rb_define_class("MPFR", rb_cNumeric);
|
2511
2532
|
rb_include_module(r_mpfr_class, rb_mComparable);
|
@@ -2591,6 +2612,7 @@ void Init_mpfr()
|
|
2591
2612
|
/* ------------------------------ Exception Related Functions End ------------------------------ */
|
2592
2613
|
|
2593
2614
|
/* ------------------------------ MPFR allocation Start ------------------------------ */
|
2615
|
+
rb_define_global_function("MPFR", r_mpfr_global_new, -1);
|
2594
2616
|
|
2595
2617
|
rb_define_alloc_func(r_mpfr_class, r_mpfr_alloc);
|
2596
2618
|
rb_define_private_method(r_mpfr_class, "initialize", r_mpfr_initialize, -1);
|
@@ -2728,6 +2750,9 @@ void Init_mpfr()
|
|
2728
2750
|
/* ------------------------------ Module MPFR::Math Start ------------------------------ */
|
2729
2751
|
|
2730
2752
|
/*
|
2753
|
+
= MPFR::Math methods
|
2754
|
+
|
2755
|
+
== Arguments and Behaivior
|
2731
2756
|
Almost all class methods in this module is the functions in "Special Functions" item of MPFR reference manual.
|
2732
2757
|
First arguments of these methods takes MPFR instance.
|
2733
2758
|
If it is not MPFR instance, it is converted to MPFR instance in this method.
|
@@ -44,7 +44,7 @@ VALUE r_mpfr_matrix_robj(MPFRMatrix *x)
|
|
44
44
|
return ret;
|
45
45
|
}
|
46
46
|
|
47
|
-
/* Allocation function for
|
47
|
+
/* Allocation function for MPFR::Matrix. */
|
48
48
|
static VALUE r_mpfr_matrix_alloc (VALUE self)
|
49
49
|
{
|
50
50
|
MPFRMatrix *ptr;
|
@@ -59,17 +59,8 @@ static VALUE r_mpfr_matrix_alloc (VALUE self)
|
|
59
59
|
/* mpfr_matrix_set(x, ptr_src); */
|
60
60
|
/* } */
|
61
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)
|
62
|
+
static void r_mpfr_matrix_set_initial_value(MPFRMatrix *ptr, int argc, VALUE *argv)
|
70
63
|
{
|
71
|
-
MPFRMatrix *ptr;
|
72
|
-
r_mpfr_get_matrix_struct(ptr, self);
|
73
64
|
int row, column, i, j;
|
74
65
|
VALUE row_ary;
|
75
66
|
switch (argc) {
|
@@ -80,7 +71,7 @@ static VALUE r_mpfr_matrix_initialize (int argc, VALUE *argv, VALUE self)
|
|
80
71
|
for (i = 0; i < row; i++) {
|
81
72
|
row_ary = rb_ary_entry(argv[0], i);
|
82
73
|
if (column != RARRAY_LEN(row_ary)) {
|
83
|
-
rb_raise(rb_eArgError, "
|
74
|
+
rb_raise(rb_eArgError, "MPFR::Matrix.new needs Array which has arrays of same sizes.");
|
84
75
|
}
|
85
76
|
for (j = 0; j < column; j++) {
|
86
77
|
r_mpfr_set_robj(mpfr_matrix_get_element(ptr, i, j), rb_ary_entry(row_ary, j), GMP_RNDN);
|
@@ -92,13 +83,37 @@ static VALUE r_mpfr_matrix_initialize (int argc, VALUE *argv, VALUE self)
|
|
92
83
|
mpfr_matrix_set_zeros(ptr);
|
93
84
|
break;
|
94
85
|
default:
|
95
|
-
rb_raise(rb_eArgError, "
|
86
|
+
rb_raise(rb_eArgError, "MPFR::Matrix.new needs one or two arguments.");
|
96
87
|
break;
|
97
88
|
}
|
89
|
+
}
|
90
|
+
|
91
|
+
/* Return new MPFR::Matrix instance. The same arguments as MPFR::Matrix.new is acceptable. */
|
92
|
+
static VALUE r_mpfr_matrix_global_new(int argc, VALUE *argv, VALUE self)
|
93
|
+
{
|
94
|
+
MPFRMatrix *ptr;
|
95
|
+
VALUE val;
|
96
|
+
r_mpfr_make_matrix_struct(val, ptr);
|
97
|
+
r_mpfr_matrix_set_initial_value(ptr, argc, argv);
|
98
|
+
return val;
|
99
|
+
}
|
100
|
+
|
101
|
+
/*
|
102
|
+
Initialization function for MPFR::Matrix.
|
103
|
+
If this method gets one argument which is two dimensional Array,
|
104
|
+
it returns MPFR::Matrix instance of which size is same as the array.
|
105
|
+
Getting two argument integers (i, j), it returns MPFR::Matrix instance of which
|
106
|
+
size is (i x j).
|
107
|
+
*/
|
108
|
+
static VALUE r_mpfr_matrix_initialize (int argc, VALUE *argv, VALUE self)
|
109
|
+
{
|
110
|
+
MPFRMatrix *ptr;
|
111
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
112
|
+
r_mpfr_matrix_set_initial_value(ptr, argc, argv);
|
98
113
|
return Qtrue;
|
99
114
|
}
|
100
115
|
|
101
|
-
/* Allocation function for
|
116
|
+
/* Allocation function for MPFR::SquareMatrix. */
|
102
117
|
static VALUE r_mpfr_square_matrix_alloc (VALUE self)
|
103
118
|
{
|
104
119
|
MPFRMatrix *ptr;
|
@@ -106,11 +121,8 @@ static VALUE r_mpfr_square_matrix_alloc (VALUE self)
|
|
106
121
|
return self;
|
107
122
|
}
|
108
123
|
|
109
|
-
|
110
|
-
static VALUE r_mpfr_square_matrix_initialize (VALUE self, VALUE arg)
|
124
|
+
static void r_mpfr_square_matrix_set_initial_value(MPFRMatrix *ptr, VALUE arg)
|
111
125
|
{
|
112
|
-
MPFRMatrix *ptr;
|
113
|
-
r_mpfr_get_matrix_struct(ptr, self);
|
114
126
|
int row, column, i, j;
|
115
127
|
VALUE row_ary;
|
116
128
|
switch(TYPE(arg)){
|
@@ -122,14 +134,14 @@ static VALUE r_mpfr_square_matrix_initialize (VALUE self, VALUE arg)
|
|
122
134
|
for (i = 0; i < row; i++) {
|
123
135
|
row_ary = rb_ary_entry(arg, i);
|
124
136
|
if (column != RARRAY_LEN(row_ary)) {
|
125
|
-
rb_raise(rb_eArgError, "
|
137
|
+
rb_raise(rb_eArgError, "MPFR::Matrix.new needs Array which has arrays of same sizes.");
|
126
138
|
}
|
127
139
|
for (j = 0; j < column; j++) {
|
128
140
|
r_mpfr_set_robj(ptr->data + i + j * row, rb_ary_entry(row_ary, j), GMP_RNDN);
|
129
141
|
}
|
130
142
|
}
|
131
143
|
}else{
|
132
|
-
rb_raise(rb_eArgError, "
|
144
|
+
rb_raise(rb_eArgError, "MPFR::SquareMatrix.new needs two dimensinal Array which has the same sizes of row and column.");
|
133
145
|
}
|
134
146
|
break;
|
135
147
|
case T_FIXNUM:
|
@@ -138,9 +150,27 @@ static VALUE r_mpfr_square_matrix_initialize (VALUE self, VALUE arg)
|
|
138
150
|
mpfr_matrix_set_zeros(ptr);
|
139
151
|
break;
|
140
152
|
default:
|
141
|
-
rb_raise(rb_eArgError, "
|
153
|
+
rb_raise(rb_eArgError, "MPFR::SquareMatrix.new needs Array or Fixnum.");
|
142
154
|
break;
|
143
155
|
}
|
156
|
+
}
|
157
|
+
|
158
|
+
/* Return new MPFR::SquareMatrix instance. The same arguments as MPFR::SquareMatrix.new is acceptable. */
|
159
|
+
static VALUE r_mpfr_square_matrix_global_new(int argc, VALUE arg)
|
160
|
+
{
|
161
|
+
MPFRMatrix *ptr;
|
162
|
+
VALUE val;
|
163
|
+
r_mpfr_make_matrix_struct(val, ptr);
|
164
|
+
r_mpfr_square_matrix_set_initial_value(ptr, arg);
|
165
|
+
return val;
|
166
|
+
}
|
167
|
+
|
168
|
+
/* Initialization function for MPFR::SquareMatrix. */
|
169
|
+
static VALUE r_mpfr_square_matrix_initialize (VALUE self, VALUE arg)
|
170
|
+
{
|
171
|
+
MPFRMatrix *ptr;
|
172
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
173
|
+
r_mpfr_square_matrix_set_initial_value(ptr, arg);
|
144
174
|
return Qtrue;
|
145
175
|
}
|
146
176
|
|
@@ -155,7 +185,7 @@ static VALUE r_mpfr_matrix_initialize_copy (VALUE self, VALUE other)
|
|
155
185
|
return Qtrue;
|
156
186
|
}
|
157
187
|
|
158
|
-
/* Allocation function for
|
188
|
+
/* Allocation function for MPFR::ColumnVector. */
|
159
189
|
static VALUE r_mpfr_col_vector_alloc (VALUE self)
|
160
190
|
{
|
161
191
|
MPFRMatrix *ptr;
|
@@ -163,11 +193,8 @@ static VALUE r_mpfr_col_vector_alloc (VALUE self)
|
|
163
193
|
return self;
|
164
194
|
}
|
165
195
|
|
166
|
-
|
167
|
-
static VALUE r_mpfr_col_vector_initialize (VALUE self, VALUE arg)
|
196
|
+
static void r_mpfr_col_vector_set_initial_value(MPFRMatrix *ptr, VALUE arg)
|
168
197
|
{
|
169
|
-
MPFRMatrix *ptr;
|
170
|
-
r_mpfr_get_matrix_struct(ptr, self);
|
171
198
|
int row, i;
|
172
199
|
switch(TYPE(arg)){
|
173
200
|
case T_ARRAY:
|
@@ -185,13 +212,31 @@ static VALUE r_mpfr_col_vector_initialize (VALUE self, VALUE arg)
|
|
185
212
|
}
|
186
213
|
break;
|
187
214
|
default:
|
188
|
-
rb_raise(rb_eArgError, "
|
215
|
+
rb_raise(rb_eArgError, "MPFR::ColumnVector.new needs Array or Fixnum.");
|
189
216
|
break;
|
190
|
-
}
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
/* Return new MPFR::ColumnVector instance. The same arguments as MPFR::ColumnVector.new is acceptable. */
|
221
|
+
static VALUE r_mpfr_col_vector_global_new(VALUE self, VALUE arg)
|
222
|
+
{
|
223
|
+
MPFRMatrix *ptr;
|
224
|
+
VALUE val;
|
225
|
+
r_mpfr_make_matrix_struct(val, ptr);
|
226
|
+
r_mpfr_col_vector_set_initial_value(ptr, arg);
|
227
|
+
return val;
|
228
|
+
}
|
229
|
+
|
230
|
+
/* Initialization function for MPFR::ColumnVector. */
|
231
|
+
static VALUE r_mpfr_col_vector_initialize (VALUE self, VALUE arg)
|
232
|
+
{
|
233
|
+
MPFRMatrix *ptr;
|
234
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
235
|
+
r_mpfr_col_vector_set_initial_value(ptr, arg);
|
191
236
|
return Qtrue;
|
192
237
|
}
|
193
238
|
|
194
|
-
/* Allocation function for
|
239
|
+
/* Allocation function for MPFR::RowVector. */
|
195
240
|
static VALUE r_mpfr_row_vector_alloc (VALUE self)
|
196
241
|
{
|
197
242
|
MPFRMatrix *ptr;
|
@@ -199,11 +244,8 @@ static VALUE r_mpfr_row_vector_alloc (VALUE self)
|
|
199
244
|
return self;
|
200
245
|
}
|
201
246
|
|
202
|
-
|
203
|
-
static VALUE r_mpfr_row_vector_initialize (VALUE self, VALUE arg)
|
247
|
+
static void r_mpfr_row_vector_set_initial_value(MPFRMatrix *ptr, VALUE arg)
|
204
248
|
{
|
205
|
-
MPFRMatrix *ptr;
|
206
|
-
r_mpfr_get_matrix_struct(ptr, self);
|
207
249
|
int column, i;
|
208
250
|
switch(TYPE(arg)){
|
209
251
|
case T_ARRAY:
|
@@ -221,9 +263,27 @@ static VALUE r_mpfr_row_vector_initialize (VALUE self, VALUE arg)
|
|
221
263
|
}
|
222
264
|
break;
|
223
265
|
default:
|
224
|
-
rb_raise(rb_eArgError, "
|
266
|
+
rb_raise(rb_eArgError, "MPFR::RowVector.new needs Array or Fixnum.");
|
225
267
|
break;
|
226
268
|
}
|
269
|
+
}
|
270
|
+
|
271
|
+
/* Return new MPFR::RowVector instance. The same arguments as MPFR::RowVector.new is acceptable. */
|
272
|
+
static VALUE r_mpfr_row_vector_global_new(int argc, VALUE arg)
|
273
|
+
{
|
274
|
+
MPFRMatrix *ptr;
|
275
|
+
VALUE val;
|
276
|
+
r_mpfr_make_matrix_struct(val, ptr);
|
277
|
+
r_mpfr_row_vector_set_initial_value(ptr, arg);
|
278
|
+
return val;
|
279
|
+
}
|
280
|
+
|
281
|
+
/* Initialization function for MPFR::RowVector. */
|
282
|
+
static VALUE r_mpfr_row_vector_initialize (VALUE self, VALUE arg)
|
283
|
+
{
|
284
|
+
MPFRMatrix *ptr;
|
285
|
+
r_mpfr_get_matrix_struct(ptr, self);
|
286
|
+
r_mpfr_row_vector_set_initial_value(ptr, arg);
|
227
287
|
return Qtrue;
|
228
288
|
}
|
229
289
|
|
@@ -281,7 +341,7 @@ static VALUE r_mpfr_matrix_element (VALUE self, VALUE row, VALUE column)
|
|
281
341
|
}
|
282
342
|
}
|
283
343
|
|
284
|
-
/* Return element at
|
344
|
+
/* Return element at _p1_.*/
|
285
345
|
static VALUE r_mpfr_matrix_at (VALUE self, VALUE arg)
|
286
346
|
{
|
287
347
|
MPFRMatrix *ptr_self;
|
@@ -343,7 +403,7 @@ static VALUE r_mpfr_matrix_each_element_with_index (VALUE self)
|
|
343
403
|
return ret;
|
344
404
|
}
|
345
405
|
|
346
|
-
/* Return array which
|
406
|
+
/* Return one dimensinal array including strings which elements of matrix is converted to by MPFR#to_strf. */
|
347
407
|
static VALUE r_mpfr_matrix_str_ary(VALUE self, VALUE format_str)
|
348
408
|
{
|
349
409
|
MPFRMatrix *ptr_self;
|
@@ -359,6 +419,7 @@ static VALUE r_mpfr_matrix_str_ary(VALUE self, VALUE format_str)
|
|
359
419
|
return rb_ary_new4(ptr_self->size, ret_val);
|
360
420
|
}
|
361
421
|
|
422
|
+
/* Return two dimensinal array including strings which elements of matrix is converted to by MPFR#to_strf. */
|
362
423
|
static VALUE r_mpfr_matrix_str_ary2(VALUE self, VALUE format_str)
|
363
424
|
{
|
364
425
|
MPFRMatrix *ptr_self;
|
@@ -379,6 +440,7 @@ static VALUE r_mpfr_matrix_str_ary2(VALUE self, VALUE format_str)
|
|
379
440
|
return rb_ary_new4(ptr_self->row, ary);
|
380
441
|
}
|
381
442
|
|
443
|
+
/* Retrn two dimensinal array including MPFR elements of matrix. */
|
382
444
|
static VALUE r_mpfr_matrix_to_array(VALUE self)
|
383
445
|
{
|
384
446
|
MPFRMatrix *ptr_self;
|
@@ -391,6 +453,7 @@ static VALUE r_mpfr_matrix_to_array(VALUE self)
|
|
391
453
|
return rb_ary_new4(ptr_self->size, ret_val);
|
392
454
|
}
|
393
455
|
|
456
|
+
/* Retrn one dimensinal array including MPFR elements of matrix. */
|
394
457
|
static VALUE r_mpfr_matrix_to_array2(VALUE self)
|
395
458
|
{
|
396
459
|
MPFRMatrix *ptr_self;
|
@@ -408,6 +471,7 @@ static VALUE r_mpfr_matrix_to_array2(VALUE self)
|
|
408
471
|
return rb_ary_new4(ptr_self->row, ary);
|
409
472
|
}
|
410
473
|
|
474
|
+
/* Return _p1_-th row vector. */
|
411
475
|
static VALUE r_mpfr_matrix_row (VALUE self, VALUE arg)
|
412
476
|
{
|
413
477
|
MPFRMatrix *ptr_self, *ptr_ret;
|
@@ -423,6 +487,7 @@ static VALUE r_mpfr_matrix_row (VALUE self, VALUE arg)
|
|
423
487
|
}
|
424
488
|
}
|
425
489
|
|
490
|
+
/* Return _p1_-th column vector. */
|
426
491
|
static VALUE r_mpfr_matrix_column (VALUE self, VALUE arg)
|
427
492
|
{
|
428
493
|
MPFRMatrix *ptr_self, *ptr_ret;
|
@@ -449,7 +514,7 @@ static VALUE r_mpfr_matrix_transpose (VALUE self)
|
|
449
514
|
return ret;
|
450
515
|
}
|
451
516
|
|
452
|
-
/* Transpose self. This method is
|
517
|
+
/* Transpose self. This method is destructive method. */
|
453
518
|
static VALUE r_mpfr_matrix_transpose2 (VALUE self)
|
454
519
|
{
|
455
520
|
MPFRMatrix *ptr_self, tmp;
|
@@ -467,6 +532,7 @@ static VALUE r_mpfr_matrix_transpose2 (VALUE self)
|
|
467
532
|
return self;
|
468
533
|
}
|
469
534
|
|
535
|
+
/* Multiply all elements by -1. This method is non-destructive. */
|
470
536
|
static VALUE r_mpfr_matrix_neg (VALUE self)
|
471
537
|
{
|
472
538
|
MPFRMatrix *ptr_self, *ptr_ret;
|
@@ -493,7 +559,7 @@ static VALUE r_mpfr_matrix_add (VALUE self, VALUE other)
|
|
493
559
|
return ret;
|
494
560
|
}
|
495
561
|
|
496
|
-
/* Return _self_ + _other_ which is
|
562
|
+
/* Return _self_ + _other_ which is MPFR::Matrix. */
|
497
563
|
static VALUE r_mpfr_matrix_add2 (VALUE self, VALUE other)
|
498
564
|
{
|
499
565
|
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
@@ -527,7 +593,7 @@ static VALUE r_mpfr_matrix_sub (VALUE self, VALUE other)
|
|
527
593
|
return ret;
|
528
594
|
}
|
529
595
|
|
530
|
-
/* Return _self_ - _other_ which is
|
596
|
+
/* Return _self_ - _other_ which is MPFR::Matrix. */
|
531
597
|
static VALUE r_mpfr_matrix_sub2 (VALUE self, VALUE other)
|
532
598
|
{
|
533
599
|
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
@@ -544,6 +610,7 @@ static VALUE r_mpfr_matrix_sub2 (VALUE self, VALUE other)
|
|
544
610
|
return ret;
|
545
611
|
}
|
546
612
|
|
613
|
+
/* Regard matrix as vector and return the value of inner product. */
|
547
614
|
static VALUE r_mpfr_vector_inner_product (VALUE self, VALUE arg)
|
548
615
|
{
|
549
616
|
MPFRMatrix *ptr_self, *ptr_arg;
|
@@ -581,7 +648,7 @@ static VALUE r_mpfr_matrix_mul_matrix (VALUE self, VALUE other)
|
|
581
648
|
return ret;
|
582
649
|
}
|
583
650
|
|
584
|
-
/* Return _self_ * _other_ which is
|
651
|
+
/* Return _self_ * _other_ which is MPFR::Matrix. */
|
585
652
|
static VALUE r_mpfr_matrix_mul_matrix2 (VALUE self, VALUE other)
|
586
653
|
{
|
587
654
|
MPFRMatrix *ptr_self, *ptr_other, *ptr_ret;
|
@@ -638,7 +705,7 @@ static VALUE r_mpfr_matrix_div_scalar (VALUE self, VALUE scalar)
|
|
638
705
|
return ret;
|
639
706
|
}
|
640
707
|
|
641
|
-
/*
|
708
|
+
/* Regard matrix as vector and return length of the vector. */
|
642
709
|
static VALUE r_mpfr_matrix_vector_norm (VALUE self)
|
643
710
|
{
|
644
711
|
MPFRMatrix *ptr_self;
|
@@ -670,7 +737,7 @@ static VALUE r_mpfr_square_matrix_dim (VALUE self)
|
|
670
737
|
return INT2FIX(ptr_self->row);
|
671
738
|
}
|
672
739
|
|
673
|
-
/*
|
740
|
+
/* Perform LU decomposition and return [matrix_l, matrix_r, indx]. */
|
674
741
|
static VALUE r_mpfr_square_matrix_lu_decomp (VALUE self)
|
675
742
|
{
|
676
743
|
MPFRMatrix *ptr_self, *ptr_ret_l, *ptr_ret_u;
|
@@ -705,6 +772,7 @@ static VALUE r_mpfr_square_matrix_lu_decomp (VALUE self)
|
|
705
772
|
}
|
706
773
|
}
|
707
774
|
|
775
|
+
/* Return determinant of matrix. */
|
708
776
|
static VALUE r_mpfr_square_matrix_determinant (VALUE self)
|
709
777
|
{
|
710
778
|
MPFRMatrix *ptr_self;
|
@@ -716,6 +784,7 @@ static VALUE r_mpfr_square_matrix_determinant (VALUE self)
|
|
716
784
|
return ret;
|
717
785
|
}
|
718
786
|
|
787
|
+
/* Perform QR decomposition and return [matrix_q, matrix_r]. */
|
719
788
|
static VALUE r_mpfr_square_matrix_qr_decomp (VALUE self)
|
720
789
|
{
|
721
790
|
MPFRMatrix *ptr_self, *ptr_q, *ptr_r;
|
@@ -727,6 +796,7 @@ static VALUE r_mpfr_square_matrix_qr_decomp (VALUE self)
|
|
727
796
|
return rb_ary_new3(2, q, r);
|
728
797
|
}
|
729
798
|
|
799
|
+
/* Return identity matrix. */
|
730
800
|
static VALUE r_mpfr_square_matrix_identity (VALUE self, VALUE size)
|
731
801
|
{
|
732
802
|
VALUE ret;
|
@@ -745,7 +815,7 @@ static VALUE r_mpfr_vector_dim (VALUE self)
|
|
745
815
|
return INT2FIX(ptr_self->size);
|
746
816
|
}
|
747
817
|
|
748
|
-
/*
|
818
|
+
/* Regard matrix as vector and return length of the vector. */
|
749
819
|
static VALUE r_mpfr_vector_abs (VALUE self)
|
750
820
|
{
|
751
821
|
MPFRMatrix *ptr_self;
|
@@ -757,7 +827,7 @@ static VALUE r_mpfr_vector_abs (VALUE self)
|
|
757
827
|
return ret;
|
758
828
|
}
|
759
829
|
|
760
|
-
/* Return element at
|
830
|
+
/* Return element at _p1_.*/
|
761
831
|
static VALUE r_mpfr_vector_element (VALUE self, VALUE arg)
|
762
832
|
{
|
763
833
|
MPFRMatrix *ptr_self;
|
@@ -774,7 +844,7 @@ static VALUE r_mpfr_vector_element (VALUE self, VALUE arg)
|
|
774
844
|
}
|
775
845
|
}
|
776
846
|
|
777
|
-
/* Set
|
847
|
+
/* Set _p2_ to _p1_ th element. */
|
778
848
|
static VALUE r_mpfr_vector_set_element (VALUE self, VALUE arg, VALUE robj)
|
779
849
|
{
|
780
850
|
MPFRMatrix *ptr_self;
|
@@ -818,6 +888,7 @@ static VALUE r_mpfr_vector_each_element_with_index (VALUE self)
|
|
818
888
|
return ret;
|
819
889
|
}
|
820
890
|
|
891
|
+
/* Return distance from _p1_, i.e. sqrt((x1 - x2)^2 + (y1 - y2)^2). */
|
821
892
|
static VALUE r_mpfr_vector_distance_from (VALUE self, VALUE arg)
|
822
893
|
{
|
823
894
|
MPFRMatrix *ptr_self, *ptr_arg;
|
@@ -834,6 +905,7 @@ static VALUE r_mpfr_vector_distance_from (VALUE self, VALUE arg)
|
|
834
905
|
return ret;
|
835
906
|
}
|
836
907
|
|
908
|
+
/* Return midpoint between _self_ and _p1_. */
|
837
909
|
static VALUE r_mpfr_vector_midpoint (VALUE self, VALUE arg)
|
838
910
|
{
|
839
911
|
MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
|
@@ -845,6 +917,7 @@ static VALUE r_mpfr_vector_midpoint (VALUE self, VALUE arg)
|
|
845
917
|
return ret;
|
846
918
|
}
|
847
919
|
|
920
|
+
/* Return dividing point of _self_ and _p1_ by _p2_. */
|
848
921
|
static VALUE r_mpfr_vector_dividing_point (VALUE self, VALUE arg, VALUE div)
|
849
922
|
{
|
850
923
|
MPFRMatrix *ptr_self, *ptr_arg, *ptr_ret;
|
@@ -858,6 +931,7 @@ static VALUE r_mpfr_vector_dividing_point (VALUE self, VALUE arg, VALUE div)
|
|
858
931
|
return ret;
|
859
932
|
}
|
860
933
|
|
934
|
+
/* Return normalized vector of _self_. */
|
861
935
|
static VALUE r_mpfr_vector_normalize (VALUE self)
|
862
936
|
{
|
863
937
|
MPFRMatrix *ptr_self, *ptr_ret;
|
@@ -871,7 +945,7 @@ static VALUE r_mpfr_vector_normalize (VALUE self)
|
|
871
945
|
}
|
872
946
|
}
|
873
947
|
|
874
|
-
/* Return normalized vector of _self_. This method is
|
948
|
+
/* Return normalized vector of _self_. This method is destructive method. */
|
875
949
|
static VALUE r_mpfr_vector_normalize2 (VALUE self)
|
876
950
|
{
|
877
951
|
MPFRMatrix *ptr_self, tmp;
|
@@ -887,6 +961,7 @@ static VALUE r_mpfr_vector_normalize2 (VALUE self)
|
|
887
961
|
return ret;
|
888
962
|
}
|
889
963
|
|
964
|
+
/* Return new vector whose length is _p1_. */
|
890
965
|
static VALUE r_mpfr_vector_set_length (VALUE self, VALUE arg)
|
891
966
|
{
|
892
967
|
MPFRMatrix *ptr_self, *ptr_ret;
|
@@ -910,6 +985,7 @@ static VALUE r_mpfr_vector_set_length (VALUE self, VALUE arg)
|
|
910
985
|
return returned_value;
|
911
986
|
}
|
912
987
|
|
988
|
+
/* Return new vector whose length is _p1_. This method is destructive. */
|
913
989
|
static VALUE r_mpfr_vector_set_length2 (VALUE self, VALUE arg)
|
914
990
|
{
|
915
991
|
MPFRMatrix *ptr_self, *ptr_ret;
|
@@ -938,29 +1014,32 @@ static VALUE r_mpfr_vector_set_length2 (VALUE self, VALUE arg)
|
|
938
1014
|
void Init_matrix()
|
939
1015
|
{
|
940
1016
|
|
941
|
-
VALUE
|
942
|
-
|
1017
|
+
VALUE tmp_r_mpfr_class = rb_define_class("MPFR", rb_cNumeric);
|
1018
|
+
|
943
1019
|
/*
|
944
1020
|
MPFR::Matrix is class in which instances means matrix and
|
945
1021
|
have MPFR instances as elements.
|
1022
|
+
MPFR::SquareMatrix, MPFR::ColumnVector, and MPFR::RowVector are subclass of MPFR::Matrix.
|
946
1023
|
*/
|
1024
|
+
r_mpfr_matrix = rb_define_class_under(tmp_r_mpfr_class, "Matrix", rb_cObject);
|
1025
|
+
r_mpfr_square_matrix = rb_define_class_under(tmp_r_mpfr_class, "SquareMatrix", r_mpfr_matrix);
|
1026
|
+
r_mpfr_col_vector = rb_define_class_under(tmp_r_mpfr_class, "ColumnVector", r_mpfr_matrix);
|
1027
|
+
r_mpfr_row_vector = rb_define_class_under(tmp_r_mpfr_class, "RowVector", r_mpfr_matrix);
|
947
1028
|
|
948
|
-
|
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
|
-
|
1029
|
+
rb_define_singleton_method(tmp_r_mpfr_class, "Matrix", r_mpfr_matrix_global_new, -1);
|
954
1030
|
rb_define_alloc_func(r_mpfr_matrix, r_mpfr_matrix_alloc);
|
955
1031
|
rb_define_private_method(r_mpfr_matrix, "initialize", r_mpfr_matrix_initialize, -1);
|
956
1032
|
rb_define_private_method(r_mpfr_matrix, "initialize_copy", r_mpfr_matrix_initialize_copy, 1);
|
957
1033
|
|
1034
|
+
rb_define_singleton_method(tmp_r_mpfr_class, "SquareMatrix", r_mpfr_square_matrix_global_new, 1);
|
958
1035
|
rb_define_alloc_func(r_mpfr_square_matrix, r_mpfr_square_matrix_alloc);
|
959
1036
|
rb_define_private_method(r_mpfr_square_matrix, "initialize", r_mpfr_square_matrix_initialize, 1);
|
960
1037
|
|
1038
|
+
rb_define_singleton_method(tmp_r_mpfr_class, "ColumnVector", r_mpfr_col_vector_global_new, 1);
|
961
1039
|
rb_define_alloc_func(r_mpfr_col_vector, r_mpfr_col_vector_alloc);
|
962
1040
|
rb_define_private_method(r_mpfr_col_vector, "initialize", r_mpfr_col_vector_initialize, 1);
|
963
1041
|
|
1042
|
+
rb_define_singleton_method(tmp_r_mpfr_class, "RowVector", r_mpfr_row_vector_global_new, 1);
|
964
1043
|
rb_define_alloc_func(r_mpfr_row_vector, r_mpfr_row_vector_alloc);
|
965
1044
|
rb_define_private_method(r_mpfr_row_vector, "initialize", r_mpfr_row_vector_initialize, 1);
|
966
1045
|
|
@@ -1021,8 +1100,11 @@ void Init_matrix()
|
|
1021
1100
|
|
1022
1101
|
rb_define_singleton_method(r_mpfr_square_matrix, "identity", r_mpfr_square_matrix_identity, 1);
|
1023
1102
|
|
1024
|
-
/*
|
1025
|
-
|
1103
|
+
/*
|
1104
|
+
This module add the functionality as vector.
|
1105
|
+
MPFR::ColumnVector and MPFR::RowVector include MPFR::Vector module.
|
1106
|
+
*/
|
1107
|
+
r_mpfr_vector_module = rb_define_module_under(tmp_r_mpfr_class, "Vector");
|
1026
1108
|
|
1027
1109
|
rb_define_method(r_mpfr_vector_module, "[]=", r_mpfr_vector_set_element, 2);
|
1028
1110
|
rb_define_method(r_mpfr_vector_module, "[]", r_mpfr_vector_element, 1);
|
data/lib/mpfr/version.rb
CHANGED
data/ruby-mpfr.gemspec
CHANGED
@@ -2,24 +2,26 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{ruby-mpfr}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.5"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Takayuki YAMAGUCHI"]
|
9
|
-
s.date = %q{2009-12-
|
9
|
+
s.date = %q{2009-12-19}
|
10
10
|
s.description = %q{ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for
|
11
|
-
multiple-precision floating-point computations.
|
11
|
+
multiple-precision floating-point computations.
|
12
|
+
ruby-mpfr also includes Matrix class of MPFR; MPFR::Matrix, MPFR::SquareMatrix,
|
13
|
+
MPFR::ColumnVector, and MPFR::RowVector.}
|
12
14
|
s.email = ["d@ytak.info"]
|
13
15
|
s.extensions = ["ext/mpfr/extconf.rb", "ext/mpfr_matrix/mpfr/extconf.rb"]
|
14
16
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc"]
|
15
17
|
s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "ext/mpfr/extconf.rb", "ext/mpfr/ruby_mpfr.c", "ext/mpfr/ruby_mpfr.h", "ext/mpfr_matrix/mpfr/extconf.rb", "ext/mpfr_matrix/mpfr/func_mpfr_matrix.c", "ext/mpfr_matrix/mpfr/func_mpfr_matrix.h", "ext/mpfr_matrix/mpfr/ruby_mpfr.h", "ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c", "ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.h", "lib/mpfr/matrix.rb", "lib/mpfr/version.rb", "ruby-mpfr.gemspec", "script/console", "script/destroy", "script/generate", "spec/mpfr/allocate_spec.rb", "spec/mpfr/arithmetic_spec.rb", "spec/mpfr/comparison_spec.rb", "spec/mpfr/constant_spec.rb", "spec/mpfr/conversion_spec.rb", "spec/mpfr/exception_spec.rb", "spec/mpfr/functions_spec.rb", "spec/mpfr/generate_number_modulue.rb", "spec/mpfr/precision_roundmode_spec.rb", "spec/mpfr/rounding_spec.rb", "spec/mpfr/set_value_spec.rb", "spec/mpfr/spec_helper.rb", "spec/mpfr/string_spec.rb", "spec/mpfr_matrix/generate_matrix_arguments.rb", "spec/mpfr_matrix/mpfr_matrix_alloc_spec.rb", "spec/mpfr_matrix/mpfr_matrix_arithmetic_spec.rb", "spec/mpfr_matrix/mpfr_matrix_set_element_spec.rb", "spec/mpfr_matrix/mpfr_matrix_string_spec.rb", "spec/mpfr_matrix/mpfr_square_matrix_spec.rb", "spec/mpfr_matrix/spec_helper.rb", "spec/spec.opts", "tasks/extconf.rake"]
|
16
|
-
s.homepage = %q{http://rubyforge.org/projects/ruby-mpfr/}
|
18
|
+
s.homepage = %q{[URL] http://rubyforge.org/projects/ruby-mpfr/}
|
17
19
|
s.post_install_message = %q{PostInstall.txt}
|
18
20
|
s.rdoc_options = ["--main", "README.rdoc"]
|
19
21
|
s.require_paths = ["lib", "ext"]
|
20
22
|
s.rubyforge_project = %q{ruby-mpfr}
|
21
23
|
s.rubygems_version = %q{1.3.5}
|
22
|
-
s.summary = %q{ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for multiple-precision floating-point computations
|
24
|
+
s.summary = %q{ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for multiple-precision floating-point computations}
|
23
25
|
|
24
26
|
if s.respond_to? :specification_version then
|
25
27
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/spec/mpfr/allocate_spec.rb
CHANGED
@@ -10,6 +10,19 @@ describe MPFR, 'when allocating objects from integer' do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
it "should equal bignum" do
|
14
|
+
MPFR.set_default_prec(120)
|
15
|
+
GenerateNumber.float(100) do |f|
|
16
|
+
MPFR.new(f).should == MPFR(f)
|
17
|
+
end
|
18
|
+
|
19
|
+
GenerateNumber.float(100) do |f|
|
20
|
+
str = f.to_s
|
21
|
+
MPFR.new(str).should == MPFR(str)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
13
26
|
it "should allocate from string" do
|
14
27
|
MPFR.set_default_prec(53)
|
15
28
|
for i in 0..1000
|
@@ -7,6 +7,12 @@ describe "initialization of matrix" do
|
|
7
7
|
@matrixes = @sizes.map{ |a| MPFR::Matrix.new(*a) }
|
8
8
|
end
|
9
9
|
|
10
|
+
it "should be same as MPFR::Matrix method" do
|
11
|
+
@matrixes.each_with_index do |a, i|
|
12
|
+
MPFR::Matrix(*@sizes[i]).should == a
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
10
16
|
it "should has size which equals size of array." do
|
11
17
|
@matrixes.each_with_index do |a, i|
|
12
18
|
a.size.should == (@sizes[i][0] * @sizes[i][1])
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-mpfr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takayuki YAMAGUCHI
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-19 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -25,6 +25,8 @@ dependencies:
|
|
25
25
|
description: |-
|
26
26
|
ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for
|
27
27
|
multiple-precision floating-point computations.
|
28
|
+
ruby-mpfr also includes Matrix class of MPFR; MPFR::Matrix, MPFR::SquareMatrix,
|
29
|
+
MPFR::ColumnVector, and MPFR::RowVector.
|
28
30
|
email:
|
29
31
|
- d@ytak.info
|
30
32
|
executables: []
|
@@ -109,6 +111,6 @@ rubyforge_project: ruby-mpfr
|
|
109
111
|
rubygems_version: 1.3.5
|
110
112
|
signing_key:
|
111
113
|
specification_version: 3
|
112
|
-
summary: ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for multiple-precision floating-point computations
|
114
|
+
summary: ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for multiple-precision floating-point computations
|
113
115
|
test_files: []
|
114
116
|
|