Blaze  3.6
SVecVarExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECVAREXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECVAREXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Exception.h>
46 #include <blaze/math/shims/Pow2.h>
48 #include <blaze/util/Assert.h>
51 #include <blaze/util/MaybeUnused.h>
52 #include <blaze/util/Types.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // GLOBAL FUNCTIONS
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
71 template< typename VT // Type of the sparse vector
72  , bool TF > // Transpose flag
73 decltype(auto) var_backend( const SparseVector<VT,TF>& sv, FalseType )
74 {
75  using BT = UnderlyingBuiltin_t<VT>;
76 
77  const size_t n ( size( ~sv ) );
78  const size_t nz( nonZeros( ~sv ) );
79 
80  BLAZE_INTERNAL_ASSERT( n > 1UL, "Invalid vector size detected" );
81  BLAZE_INTERNAL_ASSERT( n >= nz, "Invalid number of non-zero elements detected" );
82 
83  const auto meanValue( mean( ~sv ) );
84  auto variance( ( n - nz ) * pow2( meanValue ) );
85 
86  const auto end( (~sv).end() );
87  for( auto element=(~sv).begin(); element!=end; ++element ) {
88  variance += pow2( element->value() - meanValue );
89  }
90 
91  return variance * inv( BT( n-1UL ) );
92 }
94 //*************************************************************************************************
95 
96 
97 //*************************************************************************************************
105 template< typename VT // Type of the sparse vector
106  , bool TF > // Transpose flag
107 decltype(auto) var_backend( const SparseVector<VT,TF>& sv, TrueType )
108 {
109  MAYBE_UNUSED( sv );
110 
111  BLAZE_INTERNAL_ASSERT( size( ~sv ) > 1UL, "Invalid vector size detected" );
112 
113  return ElementType_t<VT>();
114 }
116 //*************************************************************************************************
117 
118 
119 //*************************************************************************************************
141 template< typename VT // Type of the sparse vector
142  , bool TF > // Transpose flag
143 decltype(auto) var( const SparseVector<VT,TF>& sv )
144 {
146 
147  if( (~sv).size() < 2UL ) {
148  BLAZE_THROW_INVALID_ARGUMENT( "Invalid input vector" );
149  }
150 
151  return var_backend( ~sv, IsZero<VT>() );
152 }
153 //*************************************************************************************************
154 
155 } // namespace blaze
156 
157 #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 SparseVector base class.
MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:372
Header file for the MAYBE_UNUSED function template.
Header file for the invert shim.
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
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
Compile time check for zero vectors or matrices.This type trait tests whether or not the given templa...
Definition: IsZero.h:107
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the UnderlyingBuiltin type trait.
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
Header file for the pow2 shim.
Header file for the exception macros of the math module.
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
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.
decltype(auto) var(const DenseMatrix< MT, SO > &dm)
Computes the variance for the given dense matrix.
Definition: DMatVarExpr.h:137
decltype(auto) pow2(const Proxy< PT, RT > &proxy)
Computing the square value of the represented element.
Definition: Proxy.h:1384
#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
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
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 function trace functionality.