openGPMP
Open Source Mathematics Package
Public Member Functions | Private Attributes | List of all members
gpmp::linalg::Tensor Class Reference

Represents a 3D tensor with basic operations. More...

#include <tensor.hpp>

Public Member Functions

 Tensor ()
 Default constructor Creates an empty tensor. More...
 
 Tensor (const std::vector< size_t > &dimensions)
 Constructor with specified dimensions. More...
 
 Tensor (const std::vector< std::vector< std::vector< double >>> &data)
 Constructor with initial data. More...
 
Tensor add (const Tensor &other) const
 Adds another tensor to the current tensor. More...
 
Tensor multiply (double scalar) const
 Multiplies the tensor by a scalar value. More...
 
Tensor multiply (const Tensor &other) const
 Multiplies the tensor by another tensor. More...
 
double get (const std::vector< size_t > &indices) const
 Gets the value at the specified indices. More...
 
void set (const std::vector< size_t > &indices, double value)
 Sets the value at the specified indices. More...
 
void display () const
 Displays the tensor. More...
 

Private Attributes

std::vector< std::vector< std::vector< double > > > data_
 < 3D vector representing the tensor data More...
 
size_t dimensions_ [3]
 

Detailed Description

Represents a 3D tensor with basic operations.

This class provides basic operations for a 3D tensor, including addition, multiplication, element access, and display functionality

Definition at line 51 of file tensor.hpp.

Constructor & Destructor Documentation

◆ Tensor() [1/3]

gpmp::linalg::Tensor::Tensor ( )

Default constructor Creates an empty tensor.

Definition at line 38 of file tensor.cpp.

38  : data_({{{}}}), dimensions_{0, 0, 0} {
39 }
size_t dimensions_[3]
Definition: tensor.hpp:127
std::vector< std::vector< std::vector< double > > > data_
< 3D vector representing the tensor data
Definition: tensor.hpp:125

◆ Tensor() [2/3]

gpmp::linalg::Tensor::Tensor ( const std::vector< size_t > &  dimensions)
explicit

Constructor with specified dimensions.

Parameters
dimensionsA vector containing three dimensions for the tensor
Exceptions
std::invalid_argumentif dimensions are empty

Constructs a tensor with specified dimensions and initializes the data

Definition at line 41 of file tensor.cpp.

41  {
42  if (dimensions.empty()) {
43  throw std::invalid_argument("Error: Tensor dimensions are empty.");
44  }
45 
46  // Assign values to dimensions_
47  for (size_t i = 0; i < 3; ++i) {
48  dimensions_[i] = dimensions[i];
49  }
50 
51  size_t totalSize = 1;
52  for (size_t dim : dimensions) {
53  totalSize *= dim;
54  }
55  data_ = std::vector<std::vector<std::vector<double>>>(
56  dimensions_[0],
57  std::vector<std::vector<double>>(
58  dimensions_[1],
59  std::vector<double>(dimensions_[2], 0.0)));
60 }

◆ Tensor() [3/3]

gpmp::linalg::Tensor::Tensor ( const std::vector< std::vector< std::vector< double >>> &  data)

Constructor with initial data.

Parameters
data3D vector representing the initial data for the tensor
Exceptions
std::invalid_argumentif data is empty

Constructs a tensor with the given initial data and sets dimensions accordingly

Definition at line 62 of file tensor.cpp.

64  : data_(data) {
65  if (data.empty() || data[0].empty()) {
66  throw std::invalid_argument("Error: Tensor data is empty.");
67  }
68 
69  dimensions_[0] = data.size();
70  dimensions_[1] = data[0].size();
71  dimensions_[2] = data[0][0].size();
72 }

References dimensions_.

Member Function Documentation

◆ add()

gpmp::linalg::Tensor gpmp::linalg::Tensor::add ( const Tensor other) const

Adds another tensor to the current tensor.

Parameters
otherTensor to be added
Returns
A new tensor representing the sum
Exceptions
std::invalid_argumentif dimensions do not match

Definition at line 75 of file tensor.cpp.

