ruby-mpfi 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +62 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +48 -0
- data/Rakefile +26 -0
- data/ext/mpfi/extconf.rb +10 -0
- data/ext/mpfi/func_mpfi_extention.c +52 -0
- data/ext/mpfi/func_mpfi_extention.h +2 -0
- data/ext/mpfi/make_c_source.rb +115 -0
- data/ext/mpfi/ruby_mpfi.c +1452 -0
- data/ext/mpfi/ruby_mpfi.h +39 -0
- data/ext/mpfi/ruby_mpfr.h +38 -0
- data/ext/mpfi/yasnippet_mpfi.el +44 -0
- data/ext/mpfi_complex/mpfi/extconf.rb +10 -0
- data/ext/mpfi_complex/mpfi/func_mpfi_extention.h +2 -0
- data/ext/mpfi_complex/mpfi/func_ruby_mpfi_complex.c +130 -0
- data/ext/mpfi_complex/mpfi/func_ruby_mpfi_complex.h +35 -0
- data/ext/mpfi_complex/mpfi/ruby_mpfi.h +39 -0
- data/ext/mpfi_complex/mpfi/ruby_mpfi_complex.c +217 -0
- data/ext/mpfi_complex/mpfi/ruby_mpfi_complex.h +15 -0
- data/ext/mpfi_complex/mpfi/ruby_mpfr.h +38 -0
- data/ext/mpfi_matrix/mpfi/extconf.rb +9 -0
- data/ext/mpfi_matrix/mpfi/func_mpfi_extention.h +2 -0
- data/ext/mpfi_matrix/mpfi/func_mpfi_matrix.c +795 -0
- data/ext/mpfi_matrix/mpfi/func_mpfi_matrix.h +103 -0
- data/ext/mpfi_matrix/mpfi/func_mpfr_matrix.h +72 -0
- data/ext/mpfi_matrix/mpfi/func_ruby_mpfi_complex.h +35 -0
- data/ext/mpfi_matrix/mpfi/ruby_mpfi.h +39 -0
- data/ext/mpfi_matrix/mpfi/ruby_mpfi_complex.h +15 -0
- data/ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.c +1200 -0
- data/ext/mpfi_matrix/mpfi/ruby_mpfi_matrix.h +13 -0
- data/ext/mpfi_matrix/mpfi/ruby_mpfr.h +38 -0
- data/ext/mpfi_matrix/mpfi/ruby_mpfr_matrix.h +13 -0
- data/lib/mpfi/matrix.rb +188 -0
- data/lib/mpfi/version.rb +3 -0
- data/ruby-mpfi.gemspec +35 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/mpfi/generate_number_module.rb +48 -0
- data/spec/mpfi/mpfi_alloc_spec.rb +55 -0
- data/spec/mpfi/mpfi_diam_arithmetic_spec.rb +25 -0
- data/spec/mpfi/mpfi_interval_arithmetic_spec.rb +105 -0
- data/spec/mpfi/mpfi_interval_functions_spec.rb +95 -0
- data/spec/mpfi/mpfi_math_functions_spec.rb +16 -0
- data/spec/mpfi/mpfi_set_operation_spec.rb +102 -0
- data/spec/mpfi/ruby-mpfi_spec.rb +11 -0
- data/spec/mpfi/spec_helper.rb +10 -0
- data/spec/mpfi_complex/spec_helper.rb +10 -0
- data/spec/mpfi_matrix/generate_matrix_arguments.rb +65 -0
- data/spec/mpfi_matrix/mpfi_matrix_alloc_spec.rb +134 -0
- data/spec/mpfi_matrix/mpfi_matrix_arithmetic_spec.rb +156 -0
- data/spec/mpfi_matrix/mpfi_matrix_interval_func_spec.rb +30 -0
- data/spec/mpfi_matrix/mpfi_matrix_set_element_spec.rb +55 -0
- data/spec/mpfi_matrix/mpfi_matrix_set_operation_spec.rb +71 -0
- data/spec/mpfi_matrix/mpfi_matrix_string_spec.rb +32 -0
- data/spec/mpfi_matrix/mpfi_matrix_subdivision_spec.rb +14 -0
- data/spec/mpfi_matrix/mpfi_square_matrix_spec.rb +37 -0
- data/spec/mpfi_matrix/mpfi_vector_spec.rb +15 -0
- data/spec/mpfi_matrix/spec_helper.rb +19 -0
- data/spec/spec.opts +1 -0
- data/tasks/extconf.rake +36 -0
- metadata +132 -0
@@ -0,0 +1,102 @@
|
|
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 checking inclusive relations" do
|
8
|
+
before(:all) do
|
9
|
+
@a = MPFI.interval(-2, 5)
|
10
|
+
@b = MPFI.interval(-3, 2)
|
11
|
+
@c = MPFI.interval(5, 8)
|
12
|
+
@d = MPFI.interval(7, 9)
|
13
|
+
@e = MPFI.interval(-2, 0)
|
14
|
+
@f = MPFI.interval(-1, 0)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be some variations of return value for method include?." do
|
18
|
+
@a.include?(@b).should be_nil
|
19
|
+
@a.include?(@c).should be_nil
|
20
|
+
@a.include?(@d).should be_nil
|
21
|
+
@a.include?(@e).should be_true
|
22
|
+
@a.include?(@f).should be_true
|
23
|
+
|
24
|
+
@a.strictly_include?(@b).should be_nil
|
25
|
+
@a.strictly_include?(@c).should be_nil
|
26
|
+
@a.strictly_include?(@d).should be_nil
|
27
|
+
@a.strictly_include?(@e).should be_nil
|
28
|
+
@a.strictly_include?(@f).should be_true
|
29
|
+
|
30
|
+
@a.intersect(@b).should be_true
|
31
|
+
@a.intersect(@c).should be_true
|
32
|
+
@a.intersect(@d).should be_nil
|
33
|
+
@a.intersect(@e).should be_true
|
34
|
+
@a.intersect(@f).should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
describe MPFI, "when making intersection of two intervals" do
|
40
|
+
before(:all) do
|
41
|
+
MPFR.set_default_prec(200)
|
42
|
+
src = [[2, 3], [4, 5], [-7, 0]]
|
43
|
+
tmp = GenerateNumber.float_arguments(2, 200).map { |a| a[0] < a[1] ? MPFI.interval(*a) : MPFI.interval(a[1], a[0])}
|
44
|
+
@base = [MPFI.interval(1, 3)] + tmp[0...20]
|
45
|
+
@intervals = src.map{ |a| MPFI.interval(*a) } + tmp[20..-1]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return intersection intervals or nil" do
|
49
|
+
res = [0, 0]
|
50
|
+
@base.each do |a|
|
51
|
+
@intervals.each do |b|
|
52
|
+
intersect = a.intersect(b)
|
53
|
+
if a.left > b.right || a.right < b.left
|
54
|
+
intersect.should be_nil
|
55
|
+
a.intersect2(b).empty?.should be_true
|
56
|
+
res[1] += 1
|
57
|
+
else
|
58
|
+
intersect.should == MPFI.interval([a.left, b.left].max, [a.right, b.right].min)
|
59
|
+
intersect.should == a.intersect2(b)
|
60
|
+
a.include?(intersect).should be_true
|
61
|
+
b.include?(intersect).should be_true
|
62
|
+
res[0] += 1
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
describe MPFI, "when checking whether interval is bounded or not bounded" do
|
71
|
+
before(:all) do
|
72
|
+
MPFR.set_default_prec(128)
|
73
|
+
@bounded = GenerateNumber.float_arguments(2, 200).map { |a| a[0] < a[1] ? MPFI.interval(*a) : MPFI.interval(a[1], a[0])}
|
74
|
+
@unbounded = [[MPFR.new(3), MPFR.pinf], [MPFR.minf, MPFR.new(-9)]].map { |a| MPFI.interval(*a)}
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should be bounded" do
|
78
|
+
@bounded.each { |a| a.bounded?.should be_true }
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should be unbounded" do
|
82
|
+
@unbounded.each { |a| a.bounded?.should be_nil }
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
describe MPFI, "when making interval" do
|
88
|
+
before(:all) do
|
89
|
+
MPFR.set_default_prec(128)
|
90
|
+
@sources = GenerateNumber.float_arguments(2, 100)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should not raise error" do
|
94
|
+
@sources.each do |a|
|
95
|
+
interval = nil
|
96
|
+
lambda { interval = (a[0] < a[1] ? MPFI.interval(a[0].to_s, a[1].to_s) : MPFI.interval(a[1].to_s, a[0].to_s)) }.should_not raise_error
|
97
|
+
interval.include?(MPFR.new(a[0].to_s)).should be_true
|
98
|
+
interval.include?(MPFR.new(a[1].to_s)).should be_true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module GenerateNumber
|
2
|
+
|
3
|
+
def self.float_matrix_interval_arguments(number, row, column, max = 100)
|
4
|
+
Array.new(number){ |j| Array.new(row){ |i| Array.new(column) { |k| GenerateNumber.float(2, max).sort } } }
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.float_matrix_arguments(number, row, column, max = 100)
|
8
|
+
ret = []
|
9
|
+
(0...number).each do
|
10
|
+
ret << Array.new(row){ |j| Array.new(column){ |i| rand(max) - rand } }
|
11
|
+
for i in 0...(ret[-1].size)
|
12
|
+
ret[-1][i].map!{ |a| (rand > 0.5 ? -a : a) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
ret
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.float_vector_arguments(number, size, max = 100)
|
19
|
+
ret = []
|
20
|
+
(0...number).each { ret << Array.new(size){ |i| rand(max) - rand } }
|
21
|
+
ret
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.float_arguments(size, number, max = 100)
|
25
|
+
ret = Array.new(number){ |j| Array.new(size){ |i| rand(max) - rand } }
|
26
|
+
for i in 0...(ret.size)
|
27
|
+
case i % 4
|
28
|
+
when 1
|
29
|
+
ret[i].map!{ |a| -a }
|
30
|
+
when 2, 3
|
31
|
+
ret[i].map!{ |a| (rand > 0.5 ? -a : a) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
ret
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.float(num, max = 100, &block)
|
38
|
+
if block_given?
|
39
|
+
for i in 0...num
|
40
|
+
a = rand(max) - rand
|
41
|
+
a = -a if rand > 0.5
|
42
|
+
yield(a)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
ary = Array.new(num){ |i| rand(max) - rand }
|
46
|
+
ary.map!{ |a| (rand > 0.5 ? -a : a) }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.string(number)
|
51
|
+
prec = MPFR.get_default_prec
|
52
|
+
max = 2 ** prec
|
53
|
+
Array.new(number) do |i|
|
54
|
+
sign = ((rand > 0.5 ? '-' : ''))
|
55
|
+
"#{sign}#{rand(max)}.#{rand(max)}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.mpfr_args(number)
|
60
|
+
ret = self.string(number)
|
61
|
+
ret.map!{ |a| MPFR.new(a) }
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
#
|
4
|
+
# 03/19/09 11:51:21
|
5
|
+
# This spec test is almost same test as the one for mpfr/matrix.
|
6
|
+
#
|
7
|
+
|
8
|
+
describe "initialization of matrix" do
|
9
|
+
before(:all) do
|
10
|
+
MPFR.set_default_prec(128)
|
11
|
+
@sizes = [[1, 2], [2, 3], [8, 5], [7, 3]]
|
12
|
+
@matrixes = @sizes.map{ |a| MPFI::Matrix.new(*a) }
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should has size which equals size of array." do
|
16
|
+
@matrixes.each_with_index do |a, i|
|
17
|
+
a.size.should == (@sizes[i][0] * @sizes[i][1])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should has size of row which equals size of array." do
|
22
|
+
@matrixes.each_with_index do |a, i|
|
23
|
+
a.row_size.should == @sizes[i][0]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should has size of column which equals size of array." do
|
28
|
+
@matrixes.each_with_index do |a, i|
|
29
|
+
a.column_size.should == @sizes[i][1]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have MPFI instances as elements." do
|
34
|
+
@matrixes.each do |m|
|
35
|
+
m.each { |a| a.should be_an_instance_of MPFI }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "argument error raises for invalid initialization argument" do
|
42
|
+
before(:all) do
|
43
|
+
MPFR.set_default_prec(256)
|
44
|
+
@data = [[[1, 2, 3], [0, 0]],
|
45
|
+
[[MPFI.new(0)], [MPFI.new(1), 3]]]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should make error for invalid array argument" do
|
49
|
+
@data.each do |ary|
|
50
|
+
lambda { MPFI::Matrix.new(ary) }.should raise_error ArgumentError
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "when methods of matrix 'dup' and 'transpose' are executed" do
|
56
|
+
before(:all) do
|
57
|
+
MPFR.set_default_prec(256)
|
58
|
+
@data = [[[1, 2], [3, 3]],
|
59
|
+
[[-1, 0.8], ['1.2342', 99]],
|
60
|
+
[[36.237582938, 7122], ['-1e7', 8237]],
|
61
|
+
[[-2.2738, 8.23, 9.237482], [11, '8237.2374', '1239']],
|
62
|
+
[[MPFI.new('23'), MPFI.new('123.23')], [MPFI.new(-3.2378), MPFI.new('1.2837')]]]
|
63
|
+
@matrixes = @data.map{ |a| MPFI::Matrix.new(a) }
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should have MPFI instances as elements." do
|
67
|
+
@matrixes.each do |m|
|
68
|
+
m.each { |a| a.should be_an_instance_of MPFI }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should create matrix which has same values as original and different object id." do
|
73
|
+
@matrixes.each do |m|
|
74
|
+
new = m.dup
|
75
|
+
m.object_id.should_not eql new.object_id
|
76
|
+
(0...(m.row_size)).each do |i|
|
77
|
+
(0...(m.column_size)).each { |j| m[i, j].should eql new[i, j] }
|
78
|
+
end
|
79
|
+
m.should == new
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should has transposed values." do
|
84
|
+
@matrixes.each do |m|
|
85
|
+
id = m.object_id
|
86
|
+
old = m.dup
|
87
|
+
new = m.transpose
|
88
|
+
m.transpose!
|
89
|
+
m.should_not == old
|
90
|
+
m.should == new
|
91
|
+
m.object_id.should == id
|
92
|
+
m.object_id.should_not == new.object_id
|
93
|
+
m.column_size.should == old.row_size
|
94
|
+
m.row_size.should == old.column_size
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "when methods of vector 'dup' and 'transpose' are executed" do
|
100
|
+
before(:all) do
|
101
|
+
MPFR.set_default_prec(768)
|
102
|
+
@data = [[1, 2, 3, 3],
|
103
|
+
[-1, 0.8, '1.2342', 99],
|
104
|
+
[36.237582938, 7122, '-1e7', 8237],
|
105
|
+
[-2.2738, 8.23, 9.237482, '8237.2374', '1239'],
|
106
|
+
[MPFI.new('23'), MPFI.new('123.23'), MPFI.new(-3.2378), MPFI.new('1.2837')]]
|
107
|
+
@column = @data.map{ |a| MPFI::ColumnVector.new(a) }
|
108
|
+
@row = @data.map{ |a| MPFI::RowVector.new(a) }
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should create matrix which has same values as original and different object id." do
|
112
|
+
(@column + @row).each do |m|
|
113
|
+
new = m.dup
|
114
|
+
m.object_id.should_not == new.object_id
|
115
|
+
m.should == new
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should change row vector to column vector and make the opposite changes" do
|
120
|
+
(@column + @row).each do |m|
|
121
|
+
id = m.object_id
|
122
|
+
old = m.dup
|
123
|
+
new = m.transpose
|
124
|
+
m.transpose!
|
125
|
+
m.should_not == old
|
126
|
+
m.should == new
|
127
|
+
m.object_id.should == id
|
128
|
+
m.object_id.should_not == new.object_id
|
129
|
+
m.column_size.should == old.row_size
|
130
|
+
m.row_size.should == old.column_size
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFI::Matrix, "when making matrix negative" do
|
4
|
+
before(:all) do
|
5
|
+
MPFR.set_default_prec(512)
|
6
|
+
@row = 8
|
7
|
+
@column = 3
|
8
|
+
@args = GenerateNumber.float_matrix_arguments(1000, @row, @column).map{ |a| MPFI::Matrix.new(a) }
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should be negative" do
|
12
|
+
zero_matrix = MPFI::Matrix.new(@row, @column)
|
13
|
+
@args.each { |m| (zero_matrix - m).should == m.neg }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe MPFI::Matrix, "when arithmetic operator applies to 2x3 matrixies" do
|
19
|
+
before(:all) do
|
20
|
+
MPFR.set_default_prec(128)
|
21
|
+
@row = 2
|
22
|
+
@column = 3
|
23
|
+
args = GenerateNumber.float_matrix_arguments(1000, @row, @column)
|
24
|
+
@args_i = args.map{ |a| MPFI::Matrix.new(a) }
|
25
|
+
@args_r = args.map{ |a| MPFR::Matrix.new(a) }
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be added" do
|
29
|
+
@args_i.each_index do |i|
|
30
|
+
if i > 0
|
31
|
+
res = @args_i[i-1] + @args_i[i]
|
32
|
+
(0...@row).each do |j|
|
33
|
+
(0...@column).each { |k| res[j, k].should eql(@args_i[i-1][j, k] + @args_i[i][j, k]) }
|
34
|
+
end
|
35
|
+
res.include?(@args_r[i-1] + @args_r[i]).should be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be subtracted" do
|
41
|
+
@args_i.each_index do |i|
|
42
|
+
if i > 0
|
43
|
+
res = @args_i[i-1] - @args_i[i]
|
44
|
+
(0...@row).each do |j|
|
45
|
+
(0...@column).each { |k| res[j, k].should eql(@args_i[i-1][j, k] - @args_i[i][j, k]) }
|
46
|
+
end
|
47
|
+
res.include?(@args_r[i-1] - @args_r[i]).should be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be multiplied" do
|
53
|
+
@args_i.each_index do |i|
|
54
|
+
if i > 0
|
55
|
+
trans = @args_i[i].transpose
|
56
|
+
res = @args_i[i-1] * trans
|
57
|
+
(0...@row).each do |j|
|
58
|
+
(0...@row).each do |k|
|
59
|
+
res2 = MPFI.new(0)
|
60
|
+
(0...@column).each { |l| res2 += @args_i[i-1][j, l] * trans[l, k] }
|
61
|
+
res[j, k].should eql res2
|
62
|
+
end
|
63
|
+
end
|
64
|
+
res.include?(@args_r[i-1] * @args_r[i].transpose).should be_true
|
65
|
+
|
66
|
+
trans = @args_i[i-1].transpose
|
67
|
+
res = trans * @args_i[i]
|
68
|
+
(0...@column).each do |j|
|
69
|
+
(0...@column).each do |k|
|
70
|
+
res2 = MPFI.new(0)
|
71
|
+
(0...@row).each { |l| res2 += trans[j, l] * @args_i[i][l, k] }
|
72
|
+
res[j, k].should eql res2
|
73
|
+
end
|
74
|
+
end
|
75
|
+
res.include?(@args_r[i-1].transpose * @args_r[i]).should be_true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be multyplied by scalar" do
|
81
|
+
@args_i.each do |a|
|
82
|
+
scalar = MPFI.new(GenerateNumber.float(1)[0])
|
83
|
+
res = a.mul_scalar(scalar)
|
84
|
+
(0...@row).each do |j|
|
85
|
+
(0...@column).each { |k| res[j, k].should eql a[j, k] * scalar }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should be divided by scalar" do
|
91
|
+
@args_i.each do |a|
|
92
|
+
scalar = MPFI.new(GenerateNumber.float(1)[0])
|
93
|
+
res = a.div_scalar(scalar)
|
94
|
+
(0...@row).each do |j|
|
95
|
+
(0...@column).each { |k| res[j, k].should eql a[j, k] / scalar }
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
describe MPFI::Matrix, "when arithmetic operator applies to 2x3 MPFR::Matrix" do
|
103
|
+
before(:all) do
|
104
|
+
MPFR.set_default_prec(175)
|
105
|
+
@row = 2
|
106
|
+
@column = 3
|
107
|
+
@args = GenerateNumber.float_matrix_arguments(1000, @row, @column)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should be added" do
|
111
|
+
@args.each_index do |i|
|
112
|
+
if i > 0
|
113
|
+
rmat = MPFR::Matrix.new(@args[i-1])
|
114
|
+
imat = MPFI::Matrix.new(@args[i])
|
115
|
+
res = imat.add(rmat)
|
116
|
+
res.should be_an_instance_of MPFI::Matrix
|
117
|
+
(0...@row).each do |j|
|
118
|
+
(0...@column).each { |k| (imat[j, k] + rmat[j, k]).should eql res[j, k] }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should be subtracted" do
|
125
|
+
@args.each_index do |i|
|
126
|
+
if i > 0
|
127
|
+
rmat = MPFR::Matrix.new(@args[i-1])
|
128
|
+
imat = MPFI::Matrix.new(@args[i])
|
129
|
+
res = imat.sub(rmat)
|
130
|
+
res.should be_an_instance_of MPFI::Matrix
|
131
|
+
(0...@row).each do |j|
|
132
|
+
(0...@column).each { |k| (imat[j, k] - rmat[j, k]).should eql res[j, k] }
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should be multiplied" do
|
139
|
+
@args.each_index do |i|
|
140
|
+
if i > 0
|
141
|
+
rmat = MPFR::Matrix.new(@args[i-1]).transpose
|
142
|
+
imat = MPFI::Matrix.new(@args[i])
|
143
|
+
res = imat.mul(rmat)
|
144
|
+
|
145
|
+
(0...@row).each do |j|
|
146
|
+
(0...@row).each do |k|
|
147
|
+
res2 = MPFI.new(0)
|
148
|
+
(0...@column).each { |l| res2 += (imat[j, l] * rmat[l, k]) }
|
149
|
+
res[j, k].should eql res2
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFI::Matrix, "when making mid_interval" do
|
4
|
+
before(:all) do
|
5
|
+
MPFR.set_default_prec(512)
|
6
|
+
@row = 4
|
7
|
+
@column = 1
|
8
|
+
@args = GenerateNumber.float_vector_arguments(1000, @row, @column).map{ |a| MPFI::ColumnVector.new(a) }
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should have mid_interval including midpoint" do
|
12
|
+
@args.each do |a|
|
13
|
+
mid_int = a.mid_interval
|
14
|
+
a.include?(mid_int).should be_true
|
15
|
+
mid_int.include?(a.mid).should be_true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have midpoint of two points." do
|
20
|
+
@args.each_with_index do |a, ind|
|
21
|
+
m = a.midpoint(@args[ind-1])
|
22
|
+
m2 = (a + @args[ind-1]).div_scalar(MPFI.new(2))
|
23
|
+
(0...@row).each do |i|
|
24
|
+
m[i].should == m2[i]
|
25
|
+
end
|
26
|
+
m.include?(a.mid.midpoint(@args[ind-1].mid)).should be_true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFI::Matrix, "when setting number to particular element" do
|
4
|
+
before(:all) do
|
5
|
+
MPFR.set_default_prec(128)
|
6
|
+
@row = 4
|
7
|
+
@column = 7
|
8
|
+
@matrix = MPFI::Matrix.new(@row, @column)
|
9
|
+
@source = GenerateNumber.float_matrix_arguments(1, @row, @column)[0]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have elements which equals to source number." do
|
13
|
+
(0...(@matrix.row_size)).each do |i|
|
14
|
+
(0...(@matrix.column_size)).each do |j|
|
15
|
+
fr = MPFI.new(@source[i][j].to_s)
|
16
|
+
@matrix.set_element(i, j, fr)
|
17
|
+
@matrix[i, j].should eql fr
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe MPFI::Matrix, "when calculating row and column" do
|
25
|
+
before(:all) do
|
26
|
+
MPFR.set_default_prec(128)
|
27
|
+
@row = 6
|
28
|
+
@column = 4
|
29
|
+
@sources = GenerateNumber.float_matrix_arguments(100, @row, @column)
|
30
|
+
@matrixies = @sources.map { |a| MPFI::Matrix.new(a) }
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have row vector." do
|
34
|
+
@matrixies.each do |m|
|
35
|
+
(0...(m.column_size)).each do |i|
|
36
|
+
col = m.column(i)
|
37
|
+
(0...(m.row_size)).each do |j|
|
38
|
+
col[j].should eql m[j, i]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should have column vector." do
|
45
|
+
@matrixies.each do |m|
|
46
|
+
(0...(m.row_size)).each do |i|
|
47
|
+
row = m.row(i)
|
48
|
+
(0...(m.column_size)).each do |j|
|
49
|
+
row[j].should eql m[i, j]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFI::Matrix, "when an interval includes other" do
|
4
|
+
before(:all) do
|
5
|
+
MPFR.set_default_prec(100)
|
6
|
+
@row = 3
|
7
|
+
@column = 2
|
8
|
+
tmp = GenerateNumber.float_matrix_interval_arguments(100, @row, @column).map{ |a| MPFI::Matrix.interval(a) }
|
9
|
+
@args1 = tmp[0...50]
|
10
|
+
@args2 = tmp[50..-1]
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
describe MPFI::Matrix, "when a box intersects other" do
|
17
|
+
before(:all) do
|
18
|
+
MPFR.set_default_prec(128)
|
19
|
+
@row = 3
|
20
|
+
@column = 2
|
21
|
+
tmp = GenerateNumber.float_matrix_interval_arguments(100, @row, @column).map{ |a| MPFI::Matrix.interval(a) }
|
22
|
+
@args1 = tmp[0...50]
|
23
|
+
@args2 = tmp[50..-1]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return box or nil" do
|
27
|
+
total = [0,0]
|
28
|
+
@args1.each do |a1|
|
29
|
+
@args2.each do |a2|
|
30
|
+
res = a1.intersect(a2)
|
31
|
+
tmp = []
|
32
|
+
(0...(a1.size)).each { |k| tmp << a1.at(k).intersect(a2.at(k)) }
|
33
|
+
if tmp.any?{ |b| NilClass === b }
|
34
|
+
res.should be_nil
|
35
|
+
total[1] += 1
|
36
|
+
else
|
37
|
+
(0...(res.size)).each { |k| res.at(k).should == tmp[k] }
|
38
|
+
a1.include?(res).should be_true
|
39
|
+
a2.include?(res).should be_true
|
40
|
+
total[0] += 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe MPFI::Matrix, "when checking whether matrix is bounded or not bounded" do
|
48
|
+
before(:all) do
|
49
|
+
MPFR.set_default_prec(128)
|
50
|
+
@row = 2
|
51
|
+
@column = 3
|
52
|
+
@bounded = GenerateNumber.float_matrix_interval_arguments(100, @row, @column).map{ |a| MPFI::Matrix.interval(a) }
|
53
|
+
@unbounded = GenerateNumber.float_matrix_interval_arguments(100, @row, @column).map do |a|
|
54
|
+
if rand > 0.5
|
55
|
+
a[rand(@row)][rand(@column)][0] = MPFR.minf
|
56
|
+
else
|
57
|
+
a[rand(@row)][rand(@column)][1] = MPFR.pinf
|
58
|
+
end
|
59
|
+
MPFI::Matrix.interval(a)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be bounded" do
|
64
|
+
@bounded.each { |a| a.bounded?.should be_true }
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be unbounded" do
|
68
|
+
@unbounded.each { |a| a.bounded?.should be_nil }
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe "when MPFI instance is converted to string by some methods." do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
MPFR.set_default_prec(128)
|
7
|
+
@row = 2
|
8
|
+
@column = 3
|
9
|
+
@matrixes = ((0..100).map { |i| MPFI::Matrix.new(GenerateNumber.float_arguments(@column, @row)) })
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return array having strings." do
|
13
|
+
@matrixes.each do |m|
|
14
|
+
m.str_ary_for_inspect.each_with_index do |s, i|
|
15
|
+
s.should eql "#{m[i % @row, i / @row].left.to_strf('%.Re')} #{m[i % @row, i / @row].right.to_strf('%.Re')}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return inspect string." do
|
21
|
+
@matrixes.each do |m|
|
22
|
+
ins = m.inspect
|
23
|
+
ins.should match(/#{Regexp.escape(sprintf("%x", m.object_id))}/)
|
24
|
+
m.each do |a|
|
25
|
+
str = "#{a.left.to_strf('%.Re')} #{a.right.to_strf('%.Re')}"
|
26
|
+
ins.should match(/#{Regexp.escape(str)}/)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFI::Matrix, "when subdividing interval" do
|
4
|
+
before(:all) do
|
5
|
+
@size = MPFR.new('1')
|
6
|
+
@arg = MPFI::ColumnVector.new([MPFI.interval(-1.8, -1.729), MPFI.interval('-1e-2', '8.2e-2'), MPFI.interval('1.82', '2.27')])
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return unchanged object" do
|
10
|
+
res = @arg.subdivision_by_size(@size)
|
11
|
+
res.size.should eql 1
|
12
|
+
res.each { |a| a.max_diam_abs.should <= @size }
|
13
|
+
end
|
14
|
+
end
|