Salome HOME
DataArrayDouble::normMin, DataArrayDouble::computeAbs, DataArrayInt::computeAbs
authorageay <ageay>
Tue, 26 Nov 2013 09:23:15 +0000 (09:23 +0000)
committerageay <ageay>
Tue, 26 Nov 2013 09:23:15 +0000 (09:23 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index 4775f960a15ec3ef8cb9eb642ec79e3d5e41bb76..88390fc3aff06eaee63d1a270f2c807e6b1c593e 100644 (file)
@@ -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;i<nbOfElems;i++,pt++)
     {
-      double val=std::abs(*pt);
+      double val(std::abs(*pt));
       if(val>ret)
         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<double>::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<double>::max());
+  std::size_t nbOfElems(getNbOfElems());
+  const double *pt(getConstPointer());
+  for(std::size_t i=0;i<nbOfElems;i++,pt++)
+    {
+      double val(std::abs(*pt));
+      if(val<ret)
+        ret=val;
+    }
+  return ret;
+}
+
 /*!
  * Accumulates values of each component of \a this array.
  *  \param [out] res - an array of length \a this->getNumberOfComponents(), 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<double,double>(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<double,double>(fabs));
+  newArr->copyStringInfoFrom(*this);
+  return newArr;
+}
+
 /*!
  * Apply a liner function to a given component of \a this array, so that
  * an array element <em>(x)</em> 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<int,int>(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<int,int>(std::abs));
+  newArr->copyStringInfoFrom(*this);
+  return newArr;
+}
+
 /*!
  * Apply a liner function to a given component of \a this array, so that
  * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
index af2e5e901c3f8353379b55b3951043738be33007..af0e9b255144a1456b1ecc90cd67e7d8d4d4a6d9 100644 (file)
@@ -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);
index d729cb00c8a9687cf963836cab1b8cc95e76d36d..18ae38854fb1a770630612b4de86c8eb98f71ab2 100644 (file)
@@ -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
index 9039ac147f287e58128500d0558857229a77897d..248cd2028a8f30c6144be5c9af2d4e04634d1e23 100644 (file)
@@ -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;
 %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);