From: abn Date: Fri, 4 Sep 2015 12:14:45 +0000 (+0200) Subject: Optimisation in getWeightedAverageValue(), reducing number of multiplications. X-Git-Tag: V7_7_0b1~4^2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=0e9fe2f3b4eee389f8a112ef70bd99a219d7ec47;p=tools%2Fmedcoupling.git Optimisation in getWeightedAverageValue(), reducing number of multiplications. --- diff --git a/src/MEDCoupling/MEDCouplingFieldDouble.cxx b/src/MEDCoupling/MEDCouplingFieldDouble.cxx index 3fbd09d47..ee6574469 100644 --- a/src/MEDCoupling/MEDCouplingFieldDouble.cxx +++ b/src/MEDCoupling/MEDCouplingFieldDouble.cxx @@ -1131,14 +1131,14 @@ double MEDCouplingFieldDouble::normMax() const } /*! - * Computes sums of values of each component of \a this field wighted with + * Computes the weighted average of values of each component of \a this field, the weights being the * values returned by buildMeasureField(). * \param [out] res - pointer to an array of result sum values, of size at least \a * this->getNumberOfComponents(), that is to be allocated by the caller. - * \param [in] isWAbs - if \c true (default), \c abs() is applied to the weighs computed by - * buildMeasureField() that makes this method slower. If a user is sure that all - * cells of the underlying mesh have correct orientation, he can put \a isWAbs == - * \c false that speeds up this method. + * \param [in] isWAbs - if \c true (default), \c abs() is applied to the weights computed by + * buildMeasureField(). It makes this method slower. If you are sure that all + * the cells of the underlying mesh have a correct orientation (no negative volume), you can put \a isWAbs == + * \c false to speed up the method. * \throw If the mesh is not set. * \throw If the data array is not set. */ @@ -1150,18 +1150,19 @@ void MEDCouplingFieldDouble::getWeightedAverageValue(double *res, bool isWAbs) c double deno=w->getArray()->accumulate(0); MEDCouplingAutoRefCountObjectPtr arr=getArray()->deepCpy(); arr->multiplyEqual(w->getArray()); - std::transform(arr->begin(),arr->end(),arr->getPointer(),std::bind2nd(std::multiplies(),1./deno)); arr->accumulate(res); + int nCompo = getArray()->getNumberOfComponents(); + std::transform(res,res+nCompo,res,std::bind2nd(std::multiplies(),1./deno)); } /*! - * Computes a sum of values of a given component of \a this field wighted with + * Computes the weighted average of values of a given component of \a this field, the weights being the * values returned by buildMeasureField(). * \param [in] compId - an index of the component of interest. - * \param [in] isWAbs - if \c true (default), \c abs() is applied to the weighs computed by - * buildMeasureField() that makes this method slower. If a user is sure that all - * cells of the underlying mesh have correct orientation, he can put \a isWAbs == - * \c false that speeds up this method. + * \param [in] isWAbs - if \c true (default), \c abs() is applied to the weights computed by + * buildMeasureField(). It makes this method slower. If you are sure that all + * the cells of the underlying mesh have a correct orientation (no negative volume), you can put \a isWAbs == + * \c false to speed up the method. * \throw If the mesh is not set. * \throw If the data array is not set. * \throw If \a compId is not valid. diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 9fb203dfe..da4aca8b9 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -11784,7 +11784,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): f.setArray(arr) f.checkCoherency() self.assertTrue(DataArrayDouble(f.integral(False)).isEqual(DataArrayDouble([-211.66121638700983,-4863.9563007698835]),1e-11)) - self.assertTrue(DataArrayDouble(f.getWeightedAverageValue()).isEqual(DataArrayDouble([45.496085813113595,1045.496085813114]),1e-11)) + self.assertTrue(DataArrayDouble(f.getWeightedAverageValue()).isEqual(DataArrayDouble([45.4960858131136,1045.496085813114]),1e-11)) self.assertTrue(DataArrayDouble(f.normL1()).isEqual(DataArrayDouble([45.49608581311362,1045.496085813114]),1e-11)) self.assertTrue(DataArrayDouble(f.normL2()).isEqual(DataArrayDouble([58.16846378340898,1046.1241521947334]),1e-11)) pass