GSL::Matrix class

Class methods

GSL::Matrix.new(n)
GSL::Matrix.new(array)
GSL::Matrix.new(arrays)

These methods create a GSL::Matrix object. One can initialize a matrix with arrays,

require 'gsl'

m = GSL::Matrix::new([1, 2, 3], [6, 5, 4], [7, 8, 1])
  -> a 3x3 matrix is created,
    1  2  3
    4  5  6
    7  8  9

With an array and rows&cols,

m = GSL::Matrix::new([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3)

Methods

GSL::Matrix#set(argv)
This method sets elements of the matrix in various manners.
GSL::Matrix#get(i, j)
This method returns the (i,j)th element of the matrix self.
GSL::Matrix#set_all(x)
This method sets all the elements of the matrix self to the value x.
GSL::Matrix#set_zero
This method sets all the elements of the matrix to zero.
GSL::Matrix#set_identity
This method sets the elements of the matrix to the corresponding elements of the identity matrix, i.e. a unit diagonal with all off-diagonal elements zero. This applies to both square and rectangular matrices.

IO

GSL::Matrix#fwrite(io)
GSL::Matrix#fwrite(filename)
GSL::Matrix#fread(io)
GSL::Matrix#fread(filename)
GSL::Matrix#fprintf(io, format)
GSL::Matrix#fprintf(filename, format)
GSL::Matrix#fscanf(io)
GSL::Matrix#fscanf(filename)

Creating row and column vectors

GSL::Matrix#row(i)
This method returns a vector of the i-th row of the matrix.
GSL::Matrix#column(i)
GSL::Matrix#col(i)
These methods return a vector of the j-th column of the matrix.
GSL::Matrix#diagonal
This method returns a vector of the diagonal of the matrix.
GSL::Matrix#set_row(i, v)
This method copies the elements of the vector v into the i-th row of the matrix. The length of the vector must be the same as the length of the row.
GSL::Matrix#set_col(j, v)
This method copies the elements of the vector v into the j-th column of the matrix. The length of the vector must be the same as the length of the column.
GSL::Matrix#swap_rows!(i, j)
This method exchanges the i-th and j-th rows of the matrix in-place.
GSL::Matrix#swap_rows(i, j)
Same as GSL::Matrix#swap_rows!(i, j), but not modifies the matrix self, and returns a new matrix.
GSL::Matrix#swap_columns!(i, j)
GSL::Matrix#swap_columns(i, j)
This method exchanges the i-th and j-th columns of the matrix.
GSL::Matrix#swap_rowcol!(i, j)
GSL::Matrix#swap_rowcol(i, j)
This method exchanges the i-th row and j-th column of the matrix. The matrix must be square for this operation to be possible.
GSL::Matrix#transpose!
This method returns a matrix of a transpose of the matrix.
GSL::Matrix#transpose
This method replaces the matrix by its transpose by copying the elements of the matrix in-place. The matrix must be square for this operation to be possible.

Matrix operations

GSL::Matrix#add(b)
GSL::Matrix#add!(b)
This method adds the elements of matrix b to the elements of the matrix. The two matrices must have the same dimensions.
GSL::Matrix#sub!(b)
GSL::Matrix#sub(b)
This method subtracts the elements of matrix b from the elements of the matrix. The two matrices must have the same dimensions.
GSL::Matrix#mul_elements!(b)
GSL::Matrix#mul_elements(b)
This method multiplies the elements of the matrix by the elements of matrix b. The two matrices must have the same dimensions.
GSL::Matrix#div_elements!(b)
GSL::Matrix#div_elements(b)
This method divides the elements of the matrix by the elements of matrix b. The two matrices must have the same dimensions.
GSL::Matrix#scale!(x)
GSL::Matrix#scale(x)
This method multiplies the elements of the matrix by the constant factor x.
GSL::Matrix#add_constant!(a)
GSL::Matrix#add_constant(a)
This method adds the constant value x to the elements of the matrix.

Finding maximum and minimum elements of matrices

GSL::Matrix#max
GSL::Matrix#min
These methods return the max/min value in the matrix.
GSL::Matrix#minmax
This method returns a two elements array [min, max], which contains the minimum and the maximum values in the matrix.
GSL::Matrix#max_index
GSL::Matrix#min_index
These methods return the index of the max/min value in the matrix.
GSL::Matrix#minmax_index
This method returns a two elements array [imin, imax], which contains the indices of the minimum and the maximum value in the matrix.

NArray

GSL::Matrix#to_na

This method converts a GSL::Matrix object into an NArray object, which is provided by the NArray extension developed by M.Tanaka. An NArray object is also converted into a GSL::Matrix object with the method to_gm or to_gslm.

ex)
require 'narray'
require 'gsl'

# an NArray object
m = NMatrix[[0, 1.2, 1],[1.5, 0, 2]]     
p m                           <----- NMatrix.float(3,2): 
                                     [ [ 0.0, 1.2, 1.0 ], 
                                       [ 1.5, 0.0, 2.0 ] ]
# GSL::Matrix
gm = GSL::Matrix.to_gm(m)                             
p gm                          <---- [ 0.000e+00 1.200e+00 1.000e+00 
                                      1.500e+00 0.000e+00 2.000e+00 ]
                                    #<GSL::Matrix>
# NArray
m2 = gm.to_na                            
p m2                          <---- NArray.float(3,2): 
                                    [ [ 0.0, 1.2, 1.0 ], 
                                      [ 1.5, 0.0, 2.0 ] ]

Note that you should compile Ruby/GSL with the option flag with-narray-include=... to use these methods.

back