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,13 @@
1
+ #ifndef RUBY_MPFI_MATRIX
2
+ #define RUBY_MPFI_MATRIX
3
+
4
+ #include "func_mpfi_matrix.h"
5
+
6
+ VALUE r_mpfi_matrix, r_mpfi_square_matrix, r_mpfi_col_vector, r_mpfi_row_vector, r_mpfi_vector_module;
7
+
8
+ void r_mpfi_matrix_free(void *ptr);
9
+
10
+ void r_mpfi_matrix_suitable_matrix_init (VALUE *other, MPFIMatrix **ptr_other, int row, int column);
11
+ VALUE r_mpfi_matrix_robj(MPFIMatrix *x);
12
+
13
+ #endif
@@ -0,0 +1,38 @@
1
+ #ifndef _RUBY_MPFR_H_
2
+ #define _RUBY_MPFR_H_
3
+
4
+ #include <ruby.h>
5
+ #include <mpfr.h>
6
+ #include <stdio.h>
7
+
8
+ typedef __mpfr_struct MPFR;
9
+
10
+ VALUE r_mpfr_class, r_mpfr_math;
11
+
12
+ #define r_mpfr_make_struct(ruby_var, c_var) { ruby_var = Data_Make_Struct(r_mpfr_class, MPFR, 0, r_mpfr_free, c_var); }
13
+ #define r_mpfr_make_struct_init(ruby_var, c_var) { r_mpfr_make_struct(ruby_var, c_var); mpfr_init(c_var); }
14
+ #define r_mpfr_make_struct_init2(ruby_var, c_var, prec) { r_mpfr_make_struct(ruby_var, c_var); mpfr_init2(c_var, prec); }
15
+ #define r_mpfr_get_struct(c_var, ruby_var) { Data_Get_Struct(ruby_var, MPFR, c_var); }
16
+
17
+ #define r_mpfr_temp_alloc(var) { var=ALLOC_N(MPFR, 1); }
18
+ #define r_mpfr_temp_alloc_init(var) { r_mpfr_temp_alloc(var); mpfr_init(var); }
19
+ #define r_mpfr_temp_alloc_init2(var, prec) { r_mpfr_temp_alloc(var); mpfr_init2(var, prec); }
20
+ #define r_mpfr_temp_free(var) { mpfr_clear(var); free(var); }
21
+
22
+ #define r_mpfr_check_number(c_val) { if(mpfr_number_p(c_val) == 0) rb_raise(rb_eArgError, "Not an ordinary number."); }
23
+ #define r_mpfr_check_positive_number(c_val) { if(mpfr_number_p(c_val) == 0 && mpfr_sgn(c_val) <= 0) rb_raise(rb_eArgError, "Not a positive number."); }
24
+ #define r_mpfr_check_non_negative_number(c_val) { if(mpfr_number_p(c_val) == 0 && mpfr_sgn(c_val) < 0) rb_raise(rb_eArgError, "Not a non negative number."); }
25
+ #define r_mpfr_check_negative_number(c_val) { if(mpfr_number_p(c_val) == 0 && mpfr_sgn(c_val) >= 0) rb_raise(rb_eArgError, "Not a negative number."); }
26
+ #define r_mpfr_check_non_positive_number(c_val) { if(mpfr_number_p(c_val) == 0 && mpfr_sgn(c_val) > 0) rb_raise(rb_eArgError, "Not a non positive number."); }
27
+
28
+ void r_mpfr_free(void *ptr);
29
+ VALUE r_mpfr_new_fr_obj(VALUE obj);
30
+ void r_mpfr_set_robj(MPFR *ptr, VALUE obj, mp_rnd_t rnd);
31
+
32
+ mp_rnd_t r_mpfr_rnd_from_value(VALUE rnd);
33
+ mp_rnd_t r_mpfr_rnd_from_optional_argument(int min, int max, int argc, VALUE *argv);
34
+ mp_rnd_t r_mpfr_prec_from_optional_argument(int min, int max, int argc, VALUE *argv);
35
+ void r_mpfr_get_rnd_prec_from_optional_arguments(mp_rnd_t *rnd, mp_prec_t *prec, int min, int max, int argc, VALUE *argv);
36
+
37
+ #endif /* _RUBY_MPFR_H_ */
38
+
@@ -0,0 +1,13 @@
1
+ #ifndef RUBY_GMP_MATRIX
2
+ #define RUBY_GMP_MATRIX
3
+
4
+ #include "func_mpfr_matrix.h"
5
+
6
+ VALUE r_mpfr_matrix, r_mpfr_square_matrix, r_mpfr_col_vector, r_mpfr_row_vector, r_mpfr_vector_module;
7
+
8
+ void r_mpfr_matrix_free(void *ptr);
9
+
10
+ void r_mpfr_matrix_suitable_matrix_init (VALUE *other, MPFRMatrix **ptr_other, int row, int column);
11
+ VALUE r_mpfr_matrix_robj(MPFRMatrix *x);
12
+
13
+ #endif
@@ -0,0 +1,188 @@
1
+ require 'mpfi/matrix.so'
2
+
3
+ class MPFI
4
+ class Matrix
5
+ def inspect
6
+ tmp = str_ary_for_inspect2
7
+ tmp.map!{ |a| "['" + a.join("', '") + "']"}
8
+ sprintf("#<%s:%x, [%s]>", self.class, __id__, tmp.join(', '))
9
+ end
10
+
11
+ def pretty_print(pp)
12
+ ary = str_ary_for_inspect2
13
+ pp.object_group(self) do
14
+ pp.text(sprintf(':%x, ', __id__))
15
+ pp.breakable
16
+ pp.text("[")
17
+ pp.nest(1) do
18
+ for j in 0...row_size
19
+ pp.breakable if j > 0
20
+ pp.text(%|["#{ary[j][0]}"|)
21
+ pp.nest(1) do
22
+ for i in 1...column_size
23
+ pp.comma_breakable
24
+ pp.text(%|"#{ary[j][i]}"|)
25
+ end
26
+ end
27
+ pp.text("]")
28
+ end
29
+ pp.text("]")
30
+ end
31
+ end
32
+ end
33
+
34
+ def to_s(delimiter = ' ')
35
+ str_ary_for_inspect.join(delimiter)
36
+ end
37
+
38
+ def self.create(a)
39
+ case a
40
+ when MPFI::Vector
41
+ if self === a
42
+ a.dup
43
+ else
44
+ self.new(a.to_a)
45
+ end
46
+ when MPFI::Matrix
47
+ if a.column_size == a.row_size
48
+ if MPFI::SquareMatrix === a
49
+ a.dup
50
+ else
51
+ MPFI::SquareMatrix.new(a.to_a)
52
+ end
53
+ else
54
+ a.dup
55
+ end
56
+ when Array
57
+ if Array == a[0] && a.size == a[0].size
58
+ MPFI::SquareMatrix.new(a)
59
+ else
60
+ self.new(a)
61
+ end
62
+ else
63
+ self.new(a)
64
+ end
65
+
66
+ end
67
+
68
+ # ary is two-dimensional Array.
69
+ def self.suitable_matrix_from_ary(ary)
70
+ if ary.size == 1
71
+ RowVector.new(ary[0])
72
+ elsif ary[0].size == 1
73
+ ColumnVector.new(ary.inject([]){ |res, val| res << val[0] })
74
+ elsif ary[0].size == ary.size
75
+ SquareMatrix.new(ary)
76
+ else
77
+ Matrix.new(ary)
78
+ end
79
+ end
80
+
81
+ def self.interval(ary)
82
+ if Array === ary && ary.all?{ |a| Array === a }
83
+ row = ary.size
84
+ column = ary[0].size
85
+ if ary.all?{ |a| a.size == column }
86
+ ret = self.new(row, column)
87
+ (0...row).each do |i|
88
+ (0...column).each do |j|
89
+ case ary[i][j]
90
+ when Array
91
+ ret[i, j] = MPFI.interval(*ary[i][j])
92
+ when String
93
+ ret[i, j] = MPFI.interval(*(ary[i][j].split))
94
+ when MPFI
95
+ ret[i, j] = ary[i][j]
96
+ else
97
+ raise ArgumentError, "Invalid class for argument"
98
+ end
99
+ end
100
+ end
101
+ else
102
+ ret = nil
103
+ end
104
+ end
105
+ ret
106
+ end
107
+
108
+ end
109
+
110
+ module Vector
111
+ def inspect
112
+ sprintf("#<%s:%x, ['%s']>", self.class, __id__, str_ary_for_inspect.join("', '"))
113
+ end
114
+
115
+ def pretty_print(pp)
116
+ ary = str_ary_for_inspect
117
+ pp.object_group(self) do
118
+ pp.text(sprintf(':%x, ', __id__))
119
+ pp.breakable
120
+ pp.text(%|["#{ary[0]}"|)
121
+ pp.nest(1) do
122
+ for i in 1...dim
123
+ pp.comma_breakable
124
+ pp.text(%|"#{ary[i]}"|)
125
+ end
126
+ end
127
+ pp.text("]")
128
+ end
129
+ end
130
+
131
+ def to_s(delimiter = ' ')
132
+ str_ary_for_inspect.join(delimiter)
133
+ end
134
+
135
+ def subdivision_by_size(size)
136
+ division = []
137
+ each { |el| division << el.subdivision((el.diam_abs / size).ceil) }
138
+ ary_size = division.inject(1) { |mul, a| mul * a.size }
139
+ ary = Array.new(ary_size) { Array.new }
140
+ num = ary_size
141
+ division.each do |div|
142
+ j = 0
143
+ repeat = ary_size / num
144
+ num = num / div.size
145
+ repeat.times do
146
+ div.size.times do |i|
147
+ num.times do
148
+ ary[j] << div[i]
149
+ j += 1
150
+ end
151
+ end
152
+ end
153
+ end
154
+ ary.map! { |a| self.class.new(a) }
155
+ end
156
+
157
+ def self.inner_product(a, b)
158
+ a.inner_product(b)
159
+ end
160
+
161
+ def self.distance(a, b)
162
+ a.distance_from(b)
163
+ end
164
+
165
+ end
166
+
167
+ class ColumnVector
168
+ def self.interval(ary)
169
+ if Array === ary && ary.all?{ |a| Array === a }
170
+ self.new(ary.map { |a| MPFI.interval(*a) })
171
+ else
172
+ nil
173
+ end
174
+ end
175
+ end
176
+
177
+ class RowVector
178
+ def self.interval(ary)
179
+ if Array === ary && ary.all?{ |a| Array === a }
180
+ self.new(ary.map { |a| MPFI.interval(*a) })
181
+ else
182
+ nil
183
+ end
184
+ end
185
+ end
186
+
187
+ end
188
+
@@ -0,0 +1,3 @@
1
+ module MPFI
2
+ VERSION = '0.0.2'
3
+ end
data/ruby-mpfi.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ruby-mpfi}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Takayuki YAMAGUCHI"]
9
+ s.date = %q{2009-12-17}
10
+ s.description = %q{FIX (describe your package)}
11
+ s.email = ["d@ytak.info"]
12
+ s.extensions = ["ext/mpfi/extconf.rb", "ext/mpfi_complex/mpfi/extconf.rb", "ext/mpfi_matrix/mpfi/extconf.rb"]
13
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt"]
14
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "ext/mpfi/extconf.rb", "ext/mpfi/func_mpfi_extention.c", "ext/mpfi/func_mpfi_extention.h", "ext/mpfi/make_c_source.rb", "ext/mpfi/ruby_mpfi.c", "ext/mpfi/ruby_mpfi.h", "ext/mpfi/ruby_mpfr.h", "ext/mpfi/yasnippet_mpfi.el", "ext/mpfi_complex/mpfi/extconf.rb", "ext/mpfi_complex/mpfi/func_mpfi_extention.h", "ext/mpfi_complex/mpfi/func_ruby_mpfi_complex.c", "ext/mpfi_complex/mpfi/func_ruby_mpfi_complex.h", "ext/mpfi_complex/mpfi/ruby_mpfi.h", "ext/mpfi_complex/mpfi/ruby_mpfi_complex.c", "ext/mpfi_complex/mpfi/ruby_mpfi_complex.h", "ext/mpfi_complex/mpfi/ruby_mpfr.h", "ext/mpfi_matrix/mpfi/extconf.rb", "ext/mpfi_matrix/mpfi/func_mpfi_extention.h", "ext/mpfi_matrix/mpfi/func_mpfi_matrix.c", "ext/mpfi_matrix/mpfi/func_mpfi_matrix.h", "ext/mpfi_matrix/mpfi/func_mpfr_matrix.h", "ext/mpfi_matrix/mpfi/func_ruby_mpfi_complex.h", "ext/mpfi_matrix/mpfi/ruby_mpfi.h", "ext/mpfi_matrix/mpfi/ruby_mpfi_complex.h", "ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c", "ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.h", "ext/mpfi_matrix/mpfi/ruby_mpfr.h", "ext/mpfi_matrix/mpfi/ruby_mpfr_matrix.h", "lib/mpfi/matrix.rb", "lib/mpfi/version.rb", "ruby-mpfi.gemspec", "script/console", "script/destroy", "script/generate", "spec/mpfi/generate_number_module.rb", "spec/mpfi/mpfi_alloc_spec.rb", "spec/mpfi/mpfi_diam_arithmetic_spec.rb", "spec/mpfi/mpfi_interval_arithmetic_spec.rb", "spec/mpfi/mpfi_interval_functions_spec.rb", "spec/mpfi/mpfi_math_functions_spec.rb", "spec/mpfi/mpfi_set_operation_spec.rb", "spec/mpfi/ruby-mpfi_spec.rb", "spec/mpfi/spec_helper.rb", "spec/mpfi_complex/spec_helper.rb", "spec/mpfi_matrix/generate_matrix_arguments.rb", "spec/mpfi_matrix/mpfi_matrix_alloc_spec.rb", "spec/mpfi_matrix/mpfi_matrix_arithmetic_spec.rb", "spec/mpfi_matrix/mpfi_matrix_interval_func_spec.rb", "spec/mpfi_matrix/mpfi_matrix_set_element_spec.rb", "spec/mpfi_matrix/mpfi_matrix_set_operation_spec.rb", "spec/mpfi_matrix/mpfi_matrix_string_spec.rb", "spec/mpfi_matrix/mpfi_matrix_subdivision_spec.rb", "spec/mpfi_matrix/mpfi_square_matrix_spec.rb", "spec/mpfi_matrix/mpfi_vector_spec.rb", "spec/mpfi_matrix/spec_helper.rb", "spec/spec.opts", "tasks/extconf.rake"]
15
+ s.homepage = %q{http://github.com/#{github_username}/#{project_name}}
16
+ s.post_install_message = %q{PostInstall.txt}
17
+ s.rdoc_options = ["--main", "README.rdoc"]
18
+ s.require_paths = ["lib", "ext"]
19
+ s.rubyforge_project = %q{ruby-mpfi}
20
+ s.rubygems_version = %q{1.3.5}
21
+ s.summary = %q{FIX (describe your package)}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_development_dependency(%q<hoe>, [">= 2.4.0"])
29
+ else
30
+ s.add_dependency(%q<hoe>, [">= 2.4.0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<hoe>, [">= 2.4.0"])
34
+ end
35
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/ruby-mpfi.rb'}"
9
+ puts "Loading ruby-mpfi gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,48 @@
1
+ module GenerateNumber
2
+
3
+ def self.interval_float_arguments(number, max = 100)
4
+ ret = Array.new(number){ |j| self.float(2).sort }
5
+ end
6
+
7
+ def self.float_arguments(size, number, max = 100)
8
+ ret = Array.new(number){ |j| Array.new(size){ |i| rand(max) - rand } }
9
+ for i in 0...(ret.size)
10
+ case i % 4
11
+ when 1
12
+ ret[i].map!{ |a| -a }
13
+ when 2, 3
14
+ ret[i].map!{ |a| (rand > 0.5 ? -a : a) }
15
+ end
16
+ end
17
+ ret
18
+ end
19
+
20
+ def self.float(num, max = 100, &block)
21
+ if block_given?
22
+ for i in 0...num
23
+ a = rand(max) - rand
24
+ a = -a if rand > 0.5
25
+ yield(a)
26
+ end
27
+ else
28
+ ary = Array.new(num){ |i| rand(max) - rand }
29
+ ary.map!{ |a| (rand > 0.5 ? -a : a) }
30
+ end
31
+ end
32
+
33
+ def self.string(number)
34
+ prec = MPFR.get_default_prec
35
+ max = 2 ** prec
36
+ Array.new(number) do |i|
37
+ sign = ((rand > 0.5 ? '-' : ''))
38
+ "#{sign}#{rand(max)}.#{rand(max)}"
39
+ end
40
+ end
41
+
42
+ def self.mpfr_args(number)
43
+ ret = self.string(number)
44
+ ret.map!{ |a| MPFR.new(a) }
45
+ end
46
+
47
+
48
+ end
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'mpfr'
4
+ require 'mpfi'
5
+ require "#{File.dirname(File.expand_path(__FILE__))}/generate_number_module.rb"
6
+
7
+ describe MPFI, "when initializing MPFI instance" do
8
+ before(:all) do
9
+ @prec = 150
10
+ MPFR.set_default_prec(@prec)
11
+ @srcs = GenerateNumber.float(1000) + GenerateNumber.float(1000).map!{ |a| MPFR.new(a.to_s) }
12
+ @args = @srcs.map{ |a| MPFI.new(a) }
13
+ end
14
+
15
+ it "should include source number and has endpoints which is MPFR" do
16
+ @args.each_with_index do |a, i|
17
+ a.get_prec.should eql @prec
18
+ endpoints = a.endpoints
19
+ endpoints.each do |b|
20
+ b.should be_an_instance_of MPFR
21
+ b.get_prec.should eql @prec
22
+ end
23
+ endpoints[0].should <= @srcs[i]
24
+ endpoints[1].should >= @srcs[i]
25
+ a.include?(@srcs[i]).should be_true
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ describe MPFI, "when initializing MPFI instance by method interval" do
32
+ before(:all) do
33
+ @prec = 130
34
+ MPFR.set_default_prec(@prec)
35
+ @srcs = GenerateNumber.interval_float_arguments(1000)
36
+ @srcs += GenerateNumber.interval_float_arguments(1000).map!{ |a| a.map!{ |b| MPFR.new(b.to_s) }}
37
+ @args = @srcs.map { |a| MPFI.interval(*a) }
38
+ end
39
+
40
+ it "should include source numbers and has endpoints which is MPFR" do
41
+ @args.each_with_index do |a, i|
42
+ a.get_prec.should eql @prec
43
+ endpoints = a.endpoints
44
+ endpoints.each do |b|
45
+ b.should be_an_instance_of MPFR
46
+ b.get_prec.should eql @prec
47
+ end
48
+ endpoints[0].should <= @srcs[i][0]
49
+ endpoints[1].should >= @srcs[i][1]
50
+ @srcs[i].each { |b| a.include?(b).should be_true }
51
+ end
52
+ end
53
+
54
+ end
55
+
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'mpfr'
4
+ require 'mpfi'
5
+ require "#{File.dirname(File.expand_path(__FILE__))}/generate_number_module.rb"
6
+
7
+ describe MPFI, "when puting arithmetic operation" do
8
+ before(:all) do
9
+ MPFR.set_default_prec(150)
10
+ @args = GenerateNumber.float(5000).map{ |a| MPFI.new(a) }
11
+ end
12
+
13
+ it "should increase diameter" do
14
+ @args.each_index do |i|
15
+ if i > 0
16
+ diam_sum = @args[i-1].diam + @args[i].diam
17
+ add = @args[i-1] + @args[i]
18
+ sub = @args[i-1] - @args[i]
19
+ add.diam.should >= diam_sum
20
+ sub.diam.should >= diam_sum
21
+ end
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,105 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'mpfr'
4
+ require 'mpfi'
5
+ require "#{File.dirname(File.expand_path(__FILE__))}/generate_number_module.rb"
6
+
7
+ #
8
+ # To test method +, -, / and *, we calculate these operator for MPFR and MPFI
9
+ # and check whether resultant MPFI instance includes MPFR instance.
10
+ # Then, this program make MPFI instance to use MPFI.new and MPFI.interval.
11
+ #
12
+
13
+ def check_arithmetic_op_include(arg1, arg2, &block)
14
+ mpfr_res = yield(MPFR.new(arg1), MPFR.new(arg2))
15
+ mpfi_res = yield(MPFI.new(arg1), MPFI.new(arg2))
16
+ mpfi_res.include?(mpfr_res)
17
+ end
18
+
19
+ def check_arithmetic_op_include2(arg1, arg2, error, &block)
20
+ ary = [MPFR.new(arg1), MPFR.new(arg2)]
21
+ mpfr_res = yield(ary[0], ary[1])
22
+ mpfi_res = yield(MPFI.interval(ary[0] - error, ary[0] + error),
23
+ MPFI.interval(ary[1] - error, ary[1] + error))
24
+ mpfi_res.include?(mpfr_res)
25
+ end
26
+
27
+ describe MPFI, "when applying arithmetic operations" do
28
+ before(:all) do
29
+ MPFR.set_default_prec(100)
30
+ @args = GenerateNumber.float(5000)
31
+ @error = MPFR.new('1e-20')
32
+ end
33
+
34
+ it "should be include" do
35
+ @args.each_index do |i|
36
+ if i > 0
37
+ (check_arithmetic_op_include(@args[i-1], @args[i]) { |a, b| a + b }).should be_true
38
+ (check_arithmetic_op_include(@args[i-1], @args[i]) { |a, b| a - b }).should be_true
39
+ (check_arithmetic_op_include(@args[i-1], @args[i]) { |a, b| a * b }).should be_true
40
+ (check_arithmetic_op_include(@args[i-1], @args[i]) { |a, b| a / b }).should be_true
41
+
42
+ (check_arithmetic_op_include2(@args[i-1], @args[i], @error) { |a, b| a + b }).should be_true
43
+ (check_arithmetic_op_include2(@args[i-1], @args[i], @error) { |a, b| a - b }).should be_true
44
+ (check_arithmetic_op_include2(@args[i-1], @args[i], @error) { |a, b| a * b }).should be_true
45
+ (check_arithmetic_op_include2(@args[i-1], @args[i], @error) { |a, b| a / b }).should be_true
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ # It is assumed that op is :add, :sub, :mul or :div.
53
+ def arithmetic_op_with_mpfr(arg_r, arg_i, op, &block)
54
+ r = MPFR.new(arg_r)
55
+ i = MPFI.new(arg_i)
56
+ res = MPFI::Math.send(op, i, r)
57
+ res.should be_an_instance_of MPFI
58
+ res.left.should eql MPFR::Math.send(op, i.left, r, MPFR::RNDD)
59
+ res.right.should eql MPFR::Math.send(op, i.right, r, MPFR::RNDU)
60
+ end
61
+
62
+ describe MPFI, "when applying arithmetic operations with MPFR" do
63
+ before(:all) do
64
+ MPFR.set_default_prec(200)
65
+ @args = GenerateNumber.float(5000)
66
+ end
67
+
68
+ it "should be include" do
69
+ @args.each_index do |i|
70
+ if i > 0
71
+ arithmetic_op_with_mpfr(@args[i-1], @args[i], :add)
72
+ arithmetic_op_with_mpfr(@args[i-1], @args[i], :sub)
73
+ arithmetic_op_with_mpfr(@args[i-1], @args[i], :mul)
74
+ arithmetic_op_with_mpfr(@args[i-1], @args[i], :div)
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ # op must be :add or :sub.
82
+ def arithmetic_op_with_mpfr2(arg_r, arg_i, op, &block)
83
+ r = MPFR.new(arg_r)
84
+ i = MPFI.interval(*arg_i)
85
+ res = MPFI::Math.send(op, i, r)
86
+ res.should be_an_instance_of MPFI
87
+ res.left.should eql MPFR::Math.send(op, i.left, r, MPFR::RNDD)
88
+ res.right.should eql MPFR::Math.send(op, i.right, r, MPFR::RNDU)
89
+ res.diam_abs.should eql i.diam_abs
90
+ end
91
+
92
+ describe MPFI, "when applying addition or subtraction with MPFR" do
93
+ before(:all) do
94
+ MPFR.set_default_prec(200)
95
+ @args_r = GenerateNumber.float(5000)
96
+ @args_i = GenerateNumber.interval_float_arguments(5000)
97
+ end
98
+
99
+ it "should be include" do
100
+ @args_r.each_index do |i|
101
+ arithmetic_op_with_mpfr2(@args_r[i], @args_i[i], :add)
102
+ arithmetic_op_with_mpfr2(@args_r[i], @args_i[i], :sub)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,95 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'mpfr'
4
+ require 'mpfi'
5
+ require "#{File.dirname(File.expand_path(__FILE__))}/generate_number_module.rb"
6
+
7
+ #
8
+ # The following is test for interval functions and endpoints.
9
+ #
10
+
11
+ describe MPFI, "when executing interval functions" do
12
+ before(:all) do
13
+ MPFR.set_default_prec(140)
14
+ @args = GenerateNumber.float(1000).map{ |a| MPFI.new(a.to_s) }
15
+ end
16
+
17
+ it "should be equal the distance between endpoints" do
18
+ @args.each { |a| a.diam_abs.should >= (a.right - a.left) }
19
+ end
20
+
21
+ it "should be equal midpoint" do
22
+ @args.each { |a| a.mid.should == ((a.right + a.left) / 2) }
23
+ end
24
+
25
+ it "should include midpoint" do
26
+ @args.each { |a| a.mid_interval.include?(a.mid).should be_true }
27
+ end
28
+
29
+ it "should be include self.alea" do
30
+ @args.each { |a| a.include?(a.alea).should be_true }
31
+ end
32
+
33
+ end
34
+
35
+ describe MPFI, "when getting endpoints of interval" do
36
+ before(:all) do
37
+ MPFR.set_default_prec(200)
38
+ @args = GenerateNumber.float(1000).map{ |a| MPFI.new(a.to_s) }
39
+ end
40
+
41
+ it "should include two endpoints" do
42
+ @args.each do |a|
43
+ a.include?(a.left).should be_true
44
+ a.include?(a.right).should be_true
45
+ end
46
+ end
47
+
48
+ it "should not strictly include interval which consists of one endpoint" do
49
+ @args.each do |a|
50
+ a.strictly_include?(MPFI.new(a.left)).should be_nil
51
+ a.strictly_include?(MPFI.new(a.right)).should be_nil
52
+ end
53
+ end
54
+
55
+ it "should equal [left, right]." do
56
+ @args.each do |a|
57
+ epts = a.endpoints
58
+ a.left.should eql epts[0]
59
+ a.right.should eql epts[1]
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ describe MPFI, "when dividing interval" do
66
+ before(:all) do
67
+ MPFR.set_default_prec(200)
68
+ @num = 5
69
+ @args = [MPFI.interval(2, 8), MPFI.interval('-1e-2', '8.2e-2'), MPFI.interval('-1e2', '-3.738e1')]
70
+ @error = MPFR.new('1e-20')
71
+ end
72
+
73
+ it "should split to some pieces" do
74
+ @args.each do |x|
75
+ res = x.subdivision(@num)
76
+ res.size.should eql @num
77
+ l = x.diam_abs / @num
78
+ res.each { |a| (a.diam_abs - l).abs.should <= @error }
79
+ (1...(@num - 1)).each { |i| res[i].left.should eql res[i-1].right }
80
+ res[0].left.should eql x.left
81
+ res[-1].right.should eql x.right
82
+ end
83
+ end
84
+
85
+ it "should return unchanged object" do
86
+ @args.each do |x|
87
+ res = x.subdivision(1)
88
+ res.size.should eql 1
89
+ l = x.diam_abs
90
+ res.each { |a| (a.diam_abs - l).abs.should <= @error }
91
+ res[0].should eql x
92
+ end
93
+ end
94
+ end
95
+
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'mpfr'
4
+ require 'mpfi'
5
+ require "#{File.dirname(File.expand_path(__FILE__))}/generate_number_module.rb"
6
+
7
+
8
+ describe MPFI, "when calculating mathematical functions" do
9
+ it "should not raise error" do
10
+ MPFR.set_default_prec(256)
11
+ num = MPFI.new('4.2184930258398098')
12
+ p MPFI::Math.sqr(num)
13
+ p MPFI::Math.sqrt(num)
14
+ end
15
+ end
16
+