Google

PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

GSL_Vector class

Class methods

GSL_Vector.GSL_Vector.new(argv)
GSL_Vector.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 = GSL_Vector.new(10) ( 'alloc' is also available )

Vector elements will be set with the 'set' method.

One can create a vector giving an array, as

v = GSL_Vector.new([1, 2, 3])

The size of the vector created is the same of the given array.

GSL_Vector.GSL_Vector.calloc(size)

This method creates a vector object, and initializes all the elements to zero.

Methods

GSL_Vector#get(i)

This returns the i-th element of the reciever vector. The [] method is also available.

p v.get(2)
p v[2] 
GSL_Vector#set(argv)

This method sets the value of the elements of the vector object. With two arguments as (i, x), where i is an integer and x is a number, the i-th element of the vector is set to x.

v.set(2, 3.5)      # v[2] = 3.5

One can give an array to set elements, as

v.set([1.2, 4.6, 6])

The size of the array given must be smaller than that of the vector.

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_zero

This method sets all the elements of the vector to zero.

GSL_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 v itself to a basis vector [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]. Note that the vector v 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 vb with elements [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]. The vector v is not changed.

GSL_Vector#each

An iterator for each vector element.

v.each do |x|
  p x
end
GSL_Vector#print

Show all the elements of the vector in %4.3e format.

GSL_Vector#fprintf(io, format)

This method writes the vector elements line-by-line to the io object io using the format specifier format. One doesn't have to use this method in a loop to print each data to the file.

GSL_Vector#fscanf(io)

This method reads formatted data from the io object io into the vector, as

v = GSL_Vector.new(15)
f = File.open("smpv.dat")
v.fscanf(f)
f.close
v.printf("%f")

The method printf is equivalent to a call of the method fprintf with an argument stdout, as v.fprintf(stdout, "%f"). The fscanf method uses the size of the vector to determine how many numbers to read.

GSL_Vector#clone

This method creates a new vector of the same elements.

GSL_Vector#swap_elements(i, j)

This method exchanges the i-th and j-th elements of the vector in-place.

GSL_Vector#reverse

This method reverses the order of the elements of the vector.

Vector operations

GSL_Vector#add!(b)

This method adds the elements of vector b to the elements of vector self. The receiver self is modified in place. The two vectors must have the same length.

GSL_Vector#add(b)

This method adds the elements of vector b to the elements of the vector self, and returns a new vector. The vector self is not changed.

GSL_Vector#sub!(b)

This method subtracts the elements of vector b from the elements of vector self. The two vectors must have the same length. The vector self is modified in place.

GSL_Vector#sub(b)

Same as GSL_Vector#sub!(b), but not modifies the reciever, and returns a new vector.

GSL_Vector#mul!(b)

This method multiplies the elements of vector self by the elements of vector b. The two vectors must have the same length. The reciever self is modified.

GSL_Vector#mul(b)

Same as GSL_Vector#mul!(b), but not modifies the reciever, and returns a new vector.

GSL_Vector#div!(b)

This method divides the elements of vector self by the elements of vector b. The two vectors must have the same length. The reciever self is modified.

GSL_Vector#div(b)

Same as GSL_Vector#div!(b), but not modifies the reciever, and returns a new vector.

GSL_Vector#scale!(x)
GSL_Vector#scale(x)

This method multiplies the elements of vector self by the constant factor x.

GSL_Vector#add_constant!(x)
GSL_Vector#add_constant(x)

This method adds the constant value x to the elements of the vector self.

Finding maximum and minimum elements of vectors

GSL_Vector#max

This method returns the maximum value in the reciever vector.

GSL_Vector#min

This method returns the munimum value in the reciever vector.

GSL_Vector#minmax

This method returns an array of two elements, the minimum and the maximum values in the vector self.

GSL_Vector#max_index

This method returns the index of the maximum value in the vector. When there are several equal maximum elements then the lowest index is returned.

GSL_Vector#min_index

This method returns the index of the minimum value in the vector. When there are several equal minimum elements then the lowest index is returned.

GSL_Vector#minmax_index

This method returns an array of two elements which has the indices of the minimum and the maximum values in the vector self.

===

GSL_Vector#to_a

This method converts the vector into a Ruby array. A Ruby array also can be converted into a GSL_Vector object with the to_v 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_v  -> 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_na
GSL_Vector#to_narray
GSL_Vector#to_NArray

This method converts a GSL_Vector object into an NArray object. An NArray object is also converted into a GSL_Vector object with the method to_v or to_gv.

ex)
require 'narray'
require 'gsl/gsl_array'

# 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::GSL_Vector>
# NArray
na = v.to_na
p na                  <---- NArray.float(4): 
                            [ 1.0, 2.0, 3.0, 4.0 ]
# GSL_Vector
v2 = na.to_gv         <---- GSL_Vector object

To enable this, one should compile Ruby/GSL with the option flag with-narray-include=....