Blaze  3.6
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 
43 #include <blaze/math/Exception.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 dense matrix
75  , bool SO > // Storage order
76 inline decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, FalseType )
77 {
78  using BT = UnderlyingBuiltin_t<MT>;
79 
80  BLAZE_INTERNAL_ASSERT( size( ~dm ) > 1UL, "Invalid matrix size detected" );
81 
82  const auto m( uniform<SO>( rows( ~dm ), columns( ~dm ), mean( ~dm ) ) );
83 
84  return sum( map( (~dm) - m, Pow2() ) ) * inv( BT( size( ~dm )-1UL ) );
85 }
87 //*************************************************************************************************
88 
89 
90 //*************************************************************************************************
98 template< typename MT // Type of the dense matrix
99  , bool SO > // Storage order
100 inline decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, TrueType )
101 {
102  MAYBE_UNUSED( dm );
103 
104  BLAZE_INTERNAL_ASSERT( size( ~dm ) > 1UL, "Invalid matrix size detected" );
105 
106  return ElementType_t<MT>();
107 }
109 //*************************************************************************************************
110 
111 
112 //*************************************************************************************************
135 template< typename MT // Type of the dense matrix
136  , bool SO > // Storage order
137 inline decltype(auto) var( const DenseMatrix<MT,SO>& dm )
138 {
140 
141  if( size( ~dm ) < 2UL ) {
142  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix" );
143  }
144 
145  return var_backend( ~dm, IsUniform<MT>() );
146 }
147 //*************************************************************************************************
148 
149 
150 //*************************************************************************************************
158 template< size_t RF // Reduction flag
159  , typename MT // Type of the dense matrix
160  , bool SO > // Storage order
161 decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, FalseType )
162 {
163  using BT = UnderlyingBuiltin_t<MT>;
164 
165  const size_t n( RF == rowwise ? columns( ~dm ) : rows( ~dm ) );
166 
167  BLAZE_INTERNAL_ASSERT( n > 1UL, "Invalid matrix size detected" );
168 
169  const auto m( expand( mean<RF>( ~dm ), n ) );
170 
171  return sum<RF>( map( (~dm) - m, Pow2() ) ) * inv( BT( n-1UL ) );
172 }
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
185 template< size_t RF // Reduction flag
186  , typename MT // Type of the dense matrix
187  , bool SO > // Storage order
188 decltype(auto) var_backend( const DenseMatrix<MT,SO>& dm, TrueType )
189 {
190  const size_t n( RF == rowwise ? rows( ~dm ) : columns( ~dm ) );
191 
192  BLAZE_INTERNAL_ASSERT( n > 0UL, "Invalid matrix size detected" );
193 
194  constexpr bool TF( ( RF == rowwise ? columnVector : rowVector ) );
195 
196  return uniform<TF>( n, ElementType_t<MT>() );
197 }
199 //*************************************************************************************************
200 
201 
202 //*************************************************************************************************
237 template< size_t RF // Reduction flag
238  , typename MT // Type of the dense matrix
239  , bool SO > // Storage order
240 inline decltype(auto) var( const DenseMatrix<MT,SO>& dm )
241 {
243 
244  BLAZE_STATIC_ASSERT_MSG( RF < 2UL, "Invalid reduction flag" );
245 
246  const size_t n( RF == rowwise ? columns( ~dm ) : rows( ~dm ) );
247 
248  if( n < 2UL ) {
249  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input matrix" );
250  }
251 
252  return var_backend<RF>( ~dm, IsUniform<MT>() );
253 }
254 //*************************************************************************************************
255 
256 } // namespace blaze
257 
258 #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 Pow2 functor.
Compile time check for uniform vectors and matrices.This type trait tests whether or not the given te...
Definition: IsUniform.h:105
Header file for the MAYBE_UNUSED function template.
Header file for the invert shim.
Header file for the implementation of a uniform matrix.
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 dense matrices.The DenseMatrix class is a base class for all dense matrix classes....
Definition: DenseMatrix.h:81
constexpr bool rowVector
Transpose flag for row vectors.
Definition: TransposeFlag.h:73
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
Header file for the DenseMatrix base class.
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
decltype(auto) var(const DenseMatrix< MT, SO > &dm)
Computes the variance for the given dense matrix.
Definition: DMatVarExpr.h:137
decltype(auto) expand(const DenseVector< VT, TF > &dv, size_t expansion)
Expansion of the given dense vector.
Definition: DVecExpandExpr.h:739
#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.
decltype(auto) map(const DenseMatrix< MT1, SO > &lhs, const DenseMatrix< MT2, SO > &rhs, OP op)
Evaluates the given binary operation on each single element of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1121