10 #ifndef LLVM_CODEGEN_PBQP_MATH_H
11 #define LLVM_CODEGEN_PBQP_MATH_H
27 length(length), data(new
PBQPNum[length]) {
32 length(length), data(new
PBQPNum[length]) {
33 std::fill(data, data + length, initVal);
38 length(v.length), data(new
PBQPNum[length]) {
39 std::copy(v.data, v.data + length, data);
50 std::copy(v.data, v.data + length, data);
61 assert(index < length &&
"Vector element access out of bounds.");
67 assert(index < length &&
"Vector element access out of bounds.");
73 assert(length == v.length &&
"Vector length mismatch.");
74 std::transform(data, data + length, v.data, data, std::plus<PBQPNum>());
80 assert(length == v.length &&
"Vector length mismatch.");
81 std::transform(data, data + length, v.data, data, std::minus<PBQPNum>());
87 return std::min_element(data, data + length) - data;
97 template <
typename OStream>
99 assert((v.
getLength() != 0) &&
"Zero-length vector badness.");
102 for (
unsigned i = 1; i < v.getLength(); ++i) {
117 rows(rows), cols(cols), data(new
PBQPNum[rows * cols]) {
123 rows(rows), cols(cols), data(new
PBQPNum[rows * cols]) {
124 std::fill(data, data + (rows * cols), initVal);
129 rows(m.rows), cols(m.cols), data(new
PBQPNum[rows * cols]) {
130 std::copy(m.data, m.data + (rows * cols), data);
139 rows = m.rows; cols = m.cols;
140 data =
new PBQPNum[rows * cols];
141 std::copy(m.data, m.data + (rows * cols), data);
153 assert(r < rows &&
"Row out of bounds.");
154 return data + (r * cols);
159 assert(r < rows &&
"Row out of bounds.");
160 return data + (r * cols);
166 for (
unsigned c = 0; c < cols; ++c)
167 v[c] = (*
this)[r][c];
174 for (
unsigned r = 0; r < rows; ++r)
175 v[r] = (*
this)[r][c];
181 std::fill(data, data + (rows * cols), val);
187 assert(r < rows &&
"Row out of bounds.");
188 std::fill(data + (r * cols), data + ((r + 1) * cols), val);
194 assert(c < cols &&
"Column out of bounds.");
195 for (
unsigned r = 0; r < rows; ++r)
203 for (
unsigned r = 0; r < rows; ++r)
204 for (
unsigned c = 0; c < cols; ++c)
205 m[c][r] = (*
this)[r][c];
213 assert(rows == cols &&
"Attempt to diagonalize non-square matrix.");
216 for (
unsigned r = 0; r < rows; ++r)
217 v[r] = (*
this)[r][r];
223 assert(rows == m.rows && cols == m.cols &&
224 "Matrix dimensions mismatch.");
225 std::transform(data, data + (rows * cols), m.data, data,
226 std::plus<PBQPNum>());
232 assert(r < rows &&
"Row out of bounds");
233 return *std::min_element(data + (r * cols), data + ((r + 1) * cols));
238 PBQPNum minElem = (*this)[0][c];
239 for (
unsigned r = 1; r < rows; ++r)
240 if ((*
this)[r][c] < minElem) minElem = (*this)[r][c];
246 assert(r < rows &&
"Row out of bounds");
247 std::transform(data + (r * cols), data + ((r + 1) * cols),
249 std::bind2nd(std::minus<PBQPNum>(), val));
255 for (
unsigned r = 0; r < rows; ++r)
256 (*
this)[r][c] -= val;
262 return find_if(data, data + (rows * cols),
263 std::bind2nd(std::not_equal_to<PBQPNum>(), 0)) ==
264 data + (rows * cols);
274 template <
typename OStream>
277 assert((m.
getRows() != 0) &&
"Zero-row matrix badness.");
279 for (
unsigned i = 0; i < m.
getRows(); ++i) {
288 #endif // LLVM_CODEGEN_PBQP_MATH_H
PBQPNum getRowMin(unsigned r) const
Returns the minimum of the given row.
Vector(const Vector &v)
Copy construct a PBQP vector.
Vector diagonalize() const
Returns the diagonal of the matrix as a vector.
Matrix(unsigned rows, unsigned cols)
Construct a PBQP Matrix with the given dimensions.
Matrix & operator+=(const Matrix &m)
Add the given matrix to this one.
unsigned getLength() const
Return the length of the vector.
bool isZero() const
Returns true if this is a zero matrix.
Vector(unsigned length)
Construct a PBQP vector of the given size.
unsigned minIndex() const
Returns the index of the minimum value in this vector.
unsigned getRows() const
Return the number of rows in this matrix.
Matrix & subFromCol(unsigned c, PBQPNum val)
Subtracts the given scalar from the elements of the given column.
~Vector()
Destroy this vector, return its memory.
Vector & operator=(const Vector &v)
Assignment operator.
Matrix & reset(PBQPNum val=0)
Reset the matrix to the given value.
PBQPNum getColMin(unsigned c) const
Returns the minimum of the given column.
Vector & operator-=(const Vector &v)
Subtract another vector from this one.
Matrix & setRow(unsigned r, PBQPNum val)
Set a single row of this matrix to the given value.
Matrix & subFromRow(unsigned r, PBQPNum val)
Subtracts the given scalar from the elements of the given row.
PBQPNum * operator[](unsigned r)
Matrix element access.
~Matrix()
Destroy this matrix, return its memory.
PBQPNum & operator[](unsigned index)
Element access.
Matrix(unsigned rows, unsigned cols, PBQPNum initVal)
Construct a PBQP Matrix with the given dimensions and initial value.
Vector getRowAsVector(unsigned r) const
Returns the given row as a vector.
const PBQPNum & operator[](unsigned index) const
Const element access.
const PBQPNum * operator[](unsigned r) const
Matrix element access.
Matrix & operator=(const Matrix &m)
Assignment operator.
Matrix(const Matrix &m)
Copy construct a PBQP matrix.
Matrix & setCol(unsigned c, PBQPNum val)
Set a single column of this matrix to the given value.
Matrix transpose() const
Matrix transpose.
OStream & operator<<(OStream &os, const Vector &v)
Output a textual representation of the given vector on the given output stream.
Vector(unsigned length, PBQPNum initVal)
Construct a PBQP vector with initializer.
unsigned getCols() const
Return the number of cols in this matrix.
Vector & operator+=(const Vector &v)
Add another vector to this one.
Vector getColAsVector(unsigned c) const
Returns the given column as a vector.