Path: | rdoc/matrix.rdoc |
Last Update: | Sun Nov 14 14:53:48 -0800 2010 |
Contents:
These methods create a GSL::Matrix object.
>> m = GSL::Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]] => GSL::Matrix [ 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 ]
m = GSL::Matrix.alloc([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3)
>> m = GSL::Matrix.alloc(1..3, 4..6, 7..9) [ 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 ] >> m2 = GSL::Matrix[1..6, 2, 3] [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 5.000e+00 6.000e+00 ]
Examples:
>> m = GSL::Matrix::Int.eye(3) => GSL::Matrix::Int [ 1 0 0 0 1 0 0 0 1 ] >> m = GSL::Matrix::Int.eye(2, 4) => GSL::Matrix::Int [ 1 0 0 0 0 1 0 0 ]
Create diagonal matrices of dimensions n*n, of values 1.0.
Creates a diagonal matrix of given elements.
Example:
>> GSL::Matrix::Int.diagonal(1..4) => GSL::Matrix::Int [ 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 ] >> GSL::Matrix::Int.diagonal(2, 5, 3) => GSL::Matrix::Int [ 2 0 0 0 5 0 0 0 3 ]
Create a matrix of all the elements 1.
Create a matrix of all the elements 1.
Example:
>> m = GSL::Matrix::Int.indgen(3, 5) => GSL::Matrix::Int [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ] >> m = GSL::Matrix::Int.indgen(3, 5, 2) => GSL::Matrix::Int [ 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ] >> m = GSL::Matrix.indgen(2, 3, 4.5, 6.7) => GSL::Matrix [ 4.500e+00 1.120e+01 1.790e+01 2.460e+01 3.130e+01 3.800e+01 ]
Matrix dimensions are limited within the range of Fixnum. For 32-bit CPU, the maximum of matrix dimension is 2^30 ~ 1e9.
Returns the number of rows of matrix self.
Returns the number of columns of matrix self.
Returns the number of rows and columns as an array.
Ex:
>> m.size1 => 3 >> m.size2 => 5 >> m.shape => [3, 5]
If args is empty and val is an Array (i.e. called with just a single Array argument), the Array‘s elements are taken as row contents. Each given row must have exactly the same number of elements as the Matrix has columns, but the number of rows given need not match the Matrix‘s row count. Extra given rows are ignored, while Matrix rows beyond those given are not affected. Otherwise, if args is empty, behaves as set_all(val).
If args is an Array and val is not, the first two elements of args must be Fixnums which specify the row and column of the element that will be set to the value of val. This special case exists to allow values returned by Matrix#max_index and Matrix#min_index to be used as indexes.
If args are two Fixnums, i and j, this method sets the (i,j)-th element of the matrix self to val.
If args is a single Fixnum, i, this method sets the element at row i/size2, column i%size2 to val.
For set, if args is empty and val is an Array of Arrays, the contents of self are set row by row from the elements (i.e. Arrays) of val.
All other args specify a submatrix (as with submatrix) whose elements are assigned from val. In this case, val can be an Array whose elements will be assigned to the rows of the submatrix, Range whose elements will be assigned to the elements of the submatrix, GSL::Matrix whose elements will be assigned to the elements of the submatrix, or Numeric that will be assigned to all elements of the submatrix.
NOTE: GSL does not provide a matrix copy function that properly copies data across overlapping memory regions, so watch out if assigning to part of a Matrix from another part of itself (see set example of GSL::Vector).
If args are two Fixnums, i and j, this method returns the (i,j)-th element of the matrix self.
If args is a single Fixnum, i, this method returns the element at row i/size2, column i%size2.
All other forms of args are treated as with Matrix#submatrix and a View object is returned.
NOTE: The behavior of the single Fixnum argument case is different from earlier versions (< 1.11.2) of Ruby/GSL. These earlier versions returned a Vector::View in this case, thereby allowing element (i,j) to be accessed as m[i][j]. THIS FORM IS NO LONGER SUPPORTED as of Ruby/GSL 1.11.2. Existing occurences of this construct will need to be replaced with the backwards compatible and more efficient m[i,j] or, equivalent to the old and less efficient form, m[i,nil][j]. For GSL::Matrix, the old form will now raise a NoMethodError because Float has no #[] method. For GSL::Matrix::Int, however, the old form will return a single bit from an element of the matrix because Fixnum and Bignum have a #[] method that allows access to the number‘s individual bits.
Examples:
>> m = GSL::Matrix[1..9, 3, 3] => GSL::Matrix [ 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 ] >> m[1, 2] => 6.0 >> m[1, 2] = 123 # m.set(1, 2, 123) => 123 >> m => GSL::Matrix [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 5.000e+00 1.230e+02 7.000e+00 8.000e+00 9.000e+00 ] >> m[1] => 2.0 >> m.set([3, 5, 2], [4, 5, 3], [7, 1, 5]) => GSL::Matrix [ 3.000e+00 5.000e+00 2.000e+00 4.000e+00 5.000e+00 3.000e+00 7.000e+00 1.000e+00 5.000e+00 ] >> m[1][1] # old/unsupported form NoMethodError: undefined method `[]' for 2.0:Float from (irb):8 >> m = GSL::Matrix::Int[1..9, 3, 3] => GSL::Matrix::Int [ 1 2 3 4 5 6 7 8 9 ] >> m[1] # m[0,1] => 2 >> m[1][0] # Bit 0 of m[0,1] => 0 >> m[1][1] # Bit 1 of m[0,1] => 1 >> m[1][2] # Bit 2 of m[0,1] => 0 >> m[1][3] # Bit 3 of m[0,1] => 0
Converts the Matrix self to a Ruby Array of Arrays.
Example:
>> GSL::Matrix.eye(3).to_a => [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
This method sets all the elements of the matrix self to the value x.
This method sets all the elements of the matrix to zero.
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.
The GSL::Matrix::View class is defined to be used as "references" to matrices. The Matrix::View class is a subclass of Matrix, and an instance of the View class created by slicing a Matrix object can be used same as the original matrix. The View object shares the data with the original matrix, i.e. any changes in the elements of the View object affect to the original.
The primary means of generating Matrix::View objects is with GSL::Matrix#submatrix (or its alias GSL::Matrix#view). Many forms are supported and they are documented here individually. All forms return a Matrix::View unless otherwise documented. In the list below, the parameter name indicates the type of the parameter: i, row, col, len, len1, and len2 are Fixnums; rows and cols are Ranges.
View covers all rows and all columns.
View covers single element at row i/size2, column i%size2.
View covers all rows and all columns.
View covers all rows with columns specified by cols.
Returns a Vector::Col::View for the column col.
View covers rows specified by rows and all columns.
View covers rows specified by rows, columns specified by cols.
Returns a Vector::Col::View for column col, rows rows.
Returns a Vector::View for row row.
Returns a Vector::View for row row, columns cols.
View covers a single element at row row, column col.
View covers all rows and len columns starting at column col.
View covers rows rows and len columns starting at column col.
View covers len rows starting at row row and all columns.
View covers len rows starting at row row and cols columns.
View covers len1 rows starting at row row and len2 columns starting at column col.
This creates a Matrix::View object from the vector self.
Ex:
>> v = Vector[1..9] => GSL::Vector [ 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 ] >> m = v.matrix_view(3, 3) => GSL::Matrix::View [ 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 ] >> m[1][1] = 99.99 => 99.99 >> v => GSL::Vector [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 9.999e+01 6.000e+00 7.000e+00 8.000e+00 9.000e+00 ] >>
These methods return i-th row of the matrix as a Vector::View object. Any modifications to the Vectror::View object returned by this method propagate to the original matrix.
These methods return a vector view of the j-th column of the matrix.
Returns a vector view of the i-th row of the matrix self beginning at offset elements past the first column and containing n elements. (>= GSL-1.10)
Returns a vector view of the j-th column of the matrix self beginning at offset elements past the first row and containing n elements. (>= GSL-1.10)
This method returns a Vector::View of the diagonal of the matrix. The matrix is not required to be square. For a rectangular matrix the length of the diagonal is the same as the smaller dimension of the matrix.
Ex:
>> m = GSL::Matrix[1..9, 3, 3] => GSL::Matrix [ 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 ] >> m.row(1) => GSL::Vector::View [ 4.000e+00 5.000e+00 6.000e+00 ] >> m.col(2) => GSL::Vector::Col::View [ 3.000e+00 6.000e+00 9.000e+00 ] >> m.col(2)[2] = 123 => 123 >> m => GSL::Matrix [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 5.000e+00 6.000e+00 7.000e+00 8.000e+00 1.230e+02 ] >> m.diagonal => GSL::Vector::View: [ 1.000e+00 5.000e+00 1.230e+02 ]
Returns a vector view view of the k-th subdiagonal of the matrix self. The matrix is not required to be square. The diagonal of the matrix corresponds to k = 0.
Returns a vector view of the k-th superdiagonal of the matrix self. The matrix is not required to be square. The diagonal of the matrix corresponds to k = 0.
Creates a GSL::Vector object "flattening" the rows of the matrix self.
>> m = GSL::Matrix[1..6, 2, 3] => GSL::Matrix [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 5.000e+00 6.000e+00 ] >> m.to_v => GSL::Vector [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 5.000e+00 6.000e+00 ]
Iterator for each of rows in the matrix self.
Iterator for each of columns in the matrix self.
Create a new matrix of the same elements.
This method returns a new vector (not a view) which contains the elements of the i-th row of the matrix self.
This method returns a new vector (not a view) which contains the elements of the j-th column of the matrix self.
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.
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.
This method exchanges the i-th and j-th rows of the matrix in-place.
This method creates a new matrix exchanging the i-th and j-th rows of the matrix self.
This method exchanges the i-th and j-th columns of the matrix in-place.
This method creates a new matrix exchanging the i-th and j-th columns of the matrix self.
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.
This method returns a matrix of a transpose of the matrix. The matrix self is not modified.
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.
Example:
>> m = GSL::Matrix::Int[1..9, 3, 3] => GSL::Matrix::Int [ 1 2 3 4 5 6 7 8 9 ] >> m.reverse_rows => GSL::Matrix::Int [ 7 8 9 4 5 6 1 2 3 ]
Example:
>> m = GSL::Matrix::Int[1..9, 3, 3] => GSL::Matrix::Int [ 1 2 3 4 5 6 7 8 9 ] >> m.reverse_rows.reverse_columns => GSL::Matrix::Int [ 9 8 7 6 5 4 3 2 1 ]
Return a copy of self with the elements rotated counterclockwise in 90-degree increments. The argument n is optional, and specifies how many 90-degree rotations are to be applied (the default value is 1). Negative values of n rotate the matrix in a clockwise direction.
Examples:
>> m = GSL::Matrix::Int[1..6, 2, 3] => GSL::Matrix::Int [ 1 2 3 4 5 6 ] >> m.rot90 => GSL::Matrix::Int [ 3 6 2 5 1 4 ] >> m.rot90(2) => GSL::Matrix::Int [ 6 5 4 3 2 1 ] >> m.rot90(3) => GSL::Matrix::Int [ 4 1 5 2 6 3 ] >> m.rot90(-1) => GSL::Matrix::Int [ 4 1 5 2 6 3 ]
This creates a matrix copying the upper half part of the matrix self, including the diagonal elements.
This creates a matrix copying the lower half part of the matrix self, including the diagonal elements.
>> m = GSL::Matrix[1..9, 3, 3] => GSL::Matrix [ 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 ] >> m.upper => GSL::Matrix [ 1.000e+00 2.000e+00 3.000e+00 0.000e+00 5.000e+00 6.000e+00 0.000e+00 0.000e+00 9.000e+00 ] >> m.lower => GSL::Matrix [ 1.000e+00 0.000e+00 0.000e+00 4.000e+00 5.000e+00 0.000e+00 7.000e+00 8.000e+00 9.000e+00 ]
Returns the horizontal concatenation of self and other.
Ex:
>> require("gsl") => true >> a = GSL::Matrix::Int[1..4, 2, 2] => GSL::Matrix::Int [ 1 2 3 4 ] >> b = GSL::Matrix::Int[5..10, 2, 3] => GSL::Matrix::Int [ 5 6 7 8 9 10 ] >> a.horzcat(b) => GSL::Matrix::Int [ 1 2 5 6 7 3 4 8 9 10 ]
Returns the vertical concatenation of self and other.
Ex:
>> a = GSL::Matrix::Int[1..4, 2, 2] => GSL::Matrix::Int [ 1 2 3 4 ] >> b = GSL::Matrix::Int[5..10, 3, 2] => GSL::Matrix::Int [ 5 6 7 8 9 10 ] >> a.vertcat(b) => GSL::Matrix::Int [ 1 2 3 4 5 6 7 8 9 10 ]
This method adds the elements of matrix b to the elements of the matrix. The two matrices must have the same dimensions.
If b is a scalar, these methods add it to all the elements of the matrix self (equivalent to the method add_constant).
This method subtracts the elements of matrix b from the elements of the matrix. The two matrices must have the same dimensions.
This method multiplies the elements of the matrix by the elements of matrix b. The two matrices must have the same dimensions. If b is a scalar, the method scale (see below) is called.
This method divides the elements of the matrix by the elements of matrix b. The two matrices must have the same dimensions.
This method multiplies the elements of the matrix by the constant factor x.
This method adds the constant value x to the elements of the matrix.
Matrix multiplication.
Ex:
>> a = GSL::Matrix[1..4, 2, 2] => GSL::Matrix [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 ] >> b = GSL::Matrix[5..8, 2, 2] => GSL::Matrix [ 5.000e+00 6.000e+00 7.000e+00 8.000e+00 ] >> a*b => GSL::Matrix [ 1.900e+01 2.200e+01 4.300e+01 5.000e+01 ] >> a*2 => GSL::Matrix [ 2.000e+00 4.000e+00 6.000e+00 8.000e+00 ] >> c = Vector[1, 2] => GSL::Vector [ 1.000e+00 2.000e+00 ] >> a*c.col => GSL::Vector::Col [ 5.000e+00 1.100e+01 ]
If b is a scalar or a Matrix, this method calculates the element-by-element divisions. If a Vector::Col is given, this method solves the linear system by using LU decomposition.
Ex:
>> m = GSL::Matrix[1..4, 2, 2] => GSL::Matrix [ 1.000e+00 2.000e+00 3.000e+00 4.000e+00 ] >> m/3 => GSL::Matrix [ 3.333e-01 6.667e-01 <--- 1/3, 2/3 1.000e+00 1.333e+00 ] <--- 3/3, 4/3 >> b = Vector[5, 6].col => GSL::Vector::Col [ 5.000e+00 6.000e+00 ] >> x = m/b <--- Solve m (x,y) = b => GSL::Vector::Col [ -4.000e+00 <--- x = -4 4.500e+00 ] <--- y = 4.5 >> m*x => GSL::Vector::Col [ 5.000e+00 6.000e+00 ]
Computes matrix power of b.
These methods return the max/min value in the matrix.
This method returns a two elements array [min, max], which contains the minimum and the maximum values in the matrix.
These methods return the index of the max/min value in the matrix.
This method returns a two elements array [imin, imax], which contains the indices of the minimum and the maximum value in the matrix.
This returns 1 if all the elements of the matrix self are zero, and 0 otherwise.
This returns true if all the elements of the matrix self are zero, and false otherwise.
(GSL-1.9 or later) Return 1 (true) if all the elements of the matrix self are strictly positive, and 0 (false) otherwise.
(GSL-1.9 or later) Return 1 (true) if all the elements of the matrix self are strictly negative, and 0 (false) otherwise.
(GSL-1.10 or later) Return 1 (true) if all the elements of the matrix self are non-negative , and 0 (false) otherwise.
Returns a Vector of ones and zeros with each element indicating whether any of the elements of the corresponding column of the matrix are nonzero.
Behaves like the method any, except that it returns 1 only if all the elements of the matrix.
This returns trace of the matrix self, the sum of the diagonal elements.
Returns matrix norm, sqrt(sum_{ij} m_{ij}^2).
Creates a new matrix, with elements +1 if x_i,j > 0, -1 if x_i,j < 0, otherwise 0. Note that this definition gives the signum of NaN as 0 rather than NaN.
Example:
>> m = GSL::Matrix::Int[-5..4, 3, 3] => GSL::Matrix::Int [ -5 -4 -3 -2 -1 0 1 2 3 ] >> m.abs => GSL::Matrix::Int [ 5 4 3 2 1 0 1 2 3 ]
Returns true if the matrices have same size and elements equal to absolute accurary eps for all the indices, and false otherwise.
The Matrix object self is converted into an NMatrix object. The matrix data are copied to newly allocated memory.
Convert NArray object into GSL::Matrix.
A GSL::Matrix::View object is created from the NArray object na. The 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.
Returns the Hilbert matrix of order n. The ij element is defined as 1/(i+j+1).
Returns the inverse of a Hilbert matrix of order n.
Ex:
>> m = GSL::Matrix.hilbert(4) => GSL::Matrix [ 1.000e+00 5.000e-01 3.333e-01 2.500e-01 5.000e-01 3.333e-01 2.500e-01 2.000e-01 3.333e-01 2.500e-01 2.000e-01 1.667e-01 2.500e-01 2.000e-01 1.667e-01 1.429e-01 ] >> invm = GSL::Matrix.invhilbert(4) => GSL::Matrix [ 1.600e+01 -1.200e+02 2.400e+02 -1.400e+02 -1.200e+02 1.200e+03 -2.700e+03 1.680e+03 2.400e+02 -2.700e+03 6.480e+03 -4.200e+03 -1.400e+02 1.680e+03 -4.200e+03 2.800e+03 ] >> invm2 = m.inv => GSL::Matrix [ 1.600e+01 -1.200e+02 2.400e+02 -1.400e+02 -1.200e+02 1.200e+03 -2.700e+03 1.680e+03 2.400e+02 -2.700e+03 6.480e+03 -4.200e+03 -1.400e+02 1.680e+03 -4.200e+03 2.800e+03 ] >> m*invm => GSL::Matrix [ 1.000e+00 5.684e-14 -2.274e-13 1.137e-13 1.998e-15 1.000e+00 -4.663e-14 3.109e-14 3.664e-15 -7.239e-14 1.000e+00 -1.017e-13 -2.442e-15 1.510e-14 -8.038e-14 1.000e+00 ] >> m*invm2 => GSL::Matrix [ 1.000e+00 0.000e+00 0.000e+00 0.000e+00 -1.554e-15 1.000e+00 -2.389e-14 8.349e-15 1.295e-15 3.405e-15 1.000e+00 -6.957e-15 1.110e-15 1.916e-14 1.707e-14 1.000e+00 ]
Returns the Pascal matrix of order n, created from Pascal‘s triangle.
>> GSL::Matrix::Int.pascal(10) => GSL::Matrix::Int [ 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 9 10 1 3 6 10 15 21 28 36 45 55 1 4 10 20 35 56 84 120 165 220 1 5 15 35 70 126 210 330 495 715 1 6 21 56 126 252 462 792 1287 2002 1 7 28 84 210 462 924 1716 3003 5005 1 8 36 120 330 792 1716 3432 6435 11440 1 9 45 165 495 1287 3003 6435 12870 24310 1 10 55 220 715 2002 5005 11440 24310 48620 ]
Creates a Vendermonde matrix from a vector or an array v.
>> GSL::Matrix.vander([1, 2, 3, 4]) => GSL::Matrix [ 1.000e+00 1.000e+00 1.000e+00 1.000e+00 8.000e+00 4.000e+00 2.000e+00 1.000e+00 2.700e+01 9.000e+00 3.000e+00 1.000e+00 6.400e+01 1.600e+01 4.000e+00 1.000e+00 ]
Creates a Toeplitz matrix from a vector or an array v.
>> GSL::Matrix::Int.toeplitz([1, 2, 3, 4, 5]) => GSL::Matrix::Int [ 1 2 3 4 5 2 1 2 3 4 3 2 1 2 3 4 3 2 1 2 5 4 3 2 1 ]
Creates a circulant matrix from a vector or an array v.
>> GSL::Matrix::Int.circulant([1, 2, 3, 4]) => GSL::Matrix::Int [ 4 1 2 3 3 4 1 2 2 3 4 1 1 2 3 4 ]