ruby-gsl-ng 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/History.txt +5 -0
- data/Manifest.txt +14 -0
- data/README.txt +8 -0
- data/Rakefile +12 -0
- data/ext/extconf.rb +4 -0
- data/ext/gslng_extensions.cpp +28 -0
- data/lib/gsl.rb +8 -0
- data/lib/gsl/backend.rb +83 -0
- data/lib/gsl/finalizer.rb +5 -0
- data/lib/gsl/vector.rb +236 -0
- data/test/test_gsl.rb +9 -0
- data/test/vector_test.rb +75 -0
- metadata +91 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
data/Rakefile
ADDED
data/ext/extconf.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
/**
|
2
|
+
* gsl_extension module: provides some simple C functions in cases where a faster version of some
|
3
|
+
* Ruby method is really necessary.
|
4
|
+
* This could actually a standard C library (not a Ruby extension), but it's easier this way.
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <gsl/gsl_vector.h>
|
8
|
+
|
9
|
+
extern "C" void Init_gslng_extensions(void) { }
|
10
|
+
|
11
|
+
/**** Vector *****/
|
12
|
+
|
13
|
+
// For Vector::map!
|
14
|
+
typedef double (*gsl_vector_callback_t)(double);
|
15
|
+
|
16
|
+
extern "C" void gsl_vector_map(gsl_vector* v, gsl_vector_callback_t callback) {
|
17
|
+
for (size_t i = 0; i < v->size; i++)
|
18
|
+
*gsl_vector_ptr(v, i) = (*callback)(*gsl_vector_const_ptr(v, i));
|
19
|
+
}
|
20
|
+
|
21
|
+
// For Vector::map_index!
|
22
|
+
typedef double (*gsl_vector_index_callback_t)(size_t);
|
23
|
+
|
24
|
+
extern "C" void gsl_vector_map_index(gsl_vector* v, gsl_vector_index_callback_t callback) {
|
25
|
+
for (size_t i = 0; i < v->size; i++)
|
26
|
+
*gsl_vector_ptr(v, i) = (*callback)(i);
|
27
|
+
}
|
28
|
+
|
data/lib/gsl.rb
ADDED
data/lib/gsl/backend.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
module GSL
|
4
|
+
# TODO: get a way to properly define the type of size_t (assumed to be :uint)
|
5
|
+
#
|
6
|
+
module Backend
|
7
|
+
extend FFI::Library
|
8
|
+
|
9
|
+
##----- Vector ------##
|
10
|
+
# memory handling
|
11
|
+
attach_function :gsl_vector_alloc, [ :size_t ], :pointer
|
12
|
+
attach_function :gsl_vector_calloc, [ :size_t ], :pointer
|
13
|
+
attach_function :gsl_vector_free, [ :pointer ], :void
|
14
|
+
|
15
|
+
# initializing
|
16
|
+
attach_function :gsl_vector_set_all, [ :pointer, :double ], :void
|
17
|
+
attach_function :gsl_vector_set_zero, [ :pointer ], :void
|
18
|
+
attach_function :gsl_vector_set_basis, [ :pointer, :size_t ], :int
|
19
|
+
|
20
|
+
# operations
|
21
|
+
attach_function :gsl_vector_add, [ :pointer, :pointer ], :int
|
22
|
+
attach_function :gsl_vector_sub, [ :pointer, :pointer ], :int
|
23
|
+
attach_function :gsl_vector_mul, [ :pointer, :pointer ], :int
|
24
|
+
attach_function :gsl_vector_div, [ :pointer, :pointer ], :int
|
25
|
+
attach_function :gsl_vector_scale, [ :pointer, :double ], :int
|
26
|
+
attach_function :gsl_vector_add_constant, [ :pointer, :double ], :int
|
27
|
+
|
28
|
+
# element access
|
29
|
+
attach_function :gsl_vector_get, [ :pointer, :size_t ], :double
|
30
|
+
attach_function :gsl_vector_set, [ :pointer, :size_t, :double ], :void
|
31
|
+
|
32
|
+
# properties
|
33
|
+
attach_function :gsl_vector_isnull, [ :pointer ], :int
|
34
|
+
attach_function :gsl_vector_ispos, [ :pointer ], :int
|
35
|
+
attach_function :gsl_vector_isneg, [ :pointer ], :int
|
36
|
+
attach_function :gsl_vector_isnonneg, [ :pointer ], :int
|
37
|
+
|
38
|
+
# max and min
|
39
|
+
attach_function :gsl_vector_max, [ :pointer ], :double
|
40
|
+
attach_function :gsl_vector_min, [ :pointer ], :double
|
41
|
+
attach_function :gsl_vector_minmax, [ :pointer, :buffer_out, :buffer_out ], :void
|
42
|
+
attach_function :gsl_vector_max_index, [ :pointer ], :size_t
|
43
|
+
attach_function :gsl_vector_min_index, [ :pointer ], :size_t
|
44
|
+
attach_function :gsl_vector_minmax_index, [ :pointer, :buffer_out, :buffer_out ], :void
|
45
|
+
|
46
|
+
# copying
|
47
|
+
attach_function :gsl_vector_memcpy, [ :pointer, :pointer ], :int
|
48
|
+
attach_function :gsl_vector_swap, [ :pointer, :pointer ], :int
|
49
|
+
|
50
|
+
# exchanging elements
|
51
|
+
attach_function :gsl_vector_swap_elements, [ :pointer, :size_t, :size_t ], :int
|
52
|
+
attach_function :gsl_vector_reverse, [ :pointer ], :int
|
53
|
+
|
54
|
+
# BLAS functions
|
55
|
+
attach_function :gsl_blas_ddot, [ :pointer, :pointer, :buffer_out ], :int
|
56
|
+
attach_function :gsl_blas_dnrm2, [ :pointer ], :double
|
57
|
+
attach_function :gsl_blas_dasum, [ :pointer ], :double
|
58
|
+
#attach_function :gsl_blas_idamax, [ :pointer ], clbas_index??
|
59
|
+
#attach_function :gsl_blas_dcopy, use this instead of memcpy?
|
60
|
+
attach_function :gsl_blas_daxpy, [ :double, :pointer, :pointer ], :int
|
61
|
+
attach_function :gsl_blas_dscal, [ :double, :pointer ], :void
|
62
|
+
|
63
|
+
# From local extension
|
64
|
+
callback :gsl_vector_callback, [ :double ], :double
|
65
|
+
attach_function :gsl_vector_map, [ :pointer, :gsl_vector_callback ], :void
|
66
|
+
|
67
|
+
callback :gsl_vector_index_callback, [ :size_t ], :double
|
68
|
+
attach_function :gsl_vector_map_index, [ :pointer, :gsl_vector_index_callback ], :void
|
69
|
+
|
70
|
+
# Sorting
|
71
|
+
attach_function :gsl_sort_vector, [ :pointer ], :void
|
72
|
+
|
73
|
+
##----- Error handling ------##
|
74
|
+
callback :error_handler_callback, [ :string, :string, :int, :int ], :void
|
75
|
+
attach_function :gsl_set_error_handler, [ :error_handler_callback ], :error_handler_callback
|
76
|
+
|
77
|
+
# TODO: pop this block from the exception stack so that it seems to be coming from the original gsl function
|
78
|
+
ErrorHandlerCallback = Proc.new {|reason, file, line, errno|
|
79
|
+
raise RuntimeError, "#{reason} at #{file}:#{line} (errno: #{errno})"
|
80
|
+
}
|
81
|
+
gsl_set_error_handler(ErrorHandlerCallback)
|
82
|
+
end
|
83
|
+
end
|
data/lib/gsl/vector.rb
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
module GSL
|
2
|
+
# A fixed-size n-dimensional vector.
|
3
|
+
#
|
4
|
+
# =Examples
|
5
|
+
# Vector[1,2,3] + Vector[2,3,4] => Vector[3,5,7]
|
6
|
+
# Vector[1,2,3] + 0.5 => Vector[1.5,2.5,3.5]
|
7
|
+
# Same goes for *, /, and - operators. The are also self-modifying versions (#add, #mul, #div, #sub).
|
8
|
+
#
|
9
|
+
# Note also that operator ^ produces the #dot product:
|
10
|
+
# Vector[1,2,3] ^ Vector[2,3,4] => 20
|
11
|
+
#
|
12
|
+
# =Notes
|
13
|
+
# * This class includes Enumerable, but certain methods are redefined (like #max and #min)
|
14
|
+
# for fast versions that don't use #each. Calling #each (and therefore, any other Enumerable's method) is slower.
|
15
|
+
# * #each is implemented through calls to #[], which can be relatively slow (compared to direct C pointer access)
|
16
|
+
# for big Vectors. It would be possible to have a faster version that iterates on the C-side and calls a block for each
|
17
|
+
# element, but in that case it wouldn't be possible to expect a return value of any type. This complicates things for methods like
|
18
|
+
# #any? which expect a boolean value.
|
19
|
+
# * Some functions (like #sum, #dot, and others) use BLAS functions (through GSL's CBLAS interface).
|
20
|
+
#--
|
21
|
+
# TODO: add type coercions
|
22
|
+
#
|
23
|
+
class Vector
|
24
|
+
include Enumerable
|
25
|
+
|
26
|
+
attr_reader :ptr # :nodoc:
|
27
|
+
attr_reader :size # Vector size
|
28
|
+
|
29
|
+
# Create a Vector of size n. If zero is true, the vector is initialized with zeros.
|
30
|
+
# Otherwise, the vector will contain garbage.
|
31
|
+
# You can optionally pass a block, in which case #map_index! will be called with it (i.e.: it works like Array.new).
|
32
|
+
def initialize(n, zero = false)
|
33
|
+
if (zero) then @ptr = GSL::Backend::gsl_vector_calloc(n)
|
34
|
+
else @ptr = GSL::Backend::gsl_vector_alloc(n) end
|
35
|
+
GSL.set_finalizer(self, :gsl_vector_free, @ptr)
|
36
|
+
|
37
|
+
@size = n # TODO: extract from @ptr
|
38
|
+
|
39
|
+
if (block_given?) then self.map_index! {|i| yield(i)} end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize_copy(other) #:nodoc:
|
43
|
+
ObjectSpace.undefine_finalizer(self) # TODO: ruby bug?
|
44
|
+
@ptr = GSL::Backend::gsl_vector_alloc(other.size)
|
45
|
+
GSL.set_finalizer(self, :gsl_vector_free, @ptr)
|
46
|
+
|
47
|
+
@size = other.size
|
48
|
+
GSL::Backend::gsl_vector_memcpy(@ptr, other.ptr)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Same as Vector.new(n, true)
|
52
|
+
def Vector.zero(n); Vector.new(n, true) end
|
53
|
+
|
54
|
+
# Create a vector from an Array
|
55
|
+
def Vector.from_array(array)
|
56
|
+
if (array.empty?) then raise "Can't create empty vector" end
|
57
|
+
Vector.new(array.size) {|i| array[i]}
|
58
|
+
end
|
59
|
+
|
60
|
+
# See #from_array
|
61
|
+
def Vector.[](*args)
|
62
|
+
Vector.from_array(args)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Generates a Vector of n random numbers between 0 and 1.
|
66
|
+
# NOTE: This simply uses Kernel::rand
|
67
|
+
def Vector.random(n); Vector.new(n).map!{|x| Kernel::rand} end
|
68
|
+
class << self; alias_method :rand, :random end
|
69
|
+
|
70
|
+
# Copy other's values into self
|
71
|
+
def copy(other); GSL::Backend::gsl_vector_memcpy(@ptr, other.ptr); return self end
|
72
|
+
|
73
|
+
# Set all values to v
|
74
|
+
def all!(v); GSL::Backend::gsl_vector_set_all(@ptr, v); return self end
|
75
|
+
alias_method :set!, :all!
|
76
|
+
|
77
|
+
# Set all values to zero
|
78
|
+
def zero!; GSL::Backend::gsl_vector_set_zero(@ptr); return self end
|
79
|
+
|
80
|
+
# Set all values to zero, except the i-th element, which is set to 1
|
81
|
+
def basis!(i); GSL::Backend::gsl_vector_set_basis(@ptr, i); return self end
|
82
|
+
|
83
|
+
# Add other to self
|
84
|
+
def add(other)
|
85
|
+
case other
|
86
|
+
when Numeric; GSL::Backend::gsl_vector_add_constant(@ptr, other.to_f)
|
87
|
+
when Vector; GSL::Backend::gsl_vector_add(@ptr, other.ptr)
|
88
|
+
else raise TypeError, "Unsupported type: #{other.class}" end
|
89
|
+
return self
|
90
|
+
end
|
91
|
+
|
92
|
+
# Substract other from self
|
93
|
+
def sub(other)
|
94
|
+
case other
|
95
|
+
when Numeric; GSL::Backend::gsl_vector_add_constant(@ptr, -other.to_f)
|
96
|
+
when Vector; GSL::Backend::gsl_vector_sub(@ptr, other.ptr)
|
97
|
+
else raise TypeError, "Unsupported type: #{other.class}" end
|
98
|
+
return self
|
99
|
+
end
|
100
|
+
|
101
|
+
# Multiply (element-by-element) other with self
|
102
|
+
def mul(other)
|
103
|
+
case other
|
104
|
+
when Numeric; GSL::Backend::gsl_blas_dscal(other.to_f, @ptr)
|
105
|
+
when Vector; GSL::Backend::gsl_vector_mul(@ptr, other.ptr)
|
106
|
+
else raise TypeError, "Unsupported type: #{other.class}" end
|
107
|
+
return self
|
108
|
+
end
|
109
|
+
|
110
|
+
# Divide (element-by-element) self by other
|
111
|
+
def div(other)
|
112
|
+
case other
|
113
|
+
when Numeric; GSL::Backend::gsl_blas_dscal(1.0 / other, @ptr)
|
114
|
+
when Vector; GSL::Backend::gsl_vector_div(@ptr, other.ptr)
|
115
|
+
else raise TypeError, "Unsupported type: #{other.class}" end
|
116
|
+
return self
|
117
|
+
end
|
118
|
+
|
119
|
+
def +(other); self.dup.add(other) end
|
120
|
+
def -(other); self.dup.sub(other) end
|
121
|
+
def *(other); self.dup.mul(other) end
|
122
|
+
def /(other); self.dup.div(other) end
|
123
|
+
|
124
|
+
# Reverse the order of elements
|
125
|
+
def reverse!; GSL::Backend::gsl_vector_reverse(@ptr); return self end
|
126
|
+
|
127
|
+
# Swap the i-th element with the j-th element
|
128
|
+
def swap(i,j); GSL::Backend::gsl_vector_swap_elements(@ptr, i, j); return self end
|
129
|
+
|
130
|
+
# Access the i-th element (throws exception if out-of-bounds)
|
131
|
+
def [](i); GSL::Backend::gsl_vector_get(@ptr, i) end
|
132
|
+
|
133
|
+
# set the i-th element (throws exception if out-of-bounds)
|
134
|
+
def []=(i, v); GSL::Backend::gsl_vector_set(@ptr, i, v.to_f) end
|
135
|
+
|
136
|
+
# if all elements are zero
|
137
|
+
def zero?; GSL::Backend::gsl_vector_isnull(@ptr) == 1 ? true : false end
|
138
|
+
|
139
|
+
# if all elements are strictly positive (>0)
|
140
|
+
def positive?; GSL::Backend::gsl_vector_ispos(@ptr) == 1 ? true : false end
|
141
|
+
|
142
|
+
#if all elements are strictly negative (<0)
|
143
|
+
def negative?; GSL::Backend::gsl_vector_isneg(@ptr) == 1 ? true : false end
|
144
|
+
|
145
|
+
# if all elements are non-negative (>=0)
|
146
|
+
def nonnegative?; GSL::Backend::gsl_vector_isnonneg(@ptr) == 1 ? true : false end
|
147
|
+
|
148
|
+
def max; GSL::Backend::gsl_vector_max(@ptr) end
|
149
|
+
|
150
|
+
def min; GSL::Backend::gsl_vector_min(@ptr) end
|
151
|
+
|
152
|
+
# Same as Array#minmax
|
153
|
+
def minmax
|
154
|
+
min = FFI::Buffer.new(:double)
|
155
|
+
max = FFI::Buffer.new(:double)
|
156
|
+
GSL::Backend::gsl_vector_minmax(@ptr, min, max)
|
157
|
+
return [min[0].get_float64(0),max[0].get_float64(0)]
|
158
|
+
end
|
159
|
+
|
160
|
+
# Same as #minmax, but returns the indices to the elements
|
161
|
+
def minmax_index
|
162
|
+
min = FFI::Buffer.new(:size_t)
|
163
|
+
max = FFI::Buffer.new(:size_t)
|
164
|
+
GSL::Backend::gsl_vector_minmax_index(@ptr, min, max)
|
165
|
+
#return [min[0].get_size_t(0),max[0].get_size_t(0)]
|
166
|
+
return [min[0].get_ulong(0),max[0].get_ulong(0)]
|
167
|
+
end
|
168
|
+
|
169
|
+
# Same as #min, but returns the index to the element
|
170
|
+
def min_index; GSL::Backend::gsl_vector_min_index(@ptr) end
|
171
|
+
|
172
|
+
# Same as #max, but returns the index to the element
|
173
|
+
def max_index; GSL::Backend::gsl_vector_max_index(@ptr) end
|
174
|
+
|
175
|
+
# Dot product between self and other (uses BLAS's ddot)
|
176
|
+
def dot(other)
|
177
|
+
out = FFI::MemoryPointer.new(:double)
|
178
|
+
GSL::Backend::gsl_blas_ddot(@ptr, other.ptr, out)
|
179
|
+
return out[0].get_double(0)
|
180
|
+
end
|
181
|
+
alias_method :^, :dot
|
182
|
+
|
183
|
+
# Norm 2 of the vector (uses BLAS's dnrm2)
|
184
|
+
def norm; GSL::Backend::gsl_blas_dnrm2(@ptr) end
|
185
|
+
alias_method :length, :norm
|
186
|
+
|
187
|
+
# Returns the sum of all elements (uses BLAS's dasum)
|
188
|
+
def sum; GSL::Backend::gsl_blas_dasum(@ptr) end
|
189
|
+
|
190
|
+
# Optimized version of: self += other * alpha (where alpha is a Numeric). Uses BLAS's daxpy.
|
191
|
+
def mul_add(other, alpha); GSL::Backend::gsl_blas_daxpy(alpha, other.ptr, @ptr); return self end
|
192
|
+
|
193
|
+
# Yields the block for each element in the Vector
|
194
|
+
def each # :yield: obj
|
195
|
+
@size.times {|i| yield(self[i])}
|
196
|
+
end
|
197
|
+
|
198
|
+
# Efficient map! implementation
|
199
|
+
def map!(&block); GSL::Backend::gsl_vector_map(@ptr, block); return self end
|
200
|
+
|
201
|
+
# Alternate version of #map!, in this case the block receives the index as a parameter.
|
202
|
+
def map_index!(&block); GSL::Backend::gsl_vector_map_index(@ptr, block); return self end
|
203
|
+
|
204
|
+
# See #map!. Returns a Vector.
|
205
|
+
def map(&block); self.dup.map!(block) end
|
206
|
+
|
207
|
+
def sort!; GSL::Backend::gsl_sort_vector(@ptr); return self end
|
208
|
+
def sort; self.dup.sort! end
|
209
|
+
|
210
|
+
def join(sep = $,)
|
211
|
+
s = "#{self[0]}"
|
212
|
+
(1..(@size-1)).each do |e|
|
213
|
+
s += sep + self[e].to_s
|
214
|
+
end
|
215
|
+
return s
|
216
|
+
end
|
217
|
+
|
218
|
+
def to_s
|
219
|
+
"[" + self.join(', ') + "]"
|
220
|
+
end
|
221
|
+
|
222
|
+
def to_a
|
223
|
+
Array.new(@size) {|i| self[i]}
|
224
|
+
end
|
225
|
+
|
226
|
+
# Element-by-element comparison (uses #each_with_index)
|
227
|
+
# Admits comparing to Array
|
228
|
+
def ==(other)
|
229
|
+
if (self.size != other.size) then return false end
|
230
|
+
self.each_with_index do |elem,i|
|
231
|
+
if (elem != other[i]) then return false end
|
232
|
+
end
|
233
|
+
return true
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
data/test/test_gsl.rb
ADDED
data/test/vector_test.rb
ADDED
@@ -0,0 +1,75 @@
|
|
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 'gsl'
|
9
|
+
|
10
|
+
class VectorTest < Test::Unit::TestCase
|
11
|
+
def test_initialize
|
12
|
+
assert_equal(10, GSL::Vector.new(10).size)
|
13
|
+
assert_equal(10, GSL::Vector.zero(10).size)
|
14
|
+
assert(GSL::Vector.zero(10).zero?)
|
15
|
+
assert_nothing_raised { GSL::Vector.random(10) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_to_s
|
19
|
+
assert_equal("[0.0]", GSL::Vector.zero(1).to_s)
|
20
|
+
assert_equal("[0.0, 0.0, 0.0]", GSL::Vector.zero(3).to_s)
|
21
|
+
assert_equal("[0.0, 1.0, 2.0]", GSL::Vector.new(3) {|i| i}.to_s)
|
22
|
+
assert_equal("[1.0, 2.0, 3.0]", GSL::Vector[1,2,3].to_s)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_equal
|
26
|
+
assert_equal(GSL::Vector[1,2,3], [1,2,3])
|
27
|
+
assert_equal(GSL::Vector[1,2,3], GSL::Vector[1,2,3])
|
28
|
+
assert_equal(GSL::Vector.zero(3), GSL::Vector.zero(3))
|
29
|
+
assert_not_equal(GSL::Vector.zero(4), GSL::Vector.zero(3))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_copies
|
33
|
+
v1 = GSL::Vector[1,2,3]
|
34
|
+
v2 = GSL::Vector[2,3,4]
|
35
|
+
assert_not_equal(v1, v2)
|
36
|
+
assert_equal(v1.copy(v2), v2)
|
37
|
+
assert_equal(v1.dup, v2)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_sets
|
41
|
+
assert_equal(GSL::Vector.zero(3).set!(1), GSL::Vector[1,1,1])
|
42
|
+
assert_equal(GSL::Vector.zero(3).set!(1).zero!, GSL::Vector.zero(3))
|
43
|
+
assert_equal(GSL::Vector.zero(3).basis!(1), GSL::Vector[0,1,0])
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_operators
|
47
|
+
assert_equal(GSL::Vector[2,3,4],GSL::Vector[1,2,3] + GSL::Vector[1,1,1])
|
48
|
+
assert_equal(GSL::Vector[1,0,3],GSL::Vector[1,2,3] * GSL::Vector[1,0,1])
|
49
|
+
assert_equal(GSL::Vector[0,1,2],GSL::Vector[1,2,3] - GSL::Vector[1,1,1])
|
50
|
+
assert_equal(GSL::Vector[0.5,1,1.5],GSL::Vector[1,2,3] / GSL::Vector[2,2,2])
|
51
|
+
|
52
|
+
assert_equal(GSL::Vector[3,6,9],GSL::Vector[1,2,3] * 3)
|
53
|
+
assert_equal(GSL::Vector[4,5,6],GSL::Vector[1,2,3] + 3)
|
54
|
+
assert_equal(GSL::Vector[-2,-1,0],GSL::Vector[1,2,3] - 3)
|
55
|
+
assert_equal(GSL::Vector[0.5,1,1.5],GSL::Vector[1,2,3] / 2)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_other
|
59
|
+
assert_equal(GSL::Vector[1,2,3], GSL::Vector[1,2,3].to_a)
|
60
|
+
assert_equal(GSL::Vector[1,2,3], GSL::Vector[3,1,2].sort)
|
61
|
+
assert_equal(6, GSL::Vector[3,1,2].sum)
|
62
|
+
assert_equal(GSL::Vector[3,1,2].mul_add(GSL::Vector[1,0,1],0.5), GSL::Vector[3.5,1,2.5])
|
63
|
+
assert_equal(3, GSL::Vector[2,1,2].norm)
|
64
|
+
assert_equal(5, GSL::Vector[3,1,2] ^ GSL::Vector[1,0,1])
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_minmax
|
68
|
+
assert_equal(1, GSL::Vector[1,2,3].min)
|
69
|
+
assert_equal(3, GSL::Vector[1,2,3].max)
|
70
|
+
assert_equal([1,3], GSL::Vector[1,2,3].minmax)
|
71
|
+
assert_equal(0, (GSL::Vector[1,2,3]).min_index)
|
72
|
+
assert_equal(2, (GSL::Vector[1,2,3]).max_index)
|
73
|
+
assert_equal([0,2], GSL::Vector[1,2,3].minmax_index)
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-gsl-ng
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- v01d
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-09 00:00:00 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ffi
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.3
|
34
|
+
version:
|
35
|
+
description: "* New-generation Ruby/GSL wrapper."
|
36
|
+
email:
|
37
|
+
- phreakuencies@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions:
|
41
|
+
- ext/extconf.rb
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- Manifest.txt
|
45
|
+
- README.txt
|
46
|
+
files:
|
47
|
+
- .autotest
|
48
|
+
- History.txt
|
49
|
+
- Manifest.txt
|
50
|
+
- README.txt
|
51
|
+
- Rakefile
|
52
|
+
- ext/extconf.rb
|
53
|
+
- ext/gslng_extensions.cpp
|
54
|
+
- lib/gsl.rb
|
55
|
+
- lib/gsl/backend.rb
|
56
|
+
- lib/gsl/finalizer.rb
|
57
|
+
- lib/gsl/vector.rb
|
58
|
+
- test/test_gsl.rb
|
59
|
+
- test/vector_test.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://ruby-gsl-ng.googlecode.com
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --main
|
67
|
+
- README.txt
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
- ext
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "0"
|
82
|
+
version:
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project: ruby-gsl-ng
|
86
|
+
rubygems_version: 1.3.5
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: "* New-generation Ruby/GSL wrapper."
|
90
|
+
test_files:
|
91
|
+
- test/test_gsl.rb
|