-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmatrix.hpp
More file actions
55 lines (43 loc) · 1.53 KB
/
matrix.hpp
File metadata and controls
55 lines (43 loc) · 1.53 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
#pragma once
#include <vector>
namespace simplex {
/// a really tirvial class for 2d-matrices. If you need more bells ans whistles go somewhere
/// else. See example for usage
template <class T>
class Matrix
{
private:
int rows = 0;
int columns = 0;
std::vector<T> values;
public:
Matrix() = default;
T & operator()(int row, int col) { return values[row*columns+col]; }
const T & operator()(int row, int col) const { return values[row*columns+col]; }
// these two allow access of the matrix like a vector. It doesn't even depend
// on row or column vector...
T & operator()(int pos) { return values[pos]; }
const T & operator()(int pos) const { return values[pos]; }
int getColumns() const { return columns; }
int getRows() const { return rows; }
void resize(int r, int c)
{
// make sure the resized vector is completely zeroed
values.clear();
values.resize(c*r);
rows = r;
columns = c;
}
};
/// trait to use our simplistic class with the Solver
template <class T> struct matrix_traits<Matrix<T>>
{
using index_t = int;
using scalar_t = T;
static index_t columns(const Matrix<T> & m) { return m.getColumns(); }
static index_t rows(const Matrix<T> & m) { return m.getRows(); }
static auto get(const Matrix<T> & m, index_t row, index_t col) { return m(row, col); }
static void set(Matrix<T> & m, index_t row, index_t col, double val) { m(row, col) = val; }
static void resize(Matrix<T> & m, index_t rows, index_t cols) { m.resize(rows, cols); }
};
}