75  {
76  if (dimensions_[0] != other.dimensions_[0] ||
77  dimensions_[1] != other.dimensions_[1] ||
78  dimensions_[2] != other.dimensions_[2]) {
79  throw std::invalid_argument(
80  "Error: Tensor dimensions do not match for addition.");
81  }
82 
83  gpmp::linalg::Tensor result;
84  result.data_ = data_;
85 
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) {
89  result.data_[i][j][k] += other.data_[i][j][k];
90  }
91  }
92  }
93 
94  return result;
95 }
Represents a 3D tensor with basic operations.
Definition: tensor.hpp:51

References data_, and dimensions_.

◆ display()

void gpmp::linalg::Tensor::display ( ) const

Displays the tensor.

Definition at line 176 of file tensor.cpp.

176  {
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] << " ";
181  }
182  std::cout << "\n";
183  }
184  std::cout << "\n";
185  }
186 }

◆ get()

double gpmp::linalg::Tensor::get ( const std::vector< size_t > &  indices) const

Gets the value at the specified indices.

Parameters
indicesVector containing three indices
Returns
The value at the specified indices
Exceptions
std::out_of_rangeif the number of indices is not 3 or if an index is out of bounds

Definition at line 143 of file tensor.cpp.

143  {
144  if (indices.size() != 3) {
145  throw std::out_of_range(
146  "Error: Invalid number of indices for tensor access.");
147  }
148 
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.");
153  }
154  }
155 
156  return data_[indices[0]][indices[1]][indices[2]];
157 }

◆ multiply() [1/2]

gpmp::linalg::Tensor gpmp::linalg::Tensor::multiply ( const Tensor other) const

Multiplies the tensor by another tensor.

Parameters
otherTensor to be multiplied
Returns
A new tensor representing the product
Exceptions
std::invalid_argumentif dimensions do not match

Definition at line 112 of file tensor.cpp.

112  {
113  if (dimensions_[2] != other.dimensions_[1]) {
114  throw std::invalid_argument(
115  "Error: Tensor dimensions do not match for multiplication.");
116  }
117 
118  gpmp::linalg::Tensor result;
119  result.dimensions_[0] = dimensions_[0];
120  result.dimensions_[1] = dimensions_[1];
121  result.dimensions_[2] = other.dimensions_[2];
122 
123  result.data_ = std::vector<std::vector<std::vector<double>>>(
124  dimensions_[0],
125  std::vector<std::vector<double>>(
126  dimensions_[1],
127  std::vector<double>(other.dimensions_[2], 0.0)));
128 
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];
135  }
136  }
137  }
138  }
139 
140  return result;
141 }

References data_, and dimensions_.

◆ multiply() [2/2]

gpmp::linalg::Tensor gpmp::linalg::Tensor::multiply ( double  scalar) const

Multiplies the tensor by a scalar value.

Parameters
scalarThe scalar value
Returns
A new tensor representing the product

Definition at line 97 of file tensor.cpp.

97  {
98  gpmp::linalg::Tensor result;
99  result.data_ = data_;
100 
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;
105  }
106  }
107  }
108 
109  return result;
110 }

References data_.

◆ set()

void gpmp::linalg::Tensor::set ( const std::vector< size_t > &  indices,
double  value 
)

Sets the value at the specified indices.

Parameters
indicesVector containing three indices
valueThe value to set
Exceptions
std::out_of_rangeif the number of indices is not 3 or if an index is out of bounds

Definition at line 159 of file tensor.cpp.

160  {
161  if (indices.size() != 3) {
162  throw std::out_of_range(
163  "Error: Invalid number of indices for tensor access.");
164  }
165 
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.");
170  }
171  }
172 
173  data_[indices[0]][indices[1]][indices[2]] = value;
174 }

Member Data Documentation

◆ data_

std::vector<std::vector<std::vector<double> > > gpmp::linalg::Tensor::data_
private

< 3D vector representing the tensor data

Array representing the dimensions of the tensor

Definition at line 125 of file tensor.hpp.

Referenced by add(), and multiply().

◆ dimensions_

size_t gpmp::linalg::Tensor::dimensions_[3]
private

Definition at line 127 of file tensor.hpp.

Referenced by add(), multiply(), and Tensor().


The documentation for this class was generated from the following files: