#ifndef LLVM_CODEGEN_PBQP_MATH_H
#define LLVM_CODEGEN_PBQP_MATH_H
#include <cassert>
#include <algorithm>
#include <functional>
namespace PBQP {
typedef float PBQPNum;
class Vector {
public:
explicit Vector(unsigned length) :
length(length), data(new PBQPNum[length]) {
}
Vector(unsigned length, PBQPNum initVal) :
length(length), data(new PBQPNum[length]) {
std::fill(data, data + length, initVal);
}
Vector(const Vector &v) :
length(v.length), data(new PBQPNum[length]) {
std::copy(v.data, v.data + length, data);
}
~Vector() { delete[] data; }
Vector& operator=(const Vector &v) {
delete[] data;
length = v.length;
data = new PBQPNum[length];
std::copy(v.data, v.data + length, data);
return *this;
}
unsigned getLength() const {
return length;
}
PBQPNum& operator[](unsigned index) {
assert(index < length && "Vector element access out of bounds.");
return data[index];
}
const PBQPNum& operator[](unsigned index) const {
assert(index < length && "Vector element access out of bounds.");
return data[index];
}
Vector& operator+=(const Vector &v) {
assert(length == v.length && "Vector length mismatch.");
std::transform(data, data + length, v.data, data, std::plus<PBQPNum>());
return *this;
}
Vector& operator-=(const Vector &v) {
assert(length == v.length && "Vector length mismatch.");
std::transform(data, data + length, v.data, data, std::minus<PBQPNum>());
return *this;
}
unsigned minIndex() const {
return std::min_element(data, data + length) - data;
}
private:
unsigned length;
PBQPNum *data;
};
template <typename OStream>
OStream& operator<<(OStream &os, const Vector &v) {
assert((v.getLength() != 0) && "Zero-length vector badness.");
os << "[ " << v[0];
for (unsigned i = 1; i < v.getLength(); ++i) {
os << ", " << v[i];
}
os << " ]";
return os;
}
class Matrix {
public:
Matrix(unsigned rows, unsigned cols) :
rows(rows), cols(cols), data(new PBQPNum[rows * cols]) {
}
Matrix(unsigned rows, unsigned cols, PBQPNum initVal) :
rows(rows), cols(cols), data(new PBQPNum[rows * cols]) {
std::fill(data, data + (rows * cols), initVal);
}
Matrix(const Matrix &m) :
rows(m.rows), cols(m.cols), data(new PBQPNum[rows * cols]) {
std::copy(m.data, m.data + (rows * cols), data);
}
~Matrix() { delete[] data; }
Matrix& operator=(const Matrix &m) {
delete[] data;
rows = m.rows; cols = m.cols;
data = new PBQPNum[rows * cols];
std::copy(m.data, m.data + (rows * cols), data);
return *this;
}
unsigned getRows() const { return rows; }
unsigned getCols() const { return cols; }
PBQPNum* operator[](unsigned r) {
assert(r < rows && "Row out of bounds.");
return data + (r * cols);
}
const PBQPNum* operator[](unsigned r) const {
assert(r < rows && "Row out of bounds.");
return data + (r * cols);
}
Vector getRowAsVector(unsigned r) const {
Vector v(cols);
for (unsigned c = 0; c < cols; ++c)
v[c] = (*this)[r][c];
return v;
}
Vector getColAsVector(unsigned c) const {
Vector v(rows);
for (unsigned r = 0; r < rows; ++r)
v[r] = (*this)[r][c];
return v;
}
Matrix& reset(PBQPNum val = 0) {
std::fill(data, data + (rows * cols), val);
return *this;
}
Matrix& setRow(unsigned r, PBQPNum val) {
assert(r < rows && "Row out of bounds.");
std::fill(data + (r * cols), data + ((r + 1) * cols), val);
return *this;
}
Matrix& setCol(unsigned c, PBQPNum val) {
assert(c < cols && "Column out of bounds.");
for (unsigned r = 0; r < rows; ++r)
(*this)[r][c] = val;
return *this;
}
Matrix transpose() const {
Matrix m(cols, rows);
for (unsigned r = 0; r < rows; ++r)
for (unsigned c = 0; c < cols; ++c)
m[c][r] = (*this)[r][c];
return m;
}
Vector diagonalize() const {
assert(rows == cols && "Attempt to diagonalize non-square matrix.");
Vector v(rows);
for (unsigned r = 0; r < rows; ++r)
v[r] = (*this)[r][r];
return v;
}
Matrix& operator+=(const Matrix &m) {
assert(rows == m.rows && cols == m.cols &&
"Matrix dimensions mismatch.");
std::transform(data, data + (rows * cols), m.data, data,
std::plus<PBQPNum>());
return *this;
}
PBQPNum getRowMin(unsigned r) const {
assert(r < rows && "Row out of bounds");
return *std::min_element(data + (r * cols), data + ((r + 1) * cols));
}
PBQPNum getColMin(unsigned c) const {
PBQPNum minElem = (*this)[0][c];
for (unsigned r = 1; r < rows; ++r)
if ((*this)[r][c] < minElem) minElem = (*this)[r][c];
return minElem;
}
Matrix& subFromRow(unsigned r, PBQPNum val) {
assert(r < rows && "Row out of bounds");
std::transform(data + (r * cols), data + ((r + 1) * cols),
data + (r * cols),
std::bind2nd(std::minus<PBQPNum>(), val));
return *this;
}
Matrix& subFromCol(unsigned c, PBQPNum val) {
for (unsigned r = 0; r < rows; ++r)
(*this)[r][c] -= val;
return *this;
}
bool isZero() const {
return find_if(data, data + (rows * cols),
std::bind2nd(std::not_equal_to<PBQPNum>(), 0)) ==
data + (rows * cols);
}
private:
unsigned rows, cols;
PBQPNum *data;
};
template <typename OStream>
OStream& operator<<(OStream &os, const Matrix &m) {
assert((m.getRows() != 0) && "Zero-row matrix badness.");
for (unsigned i = 0; i < m.getRows(); ++i) {
os << m.getRowAsVector(i);
}
return os;
}
}
#endif // LLVM_CODEGEN_PBQP_MATH_H