-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensors.cpp
105 lines (92 loc) · 2.15 KB
/
Tensors.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
//#include "stdafx.h"
#include "Tensors.h"
#include "Matrices.h"
#include "Vectors.h"
Tensor3::Tensor3(int number_axis, int number_rows, int number_columns) : axis(number_axis), rows(number_rows), columns(number_columns)
{
elements = new double** [axis];
for (int i = 0; i < axis; ++i)
{
elements[i] = new double* [rows];
for (int j = 0; j < rows; ++j)
{
elements[i][j] = new double [columns];
for (int k = 0; k < columns; ++k)
{
elements[i][j][k] = 0.;
}
}
}
}
Tensor3::Tensor3(const Tensor3& tensor) : axis(tensor.axis), rows(tensor.rows), columns(tensor.columns)
{
elements = new double** [axis];
for (int i = 0; i < axis; ++i)
{
elements[i] = new double* [rows];
for (int j = 0; j < rows; ++j)
{
elements[i][j] = new double[columns];
for (int k = 0; k < columns; ++k)
{
elements[i][j][k] = tensor.getElements()[i][j][k];
}
}
}
}
void Tensor3::setElement(int axle, int row, int column, double element)
{
(*this).elements[axle][row][column] = element;
}
void Tensor3::addElement(int axle, int row, int column, double element)
{
(*this).elements[axle][row][column] += element;
}
Tensor3& Tensor3::operator =(const Tensor3& tensor)
{
axis = tensor.getAxes();
rows = tensor.getRows();
columns = tensor.getColumns();
for (int i = 0; i < axis; ++i)
{
for (int j = 0; j < rows; ++j)
{
for (int k = 0; k < columns; ++k)
{
elements[i][j][k] = tensor.getElements()[i][j][k];
}
}
}
return (*this);
}
Matrix Tensor3::operator [](const int index)
{
int len_column = (*this).getColumns();
int len_row = (*this).getRows();
Matrix res(len_row, len_column);
for (int k = 0; k < len_row; ++k)
{
for (int j = 0; j < len_column; ++j)
{
res.setElement(k, j, (*this).getElements()[index][k][j]);
}
}
return res;
}
std::ostream& operator<<(std::ostream& out_stream, const Tensor3& tensor)
{
for (int i = 0; i < tensor.axis; ++i)
{
out_stream << "Axle number " << i << "\n";
for (int j = 0; j < tensor.rows; ++j)
{
for (int k = 0; k < tensor.columns; ++k)
{
out_stream << tensor.getElements()[i][j][k] << " ";
}
out_stream << "\n";
}
out_stream << "\n";
}
return out_stream;
}