Class: | LAMatrix |
Parent: | Object |
The class LAMatrix represents a matrix with methods implementing linear algebra.
LAMatrix methods always handles indices in "column, row" order. This is opposite from the mathematical convention, but consistent with representation of multidimensional array. Be careful when you write code to represent mathematical transformation by LAMatrix.
For example, when a = LAMatrix.new(1,3)
and b = LAMatrix.new(3,5)
, a * b
causes exception because the dimensions do not match (you are trying to multiply two matrices of size (3,1) and (5,3)). b * a
goes without error to give a row vector (which has the same dimension with LAMatrix.new(1,5)
).
In Molby, Vector3D and Transform class also represents matrix-like objects. For convenience, these classes can be used interconvertibly.
Create a new matrix. In the first form, f1...fn must be numbers, and a (n, 1) matrix (a column vector) is created. In the second form, a1...an must be array-like objects, and a (m, n) matrix (m is the maximum dimension of a1...an) is created. In the third form, obj must be either an array, a Vector3D, a Transform, or an LAMatrix.
Create a diagonal matrix. In the first form, the diagonal elements are taken from the array argument. In the second form, a diagonal matrix of the specified size with all the diagonal component element
.
Create a new LAMatrix built from column vectors. This is equivalent to LAMatrix.new([c1, ...])
.
Multiply the arguments. The arguments are interpreted as in LAMatrix.multiply!.
Create a new LAMatrix object.
matrix
must respond to a method call matrix[col, row]
, row_size
and column_size
.Perform the matrix addition. Both arguments must have the same dimensions.
Perform the matrix subtraction. Both arguments must have the same dimensions.
Perform the matrix multiplication. In the first form, a new matrix with scaled elements is returned. In the second, the multiple of the two matrices is returned.
Perform the matrix multiplication. In the first form, a new matrix with scaled elements is
returned. In the second, self * matrix.inverse
is returned.
Returns true
if and only if the dimensions are the same and all the corresponding elements of both arguments are equal. Usual caution about the comparison of floating-point numbers should be paid.
Get the element (i
,j
) of the matrix, i.e. column i
, row j
. Be careful about the order of the arguments. It follows convention of multi-dimensional arrays rather than mathematical notation.
Set the element (i
,j
) of the matrix, i.e. column i
, row j
. Be careful about the order of the arguments. It follows convention of multi-dimensional arrays rather than mathematical notation.
Calculate the eigenvalues and eigenvectors. The matrix must be symmetric. The eigenvalues are given as a column matrix, and the eigenvectors are given as an orthogonal matrix (whose column vectors are the eigenvectors).
Calculate the Frobenius norm (the Hilbert–Schmidt norm). fnorm2
returns the square of the norm.
Convert a matrix to a string like "LAMatrix[[a11,a21,...],[a12,a22,...],...,[a1N,a2N,...]]".
Calculate the inverse matrix as a new object. If the matrix is not regular, an exception is raised. inverse!
changes the object destructively and returns self.
Multiply the arguments to self.
argN
is a string "t", then the subsequent matrix is transposed (the object is not modified; it is just transposed during calculation).argN
is a string "i", then the subsequent matrix is inverted (the object is not modified).argN
is a number, then scalar multiplication is performed.argN
must be a matrix, which has the same row-size as the column-size of the last result.
Extract a submatrix beginning from (row_pos, column_pos)
and size (row_size, column_size)
. If row_size
or column_size
are given as -1, then all the elements to the end of the row/column are used. Note the order of the arguments; they are opposite to the mathematical convention.
Perform the singular value decomposition (SVD) of the given matrix. Decompose the given (m,n)
matrix to a product of three matrices, U
, S
, V
. U
is a (m,m)
orthogonal matrix, S
is a (m,n)
matrix which is zero except for min(m,n)
diagonal elements, and V
is a (n,n)
orthogonal matrix. (Usually SVD is defined as M = U*S*transpose(V)
, but this methods returns transpose(V)
rather than V
.) The singular_values is a min(m,n)
dimension column vector.