Blaze 3.9
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
49#include <blaze/util/Assert.h>
53#include <blaze/util/Types.h>
54
55
56namespace blaze {
57
58//=================================================================================================
59//
60// GLOBAL FUNCTIONS
61//
62//=================================================================================================
63
64//*************************************************************************************************
72template< typename VT // Type of the sparse vector
73 , bool TF > // Transpose flag
74decltype(auto) var_backend( const SparseVector<VT,TF>& sv, FalseType )
75{
76 using BT = UnderlyingBuiltin_t<VT>;
77
78 const size_t n ( size( *sv ) );
79 const size_t nz( nonZeros( *sv ) );
80
81 BLAZE_INTERNAL_ASSERT( n > 1UL, "Invalid vector size detected" );
82 BLAZE_INTERNAL_ASSERT( n >= nz, "Invalid number of non-zero elements detected" );
83
84 const auto meanValue( mean( *sv ) );
85 auto variance( ( n - nz ) * pow2( meanValue ) );
86
87 const auto end( (*sv).end() );
88 for( auto element=(*sv).begin(); element!=end; ++element ) {
89 variance += pow2( element->value() - meanValue );
90 }
91
92 return variance * inv( BT( n-1UL ) );
93}
95//*************************************************************************************************
96
97
98//*************************************************************************************************
106template< typename VT // Type of the sparse vector
107 , bool TF > // Transpose flag
108decltype(auto) var_backend( const SparseVector<VT,TF>& sv, TrueType )
109{
110 MAYBE_UNUSED( sv );
111
112 BLAZE_INTERNAL_ASSERT( size( *sv ) > 1UL, "Invalid vector size detected" );
113
114 return ElementType_t<VT>();
115}
117//*************************************************************************************************
118
119
120//*************************************************************************************************
142template< typename VT // Type of the sparse vector
143 , bool TF > // Transpose flag
144decltype(auto) var( const SparseVector<VT,TF>& sv )
145{
147
148 if( (*sv).size() < 2UL ) {
149 BLAZE_THROW_INVALID_ARGUMENT( "Invalid input vector" );
150 }
151
152 return var_backend( *sv, IsZero<VT>() );
153}
154//*************************************************************************************************
155
156} // namespace blaze
157
158#endif
Header file for run time assertion macros.
Header file for the function trace functionality.
Header file for the IntegralConstant class template.
Header file for the invert shim.
Header file for the MAYBE_UNUSED function template.
Header file for the UnderlyingBuiltin type trait.
Base class for sparse vectors.
Definition: SparseVector.h:72
Header file for the SparseVector base class.
decltype(auto) mean(const DenseMatrix< MT, SO > &dm)
Computes the (arithmetic) mean for the given dense matrix.
Definition: DMatMeanExpr.h:134
decltype(auto) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:405
decltype(auto) var(const DenseMatrix< MT, SO > &dm)
Computes the variance for the given dense matrix.
Definition: DMatVarExpr.h:138
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:730
decltype(auto) pow2(const Matrix< MT, SO > &m)
Computes the square for each single element of the matrix m.
Definition: Matrix.h:686
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
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
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.
Header file for the pow2 shim.
Compile time check for zero vectors or matrices.
Definition: IsZero.h:109
Header file for the IsZero type trait.
Header file for basic type definitions.