-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMXN.h
More file actions
94 lines (67 loc) · 1.4 KB
/
MatrixMXN.h
File metadata and controls
94 lines (67 loc) · 1.4 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include "VectorND.h"
#include <fstream>
template<class T>
class MatrixMN
{
public:
int num_rows_; // m_
int num_cols_; // n_
T *values_;
MatrixMN()
: values_(nullptr), num_rows_(0), num_cols_(0)
{}
MatrixMN(const int& _m, const int& _n)
: values_(nullptr), num_rows_(0), num_cols_(0)
{}
void initialize(const int& _m, const int& _n)
{
num_rows_ = _m;
num_cols_ = _n;
SAFE_DELETE_ARRAY(values_);
const int num_all = num_rows_ * num_cols_;
{
values_ = new T[num_all];
for (int i = 0; i < num_all; i++)
values_[i] = (T)0;
}
}
void cout()
{
for (int row = 0; row < num_rows_; row++)
{
for (int col = 0; col < num_cols_; col++)
{
std::cout << getValue(row, col) << " ";
}
std::cout << std::endl;
}
}
void writeTXT(std::ofstream& of) const
{
of << num_rows_ << " " << num_cols_ << std::endl;
for (int i = 0; i < num_rows_ * num_cols_; i++)
{
of << values_[i];
if (i != num_rows_ * num_cols_ - 1)
of << " ";
}
of << std::endl;
}
void multiply(const VectorND<T>& vector, VectorND<T>& result) const
{
#pragma omp parallel for
for (int row = 0; row < num_rows_; row++)
{
result.values_[row] = (T)0;
int ix = row*num_cols_;
T temp;
for (int col = 0; col < num_cols_; col++, ix++)
{
temp = values_[ix];
temp *= vector.values_[col];
result.values_[row] += temp;
}
}
}
};