Frobby 0.9.5
Matrix.h
Go to the documentation of this file.
1/* Frobby: Software for monomial ideal computations.
2 Copyright (C) 2010 University of Aarhus
3 Contact Bjarke Hammersholt Roune for license information (www.broune.com)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see http://www.gnu.org/licenses/.
17*/
18#ifndef MATRIX_GUARD
19#define MATRIX_GUARD
20
21#include <vector>
22
23class BigIntVector;
24class ColumnPrinter;
25
26class Matrix {
27 public:
28 Matrix(size_t rowCount = 0, size_t colCount = 0);
29
30 size_t getRowCount() const {return _rowCount;}
31 size_t getColCount() const {return _colCount;}
32
37 void resize(size_t rowCount, size_t colCount);
38
39 const mpq_class& operator()(size_t row, size_t col) const
40 {return _entries[toIndex(row, col)];}
41 mpq_class& operator()(size_t row, size_t col)
42 {return _entries[toIndex(row, col)];}
43
44 void swap(Matrix& mat);
45
46 private:
47 size_t toIndex(size_t row, size_t col) const {
51 size_t index = col + row * _colCount;
52 ASSERT(index < _entries.size());
53 return index;
54 }
55
56 size_t _rowCount;
57 size_t _colCount;
58 vector<mpq_class> _entries;
59};
60
61bool operator==(const Matrix& a, const Matrix& b);
63
64void print(FILE* file, const Matrix& mat);
65void print(ColumnPrinter& printer, const Matrix& mat);
66
68void product(Matrix& prod, const Matrix& a, const Matrix& b);
69
71void transpose(Matrix& trans, const Matrix& mat);
72
74void transpose(Matrix& mat);
75
78 size_t sourceRow, const mpq_class& mult);
79
81void multiplyRow(Matrix& mat, size_t row, const mpq_class& mult);
82
84void swapRows(Matrix& mat, size_t row1, size_t row2);
85
89bool rowReduce(Matrix& mat);
90
92size_t matrixRank(const Matrix& mat);
93
99
102void subMatrix(Matrix& sub, const Matrix& mat,
103 size_t rowBegin, size_t rowEnd,
104 size_t colBegin, size_t colEnd);
105
108void copyRow(Matrix& target, size_t targetRow,
109 const Matrix& source, size_t sourceRow);
110
116bool inverse(Matrix& inv, const Matrix& mat);
117
119void nullSpace(Matrix& basis, const Matrix& mat);
120
124bool solve(Matrix& sol, const Matrix& lhs, const Matrix& rhs);
125
127bool hasSameColSpace(const Matrix& a, const Matrix& b);
128
130bool hasSameRowSpace(const Matrix& a, const Matrix& b);
131
134
136bool isParallelogram(const Matrix& mat);
137
143
144#endif
size_t matrixRank(const Matrix &mat)
Returns the rank of mat.
Definition Matrix.cpp:276
void multiplyRow(Matrix &mat, size_t row, const mpq_class &mult)
Multiplies row row with mult.
Definition Matrix.cpp:156
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
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
void product(Matrix &prod, const Matrix &a, const Matrix &b)
Sets prod to a * b.
Definition Matrix.cpp:116
bool isParallelogram(const Matrix &mat)
Returns true if the rows of mat are the (4) vertices of a parallelogram.
Definition Matrix.cpp:452
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
bool rowReduce(Matrix &mat)
Reduces mat to row-echelon form, i.e.
Definition Matrix.cpp:169
void swapRows(Matrix &mat, size_t row1, size_t row2)
Swaps row row1 and row row2 of mat.
Definition Matrix.cpp:161
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
mpq_class determinant(const Matrix &mat)
Returns the determinant of mat.
Definition Matrix.cpp:410
void print(FILE *file, const Matrix &mat)
Definition Matrix.cpp:101
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
void rowReduceFully(Matrix &mat)
Reduces mat to reduced row-echelon form, i.e.
Definition Matrix.cpp:200
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
bool hasSameRowSpace(const Matrix &a, const Matrix &b)
Returns true if a and b have the same row space.
Definition Matrix.cpp:390
bool inverse(Matrix &inv, const Matrix &mat)
Sets inv to the inverse of mat.
Definition Matrix.cpp:257
bool operator==(const Matrix &a, const Matrix &b)
Definition Matrix.cpp:82
ostream & operator<<(ostream &out, const Matrix &mat)
Definition Matrix.cpp:94
bool hasSameColSpace(const Matrix &a, const Matrix &b)
Returns true if a and b have the same column space.
Definition Matrix.cpp:400
void transpose(Matrix &trans, const Matrix &mat)
Sets trans to the transpose of mat.
Definition Matrix.cpp:129
void nameFactoryRegister(NameFactory< AbstractProduct > &factory)
Registers the string returned by ConcreteProduct::getStaticName() to a function that default-construc...
size_t _colCount
Definition Matrix.h:57
void resize(size_t rowCount, size_t colCount)
Set the number of rows and columns.
Definition Matrix.cpp:61
vector< mpq_class > _entries
Definition Matrix.h:58
const mpq_class & operator()(size_t row, size_t col) const
Definition Matrix.h:39
void swap(Matrix &mat)
Definition Matrix.cpp:74
size_t getColCount() const
Definition Matrix.h:31
size_t toIndex(size_t row, size_t col) const
Definition Matrix.h:47
size_t getRowCount() const
Definition Matrix.h:30
mpq_class & operator()(size_t row, size_t col)
Definition Matrix.h:41
size_t _rowCount
Definition Matrix.h:56
#define ASSERT(X)
Definition stdinc.h:86