Blaze 3.9
DMatVarExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_DMATVAREXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_DMATVAREXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
56#include <blaze/util/Types.h>
57
58
59namespace blaze {
60
61//=================================================================================================
62//
63// GLOBAL FUNCTIONS
64//
65//=================================================================================================
66
67//*************************************************************************************************
75template< typename MT // Type of the dense matrix
76 , bool SO > // Storage order
77inline decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, FalseType )
78{
79 using BT = UnderlyingBuiltin_t<MT>;
80
81 BLAZE_INTERNAL_ASSERT( size( *dm ) > 1UL, "Invalid matrix size detected" );
82
83 const auto m( uniform<SO>( rows( *dm ), columns( *dm ), mean( *dm ) ) );
84
85 return sum( map( (*dm) - m, Pow2() ) ) * inv( BT( size( *dm )-1UL ) );
86}
88//*************************************************************************************************
89
90
91//*************************************************************************************************
99template< typename MT // Type of the dense matrix
100 , bool SO > // Storage order
101inline decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, TrueType )
102{
103 MAYBE_UNUSED( dm );
104
105 BLAZE_INTERNAL_ASSERT( size( *dm ) > 1UL, "Invalid matrix size detected" );
106
107 return ElementType_t<MT>();
108}
110//*************************************************************************************************
111
112
113//*************************************************************************************************
136template< typename MT // Type of the dense matrix
137 , bool SO > // Storage order
138inline decltype(auto) var( const DenseMatrix<MT,SO>& dm )
139{
141
142 if( size( *dm ) < 2UL ) {
143 BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix" );
144 }
145
146 return var_backend( *dm, IsUniform<MT>() );
147}
148//*************************************************************************************************
149
150
151//*************************************************************************************************
159template< ReductionFlag RF // Reduction flag
160 , typename MT // Type of the dense matrix
161 , bool SO > // Storage order
162decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, FalseType )
163{
164 using BT = UnderlyingBuiltin_t<MT>;
165
166 const size_t n( RF == rowwise ? columns( *dm ) : rows( *dm ) );
167
168 BLAZE_INTERNAL_ASSERT( n > 1UL, "Invalid matrix size detected" );
169
170 const auto m( expand( mean<RF>( *dm ), n ) );
171
172 return sum<RF>( map( (*dm) - m, Pow2() ) ) * inv( BT( n-1UL ) );
173}
175//*************************************************************************************************
176
177
178//*************************************************************************************************
186template< ReductionFlag RF // Reduction flag
187 , typename MT // Type of the dense matrix
188 , bool SO > // Storage order
189decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, TrueType )
190{
191 const size_t n( RF == rowwise ? rows( *dm ) : columns( *dm ) );
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//*************************************************************************************************
238template< ReductionFlag RF // Reduction flag
239 , typename MT // Type of the dense matrix
240 , bool SO > // Storage order
241inline decltype(auto) var( const DenseMatrix<MT,SO>& dm )
242{
244
245 BLAZE_STATIC_ASSERT_MSG( RF < 2UL, "Invalid reduction flag" );
246
247 const size_t n( RF == rowwise ? columns( *dm ) : rows( *dm ) );
248
249 if( n < 2UL ) {
250 BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix" );
251 }
252
253 return var_backend<RF>( *dm, IsUniform<MT>() );
254}
255//*************************************************************************************************
256
257} // namespace blaze
258
259#endif
Header file for the function trace functionality.
Header file for the IntegralConstant class template.
Header file for the invert shim.
Header file for the IsUniform type trait.
Header file for the MAYBE_UNUSED function template.
Header file for the reduction flags.
constexpr ReductionFlag rowwise
Reduction flag for row-wise reduction operations.
Definition: ReductionFlag.h:77
size_t ReductionFlag
Type of the reduction flags.
Definition: ReductionFlag.h:57
Compile time assertion.
Header file for the UnderlyingBuiltin type trait.
Base class for dense matrices.
Definition: DenseMatrix.h:82
Header file for the implementation of a uniform matrix.
Header file for the implementation of a uniform vector.
Header file for the DenseMatrix base class.
Header file for the Pow2 functor.
decltype(auto) mean(const DenseMatrix< MT, SO > &dm)
Computes the (arithmetic) mean for the given dense matrix.
Definition: DMatMeanExpr.h:134
decltype(auto) map(const DenseMatrix< MT1, SO > &lhs, const DenseMatrix< MT2, SO > &rhs, OP op)
Elementwise evaluation of the given binary operation on each single element of the dense matrices lhs...
Definition: DMatDMatMapExpr.h:1144
decltype(auto) var(const DenseMatrix< MT, SO > &dm)
Computes the row-/column-wise variance function for the given dense matrix.
Definition: DMatVarExpr.h:241
decltype(auto) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:405
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2156
decltype(auto) expand(const DenseVector< VT, TF > &dv, size_t expansion)
Expansion of the given dense vector.
Definition: DVecExpandExpr.h:746
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:644
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:660
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:676
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
#define BLAZE_STATIC_ASSERT_MSG(expr, msg)
Compile time assertion macro.
Definition: StaticAssert.h:123
BoolConstant< true > TrueType
Type traits base class.
Definition: IntegralConstant.h:132
BoolConstant< false > FalseType
Type/value traits base class.
Definition: IntegralConstant.h:121
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
Header file for the exception macros of the math module.
Compile time check for uniform vectors and matrices.
Definition: IsUniform.h:107
Header file for basic type definitions.