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

# GSL_Matrix class

## Class methods

`GSL_Matrix.new(argv)`
`GSL_Matrix.alloc(argv)`

These methods create a GSL_Matrix object which contains matrix elements. 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.

• with two integers and a number,

`m.set(1, 2, 3.2)         -> the (1, 2) element is set to 3.2`
• with arrays,

```m.set([6, 5, 6], [4, 5, 7], [8, 5, 21])
->
6   5   6
4   5   7
8   5  21```
• with an array and sizes,

```m.set([4, 5, 6, 8, 98, 6, 4, 3, 1], 3, 3)
->
4   5   6
8  98   6
4   3   1```
`GSL_Matrix#get(i, j)`

This method returns the (i,j)th element of the reciever matrix self.

`GSL_Matrix#set_all(x)`

This method sets all the elements of the reciever matrix 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 reciever 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.

`GSL_Matrix#fprintf(io, format)`

This method writes the matrix elements line-by-line to the io object io using the format specifier format.

`GSL_Matrix#fscanf(io)`

This method reads formatted data from the io object io into the matrix.

### 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 reciever, 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 reciever matrix.

`GSL_Matrix#transpose`

This method replaces the reciever 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 reciever 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 reciever 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 reciever 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 reciever 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 reciever 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 reciever matrix.

`GSL_Matrix#minmax`

This method returns a two elements array [min, max], which contains the minimum and the maximum values in the reciever matrix.

`GSL_Matrix#max_index`
`GSL_Matrix#min_index`

These methods return the index of the max/min value in the reciever 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 reciever matrix.

===

`GSL_Matrix#to_na`
`GSL_Matrix#to_narray`
`GSL_Matrix#to_NArray`

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_m or to_gm.

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

# 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 = m.to_gm
p gm                          <---- [ 0.000e+00 1.200e+00 1.000e+00
1.500e+00 0.000e+00 2.000e+00 ]
#<GSL::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.