Tensor
A C++ expression template library for computations on N dimensional tensors
tensor_subtraction.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file Header file for tensor sybtraction 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_SUBTRACTION_HPP
24 #define FTL_TENSOR_SUBTRACTION_HPP
25 
26 #include "tensor_expressions.hpp"
27 
28 namespace ftl {
29 
30 // ----------------------------------------------------------------------------------------------------------
31 /// @class TensorSubtraction
32 /// @brief Expression class for calculating the subtraction of two tensors.
33 /// @tparam E1 The first expression for subtraction
34 /// @tparam E2 The second expression for subtraction
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 TensorSubtraction : public TensorExpression<TensorSubtraction<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 subtraction
47  E2 const& _y; //!< Second expression for subtraction
48 public:
49  // ------------------------------------------------------------------------------------------------------
50  /// @brief Sets the expressions for subtraction and checks that they have the same ranks and dimension
51  /// @param[in] x The first expression for subtraction.
52  /// @param[in] y The second expression for subtraction
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_subtraction.
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_subtraction.
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 // ------------------------------------- SUBTRACTION 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  // TODO: Add error throwing
90  // Check that the ranks are equal
91  if (x.rank() != y.rank() && x.dim_sizes() != y.dim_sizes()) ;
92  // Throw error here
93 }
94 
95 } // End namespace ftl
96 #endif // FTL_TENSOR_SUBTRACTION_HPP
Expression class for calculating the subtraction of two tensors.
Definition: tensor_subtraction.hpp:39
typename traits::data_type data_type
Definition: tensor_subtraction.hpp:44
const size_type size() const
Returns the size of the expression.
Definition: tensor_subtraction.hpp:66
data_type operator[](size_type i) const
Adds two elements (one from each Tensor) from the tensor expression data.
Definition: tensor_subtraction.hpp:79
Definition: mapper.hpp:37
typename traits::size_type size_type
Definition: tensor_subtraction.hpp:43
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_subtraction.hpp:41
const size_type rank() const
Returns the size of the expression.
Definition: tensor_subtraction.hpp:72
TensorSubtraction(TensorExpression< E1, T1 > const &x, TensorExpression< E2, T2 > const &y)
Sets the expressions for subtraction and checks that they have the same ranks and dimension...
Definition: tensor_subtraction.hpp:85
const dim_container & dim_sizes() const
Gets the sizes of the all the dimensions of the expression.
Definition: tensor_subtraction.hpp:60
typename traits::dim_container dim_container
Definition: tensor_subtraction.hpp:42