Blaze  3.6
SMatMeanExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATMEANEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SMATMEANEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Exception.h>
50 #include <blaze/util/Assert.h>
53 #include <blaze/util/MaybeUnused.h>
55 #include <blaze/util/Types.h>
56 
57 
58 namespace blaze {
59 
60 //=================================================================================================
61 //
62 // GLOBAL FUNCTIONS
63 //
64 //=================================================================================================
65 
66 //*************************************************************************************************
74 template< typename MT // Type of the sparse matrix
75  , bool SO > // Storage order
76 inline decltype(auto) mean_backend( const SparseMatrix<MT,SO>& sm, FalseType )
77 {
78  using BT = UnderlyingBuiltin_t<MT>;
79 
80  BLAZE_INTERNAL_ASSERT( size( ~sm ) > 0UL, "Invalid matrix size detected" );
81 
82  return sum( ~sm ) * inv( BT( size( ~sm ) ) );
83 }
85 //*************************************************************************************************
86 
87 
88 //*************************************************************************************************
96 template< typename MT // Type of the sparse matrix
97  , bool SO > // Storage order
98 inline decltype(auto) mean_backend( const SparseMatrix<MT,SO>& sm, TrueType )
99 {
100  MAYBE_UNUSED( sm );
101 
102  BLAZE_INTERNAL_ASSERT( size( ~sm ) > 0UL, "Invalid matrix size detected" );
103 
104  return ElementType_t<MT>();
105 }
107 //*************************************************************************************************
108 
109 
110 //*************************************************************************************************
136 template< typename MT // Type of the sparse matrix
137  , bool SO > // Storage order
138 inline decltype(auto) mean( const SparseMatrix<MT,SO>& sm )
139 {
141 
142  if( size( ~sm ) == 0UL ) {
143  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix" );
144  }
145 
146  return mean_backend( ~sm, IsZero<MT>() );
147 }
148 //*************************************************************************************************
149 
150 
151 //*************************************************************************************************
160 template< size_t RF // Reduction flag
161  , typename MT // Type of the sparse matrix
162  , bool SO > // Storage order
163 decltype(auto) mean_backend( const SparseMatrix<MT,SO>& sm, FalseType )
164 {
165  using BT = UnderlyingBuiltin_t<MT>;
166 
167  const size_t n( RF == rowwise ? columns( ~sm ) : rows( ~sm ) );
168 
169  BLAZE_INTERNAL_ASSERT( n > 0UL, "Invalid matrix size detected" );
170 
171  return sum<RF>( ~sm ) * inv( BT( n ) );
172 }
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
186 template< size_t RF // Reduction flag
187  , typename MT // Type of the sparse matrix
188  , bool SO > // Storage order
189 decltype(auto) mean_backend( const SparseMatrix<MT,SO>& sm, TrueType )
190 {
191  const size_t n( RF == rowwise ? rows( ~sm ) : columns( ~sm ) );
192 
193  BLAZE_INTERNAL_ASSERT( n > 0UL, "Invalid matrix size detected" );
194 
195  constexpr bool TF( ( RF == rowwise ? columnVector : rowVector ) );
196 
197  return uniform<TF>( n, ElementType_t<MT>() );
198 }
200 //*************************************************************************************************
201 
202 
203 //*************************************************************************************************
239 template< size_t RF // Reduction flag
240  , typename MT // Type of the sparse matrix
241  , bool SO > // Storage order
242 decltype(auto) mean( const SparseMatrix<MT,SO>& sm )
243 {
245 
246  BLAZE_STATIC_ASSERT_MSG( RF < 2UL, "Invalid reduction flag" );
247 
248  const size_t n( RF == rowwise ? columns( ~sm ) : rows( ~sm ) );
249 
250  if( n == 0UL ) {
251  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix" );
252  }
253 
254  return mean_backend<RF>( ~sm, IsZero<MT>() );
255 }
256 //*************************************************************************************************
257 
258 } // namespace blaze
259 
260 #endif
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: IntegralConstant.h:121
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for basic type definitions.
Header file for the implementation of a uniform vector.
Header file for the MAYBE_UNUSED function template.
Header file for the invert shim.
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: IntegralConstant.h:132
#define BLAZE_STATIC_ASSERT_MSG(expr, msg)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:123
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes....
Definition: Forward.h:145
constexpr bool rowVector
Transpose flag for row vectors.
Definition: TransposeFlag.h:73
Compile time check for zero vectors or matrices.This type trait tests whether or not the given templa...
Definition: IsZero.h:107
Header file for the SparseMatrix base class.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Compile time assertion.
Header file for the UnderlyingBuiltin type trait.
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2147
decltype(auto) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:423
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
constexpr bool columnVector
Transpose flag for column vectors.
Definition: TransposeFlag.h:58
Header file for the exception macros of the math module.
decltype(auto) mean(const DenseMatrix< MT, SO > &dm)
Computes the (arithmetic) mean for the given dense matrix.
Definition: DMatMeanExpr.h:134
Header file for run time assertion macros.
Header file for the IsZero type trait.
#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
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
constexpr size_t rowwise
Reduction flag for row-wise reduction operations.
Definition: ReductionFlag.h:70
Header file for the IntegralConstant class template.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
Header file for the reduction flags.
Header file for the function trace functionality.