15 #ifndef MLIR_ANALYSIS_PRESBURGER_MATRIX_H
16 #define MLIR_ANALYSIS_PRESBURGER_MATRIX_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/raw_ostream.h"
24 namespace presburger {
27 using llvm::raw_ostream;
43 static_assert(std::is_same_v<T, DynamicAPInt> || std::is_same_v<T, Fraction>,
44 "T must be DynamicAPInt or Fraction.");
55 Matrix(
unsigned rows,
unsigned columns,
unsigned reservedRows = 0,
56 unsigned reservedColumns = 0);
62 T &
at(
unsigned row,
unsigned column) {
63 assert(row <
nRows &&
"Row outside of range");
64 assert(column <
nColumns &&
"Column outside of range");
68 T
at(
unsigned row,
unsigned column)
const {
69 assert(row <
nRows &&
"Row outside of range");
70 assert(column <
nColumns &&
"Column outside of range");
74 T &
operator()(
unsigned row,
unsigned column) {
return at(row, column); }
76 T
operator()(
unsigned row,
unsigned column)
const {
return at(row, column); }
81 void swapColumns(
unsigned column,
unsigned otherColumn);
84 void swapRows(
unsigned row,
unsigned otherRow);
117 void insertRows(
unsigned pos,
unsigned count);
133 void removeRows(
unsigned pos,
unsigned count);
136 void copyRow(
unsigned sourceRow,
unsigned targetRow);
138 void fillRow(
unsigned row,
const T &value);
142 void addToRow(
unsigned sourceRow,
unsigned targetRow,
const T &scale);
143 void addToRow(
unsigned sourceRow,
unsigned targetRow, int64_t scale) {
144 addToRow(sourceRow, targetRow, T(scale));
150 void scaleRow(
unsigned row,
const T &scale);
153 void addToColumn(
unsigned sourceColumn,
unsigned targetColumn,
184 void resize(
unsigned newNRows,
unsigned newNColumns);
201 unsigned toColumn)
const;
211 void print(raw_ostream &os)
const;
234 void moveColumns(
unsigned srcPos,
unsigned num,
unsigned dstPos);
257 unsigned reservedColumns = 0)
258 :
Matrix<DynamicAPInt>(
rows, columns, reservedRows, reservedColumns) {}
263 static IntMatrix identity(
unsigned dimension);
275 std::pair<IntMatrix, IntMatrix> computeHermiteNormalForm()
const;
279 DynamicAPInt normalizeRow(
unsigned row,
unsigned nCols);
282 DynamicAPInt normalizeRow(
unsigned row);
291 DynamicAPInt determinant(
IntMatrix *inverse =
nullptr)
const;
299 unsigned reservedColumns = 0)
307 static FracMatrix identity(
unsigned dimension);
FracMatrix(unsigned rows, unsigned columns, unsigned reservedRows=0, unsigned reservedColumns=0)
FracMatrix(Matrix< Fraction > m)
IntMatrix(Matrix< DynamicAPInt > m)
IntMatrix(unsigned rows, unsigned columns, unsigned reservedRows=0, unsigned reservedColumns=0)
This is a class to represent a resizable matrix.
void moveColumns(unsigned srcPos, unsigned num, unsigned dstPos)
Move the columns in the source range [srcPos, srcPos + num) to the specified destination [dstPos,...
bool hasConsistentState() const
Return whether the Matrix is in a consistent state with all its invariants satisfied.
void insertRows(unsigned pos, unsigned count)
Insert rows having positions pos, pos + 1, ...
unsigned getNumRows() const
void swapColumns(unsigned column, unsigned otherColumn)
Swap the given columns.
T & operator()(unsigned row, unsigned column)
unsigned nRows
The current number of rows, columns, and reserved columns.
void removeColumn(unsigned pos)
unsigned appendExtraRow()
Add an extra row at the bottom of the matrix and return its position.
unsigned nReservedColumns
void addToColumn(unsigned sourceColumn, unsigned targetColumn, const T &scale)
Add scale multiples of the source column to the target column.
Matrix< T > getSubMatrix(unsigned fromRow, unsigned toRow, unsigned fromColumn, unsigned toColumn) const
void print(raw_ostream &os) const
Print the matrix.
void copyRow(unsigned sourceRow, unsigned targetRow)
void scaleRow(unsigned row, const T &scale)
Multiply the specified row by a factor of scale.
void insertColumn(unsigned pos)
T at(unsigned row, unsigned column) const
MutableArrayRef< T > getRow(unsigned row)
Get a [Mutable]ArrayRef corresponding to the specified row.
void removeColumns(unsigned pos, unsigned count)
Remove the columns having positions pos, pos + 1, ...
void addToColumn(unsigned sourceColumn, unsigned targetColumn, int64_t scale)
void insertColumns(unsigned pos, unsigned count)
Insert columns having positions pos, pos + 1, ...
void setRow(unsigned row, ArrayRef< T > elems)
Set the specified row to elems.
std::pair< Matrix< T >, Matrix< T > > splitByBitset(ArrayRef< int > indicator)
Split the rows of a matrix into two matrices according to which bits are 1 and which are 0 in a given...
void fillRow(unsigned row, int64_t value)
void removeRow(unsigned pos)
bool operator==(const Matrix< T > &m) const
We cannot use the default implementation of operator== as it compares fields like reservedColumns etc...
SmallVector< T, 16 > data
Stores the data.
unsigned getNumColumns() const
void resizeVertically(unsigned newNRows)
unsigned getNumReservedRows() const
Return the maximum number of rows/columns that can be added without incurring a reallocation.
unsigned getNumReservedColumns() const
Matrix< T > transpose() const
SmallVector< T, 8 > preMultiplyWithRow(ArrayRef< T > rowVec) const
The given vector is interpreted as a row vector v.
static Matrix identity(unsigned dimension)
Return the identity matrix of the specified dimension.
void insertRow(unsigned pos)
SmallVector< T, 8 > postMultiplyWithColumn(ArrayRef< T > colVec) const
The given vector is interpreted as a column vector v.
void negateMatrix()
Negate the entire matrix.
void swapRows(unsigned row, unsigned otherRow)
Swap the given rows.
void resizeHorizontally(unsigned newNColumns)
void reserveRows(unsigned rows)
Reserve enough space to resize to the specified number of rows without reallocations.
void negateColumn(unsigned column)
Negate the specified column.
void resize(unsigned newNRows, unsigned newNColumns)
Resize the matrix to the specified dimensions.
void fillRow(unsigned row, const T &value)
void addToRow(unsigned sourceRow, unsigned targetRow, const T &scale)
Add scale multiples of the source row to the target row.
T operator()(unsigned row, unsigned column) const
void negateRow(unsigned row)
Negate the specified row.
T & at(unsigned row, unsigned column)
Access the element at the specified row and column.
void addToRow(unsigned sourceRow, unsigned targetRow, int64_t scale)
void removeRows(unsigned pos, unsigned count)
Remove the rows having positions pos, pos + 1, ...
Include the generated interface declarations.
A class to represent fractions.