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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 | /*************************************************************************
*
* Project
* _____ _____ __ __ _____
* / ____| __ \| \/ | __ \
* ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
* / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
*| (_) | |_) | __/ | | | |__| | | | | | | |
* \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
* | |
* |_|
*
* Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
*
* This software is licensed as described in the file LICENSE, which
* you should have received as part of this distribution. The terms
* among other details are referenced in the official documentation
* seen here : https://akielaries.github.io/openGPMP/ along with
* important files seen in this project.
*
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file. As this is an Open Source effort, all implementations
* must be of the same methodology.
*
*
*
* This software is distributed on an AS IS basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied.
*
************************************************************************/
#include <openGPMP/linalg/tensor.hpp>
#include <stdexcept>
// Constructors
gpmp::linalg::Tensor::Tensor() : data_({{{}}}), dimensions_{0, 0, 0} {
}
gpmp::linalg::Tensor::Tensor(const std::vector<size_t> &dimensions) {
if (dimensions.empty()) {
throw std::invalid_argument("Error: Tensor dimensions are empty.");
}
// Assign values to dimensions_
for (size_t i = 0; i < 3; ++i) {
dimensions_[i] = dimensions[i];
}
size_t totalSize = 1;<--- Variable 'totalSize' is assigned a value that is never used.
for (size_t dim : dimensions) {
totalSize *= dim;<--- Consider using std::accumulate algorithm instead of a raw loop.<--- Variable 'totalSize' is assigned a value that is never used.
}
data_ = std::vector<std::vector<std::vector<double>>>(
dimensions_[0],
std::vector<std::vector<double>>(
dimensions_[1],
std::vector<double>(dimensions_[2], 0.0)));
}
gpmp::linalg::Tensor::Tensor(
const std::vector<std::vector<std::vector<double>>> &data)
: data_(data) {
if (data.empty() || data[0].empty()) {
throw std::invalid_argument("Error: Tensor data is empty.");
}
dimensions_[0] = data.size();
dimensions_[1] = data[0].size();
dimensions_[2] = data[0][0].size();
}
gpmp::linalg::Tensor
gpmp::linalg::Tensor::add(const gpmp::linalg::Tensor &other) const {
if (dimensions_[0] != other.dimensions_[0] ||
dimensions_[1] != other.dimensions_[1] ||
dimensions_[2] != other.dimensions_[2]) {
throw std::invalid_argument(
"Error: Tensor dimensions do not match for addition.");
}
gpmp::linalg::Tensor result;
result.data_ = data_;
for (size_t i = 0; i < dimensions_[0]; ++i) {
for (size_t j = 0; j < dimensions_[1]; ++j) {
for (size_t k = 0; k < dimensions_[2]; ++k) {
result.data_[i][j][k] += other.data_[i][j][k];
}
}
}
return result;
}
gpmp::linalg::Tensor gpmp::linalg::Tensor::multiply(double scalar) const {
gpmp::linalg::Tensor result;
result.data_ = data_;
for (size_t i = 0; i < dimensions_[0]; ++i) {
for (size_t j = 0; j < dimensions_[1]; ++j) {
for (size_t k = 0; k < dimensions_[2]; ++k) {
result.data_[i][j][k] *= scalar;
}
}
}
return result;
}
gpmp::linalg::Tensor gpmp::linalg::Tensor::multiply(const Tensor &other) const {
if (dimensions_[2] != other.dimensions_[1]) {
throw std::invalid_argument(
"Error: Tensor dimensions do not match for multiplication.");
}
gpmp::linalg::Tensor result;
result.dimensions_[0] = dimensions_[0];
result.dimensions_[1] = dimensions_[1];
result.dimensions_[2] = other.dimensions_[2];
result.data_ = std::vector<std::vector<std::vector<double>>>(
dimensions_[0],
std::vector<std::vector<double>>(
dimensions_[1],
std::vector<double>(other.dimensions_[2], 0.0)));
for (size_t i = 0; i < dimensions_[0]; ++i) {
for (size_t j = 0; j < dimensions_[1]; ++j) {
for (size_t k = 0; k < other.dimensions_[2]; ++k) {
for (size_t l = 0; l < dimensions_[2]; ++l) {
result.data_[i][j][k] +=
data_[i][j][l] * other.data_[l][j][k];
}
}
}
}
return result;
}
double gpmp::linalg::Tensor::get(const std::vector<size_t> &indices) const {
if (indices.size() != 3) {
throw std::out_of_range(
"Error: Invalid number of indices for tensor access.");
}
for (size_t i = 0; i < 3; ++i) {
if (indices[i] >= dimensions_[i]) {
throw std::out_of_range(
"Error: Index out of bounds for tensor access.");
}
}
return data_[indices[0]][indices[1]][indices[2]];
}
void gpmp::linalg::Tensor::set(const std::vector<size_t> &indices,
double value) {
if (indices.size() != 3) {
throw std::out_of_range(
"Error: Invalid number of indices for tensor access.");
}
for (size_t i = 0; i < 3; ++i) {
if (indices[i] >= dimensions_[i]) {
throw std::out_of_range(
"Error: Index out of bounds for tensor access.");
}
}
data_[indices[0]][indices[1]][indices[2]] = value;
}
void gpmp::linalg::Tensor::display() const {
for (size_t i = 0; i < dimensions_[0]; ++i) {
for (size_t j = 0; j < dimensions_[1]; ++j) {
for (size_t k = 0; k < dimensions_[2]; ++k) {
std::cout << data_[i][j][k] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
}
|