Matrix.h
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2010 University of Aarhus
Contact Bjarke Hammersholt Roune for license information (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#ifndef MATRIX_GUARD
#define MATRIX_GUARD
#include <vector>
class BigIntVector;
class ColumnPrinter;
class Matrix {
public:
Matrix(size_t rowCount = 0, size_t colCount = 0);
size_t getRowCount() const {return _rowCount;}
size_t getColCount() const {return _colCount;}
void resize(size_t rowCount, size_t colCount);
const mpq_class& operator()(size_t row, size_t col) const
{return _entries[toIndex(row, col)];}
mpq_class& operator()(size_t row, size_t col)
{return _entries[toIndex(row, col)];}
void swap(Matrix& mat);
private:
size_t toIndex(size_t row, size_t col) const {
ASSERT(row < _rowCount);
ASSERT(col < _colCount);
ASSERT(_entries.size() == _rowCount * _colCount);
size_t index = col + row * _colCount;
ASSERT(index < _entries.size());
return index;
}
size_t _rowCount;
size_t _colCount;
vector<mpq_class> _entries;
};
bool operator==(const Matrix& a, const Matrix& b);
ostream& operator<<(ostream& out, const Matrix& mat);
void print(FILE* file, const Matrix& mat);
void print(ColumnPrinter& printer, const Matrix& mat);
void product(Matrix& prod, const Matrix& a, const Matrix& b);
void transpose(Matrix& trans, const Matrix& mat);
void transpose(Matrix& mat);
void addMultiplyRow(Matrix& mat, size_t resultRow,
size_t sourceRow, const mpq_class& mult);
void multiplyRow(Matrix& mat, size_t row, const mpq_class& mult);
void swapRows(Matrix& mat, size_t row1, size_t row2);
bool rowReduce(Matrix& mat);
size_t matrixRank(const Matrix& mat);
void rowReduceFully(Matrix& mat);
void subMatrix(Matrix& sub, const Matrix& mat,
size_t rowBegin, size_t rowEnd,
size_t colBegin, size_t colEnd);
void copyRow(Matrix& target, size_t targetRow,
const Matrix& source, size_t sourceRow);
bool inverse(Matrix& inv, const Matrix& mat);
void nullSpace(Matrix& basis, const Matrix& mat);
bool solve(Matrix& sol, const Matrix& lhs, const Matrix& rhs);
bool hasSameColSpace(const Matrix& a, const Matrix& b);
bool hasSameRowSpace(const Matrix& a, const Matrix& b);
mpq_class determinant(const Matrix& mat);
bool isParallelogram(const Matrix& mat);
mpq_class getParallelogramAreaSq(const Matrix& mat);
#endif
matrixRank
size_t matrixRank(const Matrix &mat)
Returns the rank of mat.
Definition Matrix.cpp:276
multiplyRow
void multiplyRow(Matrix &mat, size_t row, const mpq_class &mult)
Multiplies row row with mult.
Definition Matrix.cpp:156
solve
bool solve(Matrix &sol, const Matrix &lhs, const Matrix &rhs)
Sets sol to some matrix such that lhs*sol=rhs and returns true if such a matrix exists.
Definition Matrix.cpp:348
getParallelogramAreaSq
mpq_class getParallelogramAreaSq(const Matrix &mat)
Returns the square of the area of the parallelogram whose vertices are the 4 rows of mat.
Definition Matrix.cpp:456
product
void product(Matrix &prod, const Matrix &a, const Matrix &b)
Sets prod to a * b.
Definition Matrix.cpp:116
isParallelogram
bool isParallelogram(const Matrix &mat)
Returns true if the rows of mat are the (4) vertices of a parallelogram.
Definition Matrix.cpp:452
addMultiplyRow
void addMultiplyRow(Matrix &mat, size_t resultRow, size_t sourceRow, const mpq_class &mult)
Adds mult times row sourceRow to row resultRow of mat.
Definition Matrix.cpp:147
rowReduce
bool rowReduce(Matrix &mat)
Reduces mat to row-echelon form, i.e.
Definition Matrix.cpp:169
swapRows
void swapRows(Matrix &mat, size_t row1, size_t row2)
Swaps row row1 and row row2 of mat.
Definition Matrix.cpp:161
nullSpace
void nullSpace(Matrix &basis, const Matrix &mat)
Sets the columns of basis to a basis of the null space of mat.
Definition Matrix.cpp:296
determinant
mpq_class determinant(const Matrix &mat)
Returns the determinant of mat.
Definition Matrix.cpp:410
print
void print(FILE *file, const Matrix &mat)
Definition Matrix.cpp:101
subMatrix
void subMatrix(Matrix &sub, const Matrix &mat, size_t rowBegin, size_t rowEnd, size_t colBegin, size_t colEnd)
Sets sub to the sub-matrix of mat with rows in the interval [rowBegin, rowEnd) and columns in the int...
Definition Matrix.cpp:225
rowReduceFully
void rowReduceFully(Matrix &mat)
Reduces mat to reduced row-echelon form, i.e.
Definition Matrix.cpp:200
copyRow
void copyRow(Matrix &target, size_t targetRow, const Matrix &source, size_t sourceRow)
Copies row sourceRow from source to row targetRow of target.
Definition Matrix.cpp:246
hasSameRowSpace
bool hasSameRowSpace(const Matrix &a, const Matrix &b)
Returns true if a and b have the same row space.
Definition Matrix.cpp:390
inverse
bool inverse(Matrix &inv, const Matrix &mat)
Sets inv to the inverse of mat.
Definition Matrix.cpp:257
operator==
bool operator==(const Matrix &a, const Matrix &b)
Definition Matrix.cpp:82
operator<<
ostream & operator<<(ostream &out, const Matrix &mat)
Definition Matrix.cpp:94
hasSameColSpace
bool hasSameColSpace(const Matrix &a, const Matrix &b)
Returns true if a and b have the same column space.
Definition Matrix.cpp:400
transpose
void transpose(Matrix &trans, const Matrix &mat)
Sets trans to the transpose of mat.
Definition Matrix.cpp:129
BigIntVector
Definition BigIntVector.h:24
ColumnPrinter
Definition ColumnPrinter.h:27
Matrix
Definition Matrix.h:26
Matrix::_colCount
size_t _colCount
Definition Matrix.h:57
Matrix::resize
void resize(size_t rowCount, size_t colCount)
Set the number of rows and columns.
Definition Matrix.cpp:61
Matrix::_entries
vector< mpq_class > _entries
Definition Matrix.h:58
Matrix::operator()
const mpq_class & operator()(size_t row, size_t col) const
Definition Matrix.h:39
Matrix::swap
void swap(Matrix &mat)
Definition Matrix.cpp:74
Matrix::getColCount
size_t getColCount() const
Definition Matrix.h:31
Matrix::toIndex
size_t toIndex(size_t row, size_t col) const
Definition Matrix.h:47
Matrix::getRowCount
size_t getRowCount() const
Definition Matrix.h:30
Matrix::operator()
mpq_class & operator()(size_t row, size_t col)
Definition Matrix.h:41
Matrix::_rowCount
size_t _rowCount
Definition Matrix.h:56
Matrix::Matrix
Matrix(size_t rowCount=0, size_t colCount=0)
Definition Matrix.cpp:57
ASSERT
#define ASSERT(X)
Definition stdinc.h:86