Tensor
A C++ expression template library for computations on N dimensional tensors
tensor_addition.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file Header file for tensor addition for tensor library.
3 // ----------------------------------------------------------------------------------------------------------
4 
5 /*
6  * ----------------------------------------------------------------------------------------------------------
7  * Tensor is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Tensor is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with tensor; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  * ----------------------------------------------------------------------------------------------------------
21  */
22 
23 #ifndef FTL_TENSOR_ADDITION_HPP
24 #define FTL_TENSOR_ADDITION_HPP
25 
26 #include "tensor_expressions.hpp"
27 
28 namespace ftl {
29 
30 // ----------------------------------------------------------------------------------------------------------
31 /// @class TensorAddition
32 /// @brief Expression class for calculating the addition of two tensors.
33 /// @tparam E1 The first expression for addition
34 /// @tparam E2 The second expression for addition
35 /// @tparam T1 The traits of the first expression
36 /// @tparam T2 The traits if the second expression
37 // ----------------------------------------------------------------------------------------------------------
38 template <typename E1, typename E2, typename T1, typename T2>
39 class TensorAddition : public TensorExpression<TensorAddition<E1, E2, T1, T2>, T1> {
40 public:
41  using traits = T1;
42  using dim_container = typename traits::dim_container;
43  using size_type = typename traits::size_type;
44  using data_type = typename traits::data_type;
45 private:
46  E1 const& _x; //!< First expression for addition
47  E2 const& _y; //!< Second expression for addition
48 public:
49  // ------------------------------------------------------------------------------------------------------
50  /// @brief Sets the expressions for addition and checks that they have the same ranks and dimension
51  /// @param[in] x The first expression for addition.
52  /// @param[in] y The second expression for addition
53  // ------------------------------------------------------------------------------------------------------
55 
56  // ------------------------------------------------------------------------------------------------------
57  /// @brief Gets the sizes of the all the dimensions of the expression.
58  /// @return A constant reference to the dimension size vector of the expression
59  // ------------------------------------------------------------------------------------------------------
60  inline const dim_container& dim_sizes() const { return _x.dim_sizes(); }
61 
62  // ------------------------------------------------------------------------------------------------------
63  /// @brief Returns the size of the expression.
64  /// @return The size of the tensor_addition.
65  // ------------------------------------------------------------------------------------------------------
66  inline const size_type size() const { return _x.size(); }
67 
68  // ------------------------------------------------------------------------------------------------------
69  /// @brief Returns the size of the expression.
70  /// @return The size of the tensor_addition.
71  // ------------------------------------------------------------------------------------------------------
72  inline const size_type rank() const { return _x.rank(); }
73 
74  // ------------------------------------------------------------------------------------------------------
75  /// @brief Adds two elements (one from each Tensor) from the tensor expression data.
76  /// @param[in] i The element in the expression which must be fetched.
77  /// @return The result of the subtraction of the Tensors.
78  // ------------------------------------------------------------------------------------------------------
79  inline data_type operator[](size_type i) const { return _x[i] + _y[i]; }
80 };
81 
82 // ------------------------------------- ADDITION IMPLEMENTATIONS -------------------------------------------
83 
84 template <typename E1, typename E2, typename T1, typename T2>
86  const TensorExpression<E2, T2>& y)
87 : _x(x), _y(y)
88 {
89  int i = 0;
90  while (x.dim_sizes()[i] == y.dim_sizes()[i]) ++i; // Check equality of dimension sizes
91 
92  // TODO: Add error throwing
93  // Check that the ranks are equal
94  if (x.rank() != y.rank() || i != x.rank()) ;
95  // Throw error here
96 }
97 
98 } // End namespace ftl
99 #endif // FTL_TENSOR_ADDITION_HPP
Expression class for calculating the addition of two tensors.
Definition: tensor_addition.hpp:39
data_type operator[](size_type i) const
Adds two elements (one from each Tensor) from the tensor expression data.
Definition: tensor_addition.hpp:79
typename traits::size_type size_type
Definition: tensor_addition.hpp:43
Definition: mapper.hpp:37
Defines a general tensor expression so that opertions on tensor expressions can be defined the syntax...
Definition: tensor_expression_interface.hpp:49
T1 traits
Definition: tensor_addition.hpp:41
typename traits::data_type data_type
Definition: tensor_addition.hpp:44
typename traits::dim_container dim_container
Definition: tensor_addition.hpp:42
const size_type rank() const
Returns the size of the expression.
Definition: tensor_addition.hpp:72
const size_type size() const
Returns the size of the expression.
Definition: tensor_addition.hpp:66
TensorAddition(TensorExpression< E1, T1 > const &x, TensorExpression< E2, T2 > const &y)
Sets the expressions for addition and checks that they have the same ranks and dimension.
Definition: tensor_addition.hpp:85
const dim_container & dim_sizes() const
Gets the sizes of the all the dimensions of the expression.
Definition: tensor_addition.hpp:60