-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensor4.cpp
153 lines (139 loc) · 3.37 KB
/
Tensor4.cpp
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
149
150
151
152
153
//#include "stdafx.h"
#include "Tensor4.h"
#include "Tensors.h"
#include "Matrices.h"
#include "Vectors.h"
Tensor4::Tensor4(int number_axis1, int number_axis2, int number_rows, int number_columns) : axle1(number_axis1), axle2(number_axis2), rows(number_rows), columns(number_columns)
{
elements = new double***[axle1];
for (int i = 0; i < axle1; ++i)
{
elements[i] = new double** [axle2];
for (int j = 0; j < axle2; ++j)
{
elements[i][j] = new double* [rows];
for (int k = 0; k < rows; ++k)
{
elements[i][j][k] = new double[columns];
for (int l = 0; l < columns; ++l)
{
elements[i][j][k][l] = 0.;
}
}
}
}
}
Tensor4::Tensor4(const Tensor4& tensor) : axle1(tensor.axle1), axle2(tensor.axle2), rows(tensor.rows), columns(tensor.columns)
{
elements = new double*** [axle1];
for (int i = 0; i < axle1; ++i)
{
elements[i] = new double** [axle2];
for (int j = 0; j < axle2; ++j)
{
elements[i][j] = new double* [rows];
for (int k = 0; k < rows; ++k)
{
elements[i][j][k] = new double[columns];
for (int l = 0; l < columns; ++l)
{
elements[i][j][k][l] = tensor.getElements()[i][j][k][l];
}
}
}
}
}
void Tensor4::setElement(int axle1, int axle2, int row, int column, double element)
{
(*this).elements[axle1][axle2][row][column] = element;
}
void Tensor4::addElement(int axle1, int axle2, int row, int column, double element)
{
(*this).elements[axle1][axle2][row][column] += element;
}
void Tensor4::setMatrix(int axle1, int axle2, const Matrix &M)
{
for (int i = 0; i < M.getRows(); ++i)
{
for (int j = 0; j < M.getColumns(); ++j)
{
(*this).setElement(axle1, axle2, i, j, M.getElements()[i][j]);
}
}
}
void Tensor4::addMatrix(int axle1, int axle2, const Matrix &M)
{
for (int i = 0; i < M.getRows(); ++i)
{
for (int j = 0; j < M.getColumns(); ++j)
{
(*this).addElement(axle1, axle2, i, j, M.getElements()[i][j]);
}
}
}
Tensor4& Tensor4::operator =(const Tensor4& tensor)
{
axle1 = tensor.getAxle1();
axle2 = tensor.getAxle2();
rows = tensor.getRows();
columns = tensor.getColumns();
{
elements = new double*** [axle1];
for (int i = 0; i < axle1; ++i)
{
elements[i] = new double** [axle2];
for (int j = 0; j < axle2; ++j)
{
elements[i][j] = new double* [rows];
for (int k = 0; k < rows; ++k)
{
elements[i][j][k] = new double[columns];
for (int l = 0; l < columns; ++l)
{
elements[i][j][k][l] = tensor.getElements()[i][j][k][l];
}
}
}
}
}
return (*this);
}
Tensor3 Tensor4::operator [](const int index)
{
int axle = (*this).getAxle2();
int len_column = (*this).getColumns();
int len_row = (*this).getRows();
Tensor3 res(axle, len_row, len_column);
for (int i = 0; i < axle; ++i)
{
for (int k = 0; k < len_row; ++k)
{
for (int j = 0; j < len_column; ++j)
{
res.setElement(i, k, j, (*this).getElements()[index][i][k][j]);
}
}
}
return res;
}
std::ostream& operator<<(std::ostream& out_stream, const Tensor4& tensor)
{
for (int m = 0; m < tensor.axle1; ++m)
{
out_stream << "Axle1 number " << m << "\n";
for (int i = 0; i < tensor.axle2; ++i)
{
out_stream << "Axle2 number " << i << "\n";
for (int j = 0; j < tensor.rows; ++j)
{
for (int k = 0; k < tensor.columns; ++k)
{
out_stream << tensor.getElements()[m][i][j][k] << " ";
}
out_stream << "\n";
}
out_stream << "\n";
}
}
return out_stream;
}