本文整理汇总了C++中aol::Vector::sum方法的典型用法代码示例。如果您正苦于以下问题:C++ Vector::sum方法的具体用法?C++ Vector::sum怎么用?C++ Vector::sum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aol::Vector
的用法示例。
在下文中一共展示了Vector::sum方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Exception
typename aol::Vector<_DataType>::RealType aol::Vector<_DataType>:: getWeightedMedianValue( const aol::Vector<RealType> &Weights ) const {
const int numVals = this->size();
if ( numVals != Weights.size() )
throw Exception ( "aol::Vector<DataType>::getWeightedMedianValue: numVals != Weights.size() !\n", __FILE__, __LINE__ );
if ( Weights.getMinValue() <= 0 )
throw Exception ( "aol::Vector<DataType>::getWeightedMedianValue: Nonpositive weights are not supported!\n", __FILE__, __LINE__ );
std::vector<std::pair<_DataType, RealType> > valuesAndWeights;
valuesAndWeights.reserve ( numVals );
for ( int i = 0; i < numVals; ++i )
valuesAndWeights.push_back ( std::pair<_DataType, RealType> ( this->get ( i ), Weights[i] ) );
std::sort( valuesAndWeights.begin(), valuesAndWeights.end() );
const RealType halfOfTotalWeight = 0.5 * Weights.sum();
RealType weight = 0;
for ( int i = 0; i < numVals; ++i ) {
weight += valuesAndWeights[i].second;
if ( ( weight > halfOfTotalWeight ) || ( i == ( numVals - 1 ) ) )
return valuesAndWeights[i].first;
else if ( aol::appeqAbsolute ( weight, halfOfTotalWeight ) ) {
return ( valuesAndWeights[i].first * valuesAndWeights[i].second + valuesAndWeights[i+1].first * valuesAndWeights[i+1].second ) / ( valuesAndWeights[i].second + valuesAndWeights[i+1].second );
}
}
return valuesAndWeights[numVals-1].first;
}
示例2: get
typename aol::Vector<_DataType>::RealType aol::Vector<_DataType>::getWeightedMeanValue ( const aol::Vector<RealType> &Weights ) const {
const int numVals = this->size();
RealType mean = 0;
for ( int i = 0; i < numVals; ++i )
mean += Weights[i] * get(i);
mean /= Weights.sum();
return mean;
}