Matrices

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 = "%e")
GSL::Matrix#fprintf(filename, format = "%e")
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
The Matrix object self is converted into an NMatrix object. The matrix data are copied to newly allocated memory.
GSL::Matrix.to_gm(na)
A GSL::Vector::View object is created. The array data of na are not copied, thus any modifications to the View object affect on the original NArray object na. The View object can be used as a reference to the NMatrix object.

( back