-
Notifications
You must be signed in to change notification settings - Fork 0
/
DenseMatrix.h
81 lines (69 loc) · 2.03 KB
/
DenseMatrix.h
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
#pragma once
#include <vector>
#include <assert.h>
#include <iomanip>
#include "NumericTypeConcept.h"
#include "Triplet.h"
namespace Barta {
template<NumericType T>
class DenseMatrix {
using Values = std::vector<std::vector<T>>;
using TripletType = Triplet<T>;
using VectorType = std::vector<T>;
unsigned int width;
unsigned int height;
Values values;
public:
DenseMatrix(
unsigned int width,
unsigned int height
) :
width(width),
height(height)
{
this->values.resize(this->height);
for (auto& row : this->values) {
row.resize(this->width, static_cast<T>(0));
}
}
DenseMatrix(
unsigned int width,
unsigned int height,
std::vector<TripletType> triplets
) :
DenseMatrix(
width,
height
)
{
for (const auto triplet : triplets) {
this->values[triplet.row][triplet.col] += triplet.val;
}
}
VectorType operator * (const VectorType& v) const {
assert(this->height == v.size());
auto ret = VectorType(v.size(), static_cast<T>(0));
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
ret[i] += this->values[i][j] * v[j];
}
}
return ret;
}
std::string toString() const {
std::stringstream ss;
constexpr const unsigned int w = 6;
ss << "[";
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
ss << std::setw(w) << this->values[i][j];
}
if (i == this->height - 1) {
ss << "]";
}
ss << std::endl;
}
return ss.str();
}
};
}