forked from Alfiuman/WhydahGally-LSTM-MLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
148 lines (126 loc) · 2.27 KB
/
Matrix.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
#include <iostream>
#include "Definitions.h"
namespace WhydahGally
{
template<typename T> struct Matrix
{
//Struct built around a dynamic allocated array in order to have a proper functioning matrix.
//Everything is public in order to simulate as much as possible a normal array.
int rows_;
int cols_;
T* elements_;
Matrix<T>(int x, int y = 1)
: rows_(x), cols_(y)
{
if (x <= 0)
{
rows_ = 1;
}
else
{
rows_ = x;
}
if (y <= 0)
{
cols_ = 1;
}
else
{
cols_ = y;
}
elements_ = new T[rows_ * cols_];
memset(elements_, 0, (rows_ * cols_) * sizeof(T));
}
Matrix<T>(const Matrix& x)
{
//Constructing a matrix copying another one.
if (x.rows_ <= 0)
{
rows_ = 1;
}
else
{
rows_ = x.rows_;
}
if (x.cols_ <= 0)
{
cols_ = 1;
}
else
{
cols_ = x.cols_;
}
elements_ = new T[rows_ * cols_];
for (int i = 0; i < rows_; ++i)
{
for (int j = 0; j < cols_; ++j)
{
elements_[i * cols_ + j] = x.elements_[i * x.cols_ + j];
}
}
}
~Matrix<T>()
{
delete[] elements_;
}
void resize(int x = 1, int y = 1)
{
//Resizing the matrix and zeroing its elements.
if (x <= 0)
{
rows_ = 1;
}
else
{
rows_ = x;
}
if (y <= 0)
{
cols_ = 1;
}
else
{
cols_ = y;
}
delete[] elements_;
elements_ = new T[rows_ * cols_];
memset(elements_, 0, (rows_ * cols_) * sizeof(T));
}
void assign(T x)
{
//Populating the entire matix with a value.
std::fill(elements_, elements_ + (rows_ * cols_), x);;
}
Matrix& copy(const Matrix& right)
{
//Reconstructing an existing matrix copying another one.
if (right.rows_ <= 0)
{
rows_ = 1;
}
else
{
rows_ = right.rows_;
}
if (right.cols_ <= 0)
{
cols_ = 1;
}
else
{
cols_ = right.cols_;
}
delete[] elements_;
elements_ = new T[rows_ * cols_];
for (int i = 0; i < rows_; ++i)
{
for (int j = 0; j < cols_; ++j)
{
elements_[i * cols_ + j] = right.elements_[i * right.cols_ + j];
}
}
return *this;
}
};
}