Regina 7.4 Calculation Engine
|
Represents a matrix of elements of the given type T. More...
#include <maths/matrix.h>
Public Types | |
using | value_type = T |
The type of element that is stored in this matrix. | |
Public Member Functions | |
Matrix () | |
Creates a new uninitialised matrix. | |
Matrix (size_t size) | |
Creates a new square matrix of the given size. | |
Matrix (size_t rows, size_t cols) | |
Creates a new matrix of the given size. | |
Matrix (std::initializer_list< std::initializer_list< T > > data) | |
Creates a new matrix containing the given hard-coded entries. | |
Matrix (const Matrix &src) | |
Creates a new matrix that is a clone of the given matrix. | |
template<typename U > | |
Matrix (const Matrix< U > &src) | |
Creates a new clone of the given matrix, which may hold objects of a different type. | |
Matrix (Matrix &&src) noexcept | |
Moves the given matrix into this new matrix. | |
~Matrix () | |
Destroys this matrix. | |
Matrix & | operator= (const Matrix &src) |
Copies the given matrix into this matrix. | |
Matrix & | operator= (Matrix &&src) noexcept |
Moves the given matrix into this matrix. | |
void | fill (const T &value) |
Sets every entry in the matrix to the given value. | |
void | initialise (const T &value) |
Deprecated function that sets every entry in the matrix to the given value. | |
void | swap (Matrix &other) noexcept |
Swaps the contents of this and the given matrix. | |
size_t | rows () const |
Returns the number of rows in this matrix. | |
size_t | columns () const |
Returns the number of columns in this matrix. | |
bool | initialised () const |
Determines whether this matrix is initialised or uninitialised. | |
T & | entry (size_t row, size_t column) |
Returns a read-write reference to the entry at the given row and column. | |
const T & | entry (size_t row, size_t column) const |
Returns a read-only reference to the entry at the given row and column. | |
void | set (size_t row, size_t column, const T &value) |
Python-only routine that sets the entry at the given row and column. | |
Matrix | transpose () const |
Returns the transpose of this matrix. | |
bool | operator== (const Matrix &other) const |
Determines whether this and the given matrix are identical. | |
void | swapRows (size_t first, size_t second) |
Swaps the elements of the two given rows in the matrix. | |
void | swapCols (size_t first, size_t second, size_t fromRow=0) |
Swaps the elements of the two given columns in the matrix. | |
void | writeTextShort (std::ostream &out) const |
Writes a short text representation of this object to the given output stream. | |
void | writeTextLong (std::ostream &out) const |
Writes a detailed text representation of this object to the given output stream. | |
void | makeIdentity () |
Turns this matrix into an identity matrix. | |
bool | isIdentity () const |
Determines whether this matrix is a square identity matrix. | |
bool | isZero () const |
Determines whether this is the zero matrix. | |
void | addRow (size_t source, size_t dest) |
Adds the given source row to the given destination row. | |
void | addRowFrom (size_t source, size_t dest, size_t fromCol) |
Adds a portion of the given source row to the given destination row. | |
void | addRow (size_t source, size_t dest, T copies, size_t fromCol=0) |
Adds the given number of copies of the given source row to the given destination row. | |
void | addCol (size_t source, size_t dest) |
Adds the given source column to the given destination column. | |
void | addColFrom (size_t source, size_t dest, size_t fromRow=0) |
Adds a portion of the given source column to the given destination column. | |
void | addCol (size_t source, size_t dest, T copies, size_t fromRow=0) |
Adds the given number of copies of the given source column to the given destination column. | |
void | multRow (size_t row, T factor, size_t fromCol=0) |
Multiplies the given row by the given factor. | |
void | multCol (size_t column, T factor, size_t fromRow=0) |
Multiplies the given column by the given factor. | |
void | combRows (size_t row1, size_t row2, T coeff11, T coeff12, T coeff21, T coeff22, size_t fromCol=0) |
Rewrites two rows as linear combinations of those two rows. | |
void | combCols (size_t col1, size_t col2, T coeff11, T coeff12, T coeff21, T coeff22, size_t fromRow=0) |
Rewrites two columns as linear combinations of those two columns. | |
template<typename U > | |
Matrix< decltype(T() *U())> | operator* (const Matrix< U > &other) const |
Multiplies this by the given matrix, and returns the result. | |
template<typename U > | |
Vector< decltype(T() *U())> | operator* (const Vector< U > &other) const |
Multiplies this matrix by the given vector, and returns the result. | |
T | det () const |
Evaluates the determinant of the matrix. | |
void | negateRow (size_t row) |
Negates all elements in the given row. | |
void | negateCol (size_t col) |
Negates all elements in the given column. | |
void | divRowExact (size_t row, const T &divBy) |
Divides all elements of the given row by the given integer. | |
void | divColExact (size_t col, const T &divBy) |
Divides all elements of the given column by the given integer. | |
T | gcdRow (size_t row) |
Computes the greatest common divisor of all elements of the given row. | |
T | gcdCol (size_t col) |
Computes the greatest common divisor of all elements of the given column. | |
void | reduceRow (size_t row) |
Reduces the given row by dividing all its elements by their greatest common divisor. | |
void | reduceCol (size_t col) |
Reduces the given column by dividing all its elements by their greatest common divisor. | |
size_t | rowEchelonForm () |
Transforms this matrix into row echelon form. | |
size_t | columnEchelonForm () |
Transforms this matrix into column echelon form. | |
size_t | rank () const & |
A non-destructive routine that returns the rank of this matrix whilst preserving the contents of the matrix. | |
size_t | rank () && |
A destructive routine that returns the rank of this matrix. | |
std::string | str () const |
Returns a short text representation of this object. | |
std::string | utf8 () const |
Returns a short text representation of this object using unicode characters. | |
std::string | detail () const |
Returns a detailed text representation of this object. | |
Static Public Member Functions | |
static Matrix | identity (size_t size) |
Returns an identity matrix of the given size. | |
Represents a matrix of elements of the given type T.
As of Regina 7.4, the extra boolean ring template parameter is gone; instead the relevant functions are only enabled in scenarios where T is a ring type (in particular, when the specialisation RingTraits<T>
is available). Nowadays you should always just use the type Matrix<T>
.
The header maths/matrixops.h contains several additional algorithms that work with the specific class Matrix<Integer>.
This class implements C++ move semantics and adheres to the C++ Swappable requirement. It is designed to avoid deep copies wherever possible, even when passing or returning objects by value.
T | the type of each individual matrix element. This type should have a default constructor and an assignment operator, and should be writeable to an output stream using the standard stream operator << . |
using regina::Matrix< typename >::value_type = T |
The type of element that is stored in this matrix.
|
inline |
Creates a new uninitialised matrix.
You must initialise this matrix using the assignment operator before you can use it for any purpose. The only exceptions are:
|
inline |
Creates a new square matrix of the given size.
Both the number of rows and the number of columns will be set to size.
All entries will be initialised using their default constructors. In particular, this means that for Regina's own integer classes (Integer, LargeInteger and NativeInteger), all entries will be initialised to zero.
int
or long
), then the matrix elements will not be initialised to any particular value.size | the number of rows and columns in the new matrix. |
|
inline |
Creates a new matrix of the given size.
All entries will be initialised using their default constructors. In particular, this means that for Regina's own integer classes (Integer, LargeInteger and NativeInteger), all entries will be initialised to zero.
int
or long
), then the matrix elements will not be initialised to any particular value.rows | the number of rows in the new matrix. |
cols | the number of columns in the new matrix. |
|
inline |
Creates a new matrix containing the given hard-coded entries.
This constructor can be used (for example) to create hard-coded examples directly in C++ code.
Each element of the initialiser list data describes a single row of the matrix.
data | the rows of the matrix, each given as a list of elements. |
|
inline |
Creates a new matrix that is a clone of the given matrix.
This constructor induces a deep copy of src.
This routine is safe to call even if src is uninitialised (in which case this matrix will become uninitialised also).
src | the matrix to clone. |
|
inlineexplicit |
Creates a new clone of the given matrix, which may hold objects of a different type.
This constructor induces a deep copy of src.
This routine is safe to call even if src is uninitialised (in which case this matrix will become uninitialised also).
This constructor is marked as explicit in the hope of avoiding accidental (and unintentional) mixing of matrix classes.
U | the type of object held by the given matrix src. It must be possible to assign an object of type U to an object of type T. |
src | the matrix to clone. |
|
inlinenoexcept |
Moves the given matrix into this new matrix.
This is a fast (constant time) operation.
The matrix that is passed (src) will no longer be usable.
This routine is safe to call even if src is uninitialised (in which case this matrix will become uninitialised also).
src | the matrix to move. |
|
inline |
Destroys this matrix.
This destructor is safe to call even if src is uninitialised.
|
inline |
Adds the given source column to the given destination column.
RingTraits<T>
is available. source | the columns to add. |
dest | the column that will be added to. |
|
inline |
Adds the given number of copies of the given source column to the given destination column.
Note that copies is passed by value in case it is an element of the row to be changed.
If the optional argument fromRow is passed, then the operation will only be performed for the elements from that row down to the bottom of the column (inclusive).
RingTraits<T>
is available. source | the columns to add. |
dest | the column that will be added to. |
copies | the number of copies of source to add to dest. |
fromRow | the starting point in the column from which the operation will be performed. |
|
inline |
Adds a portion of the given source column to the given destination column.
This is similar to addCol(), except that the operation will only be performed for the elements from the row fromRow down to the bottom of the column (inclusive).
RingTraits<T>
is available. source | the columns to add. |
dest | the column that will be added to. |
fromRow | the starting point in the column from which the operation will be performed. |
|
inline |
Adds the given source row to the given destination row.
RingTraits<T>
is available. source | the row to add. |
dest | the row that will be added to. |
|
inline |
Adds the given number of copies of the given source row to the given destination row.
Note that copies is passed by value in case it is an element of the row to be changed.
If the optional argument fromCol is passed, then the operation will only be performed for the elements from that column to the rightmost end of the row (inclusive).
RingTraits<T>
is available. source | the row to add. |
dest | the row that will be added to. |
copies | the number of copies of source to add to dest. |
fromCol | the starting point in the row from which the operation will be performed. |
|
inline |
Adds a portion of the given source row to the given destination row.
This is similar to addRow(), except that the operation will only be performed for the elements from the column fromCol to the rightmost end of the row (inclusive).
RingTraits<T>
is available. source | the row to add. |
dest | the row that will be added to. |
fromCol | the starting point in the row from which the operation will be performed. |
|
inline |
Transforms this matrix into column echelon form.
The transformation will perform only column operations.
This is simpler than the global routine regina::columnEchelonForm(): it does not return the change of basis matrices, and it processes all rows in order from left to right (instead of passing a custom row list).
Our convention is that a matrix is in column echelon form if:
|
inline |
Returns the number of columns in this matrix.
|
inline |
Rewrites two columns as linear combinations of those two columns.
Specifically, if C1 and C2 are the original values of columns col1 and col2 respectively, then:
coeff11 * C1 + coeff12 * C2
;coeff21 * C1 + coeff22 * C2
.The four coefficients are passed by value, in case they are elements of the columns to be changed.
If the optional argument fromRow is passed, then the operation will only be performed for the elements from that column down to the bottom of each column (inclusive).
RingTraits<T>
is available. col1 | the first column to operate on. |
col2 | the second column to operate on. |
coeff11 | the coefficient of column col1 to use when rewriting column col1. |
coeff12 | the coefficient of column col2 to use when rewriting column col1. |
coeff21 | the coefficient of column col1 to use when rewriting column col2. |
coeff22 | the coefficient of column col2 to use when rewriting column col2. |
fromRow | the starting point in the columns from which the operation will be performed. |
|
inline |
Rewrites two rows as linear combinations of those two rows.
Specifically, if R1 and R2 are the original values of rows row1 and row2 respectively, then:
coeff11 * R1 + coeff12 * R2
;coeff21 * R1 + coeff22 * R2
.The four coefficients are passed by value, in case they are elements of the rows to be changed.
If the optional argument fromCol is passed, then the operation will only be performed for the elements from that column to the rightmost end of each row (inclusive).
RingTraits<T>
is available. row1 | the first row to operate on. |
row2 | the second row to operate on. |
coeff11 | the coefficient of row row1 to use when rewriting row row1. |
coeff12 | the coefficient of row row2 to use when rewriting row row1. |
coeff21 | the coefficient of row row1 to use when rewriting row row2. |
coeff22 | the coefficient of row row2 to use when rewriting row row2. |
fromCol | the starting point in the rows from which the operation will be performed. |
|
inline |
Evaluates the determinant of the matrix.
This algorithm has quartic complexity, and uses the dynamic programming approach of Mahajan and Vinay. For further details, see Meena Mahajan and V. Vinay, "Determinant: Combinatorics, algorithms, and complexity", Chicago J. Theor. Comput. Sci., Vol. 1997, Article 5.
Although the Matrix class does not formally support empty matrices, if this is found to be a 0-by-0 matrix then the determinant returned will be 1.
RingTraits<T>
is available. FailedPrecondition | This matrix is not square. |
|
inherited |
Returns a detailed text representation of this object.
This text may span many lines, and should provide the user with all the information they could want. It should be human-readable, should not contain extremely long lines (which cause problems for users reading the output in a terminal), and should end with a final newline. There are no restrictions on the underlying character set.
|
inline |
Divides all elements of the given column by the given integer.
This can only be used when the given integer divides into all column elements exactly (with no remainder). For the Integer class, this may be much faster than ordinary division.
col | the index of the column whose elements should be divided by divBy. |
divBy | the integer to divide each column element by. |
|
inline |
Divides all elements of the given row by the given integer.
This can only be used when the given integer divides into all row elements exactly (with no remainder). For the Integer class, this may be much faster than ordinary division.
row | the index of the row whose elements should be divided by divBy. |
divBy | the integer to divide each row element by. |
|
inline |
Returns a read-write reference to the entry at the given row and column.
Rows and columns are numbered beginning at zero.
matrix.entry(r, c).negate()
will work, but matrix.entry(r, c) = value
will not; instead you will need to call matrix.set(r, c, value)
.row | the row of the desired entry; this must be between 0 and rows()-1 inclusive. |
column | the column of the desired entry; this must be between 0 and columns()-1 inclusive. |
|
inline |
Returns a read-only reference to the entry at the given row and column.
Rows and columns are numbered beginning at zero.
row | the row of the desired entry; this must be between 0 and rows()-1 inclusive. |
column | the column of the desired entry; this must be between 0 and columns()-1 inclusive. |
|
inline |
Sets every entry in the matrix to the given value.
value | the value to assign to each entry. |
|
inline |
Computes the greatest common divisor of all elements of the given column.
The value returned is guaranteed to be non-negative.
col | the index of the column whose gcd should be computed. |
|
inline |
Computes the greatest common divisor of all elements of the given row.
The value returned is guaranteed to be non-negative.
row | the index of the row whose gcd should be computed. |
|
inlinestatic |
Returns an identity matrix of the given size.
The matrix returned will have size rows and size columns.
RingTraits<T>
is available.size | the number of rows and columns of the matrix to build. |
|
inline |
Deprecated function that sets every entry in the matrix to the given value.
value | the value to assign to each entry. |
|
inline |
Determines whether this matrix is initialised or uninitialised.
The only ways for a matrix to be uninitialised are:
true
if this matrix is initialised, or false
if it is uninitialised.
|
inline |
Determines whether this matrix is a square identity matrix.
If this matrix is square, isIdentity() will return true
if and only if the matrix has ones in the main diagonal and zeroes everywhere else.
If this matrix is not square, isIdentity() will always return false
(even if makeIdentity() was called earlier).
RingTraits<T>
is available.true
if and only if this is a square identity matrix.
|
inline |
Determines whether this is the zero matrix.
RingTraits<T>
is available.true
if and only if all entries in the matrix are zero.
|
inline |
Turns this matrix into an identity matrix.
This matrix need not be square; after this routine it will have entry(r,c)
equal to 1 if r == c
and 0 otherwise.
RingTraits<T>
is available.
|
inline |
Multiplies the given column by the given factor.
Note that factor is passed by value in case it is an element of the row to be changed.
If the optional argument fromRow is passed, then the operation will only be performed for the elements from that row down to the bottom of the column (inclusive).
RingTraits<T>
is available. column | the column to work with. |
factor | the factor by which to multiply the given column. |
fromRow | the starting point in the column from which the operation will be performed. |
|
inline |
Multiplies the given row by the given factor.
Note that factor is passed by value in case it is an element of the row to be changed.
If the optional argument fromCol is passed, then the operation will only be performed for the elements from that column to the rightmost end of the row (inclusive).
RingTraits<T>
is available. row | the row to work with. |
factor | the factor by which to multiply the given row. |
fromCol | the starting point in the row from which the operation will be performed. |
|
inline |
Negates all elements in the given column.
col | the index of the column whose elements should be negated. |
|
inline |
Negates all elements in the given row.
row | the index of the row whose elements should be negated. |
|
inline |
Multiplies this by the given matrix, and returns the result.
This matrix is not changed.
The two matrices being multiplied may use different underlying types (e.g., you can multiply a matrix of LargeInteger objects with a matrix of native C++ long integers). The type of object that is stored in the resulting matrix will be deduced accordingly (specifically, it will be the type obtained by multiplying objects of types T and U using the binary multiplication operator).
RingTraits<E>
is available. (In many cases, E will just be the matrix element type T.) other | the other matrix to multiply this matrix by. |
this * other
.
|
inline |
Multiplies this matrix by the given vector, and returns the result.
The given vector is treated as a column vector.
The matrix and vector may use different underlying types (e.g., you can multiply a matrix of LargeInteger objects with a vector of native C++ long integers). The type of object that is stored in the resulting vector will be deduced accordingly (specifically, it will be the type obtained by multiplying objects of types T and U using the binary multiplication operator).
RingTraits<E>
is available. (In many cases, E will just be the matrix element type T.) other | the vector to multiply this matrix by. |
this * other
, which will be a vector whose length is the number of rows in this matrix.
|
inline |
Copies the given matrix into this matrix.
It does not matter if this and the given matrix have different sizes; if they do then this matrix will be resized as a result.
This operator induces a deep copy of src.
This routine is safe to call even if src is uninitialised (in which case this matrix will become uninitialised also).
src | the matrix to copy. |
|
inlinenoexcept |
Moves the given matrix into this matrix.
This is a fast (constant time) operation.
It does not matter if this and the given matrix have different sizes; if they do then this matrix will be resized as a result.
The matrix that is passed (src) will no longer be usable.
This routine is safe to call even if src is uninitialised (in which case this matrix will become uninitialised also).
src | the matrix to move. |
|
inline |
Determines whether this and the given matrix are identical.
Two matrices are identical if and only if (i) their dimensions are the same, and (ii) the corresponding elements of each matrix are equal.
Note that this routine can happily deal with two matrices of different dimensions (in which case it will always return false
).
This routine returns true
if and only if the inequality operator (!=) returns false
.
other | the matrix to compare with this. |
true
if the matrices are equal as described above, or false
otherwise.
|
inline |
A destructive routine that returns the rank of this matrix.
Here "destructive" means that this routine modifies the matrix directly as it performs the rank computation. For this reason, it is declared as an rvalue reference member function: it should only be used if you do not care about the contents of the matrix afterwards.
To use this destructive rank computation, you can call std::move(matrix).rank()
.
If you need to preserve the contents of the matrix, you should instead call the const version of this function, which you can simply access as matrix.rank()
. The (minor) cost of this constness will be the extra overhead of an internal deep copy.
|
inline |
A non-destructive routine that returns the rank of this matrix whilst preserving the contents of the matrix.
Normally, a rank computation would involve modifying the matrix directly (e.g., by converting it to row echelon form). In contrast, this routine will leave the matrix unchanged. The cost is an extra deep copy in the implementation.
If your matrix is disposable (i.e., you will never need to use it again), then it is faster to use the rvalue reference version of this routine, which will avoid the extra overhead of the deep copy. To do this, replace matrix.rank()
with std::move(matrix).rank()
.
|
inline |
Reduces the given column by dividing all its elements by their greatest common divisor.
It is guaranteed that, if the column is changed at all, it will be divided by a positive integer.
col | the index of the column to reduce. |
|
inline |
Reduces the given row by dividing all its elements by their greatest common divisor.
It is guaranteed that, if the row is changed at all, it will be divided by a positive integer.
row | the index of the row to reduce. |
|
inline |
Transforms this matrix into row echelon form.
The transformation will perform only row operations.
This is simpler than the global routine regina::columnEchelonForm(): it does not return the change of basis matrices, and it processes all columns in order from left to right (instead of passing a custom column list).
Our convention is that a matrix is in row echelon form if:
|
inline |
Returns the number of rows in this matrix.
void regina::Matrix< typename >::set | ( | size_t | row, |
size_t | column, | ||
const T & | value ) |
Python-only routine that sets the entry at the given row and column.
Rows and columns are numbered beginning at zero.
entry(row, column) = value
.matrix.set(row, column, value)
. The entry() routine does give read-write access to matrix elements in Python, but it does not allow them to be set using the assignment operator. In other words, code such as matrix.entry(r, c).negate()
will work, but matrix.entry(r, c) = value
will not.
|
inherited |
Returns a short text representation of this object.
This text should be human-readable, should use plain ASCII characters where possible, and should not contain any newlines.
Within these limits, this short text ouptut should be as information-rich as possible, since in most cases this forms the basis for the Python __str__()
and __repr__()
functions.
__str__()
will use precisely this function, and for most classes the Python __repr__()
function will incorporate this into its output.
|
inlinenoexcept |
Swaps the contents of this and the given matrix.
other | the matrix whose contents are to be swapped with this. |
|
inline |
Swaps the elements of the two given columns in the matrix.
This operation is linear time (unlike swapping rows, which is constant time).
If the optional argument fromRow is passed, then the operation will only be performed for the elements from that row down to the bottom of each column (inclusive).
first | the first column to swap. |
second | the second column to swap. |
fromRow | the starting point in each column from which the operation will be performed. |
|
inline |
Swaps the elements of the two given rows in the matrix.
This operation is constant time (unlike swapping columns, which is linear time).
Unlike swapCols(), this operation does not take a fromCol argument. This is because swapping rows is already as fast possible (internally, just a single pointer swap), and so iterating along only part of the row would slow the routine down considerably.
first | the first row to swap. |
second | the second row to swap. |
|
inline |
Returns the transpose of this matrix.
This matrix is not changed.
|
inherited |
Returns a short text representation of this object using unicode characters.
Like str(), this text should be human-readable, should not contain any newlines, and (within these constraints) should be as information-rich as is reasonable.
Unlike str(), this function may use unicode characters to make the output more pleasant to read. The string that is returned will be encoded in UTF-8.
|
inline |
Writes a detailed text representation of this object to the given output stream.
out | the output stream to which to write. |
|
inline |
Writes a short text representation of this object to the given output stream.
out | the output stream to which to write. |