From e6f4a73d64d6b6bfcaad1dd6e17e81fce39d60ed Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 26 Nov 2013 09:23:15 +0000 Subject: [PATCH] DataArrayDouble::normMin, DataArrayDouble::computeAbs, DataArrayInt::computeAbs --- src/MEDCoupling/MEDCouplingMemArray.cxx | 101 ++++++++++++++++-- src/MEDCoupling/MEDCouplingMemArray.hxx | 3 + src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 18 ++++ src/MEDCoupling_Swig/MEDCouplingMemArray.i | 5 + 4 files changed, 116 insertions(+), 11 deletions(-) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 4775f960a..88390fc3a 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -3296,25 +3296,50 @@ double DataArrayDouble::norm2() const /*! * Returns the maximum norm of the vector defined by \a this array. + * This method works even if the number of components is diferent from one. + * If the number of elements in \a this is 0, -1. is returned. * \return double - the value of the maximum norm, i.e. - * the maximal absolute value among values of \a this array. + * the maximal absolute value among values of \a this array (whatever its number of components). * \throw If \a this is not allocated. */ double DataArrayDouble::normMax() const { checkAllocated(); - double ret=-1.; - std::size_t nbOfElems=getNbOfElems(); - const double *pt=getConstPointer(); + double ret(-1.); + std::size_t nbOfElems(getNbOfElems()); + const double *pt(getConstPointer()); for(std::size_t i=0;iret) ret=val; } return ret; } +/*! + * Returns the minimum norm (absolute value) of the vector defined by \a this array. + * This method works even if the number of components is diferent from one. + * If the number of elements in \a this is 0, std::numeric_limits::max() is returned. + * \return double - the value of the minimum norm, i.e. + * the minimal absolute value among values of \a this array (whatever its number of components). + * \throw If \a this is not allocated. + */ +double DataArrayDouble::normMin() const +{ + checkAllocated(); + double ret(std::numeric_limits::max()); + std::size_t nbOfElems(getNbOfElems()); + const double *pt(getConstPointer()); + for(std::size_t i=0;igetNumberOfComponents(), allocated @@ -4010,17 +4035,44 @@ void DataArrayDouble::sortPerTuple(bool asc) /*! * Converts every value of \a this array to its absolute value. - * \throw If \a this is not allocated. + * \b WARNING this method is non const. If a new DataArrayDouble instance should be built containing the result of abs DataArrayDouble::computeAbs + * should be called instead. + * + * \throw If \a this is not allocated. + * \sa DataArrayDouble::computeAbs */ void DataArrayDouble::abs() { checkAllocated(); - double *ptr=getPointer(); - std::size_t nbOfElems=getNbOfElems(); + double *ptr(getPointer()); + std::size_t nbOfElems(getNbOfElems()); std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun(fabs)); declareAsNew(); } +/*! + * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this. + * This method is a const method (that do not change any values in \a this) contrary to DataArrayDouble::abs method. + * + * \return DataArrayDouble * - the new instance of DataArrayDouble containing the + * same number of tuples and component as \a this array. + * The caller is to delete this result array using decrRef() as it is no more + * needed. + * \throw If \a this is not allocated. + * \sa DataArrayDouble::abs + */ +DataArrayDouble *DataArrayDouble::computeAbs() const +{ + checkAllocated(); + DataArrayDouble *newArr(DataArrayDouble::New()); + int nbOfTuples(getNumberOfTuples()); + int nbOfComp(getNumberOfComponents()); + newArr->alloc(nbOfTuples,nbOfComp); + std::transform(begin(),end(),newArr->getPointer(),std::ptr_fun(fabs)); + newArr->copyStringInfoFrom(*this); + return newArr; +} + /*! * Apply a liner function to a given component of \a this array, so that * an array element (x) becomes \f$ a * x + b \f$. @@ -8902,17 +8954,44 @@ int DataArrayInt::getMinValueInArray() const /*! * Converts every value of \a this array to its absolute value. - * \throw If \a this is not allocated. + * \b WARNING this method is non const. If a new DataArrayInt instance should be built containing the result of abs DataArrayInt::computeAbs + * should be called instead. + * + * \throw If \a this is not allocated. + * \sa DataArrayInt::computeAbs */ void DataArrayInt::abs() { checkAllocated(); - int *ptr=getPointer(); - std::size_t nbOfElems=getNbOfElems(); + int *ptr(getPointer()); + std::size_t nbOfElems(getNbOfElems()); std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun(std::abs)); declareAsNew(); } +/*! + * This method builds a new instance of \a this object containing the result of std::abs applied of all elements in \a this. + * This method is a const method (that do not change any values in \a this) contrary to DataArrayInt::abs method. + * + * \return DataArrayInt * - the new instance of DataArrayInt containing the + * same number of tuples and component as \a this array. + * The caller is to delete this result array using decrRef() as it is no more + * needed. + * \throw If \a this is not allocated. + * \sa DataArrayInt::abs + */ +DataArrayInt *DataArrayInt::computeAbs() const +{ + checkAllocated(); + DataArrayInt *newArr(DataArrayInt::New()); + int nbOfTuples(getNumberOfTuples()); + int nbOfComp(getNumberOfComponents()); + newArr->alloc(nbOfTuples,nbOfComp); + std::transform(begin(),end(),newArr->getPointer(),std::ptr_fun(std::abs)); + newArr->copyStringInfoFrom(*this); + return newArr; +} + /*! * Apply a liner function to a given component of \a this array, so that * an array element (x) becomes \f$ a * x + b \f$. diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index af2e5e901..af0e9b255 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -309,6 +309,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT double getAverageValue() const; MEDCOUPLING_EXPORT double norm2() const; MEDCOUPLING_EXPORT double normMax() const; + MEDCOUPLING_EXPORT double normMin() const; MEDCOUPLING_EXPORT void accumulate(double *res) const; MEDCOUPLING_EXPORT double accumulate(int compId) const; MEDCOUPLING_EXPORT DataArrayDouble *accumulatePerChunck(const int *bgOfIndex, const int *endOfIndex) const; @@ -331,6 +332,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayDouble *buildEuclidianDistanceDenseMatrixWith(const DataArrayDouble *other) const; MEDCOUPLING_EXPORT void sortPerTuple(bool asc); MEDCOUPLING_EXPORT void abs(); + MEDCOUPLING_EXPORT DataArrayDouble *computeAbs() const; MEDCOUPLING_EXPORT void applyLin(double a, double b, int compoId); MEDCOUPLING_EXPORT void applyLin(double a, double b); MEDCOUPLING_EXPORT void applyInv(double numerator); @@ -549,6 +551,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT int getMinValue(int& tupleId) const; MEDCOUPLING_EXPORT int getMinValueInArray() const; MEDCOUPLING_EXPORT void abs(); + MEDCOUPLING_EXPORT DataArrayInt *computeAbs() const; MEDCOUPLING_EXPORT void applyLin(int a, int b, int compoId); MEDCOUPLING_EXPORT void applyLin(int a, int b); MEDCOUPLING_EXPORT void applyInv(int numerator); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index d729cb00c..18ae38854 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -13978,6 +13978,24 @@ class MEDCouplingBasicsTest(unittest.TestCase): pass pass + def testSwig2normMinComputeAbs1(self): + d=DataArrayDouble([4,-5,2,6.1,-7.33,1,-1,3e2,0.07,-0.009,-6,-1e30],4,3) + d.setInfoOnComponents(["XX [m]","YYY [km]","ABSJJ [MW]"]) + d0=d.computeAbs() + dExp=d.deepCpy() ; dExp.abs() + self.assertTrue(dExp.isEqual(d0,1e-12)) + e=d0-DataArrayDouble([4,5,2,6.1,7.33,1,1,3e2,0.07,0.009,6,1e30],4,3) + self.assertAlmostEqual(0.,e.normMin(),13) + self.assertAlmostEqual(0.009,d.normMin(),13) + # + di=DataArrayInt([3,-12,5,6,14,16,-23,100,23,-1,0,-6],4,3) + di.setInfoOnComponents(["XX [m]","YYY [km]","ABSJJ [MW]"]) + d0i=di.computeAbs() + diExp=di.deepCpy() ; diExp.abs() + self.assertTrue(diExp.isEqual(d0i)) + self.assertEqual([3,12,5,6,14,16,23,100,23,1,0,6],d0i.getValues()) + pass + def setUp(self): pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 9039ac147..248cd2028 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -69,6 +69,7 @@ %newobject ParaMEDMEM::DataArrayInt::getIdsNotEqualList; %newobject ParaMEDMEM::DataArrayInt::sumPerTuple; %newobject ParaMEDMEM::DataArrayInt::negate; +%newobject ParaMEDMEM::DataArrayInt::computeAbs; %newobject ParaMEDMEM::DataArrayInt::getIdsInRange; %newobject ParaMEDMEM::DataArrayInt::getIdsNotInRange; %newobject ParaMEDMEM::DataArrayInt::Aggregate; @@ -151,6 +152,7 @@ %newobject ParaMEDMEM::DataArrayDouble::getIdsInRange; %newobject ParaMEDMEM::DataArrayDouble::getIdsNotInRange; %newobject ParaMEDMEM::DataArrayDouble::negate; +%newobject ParaMEDMEM::DataArrayDouble::computeAbs; %newobject ParaMEDMEM::DataArrayDouble::applyFunc; %newobject ParaMEDMEM::DataArrayDouble::applyFunc2; %newobject ParaMEDMEM::DataArrayDouble::applyFunc3; @@ -544,6 +546,7 @@ namespace ParaMEDMEM double getAverageValue() const throw(INTERP_KERNEL::Exception); double norm2() const throw(INTERP_KERNEL::Exception); double normMax() const throw(INTERP_KERNEL::Exception); + double normMin() const throw(INTERP_KERNEL::Exception); double accumulate(int compId) const throw(INTERP_KERNEL::Exception); DataArrayDouble *fromPolarToCart() const throw(INTERP_KERNEL::Exception); DataArrayDouble *fromCylToCart() const throw(INTERP_KERNEL::Exception); @@ -562,6 +565,7 @@ namespace ParaMEDMEM DataArrayDouble *buildEuclidianDistanceDenseMatrixWith(const DataArrayDouble *other) const throw(INTERP_KERNEL::Exception); void sortPerTuple(bool asc) throw(INTERP_KERNEL::Exception); void abs() throw(INTERP_KERNEL::Exception); + DataArrayDouble *computeAbs() const throw(INTERP_KERNEL::Exception); void applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception); void applyLin(double a, double b) throw(INTERP_KERNEL::Exception); void applyInv(double numerator) throw(INTERP_KERNEL::Exception); @@ -2553,6 +2557,7 @@ namespace ParaMEDMEM int getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception); int getMinValueInArray() const throw(INTERP_KERNEL::Exception); void abs() throw(INTERP_KERNEL::Exception); + DataArrayInt *computeAbs() const throw(INTERP_KERNEL::Exception); void applyLin(int a, int b, int compoId) throw(INTERP_KERNEL::Exception); void applyLin(int a, int b) throw(INTERP_KERNEL::Exception); void applyInv(int numerator) throw(INTERP_KERNEL::Exception); -- 2.39.2