int main(int argc, char *argv[]) { int A[25]={1,5,8,7,9,16,27,97,233,89,4,15,5,66}; printf("%d ",MaxElement(A)); return 0; }
void CalcFinalCtrsImpl( const ECtrType ctrType, const ui64 ctrLeafCountLimit, const TVector<int>& permutedTargetClass, const TVector<float>& permutedTargets, const ui64 learnSampleCount, int targetClassesCount, TVector<ui64>* hashArr, TCtrValueTable* result ) { TDenseHash<ui64, ui32> tmpHash; auto leafCount = ReindexHash( learnSampleCount, ctrLeafCountLimit, hashArr, &tmpHash).first; auto hashIndexBuilder = result->GetIndexHashBuilder(leafCount); for (const auto& kv : tmpHash) { hashIndexBuilder.SetIndex(kv.Key(), kv.Value()); } TArrayRef<int> ctrIntArray; TArrayRef<TCtrMeanHistory> ctrMean; if (ctrType == ECtrType::BinarizedTargetMeanValue || ctrType == ECtrType::FloatTargetMeanValue) { ctrMean = result->AllocateBlobAndGetArrayRef<TCtrMeanHistory>(leafCount); } else if (ctrType == ECtrType::Counter || ctrType == ECtrType::FeatureFreq) { ctrIntArray = result->AllocateBlobAndGetArrayRef<int>(leafCount); result->CounterDenominator = 0; } else { result->TargetClassesCount = targetClassesCount; ctrIntArray = result->AllocateBlobAndGetArrayRef<int>(leafCount * targetClassesCount); } Y_ASSERT(hashArr->size() == learnSampleCount); int targetBorderCount = targetClassesCount - 1; auto hashArrPtr = hashArr->data(); for (ui32 z = 0; z < learnSampleCount; ++z) { const ui64 elemId = hashArrPtr[z]; if (ctrType == ECtrType::BinarizedTargetMeanValue) { TCtrMeanHistory& elem = ctrMean[elemId]; elem.Add(static_cast<float>(permutedTargetClass[z]) / targetBorderCount); } else if (ctrType == ECtrType::Counter || ctrType == ECtrType::FeatureFreq) { ++ctrIntArray[elemId]; } else if (ctrType == ECtrType::FloatTargetMeanValue) { TCtrMeanHistory& elem = ctrMean[elemId]; elem.Add(permutedTargets[z]); } else { TArrayRef<int> elem = MakeArrayRef(ctrIntArray.data() + targetClassesCount * elemId, targetClassesCount); ++elem[permutedTargetClass[z]]; } } if (ctrType == ECtrType::Counter) { result->CounterDenominator = *MaxElement(ctrIntArray.begin(), ctrIntArray.end()); } if (ctrType == ECtrType::FeatureFreq) { result->CounterDenominator = static_cast<int>(learnSampleCount); } }
void CalcSoftmax(const TVector<double>& approx, TVector<double>* softmax) { double maxApprox = *MaxElement(approx.begin(), approx.end()); double sumExpApprox = 0; for (int dim = 0; dim < approx.ysize(); ++dim) { double expApprox = exp(approx[dim] - maxApprox); (*softmax)[dim] = expApprox; sumExpApprox += expApprox; } for (auto& curSoftmax : *softmax) { curSoftmax /= sumExpApprox; } }
void SimpleDraw::DrawGraph2D( const StdVector< StdVector <double> > & data, double maximum) { int w = data.front().size(); int h = data.size(); // Compute length of each grid quad double q = 1.0 / Max(w,h); // Find actual data max double actualMax = -DBL_MAX; for(int i = 0; i < (int)data.size(); i++) actualMax = Max(actualMax, MaxElement(data[i])); uchar rgb[3]; glDisable(GL_LIGHTING); glBegin(GL_QUADS); for(int y = 0; y < h - 1; y++) { for(int x = 0; x < w - 1; x++) { double c1 = Max(0, data[y][x]); double c2 = Max(0, data[y+1][x]); double c3 = Max(0, data[y][x+1]); double c4 = Max(0, data[y+1][x+1]); ColorMap::jetColorMap(rgb, c1, 0, actualMax); glColor3ubv(rgb); glVertex3dv(Vec3d(y * q, x * q, c1 / maximum)); ColorMap::jetColorMap(rgb, c3, 0, actualMax); glColor3ubv(rgb); glVertex3dv(Vec3d(y * q, (x+1) * q, c3 / maximum)); ColorMap::jetColorMap(rgb, c4, 0, actualMax); glColor3ubv(rgb); glVertex3dv(Vec3d((y+1) * q, (x+1) * q, c4 / maximum)); ColorMap::jetColorMap(rgb, c2, 0, actualMax); glColor3ubv(rgb); glVertex3dv(Vec3d((y+1) * q, x * q, c2 / maximum)); } } glEnd(); glEnable(GL_LIGHTING); }
//--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppRescale.htm const std::vector<std::vector<double> > Rescale( std::vector<std::vector<double> > v, const double newMin, const double newMax) { const double oldMin = MinElement(v); const double oldMax = MaxElement(v); typedef std::vector<std::vector<double> >::iterator RowIter; RowIter y = v.begin(); const RowIter maxy = v.end(); for ( ; y!=maxy; ++y) { typedef std::vector<double>::iterator ColIter; ColIter x = y->begin(); const ColIter maxx = y->end(); for ( ; x!=maxx; ++x) { *x = Rescale(*x,oldMin,oldMax,newMin,newMax); } } return v; }
void ribi::DrawCanvas::PlotSurface( std::ostream& os, const std::vector<std::vector<double> >& v, const bool use_normal_color_system, const bool as_screen_coordinat_system) { assert(v.empty() == false && "Surface must have a size"); assert(v[0].size() > 0 && "Surface must have a two-dimensional size"); //Obtain the ASCII art gradient and its size static const std::vector<char> asciiArtGradient = GetAsciiArtGradient(); static const int nAsciiArtGradientChars = asciiArtGradient.size(); //Minimum and maximum are not given, so these need to be calculated const double minVal = MinElement(v); double maxVal = MaxElement(v); if (minVal == maxVal) { maxVal = minVal == 0.0 ? 1.0 : minVal * 2.0; } //Draw the pixels const auto row_function( [](const std::vector<double>& row, std::ostream& os, const double minVal, const double maxVal, const bool use_normal_color_system) { //Iterate through each row's columns const std::vector<double>::const_iterator colEnd = row.end(); for (std::vector<double>::const_iterator col = row.begin(); col != colEnd; ++col) { //Scale the found grey value to an ASCII art character assert(maxVal != minVal); assert(maxVal - minVal != 0.0); assert(maxVal > minVal); const double greyValueDouble = ( (*col) - minVal) / (maxVal - minVal); assert(greyValueDouble >= 0.0 && greyValueDouble <= 1.0); const int greyValueInt = (use_normal_color_system ? greyValueDouble : 1.0 - greyValueDouble ) * nAsciiArtGradientChars; const int greyValue = ( greyValueInt < 0 ? 0 : (greyValueInt > nAsciiArtGradientChars - 1 ? nAsciiArtGradientChars - 1: greyValueInt) ); assert(greyValue >= 0 && greyValue < nAsciiArtGradientChars); os << asciiArtGradient[greyValue]; } os << std::endl; } ); //Iterator through all rows if (as_screen_coordinat_system) { for (const auto row: v) { row_function(row,os,minVal,maxVal,use_normal_color_system); } } else { const auto rowEnd = v.rend(); for (auto row = v.rbegin(); row != rowEnd; ++row) { row_function(*row,os,minVal,maxVal,use_normal_color_system); } } }