ruby-gsl-ngx 0.2.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ module GSLng
2
+ class Vector
3
+ # A View of a Vector.
4
+ #
5
+ # Views reference an existing Vector (or a row/column from a Matrix) and can be used to access parts of it without
6
+ # having to copy it entirely. You can treat a View just like a Vector.
7
+ # But note that modifying elements of a View will modify the elements of the original Vector/Matrix.
8
+ #
9
+ class View < Vector
10
+ # @return [Vector,Matrix] The owner of the data this view accesses
11
+ attr_reader :owner
12
+
13
+ def initialize(ptr, owner, size, stride = 1) # @private
14
+ @backend = GSLng.backend
15
+ @owner,@size,@stride = owner,size,stride
16
+ @ptr = FFI::AutoPointer.new(ptr, View.method(:release))
17
+ @ptr_value = @ptr.to_i
18
+ end
19
+
20
+ def View.release(ptr)
21
+ GSLng.backend.gsl_vector_free(ptr)
22
+ end
23
+
24
+ # Returns a Vector (*NOT* a View) copied from this view. In other words,
25
+ # you'll get a Vector which you can modify without modifying {#owner}'s elements
26
+ # @return [Vector]
27
+ def dup
28
+ v = Vector.new(@size)
29
+ @backend.gsl_vector_memcpy(v.ptr, @ptr)
30
+ return v
31
+ end
32
+ alias_method :clone, :dup
33
+ alias_method :to_vector, :dup
34
+
35
+ def view # @private
36
+ raise "Can't create a View from a View"
37
+ end
38
+
39
+ def inspect # @private
40
+ "#{self}:VectorView"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["ronen barzel", "v01d"]
5
+ gem.email = ["ronen@barzel.org"]
6
+ gem.description = "Ruby/GSL new-generation wrapper (memory leak fix)"
7
+ gem.summary = "Ruby/GSL new-generation wrapper (memory leak fix)"
8
+ gem.homepage = "http://github.com/ronen/gsl-ng"
9
+
10
+ gem.extra_rdoc_files = ["README.rdoc", "ext/extconf.rb", "ext/gslng_extensions.cpp", "ext/plotting.cpp", "lib/core_extensions/array.rb", "lib/gslng.rb", "lib/gslng/backend.rb", "lib/gslng/backend_components/error_handling.rb", "lib/gslng/backend_components/matrix.rb", "lib/gslng/backend_components/rng.rb", "lib/gslng/backend_components/special.rb", "lib/gslng/backend_components/stats.rb", "lib/gslng/backend_components/vector.rb", "lib/gslng/matrix.rb", "lib/gslng/matrix_view.rb", "lib/gslng/plotter.rb", "lib/gslng/rng/gaussian.rb", "lib/gslng/rng/rng.rb", "lib/gslng/rng/uniform.rb", "lib/gslng/special.rb", "lib/gslng/vector.rb", "lib/gslng/vector_view.rb"]
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+
15
+ gem.name = "ruby-gsl-ngx"
16
+ gem.require_paths = ["lib", "ext"]
17
+ gem.version = "0.2.6.1"
18
+ gem.extensions = ["ext/extconf.rb"]
19
+
20
+ gem.add_dependency "ffi"
21
+ gem.add_development_dependency "yard"
22
+ end
data/test/benchmark.rb ADDED
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/ruby
2
+ $:.unshift('../lib')
3
+ $:.unshift('../ext')
4
+ require 'benchmark'
5
+ require 'gslng'
6
+ require 'rbgsl'
7
+ require 'narray'
8
+ include Benchmark
9
+
10
+ puts '#################################### Vector ####################################'
11
+
12
+ n=500000
13
+ size = 50000
14
+ puts "Vector#[]"
15
+ bmbm do |x|
16
+ v = GSLng::Vector.random(size)
17
+ gv = GSL::Vector.alloc(v.to_a)
18
+ x.report("rb-gsl :") {n.times {i=rand(size); gv[i]}}
19
+ x.report("GSLng :") {n.times {i=rand(size); v[i]}}
20
+ end
21
+ puts
22
+
23
+ n = 100
24
+ size = 5000
25
+ puts "Vector#each - vector of #{size} elements"
26
+ bmbm do |x|
27
+ v = GSLng::Vector.zero(size)
28
+ gv = GSL::Vector.alloc(v.to_a)
29
+ x.report("rb-gsl :") {n.times {s = 0; gv.each do |e| s += e end}}
30
+ x.report("GSLng :") {n.times {s = 0; v.each do |e| s += e end}}
31
+ end
32
+ puts
33
+
34
+ n = 500
35
+ size = 50000
36
+ puts "Norm (BLAS) - vector of #{size} elements"
37
+ bmbm do |x|
38
+ v = GSLng::Vector.random(size)
39
+ gv = GSL::Vector.alloc(v.to_a)
40
+ x.report("rb-gsl :") {n.times {gv.dnrm2}}
41
+ x.report("GSLng :") {n.times {v.norm}}
42
+ end
43
+ puts
44
+
45
+ n=10
46
+ size = 50000
47
+ puts "Vector#map!"
48
+ bmbm do |x|
49
+ v = GSLng::Vector.random(size)
50
+ gv = GSL::Vector.alloc(v.to_a)
51
+ i = rand(size)
52
+ l = lambda{|e| rand}
53
+ x.report("rb-gsl :") {n.times {gv.collect!{|e| rand}}}
54
+ x.report("GSLng :") {n.times {v.map!{|e| rand}}}
55
+ end
56
+ puts
57
+
58
+
59
+ n=5000
60
+ size = 5000
61
+ puts "Vector product - two vectors of #{size} elements"
62
+ bmbm do |x|
63
+ v,v2 = GSLng::Vector.random(size),GSLng::Vector.random(size)
64
+ gv,gv2 = GSL::Vector.alloc(v.to_a),GSL::Vector.alloc(v2.to_a)
65
+ x.report("rb-gsl :") {n.times {gv.mul!(gv2)}}
66
+ x.report("GSLng :") {n.times {v.mul!(v2)}}
67
+ end
68
+ puts
69
+
70
+ n=500
71
+ size = 5000
72
+ puts "Sort - vector of #{size} elements"
73
+ bmbm do |x|
74
+ v = GSLng::Vector.random(size)
75
+ gv = GSL::Vector.alloc(v.to_a)
76
+ x.report("rb-gsl :") {n.times {gv.sort!}}
77
+ x.report("GSLng :") {n.times {v.sort!}}
78
+ end
79
+ puts
80
+
81
+ n=500
82
+ size = 5000
83
+ puts "Vector#to_a"
84
+ bmbm do |x|
85
+ v = GSLng::Vector.random(size)
86
+ gv = GSL::Vector.alloc(v.to_a)
87
+ x.report("rb-gsl :") {n.times {gv.to_a}}
88
+ x.report("GSLng :") {n.times {v.to_a}}
89
+ end
90
+ puts
91
+
92
+ n=500
93
+ size = 50000
94
+ puts "Vector::from_array"
95
+ bmbm do |x|
96
+ a = Array.new(size) { rand }
97
+ x.report("rb-gsl :") {n.times {GSL::Vector.alloc(a)}}
98
+ x.report("GSLng :") {n.times {GSLng::Vector.from_array(a)}}
99
+ end
100
+ puts
101
+
102
+ puts '#################################### Matrix ####################################'
103
+
104
+ n=500000
105
+ size = 50
106
+ puts "Matrix#[] - 50 x 50"
107
+ bmbm do |x|
108
+ v = GSLng::Matrix.random(size, size)
109
+ gv = GSL::Matrix.alloc(size, size)
110
+ x.report("rb-gsl :") {n.times {i,j=rand(size),rand(size); gv[i,j]}}
111
+ x.report("GSLng :") {n.times {i,j=rand(size),rand(size); v[i,j]}}
112
+ end
113
+ puts
@@ -0,0 +1,92 @@
1
+ #################################### Vector ####################################
2
+ Vector#[]
3
+ Rehearsal --------------------------------------------
4
+ rb-gsl : 0.300000 0.000000 0.300000 ( 0.301650)
5
+ GSLng : 0.360000 0.000000 0.360000 ( 0.369323)
6
+ ----------------------------------- total: 0.660000sec
7
+
8
+ user system total real
9
+ rb-gsl : 0.310000 0.000000 0.310000 ( 0.387314)
10
+ GSLng : 0.340000 0.000000 0.340000 ( 0.348130)
11
+
12
+ Vector#each - vector of 5000 elements
13
+ Rehearsal --------------------------------------------
14
+ rb-gsl : 0.220000 0.000000 0.220000 ( 0.218779)
15
+ GSLng : 0.200000 0.000000 0.200000 ( 0.205919)
16
+ ----------------------------------- total: 0.420000sec
17
+
18
+ user system total real
19
+ rb-gsl : 0.220000 0.000000 0.220000 ( 0.231055)
20
+ GSLng : 0.200000 0.000000 0.200000 ( 0.203754)
21
+
22
+ Norm (BLAS) - vector of 50000 elements
23
+ Rehearsal --------------------------------------------
24
+ rb-gsl : 0.520000 0.000000 0.520000 ( 0.521935)
25
+ GSLng : 0.520000 0.000000 0.520000 ( 0.521729)
26
+ ----------------------------------- total: 1.040000sec
27
+
28
+ user system total real
29
+ rb-gsl : 0.520000 0.000000 0.520000 ( 0.521828)
30
+ GSLng : 0.520000 0.000000 0.520000 ( 0.521704)
31
+
32
+ Vector#map!
33
+ Rehearsal --------------------------------------------
34
+ rb-gsl : 0.240000 0.000000 0.240000 ( 0.239012)
35
+ GSLng : 0.260000 0.000000 0.260000 ( 0.259632)
36
+ ----------------------------------- total: 0.500000sec
37
+
38
+ user system total real
39
+ rb-gsl : 0.250000 0.000000 0.250000 ( 0.245187)
40
+ GSLng : 0.240000 0.000000 0.240000 ( 0.235865)
41
+
42
+ Vector product - two vectors of 5000 elements
43
+ Rehearsal --------------------------------------------
44
+ rb-gsl : 0.620000 0.000000 0.620000 ( 0.655681)
45
+ GSLng : 0.620000 0.010000 0.630000 ( 0.652757)
46
+ ----------------------------------- total: 1.250000sec
47
+
48
+ user system total real
49
+ rb-gsl : 1.170000 0.000000 1.170000 ( 1.178990)
50
+ GSLng : 1.170000 0.000000 1.170000 ( 1.172545)
51
+
52
+ Sort - vector of 5000 elements
53
+ Rehearsal --------------------------------------------
54
+ rb-gsl : 0.440000 0.000000 0.440000 ( 0.441440)
55
+ GSLng : 0.440000 0.000000 0.440000 ( 0.444755)
56
+ ----------------------------------- total: 0.880000sec
57
+
58
+ user system total real
59
+ rb-gsl : 0.430000 0.000000 0.430000 ( 0.440950)
60
+ GSLng : 0.450000 0.010000 0.460000 ( 0.447733)
61
+
62
+ Vector#to_a
63
+ Rehearsal --------------------------------------------
64
+ rb-gsl : 0.470000 0.000000 0.470000 ( 0.490407)
65
+ GSLng : 0.470000 0.000000 0.470000 ( 0.472971)
66
+ ----------------------------------- total: 0.940000sec
67
+
68
+ user system total real
69
+ rb-gsl : 0.450000 0.000000 0.450000 ( 0.452365)
70
+ GSLng : 0.460000 0.000000 0.460000 ( 0.450768)
71
+
72
+ Vector::from_array
73
+ Rehearsal --------------------------------------------
74
+ rb-gsl : 0.730000 0.140000 0.870000 ( 0.868696)
75
+ GSLng : 0.650000 0.160000 0.810000 ( 0.811343)
76
+ ----------------------------------- total: 1.680000sec
77
+
78
+ user system total real
79
+ rb-gsl : 0.730000 0.000000 0.730000 ( 0.727155)
80
+ GSLng : 0.650000 0.000000 0.650000 ( 0.644819)
81
+
82
+ #################################### Matrix ####################################
83
+ Matrix#[] - 50 x 50
84
+ Rehearsal --------------------------------------------
85
+ rb-gsl : 0.380000 0.080000 0.460000 ( 0.455793)
86
+ GSLng : 0.530000 0.000000 0.530000 ( 0.542278)
87
+ ----------------------------------- total: 0.990000sec
88
+
89
+ user system total real
90
+ rb-gsl : 0.360000 0.000000 0.360000 ( 0.367216)
91
+ GSLng : 0.510000 0.000000 0.510000 ( 0.510689)
92
+
@@ -0,0 +1,145 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
2
+ $:.unshift File.join(File.dirname(__FILE__),'..','ext')
3
+
4
+ require 'test/unit'
5
+ require 'gslng'
6
+
7
+ include GSLng
8
+
9
+ class TestMatrix < Test::Unit::TestCase
10
+ def test_initialize
11
+ assert_equal([5,5], Matrix.new(5,5).size)
12
+ assert_equal([5,5], Matrix.zero(5,5).size)
13
+ assert(Matrix.zero(5,5).zero?)
14
+ assert_nothing_raised { Matrix.random(5,5) }
15
+ end
16
+
17
+ def test_to_s
18
+ assert_equal("[0.0 0.0 0.0]", Matrix[0, 0, 0].to_s)
19
+ assert_equal("[0.0 1.0 2.0]", Matrix[0,1,2].to_s)
20
+ assert_equal("[1.0 2.0 3.0;\n 2.0 3.0 4.0]", Matrix[[1,2,3],[2,3,4]].to_s)
21
+ assert_equal("0.0 0.0 0.0", Matrix[0, 0, 0].join(' '))
22
+ assert_equal("1.0 2.0 3.0 2.0 3.0 4.0", Matrix[[1,2,3],[2,3,4]].join(' '))
23
+ end
24
+
25
+ def test_to_a
26
+ assert_equal([[0.0, 0.0, 0.0]], Matrix[0, 0, 0].to_a)
27
+ assert_equal([[0.0, 1.0, 2.0]], Matrix[0,1,2].to_a)
28
+ assert_equal([[1.0, 2.0, 3.0],[2.0, 3.0, 4.0]], Matrix[[1,2,3],[2,3,4]].to_a)
29
+ end
30
+
31
+ def test_equal
32
+ assert_equal(Matrix[0, 0, 0], Matrix[0, 0, 0])
33
+ assert_equal(Matrix[[1,2,3],[2,3,4]], Matrix[[1,2,3],[2,3,4]])
34
+ m = Matrix[[1,2,3],[2,3,4]]
35
+ assert_equal(m, m.dup)
36
+ assert_equal(Matrix[0,1,2], Matrix[0...3])
37
+ assert_equal(Matrix[[0,1,2],[1,2,3]], Matrix[0...3,1...4])
38
+ end
39
+
40
+ def test_set_get
41
+ m = Matrix[[1,2,3],[2,3,4]]
42
+ m[0,0] = 3
43
+ assert_equal(Matrix[[3,2,3],[2,3,4]], m)
44
+ assert_equal(2, m[1,0])
45
+ end
46
+
47
+ def test_each
48
+ m = Matrix[[1,2,3],[3,4,5]]
49
+ a = []
50
+ m.each {|e| a << e}
51
+ assert_equal([1,2,3,3,4,5], a)
52
+ a = []
53
+ m.each {|e| a << e}
54
+ assert_equal([1,2,3,3,4,5], a)
55
+ a = []
56
+ m.each_row {|r| r.each {|e| a << e}}
57
+ assert_equal([1,2,3,3,4,5], a)
58
+ a = []
59
+ m.each_column {|c| c.each {|e| a << e}}
60
+ assert_equal([1,3,2,4,3,5], a)
61
+ a = []
62
+ m.each_vec_row {|r| r.each {|e| a << e}}
63
+ assert_equal([1,2,3,3,4,5], a)
64
+ a = []
65
+ m.each_vec_column {|c| c.each {|e| a << e}}
66
+ assert_equal([1,3,2,4,3,5], a)
67
+ end
68
+
69
+ def test_complex_get
70
+ m = Matrix[[1,2,3],[2,3,4]]
71
+ assert_equal(m, m[:*,:*])
72
+ assert_equal(Matrix[1, 2, 3], m[0,:*])
73
+ assert_equal(Matrix[2, 3, 4], m[1,:*])
74
+ assert_equal(Matrix[1, 2], m[:*,0])
75
+ assert_equal(Matrix[2, 3], m[:*,1])
76
+ assert_equal(Matrix[3, 4], m[:*,2])
77
+ end
78
+
79
+ def test_complex_set
80
+ m = Matrix[[1,2,3],[2,3,4]]
81
+ m[0,:*] = 1
82
+ assert_equal(Matrix[[1,1,1],[2,3,4]], m)
83
+ m[0,:*] = Vector[1,2,4]
84
+ assert_equal(Matrix[[1,2,4],[2,3,4]], m)
85
+
86
+ m[:*,0] = 1
87
+ assert_equal(Matrix[[1,2,4],[1,3,4]], m)
88
+ m[:*,0] = Vector[1,2]
89
+ assert_equal(Matrix[[1,2,4],[2,3,4]], m)
90
+
91
+ m[:*,:*] = 1
92
+ assert_equal(Matrix[[1,1,1],[1,1,1]], m)
93
+ end
94
+
95
+ def test_operators
96
+ assert_equal(Matrix[[1,4,3],[2,4,4]],Matrix[[1,2,3],[2,3,4]] + Matrix[[0,2,0],[0,1,0]])
97
+ assert_equal(Matrix[[1.5,2.5,3.5],[2.5,3.5,4.5]],Matrix[[1,2,3],[2,3,4]] + 0.5)
98
+ assert_equal(Matrix[[1.5,2.5,3.5],[2.5,3.5,4.5]],0.5 + Matrix[[1,2,3],[2,3,4]])
99
+
100
+ assert_equal(Matrix[[0,2,0],[0,4,0],[0,6,0]],Matrix[1,2,3].transpose * Vector[0,2,0])
101
+ assert_equal(Matrix[[4],[6]],Matrix[[1,2,3],[2,3,4]] * Vector[0,2,0].transpose)
102
+ assert_equal(Matrix[4, 6],Vector[0,2,0] * Matrix[[1,2],[2,3],[4,5]])
103
+ assert_equal(Matrix[[3,6],[9,12]],Matrix[[1,2],[3,4]] * 3)
104
+
105
+ assert_equal(Matrix[[4,6],[2,3]],Matrix[[0,2,0],[0,1,0]] * Matrix[[1,2],[2,3],[4,5]])
106
+ assert_equal(Matrix[[0,4,0],[0,1,0]],Matrix[[0,2,0],[0,1,0]] ^ Matrix[[0,2,0],[0,1,0]])
107
+ end
108
+
109
+ def test_swaps
110
+ m = Matrix[[1,2,3],[2,3,4],[3,4,5]]
111
+ assert_equal(Matrix[[2,3,4],[1,2,3],[3,4,5]], m.swap_rows(0,1))
112
+ assert_equal(Matrix[[3,2,4],[2,1,3],[4,3,5]], m.swap_columns(0,1))
113
+ assert_equal(Matrix[[3,2,4],[2,1,3],[4,3,5]], m.swap_rowcol(0,0))
114
+
115
+ m = Matrix[[1,2,3],[4,5,6],[7,8,9]]
116
+ assert_equal(Matrix[[0, 0, 0],[0, 1, 2],[0, 4, 5]], m.slide(1, 1))
117
+
118
+ m = Matrix[[1,2,3],[4,5,6],[7,8,9]]
119
+ assert_equal(Matrix[[5, 6, 0],[8, 9, 0],[0, 0, 0]], m.slide(-1, -1))
120
+
121
+ m = Matrix[[1,2,3],[4,5,6],[7,8,9]]
122
+ assert_equal(Matrix[[4, 5, 6],[7, 8, 9],[0, 0, 0]], m.slide(-1, 0))
123
+ end
124
+
125
+ def test_view
126
+ m,view = nil,nil
127
+ assert_nothing_raised {
128
+ m = Matrix[[1,2,3],[2,3,4]]
129
+ view = m.view
130
+ view[0,0] = 3
131
+ }
132
+ assert_equal(Matrix[[3,2,3],[2,3,4]], m)
133
+ assert_equal(Matrix[[3,2,3],[2,3,4]], view)
134
+ assert_equal(Matrix[3,4], m.view(1, 1))
135
+ assert_equal(Matrix[3,2], m.view(0, 0, nil, 1).transpose)
136
+ assert_equal(Matrix[[3],[2]], m.column_view(0))
137
+ assert_equal(Matrix[3,2,3], m.row_view(0))
138
+ end
139
+
140
+ def test_map
141
+ m = Matrix[[1,2,3],[2,3,4]]
142
+ assert_equal([['1.0','2.0','3.0'],['2.0','3.0','4.0']], m.map_array {|e| e.to_s})
143
+ assert_equal(Matrix[[2,3,4],[3,4,5]], m.map {|e| e+1})
144
+ end
145
+ end
data/test/rng_test.rb ADDED
@@ -0,0 +1,27 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
+ $:.unshift File.join(File.dirname(__FILE__),'..','ext')
6
+
7
+ require 'test/unit'
8
+ require 'gslng'
9
+ include GSLng
10
+
11
+ class RNGTest < Test::Unit::TestCase
12
+ def test_rng
13
+ RNG.new
14
+ end
15
+
16
+ def test_uniform
17
+ three_epsilon = Vector.new(3).fill!(Float::EPSILON)
18
+ uniform = RNG::Uniform.new(0, 2)
19
+ assert(Vector[1.99948349781334, 0.325819750782102, 0.56523561058566] - Vector.new(3) { uniform.sample } <= three_epsilon)
20
+ end
21
+
22
+ def test_gaussian
23
+ three_epsilon = Vector.new(3).fill!(Float::EPSILON)
24
+ gaussian = RNG::Gaussian.new
25
+ assert(Vector[0.133918608118676, -0.0881009918314384, 1.67440840625377] - Vector.new(3) { gaussian.sample } <= three_epsilon)
26
+ end
27
+ end