Class Matrix2
- All Implemented Interfaces:
Serializable,Cloneable,LenientComparable,Matrix
-
Field Summary
FieldsModifier and TypeFieldDescriptiondoubleThe first matrix element in the first row.doubleThe second matrix element in the first row.doubleThe first matrix element in the second row.doubleThe second matrix element in the second row.static final intThe matrix size, which is 2. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Matrix2castOrCopy(Matrix matrix) Casts or copies the given matrix to aMatrix2implementation.clone()Returns a clone of this matrix.booleanReturnstrueif the specified object is of typeMatrix2and all of the data members are equal to the corresponding data members in this matrix.final doublegetElement(int row, int column) Retrieves the value at the specified row and column of this matrix.final double[]Returns all matrix elements in a flat, row-major (column indices vary fastest) array.final intReturns the number of columns in this matrix, which is always 2 in this implementation.final intReturns the number of rows in this matrix, which is always 2 in this implementation.intReturns a hash code value based on the data values in this object.inverse()Returns the inverse of this matrix.final booleanReturnstrueif this matrix represents an affine transform.final booleanReturnstrueif this matrix is an identity matrix.double[]multiply(double[] vector) Returns a new vector which is the result of multiplying this matrix with the specified vector.final voidsetElement(int row, int column, double value) Modifies the value at the specified row and column of this matrix.final voidsetElements(double[] elements) Sets all matrix elements from a flat, row-major (column indices vary fastest) array.voidsetToRotation(double θ) Sets the elements to a rotation matrix of the given arithmetic angle.voidSets the value of this matrix to its transpose.Methods inherited from class MatrixSIS
convertAfter, convertBefore, equals, equals, getNumber, multiply, normalizeColumns, removeColumns, removeRows, setMatrix, setNumber, solve, toString, translate
-
Field Details
-
SIZE
public static final int SIZEThe matrix size, which is 2.- See Also:
-
m00
public double m00The first matrix element in the first row. -
m01
public double m01The second matrix element in the first row. -
m10
public double m10The first matrix element in the second row. -
m11
public double m11The second matrix element in the second row.
-
-
Constructor Details
-
Matrix2
public Matrix2()Creates a new identity matrix. -
Matrix2
public Matrix2(double m00, double m01, double m10, double m11) Creates a new matrix initialized to the specified values.- Parameters:
m00- the first matrix element in the first row.m01- the second matrix element in the first row.m10- the first matrix element in the second row.m11- the second matrix element in the second row.
-
Matrix2
Creates a new matrix initialized to the specified values. The length of the given array must be 4 and the values in the same order as the above constructor.- Parameters:
elements- elements of the matrix. Column indices vary fastest.- Throws:
IllegalArgumentException- if the given array does not have the expected length.- See Also:
-
-
Method Details
-
castOrCopy
Casts or copies the given matrix to aMatrix2implementation. If the givenmatrixis already an instance ofMatrix2, then it is returned unchanged. Otherwise this method verifies the matrix size, then copies all elements in a newMatrix2object.- Parameters:
matrix- the matrix to cast or copy, ornull.- Returns:
- the matrix argument if it can be safely cast (including
nullargument), or a copy of the given matrix otherwise. - Throws:
MismatchedMatrixSizeException- if the size of the given matrix is not 2×2.
-
getNumRow
-
getNumCol
-
getElement
public final double getElement(int row, int column) Retrieves the value at the specified row and column of this matrix. This method can be invoked when the matrix size or type is unknown. If the matrix is known to be an instance ofMatrix2, then them00…m11fields can be read directly for efficiency.- Parameters:
row- the row index, which can only be 0 or 1.column- the column index, which can only be 0 or 1.- Returns:
- the current value at the given row and column.
-
setElement
public final void setElement(int row, int column, double value) Modifies the value at the specified row and column of this matrix. This method can be invoked when the matrix size or type is unknown. If the matrix is known to be an instance ofMatrix2, then them00…m11fields can be set directly for efficiency.- Parameters:
row- the row index, which can only be 0 or 1.column- the column index, which can only be 0 or 1.value- the new value to set at the given row and column.
-
getElements
public final double[] getElements()Returns all matrix elements in a flat, row-major (column indices vary fastest) array. The array length is 4.- Overrides:
getElementsin classMatrixSIS- Returns:
- a copy of all current matrix elements in a row-major array.
-
setElements
public final void setElements(double[] elements) Sets all matrix elements from a flat, row-major (column indices vary fastest) array. The array length shall be 4.- Overrides:
setElementsin classMatrixSIS- Parameters:
elements- The new matrix elements in a row-major array.- See Also:
-
setToRotation
public void setToRotation(double θ) Sets the elements to a rotation matrix of the given arithmetic angle. Angle 0 is oriented toward positive x axis, rotation is counter-clockwise and the unit of measurement is radians. The resulting matrix is not affine in the sense ofisAffine(). The matrix is:┌ ┐ │ cos(θ) −sin(θ) │ │ sin(θ) cos(θ) │ └ ┘- Parameters:
θ- arithmetic rotation angle in radians.- Since:
- 1.5
-
isAffine
public final boolean isAffine()Returnstrueif this matrix represents an affine transform. A transform is affine if the matrix is square and its last row contains only zeros, except in the last column which contains 1.- Overrides:
isAffinein classMatrixSIS- Returns:
trueif this matrix represents an affine transform.- See Also:
-
isIdentity
public final boolean isIdentity()Returnstrueif this matrix is an identity matrix. This method is equivalent to the following code, except that it is potentially more efficient:return Matrices.isIdentity(this, 0.0);- Specified by:
isIdentityin interfaceMatrix- Specified by:
isIdentityin classMatrixSIS- Returns:
trueif this matrix is an identity matrix.- See Also:
-
transpose
public void transpose()Sets the value of this matrix to its transpose.- Specified by:
transposein classMatrixSIS
-
multiply
public double[] multiply(double[] vector) Returns a new vector which is the result of multiplying this matrix with the specified vector. In other words, returnsthis×vector. The length of the given vector must be equal to the number of columns in this matrix, and the length of the returned vector will be equal to the number of rows in this matrix.Relationship with coordinate operations
In the context of coordinate operations,Matrix.multiply(vector)is related toAffineTransform.transform(…)except that the lastvectornumber is implicitly 1 inAffineTransformoperations. While thismultiply(double[])method could be used for coordinate transformation, it is not its purpose. This method is designed for occasional uses when accuracy is more important than performance.- Overrides:
multiplyin classMatrixSIS- Parameters:
vector- the vector to multiply to this matrix.- Returns:
- the result of
this×vector. - Since:
- 1.5
-
inverse
Returns the inverse of this matrix.- Overrides:
inversein classMatrixSIS- Returns:
- the inverse of this matrix.
- Throws:
NoninvertibleMatrixException- if this matrix is not invertible.- See Also:
-
clone
Returns a clone of this matrix.- Specified by:
clonein interfaceMatrix- Overrides:
clonein classMatrixSIS- Returns:
- a new matrix of the same class and with the same values as this matrix.
- See Also:
-
equals
Returnstrueif the specified object is of typeMatrix2and all of the data members are equal to the corresponding data members in this matrix.- Specified by:
equalsin interfaceLenientComparable- Overrides:
equalsin classMatrixSIS- Parameters:
object- the object to compare with this matrix for equality.- Returns:
trueif the given object is equal to this matrix.- See Also:
-
hashCode
public int hashCode()Returns a hash code value based on the data values in this object.- Overrides:
hashCodein classMatrixSIS- Returns:
- a hash code value for this matrix.
-