42 if (dimensions.empty()) {
43 throw std::invalid_argument(
"Error: Tensor dimensions are empty.");
47 for (
size_t i = 0; i < 3; ++i) {
48 dimensions_[i] = dimensions[i];
52 for (
size_t dim : dimensions) {
55 data_ = std::vector<std::vector<std::vector<double>>>(
57 std::vector<std::vector<double>>(
59 std::vector<double>(dimensions_[2], 0.0)));
63 const std::vector<std::vector<std::vector<double>>> &data)
65 if (data.empty() || data[0].empty()) {
66 throw std::invalid_argument(
"Error: Tensor data is empty.");
79 throw std::invalid_argument(
80 "Error: Tensor dimensions do not match for addition.");
86 for (
size_t i = 0; i < dimensions_[0]; ++i) {
87 for (
size_t j = 0; j < dimensions_[1]; ++j) {
88 for (
size_t k = 0; k < dimensions_[2]; ++k) {
101 for (
size_t i = 0; i < dimensions_[0]; ++i) {
102 for (
size_t j = 0; j < dimensions_[1]; ++j) {
103 for (
size_t k = 0; k < dimensions_[2]; ++k) {
104 result.
data_[i][j][k] *= scalar;
114 throw std::invalid_argument(
115 "Error: Tensor dimensions do not match for multiplication.");
123 result.
data_ = std::vector<std::vector<std::vector<double>>>(
125 std::vector<std::vector<double>>(
129 for (
size_t i = 0; i < dimensions_[0]; ++i) {
130 for (
size_t j = 0; j < dimensions_[1]; ++j) {
131 for (
size_t k = 0; k < other.
dimensions_[2]; ++k) {
132 for (
size_t l = 0; l < dimensions_[2]; ++l) {
133 result.
data_[i][j][k] +=
134 data_[i][j][l] * other.
data_[l][j][k];
144 if (indices.size() != 3) {
145 throw std::out_of_range(
146 "Error: Invalid number of indices for tensor access.");
149 for (
size_t i = 0; i < 3; ++i) {
150 if (indices[i] >= dimensions_[i]) {
151 throw std::out_of_range(
152 "Error: Index out of bounds for tensor access.");
156 return data_[indices[0]][indices[1]][indices[2]];
161 if (indices.size() != 3) {
162 throw std::out_of_range(
163 "Error: Invalid number of indices for tensor access.");
166 for (
size_t i = 0; i < 3; ++i) {
167 if (indices[i] >= dimensions_[i]) {
168 throw std::out_of_range(
169 "Error: Index out of bounds for tensor access.");
173 data_[indices[0]][indices[1]][indices[2]] = value;
177 for (
size_t i = 0; i < dimensions_[0]; ++i) {
178 for (
size_t j = 0; j < dimensions_[1]; ++j) {
179 for (
size_t k = 0; k < dimensions_[2]; ++k) {
180 std::cout << data_[i][j][k] <<
" ";
Represents a 3D tensor with basic operations.
void set(const std::vector< size_t > &indices, double value)
Sets the value at the specified indices.
Tensor add(const Tensor &other) const
Adds another tensor to the current tensor.
double get(const std::vector< size_t > &indices) const
Gets the value at the specified indices.
void display() const
Displays the tensor.
Tensor multiply(double scalar) const
Multiplies the tensor by a scalar value.
Tensor()
Default constructor Creates an empty tensor.
std::vector< std::vector< std::vector< double > > > data_
< 3D vector representing the tensor data