10 #ifndef LLVM_CODEGEN_PBQP_MATH_H
11 #define LLVM_CODEGEN_PBQP_MATH_H
30 : Length(Length), Data(new
PBQPNum[Length]) {
37 : Length(Length), Data(new
PBQPNum[Length]) {
41 std::fill(Data, Data + Length, InitVal);
46 : Length(V.Length), Data(new
PBQPNum[Length]) {
49 std::copy(V.Data, V.Data + Length, Data);
54 : Length(V.Length), Data(V.Data) {
72 std::copy(V.Data, V.Data + Length, Data);
88 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
89 if (Length != V.Length)
91 return std::equal(Data, Data + Length, V.Data);
96 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
102 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
103 assert(Index < Length &&
"Vector element access out of bounds.");
109 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
110 assert(Index < Length &&
"Vector element access out of bounds.");
116 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
117 assert(Length == V.Length &&
"Vector length mismatch.");
118 std::transform(Data, Data + Length, V.Data, Data, std::plus<PBQPNum>());
124 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
125 assert(Length == V.Length &&
"Vector length mismatch.");
126 std::transform(Data, Data + Length, V.Data, Data, std::minus<PBQPNum>());
132 assert(Length != 0 && Data !=
nullptr &&
"Invalid vector");
133 return std::min_element(Data, Data + Length) - Data;
143 unsigned *VBegin =
reinterpret_cast<unsigned*
>(V.Data);
144 unsigned *VEnd =
reinterpret_cast<unsigned*
>(V.Data + V.Length);
150 template <
typename OStream>
152 assert((V.
getLength() != 0) &&
"Zero-length vector badness.");
155 for (
unsigned i = 1; i < V.getLength(); ++i)
170 Rows(Rows), Cols(Cols), Data(new
PBQPNum[Rows * Cols]) {
176 : Rows(Rows), Cols(Cols), Data(new
PBQPNum[Rows * Cols]) {
177 std::fill(Data, Data + (Rows * Cols), InitVal);
182 : Rows(M.Rows), Cols(M.Cols), Data(new
PBQPNum[Rows * Cols]) {
183 std::copy(M.Data, M.Data + (Rows * Cols), Data);
188 : Rows(M.Rows), Cols(M.Cols), Data(M.Data) {
199 Rows = M.Rows; Cols = M.Cols;
200 Data =
new PBQPNum[Rows * Cols];
201 std::copy(M.Data, M.Data + (Rows * Cols), Data);
218 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
219 if (Rows != M.Rows || Cols != M.Cols)
221 return std::equal(Data, Data + (Rows * Cols), M.Data);
226 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
232 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
238 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
239 assert(R < Rows &&
"Row out of bounds.");
240 return Data + (R * Cols);
245 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
246 assert(R < Rows &&
"Row out of bounds.");
247 return Data + (R * Cols);
252 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
254 for (
unsigned C = 0; C < Cols; ++C)
255 V[C] = (*
this)[R][C];
261 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
263 for (
unsigned R = 0; R < Rows; ++R)
264 V[R] = (*
this)[R][C];
270 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
271 std::fill(Data, Data + (Rows * Cols), Val);
277 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
278 assert(R < Rows &&
"Row out of bounds.");
279 std::fill(Data + (R * Cols), Data + ((R + 1) * Cols), Val);
285 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
286 assert(C < Cols &&
"Column out of bounds.");
287 for (
unsigned R = 0; R < Rows; ++R)
294 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
296 for (
unsigned r = 0; r < Rows; ++r)
297 for (
unsigned c = 0; c < Cols; ++c)
298 M[c][r] = (*
this)[r][c];
306 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
307 assert(Rows == Cols &&
"Attempt to diagonalize non-square matrix.");
309 for (
unsigned r = 0; r < Rows; ++r)
310 V[r] = (*
this)[r][r];
316 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
317 assert(Rows == M.Rows && Cols == M.Cols &&
318 "Matrix dimensions mismatch.");
319 std::transform(Data, Data + (Rows * Cols), M.Data, Data,
320 std::plus<PBQPNum>());
325 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
333 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
334 assert(R < Rows &&
"Row out of bounds");
335 return *std::min_element(Data + (R * Cols), Data + ((R + 1) * Cols));
340 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
341 PBQPNum MinElem = (*this)[0][C];
342 for (
unsigned R = 1; R < Rows; ++R)
343 if ((*
this)[R][C] < MinElem)
344 MinElem = (*this)[R][C];
350 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
351 assert(R < Rows &&
"Row out of bounds");
352 std::transform(Data + (R * Cols), Data + ((R + 1) * Cols),
354 std::bind2nd(std::minus<PBQPNum>(), Val));
360 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
361 for (
unsigned R = 0; R < Rows; ++R)
362 (*
this)[R][C] -= Val;
368 assert(Rows != 0 && Cols != 0 && Data !=
nullptr &&
"Invalid matrix");
369 return find_if(Data, Data + (Rows * Cols),
370 std::bind2nd(std::not_equal_to<PBQPNum>(), 0)) ==
371 Data + (Rows * Cols);
381 unsigned *MBegin =
reinterpret_cast<unsigned*
>(M.Data);
382 unsigned *MEnd =
reinterpret_cast<unsigned*
>(M.Data + (M.Rows * M.Cols));
388 template <
typename OStream>
390 assert((M.
getRows() != 0) &&
"Zero-row matrix badness.");
391 for (
unsigned i = 0; i < M.
getRows(); ++i)
396 template <
typename Metadata>
406 template <
typename Metadata>
408 return hash_value(static_cast<const Vector&>(V));
411 template <
typename Metadata>
421 template <
typename Metadata>
423 return hash_value(static_cast<const Matrix&>(M));
429 #endif // LLVM_CODEGEN_PBQP_MATH_H
OStream & operator<<(OStream &OS, const Vector &V)
Output a textual representation of the given vector on the given output stream.
unsigned getCols() const
Return the number of cols in this matrix.
unsigned minIndex() const
Returns the index of the minimum value in this vector.
Matrix & operator=(const Matrix &M)
Copy-assignment operator.
friend hash_code hash_value(const Matrix &)
Return a hash_code for the given matrix.
Vector & operator-=(const Vector &V)
Subtract another vector from this one.
Matrix & reset(PBQPNum Val=0)
Reset the matrix to the given value.
Matrix & setRow(unsigned R, PBQPNum Val)
Set a single row of this matrix to the given value.
PBQPNum * operator[](unsigned R)
Matrix element access.
Matrix & operator=(Matrix &&M)
Move-assignment operator.
PBQPNum & operator[](unsigned Index)
Element access.
MDMatrix(const Matrix &m)
Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
Construct a PBQP Matrix with the given dimensions and initial value.
Matrix(unsigned Rows, unsigned Cols)
Construct a PBQP Matrix with the given dimensions.
bool isZero() const
Returns true if this is a zero matrix.
bool operator==(const Matrix &M) const
Comparison operator.
Vector getRowAsVector(unsigned R) const
Returns the given row as a vector.
PBQPNum getRowMin(unsigned R) const
Returns the minimum of the given row.
Vector getColAsVector(unsigned C) const
Returns the given column as a vector.
~Vector()
Destroy this vector, return its memory.
const PBQPNum & operator[](unsigned Index) const
Const element access.
Matrix & setCol(unsigned C, PBQPNum Val)
Set a single column of this matrix to the given value.
Vector(unsigned Length)
Construct a PBQP vector of the given size.
Vector & operator=(Vector &&V)
Move-assignment operator.
Matrix & operator+=(const Matrix &M)
Add the given matrix to this one.
friend hash_code hash_value(const Vector &)
Return a hash_value for the given vector.
Vector & operator=(const Vector &V)
Copy-assignment operator.
unsigned getLength() const
Return the length of the vector.
Matrix(Matrix &&M)
Move construct a PBQP matrix.
const Metadata & getMetadata() const
Vector(const Vector &V)
Copy construct a PBQP vector.
hash_code hash_value(const Vector &V)
Return a hash_value for the given vector.
bool operator==(const Vector &V) const
Comparison operator.
Matrix & subFromCol(unsigned C, PBQPNum Val)
Subtracts the given scalar from the elements of the given column.
const PBQPNum * operator[](unsigned R) const
Matrix element access.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Matrix operator+(const Matrix &M)
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
An opaque object representing a hash code.
const Metadata & getMetadata() const
unsigned getRows() const
Return the number of rows in this matrix.
Vector(Vector &&V)
Move construct a PBQP vector.
Vector & operator+=(const Vector &V)
Add another vector to this one.
Matrix & subFromRow(unsigned R, PBQPNum Val)
Subtracts the given scalar from the elements of the given row.
PBQPNum getColMin(unsigned C) const
Returns the minimum of the given column.
Matrix(const Matrix &M)
Copy construct a PBQP matrix.
Matrix transpose() const
Matrix transpose.
Vector diagonalize() const
Returns the diagonal of the matrix as a vector.
Vector(unsigned Length, PBQPNum InitVal)
Construct a PBQP vector with initializer.
~Matrix()
Destroy this matrix, return its memory.
MDVector(const Vector &v)