GSL::Vector.new(argv)GSL::Vector.alloc(argv)These methods create a GSL::Vector object. With an integer, a vector object is initialized in a given size,
require('gsl')
v = Vector.new(10) ( or use 'alloc' )
Vector elements will be set with the 'set' method.
One can create a vector by giving an array, as
v = Vector.new([1, 2, 3]) v = Vector.new(1, 2, 3)
The vector created has the same length of the given array.
GSL::Vector.GSL::Vector.calloc(size)GSL::Vector#get(i)This returns the i-th element of the vector self. The [] method is also available.
ex)
p v.get(2) p v[2]
GSL::Vector#set(i, val)This method sets the i-th element of the vector self to val.
v.set(2, 3.5) # v[2] = 3.5
GSL::Vector#set_all(x)This method sets all the elements of the vector to the value x.
v.set_all(3.5)
GSL::Vector#set_zeroGSL::Vector#set_basis!(i)This method makes a basis vector by setting all the elements of the vector to zero except for the i-th element, which is set to one. For a vector v of the size 10, the method
v.set_basis!(4)
sets the vector self to a basis vector [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]. Note that the vector self is modified.
GSL::Vector#set_basis(i)This method returns a basis vector by setting all the elements of the vector to zero except for the i-th element which is set to one. For a vector v of the size 10, the method
vb = v.set_basis(4)
creates a new vector self with elements [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]. The vector self is not changed.
GSL::Vector#eachIterator for each vector element.
v.each do |x| p x end
GSL::Vector#each_indexGSL::Vector#printGSL::Vector#fprintf(io, format)GSL::Vector#fprintf(filename, format)GSL::Vector#fscanf(io)GSL::Vector#fscanf(filename)GSL::Vector#fwrite(io)GSL::Vector#fwrite(filename)GSL::Vector#fread(io)GSL::Vector#fread(filename)GSL::Vector#cloneGSL::Vector#swap_elements(i, j)GSL::Vector#reverseThe GSL::Vector::View class is defined to be used as "references" to
vectors. The Vector::View class is a subclass of the class Vector,
so an instance of the View class created by slicing a Vector object
can be used same as a Vector object. The
View object shares the data with the original vector i.e. modifications
in the elements of the View object affect to the original vector.
GSL::Vector#subvector(offset, n)This method creates a Vector::View object slicing n elements
of the vector self from the offset offset.
ex)
#!/usr/bin/env ruby
require("gsl")
v = Vector.new([1, 2, 3, 4, 5, 6])
view = v.subvector(1, 4)
p view.class <----- GSL::Vector::View
view.print <----- [ 2.000e+00 3.000e+00 4.000e+00 5.000e+00 ]
view[2] = 99
view.print <----- [ 2.000e+00 3.000e+00 9.900e+01 5.000e+00 ]
v.print <----- [ 1.000e+00 2.000e+00 3.000e+00 9.900e+01 5.000e+00 6.000e+00 ]GSL::Vector#subvector_with_stride(offset, n, stride)GSL::Vectir#matrix_view(n1, n2)This creates a Matrix::View object from the vector self.
It enables to use the vector as a Matrix object.
ex)
v2 = Vector.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
mview = v2.matrix_view(3, 3)
p mview.class <----- GSL::Matrix::View
mview.print <----- [ 1.000e+00 2.000e+00 3.000e+00
4.000e+00 5.000e+00 6.000e+00
7.000e+00 8.000e+00 9.000e+00 ]
mview.set(2, 1, 99.9)
mview.print <----- [ 1.000e+00 2.000e+00 3.000e+00
4.000e+00 5.000e+00 6.000e+00
7.000e+00 9.990e+01 9.000e+00 ]
v2.print
<----- [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 5.000e+00 6.000e+00 7.000e+00 9.990e+01 9.000e+00 ]GSL::Vector#add!(b)GSL::Vector#add(b)GSL::Vector#sub!(b)GSL::Vector#sub(b)GSL::Vector#mul!(b)GSL::Vector#mul(b)GSL::Vector#div!(b)GSL::Vector#div(b)GSL::Vector#scale!(x)GSL::Vector#scale(x)GSL::Vector#add_constant!(x)GSL::Vector#add_constant(x)GSL::Vector#reverseGSL::Vector#swap_elementsGSL::Vector#cloneGSL::Vector#maxGSL::Vector#minGSL::Vector#minmaxGSL::Vector#max_indexGSL::Vector#min_indexGSL::Vector#minmax_indexGSL::Vector#to_aThis method converts the vector into a Ruby array. A Ruby array also can be
converted into a GSL::Vector object with the to_gv method. For example,
v = GSL::Vector.alloc([1, 2, 3, 4, 5]) v.print -> 1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 a = v.to_a -> GSL::Vector to an array p a -> [1.0, 2.0, 3.0, 4.0, 5.0] a[2] = 12.0 v2 = a.to_gv -> a new GSL::Vector object v2.print -> 1.0000e+00 2.0000e+00 1.2000e+01 4.0000e+00 5.0000e+00
GSL::Vector#to_naGSL::Vector.to_gv(na)GSL::Vector.to_gslv(na)A GSL::Vector object is converted into an NArray object and vice versa. To enable this, you should compile Ruby/GSL with an option, as
% ruby setup.rb config -- --with-narray-include=<dir where narray.h is found>
ex)
require 'narray'
require 'gsl'
# GSL::Vector
v = GSL::Vector.new([1, 2, 3, 4])
p v <---- [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 ]
#<GSL::Vector>
# NArray
na = v.to_na
p na <---- NArray.float(4):
[ 1.0, 2.0, 3.0, 4.0 ]
# GSL::Vector
v2 = Vector.to_gv(na) <---- to GSL::Vector object