Blaze 3.9
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
54namespace blaze {
55
56//=================================================================================================
57//
58// GLOBAL FUNCTIONS
59//
60//=================================================================================================
61
62//*************************************************************************************************
89template< typename VT // Type of the sparse vector
90 , bool TF // Transpose flag
91 , typename OP > // Type of the reduction operation
92inline decltype(auto) reduce( const SparseVector<VT,TF>& sv, OP op )
93{
95
96 using CT = CompositeType_t<VT>;
98
99 if( (*sv).size() == 0UL ) return RT{};
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 RT{};
109
110 RT 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//*************************************************************************************************
140template< typename VT // Type of the sparse vector
141 , bool TF > // Transpose flag
142inline decltype(auto) sum( const SparseVector<VT,TF>& sv )
143{
145
146 return reduce( *sv, Add() );
147}
148//*************************************************************************************************
149
150
151//*************************************************************************************************
168template< typename VT // Type of the sparse vector
169 , bool TF > // Transpose flag
170inline decltype(auto) prod( const SparseVector<VT,TF>& sv )
171{
173
174 return reduce( *sv, Mult() );
175}
176//*************************************************************************************************
177
178
179//*************************************************************************************************
200template< typename VT // Type of the sparse vector
201 , bool TF > // Transpose flag
202inline decltype(auto) min( const SparseVector<VT,TF>& sv )
203{
205
206 return reduce( *sv, Min() );
207}
208//*************************************************************************************************
209
210
211//*************************************************************************************************
232template< typename VT // Type of the sparse vector
233 , bool TF > // Transpose flag
234inline decltype(auto) max( const SparseVector<VT,TF>& sv )
235{
237
238 return reduce( *sv, Max() );
239}
240//*************************************************************************************************
241
242
243//*************************************************************************************************
260template< typename VT // Type of the sparse vector
261 , bool TF > // Transpose flag
262inline size_t argmin( const SparseVector<VT,TF>& sv )
263{
264 if( (*sv).size() < 2UL )
265 return 0UL;
266
267 CompositeType_t<VT> a( *sv ); // Evaluation of the sparse vector operand
268
269 if( a.nonZeros() == 0UL )
270 return 0UL;
271
272 const auto end( a.end() );
273 auto element( a.begin() );
274 size_t index( element->index() );
275 auto min( element->value() );
276
277 ++element;
278
279 for( ; element!=end; ++element ) {
280 auto cur( element->value() );
281 if( cur < min ) {
282 index = element->index();
283 min = std::move( cur );
284 }
285 }
286
287 return index;
288}
289//*************************************************************************************************
290
291
292//*************************************************************************************************
309template< typename VT // Type of the sparse vector
310 , bool TF > // Transpose flag
311inline size_t argmax( const SparseVector<VT,TF>& sv )
312{
313 if( (*sv).size() < 2UL )
314 return 0UL;
315
316 CompositeType_t<VT> a( *sv ); // Evaluation of the sparse vector operand
317
318 if( a.nonZeros() == 0UL )
319 return 0UL;
320
321 const auto end( a.end() );
322 auto element( a.begin() );
323 size_t index( element->index() );
324 auto max( element->value() );
325
326 ++element;
327
328 for( ; element!=end; ++element ) {
329 auto cur( element->value() );
330 if( max < cur ) {
331 index = element->index();
332 max = std::move( cur );
333 }
334 }
335
336 return index;
337}
338//*************************************************************************************************
339
340} // namespace blaze
341
342#endif
Header file for auxiliary alias declarations.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.
Definition: Aliases.h:110
Header file for run time assertion macros.
Header file for the function trace functionality.
Base class for sparse vectors.
Definition: SparseVector.h:72
Header file for the SparseVector base class.
Header file for the Add functor.
Header file for the Mult functor.
decltype(auto) reduce(const DenseMatrix< MT, SO > &dm, OP op)
Performs a custom reduction operation on the given dense matrix.
Definition: DMatReduceExpr.h:2025
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:584
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
size_t argmax(const SparseVector< VT, TF > &sv)
Returns the index of the first largest non-zero element of the sparse vector.
Definition: SVecReduceExpr.h:311
size_t argmin(const SparseVector< VT, TF > &sv)
Returns the index of the first smallest non-zero element of the sparse vector.
Definition: SVecReduceExpr.h:262
decltype(auto) max(const SparseVector< VT, TF > &sv)
Returns the largest element of the sparse vector.
Definition: SVecReduceExpr.h:234
decltype(auto) min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SVecReduceExpr.h:202
decltype(auto) prod(const SparseVector< VT, TF > &sv)
Reduces the given sparse vector by means of multiplication.
Definition: SVecReduceExpr.h:170
decltype(auto) sum(const SparseVector< VT, TF > &sv)
Reduces the given sparse vector by means of addition.
Definition: SVecReduceExpr.h:142
typename RemoveCV< T >::Type RemoveCV_t
Auxiliary alias declaration for the RemoveCV type trait.
Definition: RemoveCV.h:97
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
Header file for the Max functor.
Header file for the Min functor.
Generic wrapper for the addition operator.
Definition: Add.h:85
Generic wrapper for the max() function.
Definition: Max.h:82
Generic wrapper for the min() function.
Definition: Min.h:82
Generic wrapper for the multiplication operator.
Definition: Mult.h:82
Header file for basic type definitions.