Matrix.cpp
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===- Matrix.cpp - MLIR Matrix Class -------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Analysis/Presburger/Matrix.h"
namespace mlir {
Matrix::Matrix(unsigned rows, unsigned columns)
: nRows(rows), nColumns(columns), data(nRows * nColumns) {}
Matrix Matrix::identity(unsigned dimension) {
Matrix matrix(dimension, dimension);
for (unsigned i = 0; i < dimension; ++i)
matrix(i, i) = 1;
return matrix;
}
int64_t &Matrix::at(unsigned row, unsigned column) {
assert(row < getNumRows() && "Row outside of range");
assert(column < getNumColumns() && "Column outside of range");
return data[row * nColumns + column];
}
int64_t Matrix::at(unsigned row, unsigned column) const {
assert(row < getNumRows() && "Row outside of range");
assert(column < getNumColumns() && "Column outside of range");
return data[row * nColumns + column];
}
int64_t &Matrix::operator()(unsigned row, unsigned column) {
return at(row, column);
}
int64_t Matrix::operator()(unsigned row, unsigned column) const {
return at(row, column);
}
unsigned Matrix::getNumRows() const { return nRows; }
unsigned Matrix::getNumColumns() const { return nColumns; }
void Matrix::resizeVertically(unsigned newNRows) {
nRows = newNRows;
data.resize(nRows * nColumns);
}
void Matrix::swapRows(unsigned row, unsigned otherRow) {
assert((row < getNumRows() && otherRow < getNumRows()) &&
"Given row out of bounds");
if (row == otherRow)
return;
for (unsigned col = 0; col < nColumns; col++)
std::swap(at(row, col), at(otherRow, col));
}
void Matrix::swapColumns(unsigned column, unsigned otherColumn) {
assert((column < getNumColumns() && otherColumn < getNumColumns()) &&
"Given column out of bounds");
if (column == otherColumn)
return;
for (unsigned row = 0; row < nRows; row++)
std::swap(at(row, column), at(row, otherColumn));
}
ArrayRef<int64_t> Matrix::getRow(unsigned row) const {
return {&data[row * nColumns], nColumns};
}
void Matrix::addToRow(unsigned sourceRow, unsigned targetRow, int64_t scale) {
if (scale == 0)
return;
for (unsigned col = 0; col < nColumns; ++col)
at(targetRow, col) += scale * at(sourceRow, col);
return;
}
void Matrix::print(raw_ostream &os) const {
for (unsigned row = 0; row < nRows; ++row) {
for (unsigned column = 0; column < nColumns; ++column)
os << at(row, column) << ' ';
os << '\n';
}
}
void Matrix::dump() const { print(llvm::errs()); }
} // namespace mlir