SVecReduceExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECREDUCEEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECREDUCEEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
49 #include <blaze/util/Assert.h>
51 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // GLOBAL FUNCTIONS
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
89 template< typename VT // Type of the sparse vector
90  , bool TF // Transpose flag
91  , typename OP > // Type of the reduction operation
92 inline decltype(auto) reduce( const SparseVector<VT,TF>& sv, OP op )
93 {
95 
96  using CT = CompositeType_t<VT>;
97  using ET = ElementType_t<VT>;
98 
99  if( (~sv).size() == 0UL ) return ET{};
100 
101  CT tmp( ~sv );
102 
103  BLAZE_INTERNAL_ASSERT( tmp.size() == (~sv).size(), "Invalid vector size" );
104 
105  const auto end( tmp.end() );
106  auto element( tmp.begin() );
107 
108  if( element == end ) return ET{};
109 
110  ET redux( element->value() );
111  ++element;
112 
113  for( ; element!=end; ++element ) {
114  redux = op( redux, element->value() );
115  }
116 
117  return redux;
118 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
140 template< typename VT // Type of the sparse vector
141  , bool TF > // Transpose flag
142 inline decltype(auto) sum( const SparseVector<VT,TF>& sv )
143 {
145 
146  return reduce( ~sv, Add() );
147 }
148 //*************************************************************************************************
149 
150 
151 //*************************************************************************************************
168 template< typename VT // Type of the sparse vector
169  , bool TF > // Transpose flag
170 inline decltype(auto) prod( const SparseVector<VT,TF>& sv )
171 {
173 
174  return reduce( ~sv, Mult() );
175 }
176 //*************************************************************************************************
177 
178 
179 //*************************************************************************************************
200 template< typename VT // Type of the sparse vector
201  , bool TF > // Transpose flag
202 inline decltype(auto) min( const SparseVector<VT,TF>& sv )
203 {
205 
206  return reduce( ~sv, Min() );
207 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
232 template< typename VT // Type of the sparse vector
233  , bool TF > // Transpose flag
234 inline decltype(auto) max( const SparseVector<VT,TF>& sv )
235 {
237 
238  return reduce( ~sv, Max() );
239 }
240 //*************************************************************************************************
241 
242 } // namespace blaze
243 
244 #endif
Header file for auxiliary alias declarations.
Header file for basic type definitions.
Header file for the SparseVector base class.
Header file for the Add functor.
decltype(auto) prod(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of multiplication.
Definition: DMatReduceExpr.h:2219
Generic wrapper for the addition operator.
Definition: Add.h:80
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
decltype(auto) reduce(const DenseMatrix< MT, SO > &dm, OP op)
Performs a custom reduction operation on the given dense matrix.
Definition: DMatReduceExpr.h:2016
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1147
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2146
Header file for the Mult functor.
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1179
MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:438
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Generic wrapper for the max() function.
Definition: Max.h:79
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Generic wrapper for the min() function.
Definition: Min.h:79
Header file for the Min functor.
Generic wrapper for the multiplication operator.
Definition: Mult.h:77
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:138
Header file for the Max functor.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the function trace functionality.