Salome HOME
Addition getMaxAbsValueInArray and getMaxAbsValue
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 17 Nov 2017 13:35:21 +0000 (14:35 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 17 Nov 2017 13:35:21 +0000 (14:35 +0100)
src/MEDCoupling/MEDCouplingMemArray.hxx
src/MEDCoupling/MEDCouplingMemArray.txx
src/MEDCoupling_Swig/MEDCouplingBasicsTest6.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index 313f9a7c8aaee907ae8167730615ef1ab49b5e2f..12983af923ea989012e7c86c9f87d19046cde5f1 100644 (file)
@@ -285,6 +285,8 @@ namespace MEDCoupling
     void setContigPartOfSelectedValuesSlice(int tupleIdStart, const DataArray *aBase, int bg, int end2, int step);
     T getMaxValue(int& tupleId) const;
     T getMaxValueInArray() const;
+    T getMaxAbsValue(std::size_t& tupleId) const;
+    T getMaxAbsValueInArray() const;
     T getMinValue(int& tupleId) const;
     T getMinValueInArray() const;
     MEDCOUPLING_EXPORT void getTuple(int tupleId, T *res) const { std::copy(_mem.getConstPointerLoc(tupleId*_info_on_compo.size()),_mem.getConstPointerLoc((tupleId+1)*_info_on_compo.size()),res); }
index 86dc8a297a987ea75725bbaabd2b98af5efdfbb7..9fed17742829dde665a54f714774c2a5d967b6b4 100644 (file)
@@ -2185,6 +2185,7 @@ namespace MEDCoupling
    *  \return double - the maximal value among all values of \a this array.
    *  \throw If \a this->getNumberOfComponents() != 1
    *  \throw If \a this->getNumberOfTuples() < 1
+   *  \sa getMaxAbsValue, getMinValue
    */
   template<class T>
   T DataArrayTemplate<T>::getMaxValue(int& tupleId) const
@@ -2206,6 +2207,7 @@ namespace MEDCoupling
    *  one component.
    *  \return double - the maximal value among all values of \a this array.
    *  \throw If \a this is not allocated.
+   *  \sa getMaxAbsValueInArray, getMinValueInArray
    */
   template<class T>
   T DataArrayTemplate<T>::getMaxValueInArray() const
@@ -2215,6 +2217,50 @@ namespace MEDCoupling
     return *loc;
   }
   
+  /*!
+   * Returns the maximal absolute value in \a this and the first occurrence location associated to it.
+   * \return the element in this (positive or negative) having the max abs value in \a this.
+   *  \throw If \a this is not allocated.
+   *  \throw If \a this is non one component array.
+   *  \throw If \a this is empty.
+   */
+  template<class T>
+  T DataArrayTemplate<T>::getMaxAbsValue(std::size_t& tupleId) const
+  {
+    checkAllocated();
+    if(getNumberOfComponents()!=1)
+      throw INTERP_KERNEL::Exception("DataArrayDouble::getMaxAbsValue : must be applied on DataArrayDouble with only one component, you can call 'rearrange' method before or call 'getMaxValueInArray' method !");
+    std::size_t nbTuples(this->getNumberOfTuples());
+    if(nbTuples==0)
+      throw INTERP_KERNEL::Exception("DataArrayTemplate<T>::getMaxAbsValue : empty array !");
+    T ret((T)-1);
+    tupleId=0;
+    const T *pt(begin());
+    for(std::size_t i=0;i<nbTuples;i++,pt++)
+      {
+        T cand(std::abs(*pt));
+        if(cand>ret)
+          {
+            ret=cand;
+            tupleId=i;
+          }
+      }
+    return this->getIJ(tupleId,0);
+  }
+
+  /*!
+   * Returns the maximal absolute value in \a this.
+   *  \throw If \a this is not allocated.
+   *  \throw If \a this is non one component array.
+   *  \throw If \a this is empty.
+   */
+  template<class T>
+  T DataArrayTemplate<T>::getMaxAbsValueInArray() const
+  {
+    std::size_t dummy;
+    return getMaxAbsValue(dummy);
+  }
+
   /*!
    * Returns the minimal value and its location within \a this one-dimensional array.
    *  \param [out] tupleId - index of the tuple holding the minimal value.
index eb9a3930cbe9749407e692c70cebf72d9abe3748..67a6a63bf555968c2701c2a55a59ccaa5404ef1f 100644 (file)
@@ -166,6 +166,17 @@ class MEDCouplingBasicsTest6(unittest.TestCase):
         self.assertTrue((d-center).magnitude().isUniform(radius,1e-10))
         self.assertAlmostEqual(ang,-4.712389294301196,12)
         pass
+
+    def testDAMaxAbsValue(self):
+        d=DataArrayDouble([-2,3,1.2,-2.9])
+        a,b=d.getMaxAbsValue()
+        self.assertAlmostEqual(a,3.,13)
+        self.assertEqual(b,1)
+        a,b=(-d).getMaxAbsValue()
+        self.assertAlmostEqual(a,-3.,13)
+        self.assertEqual(b,1)
+        self.assertAlmostEqual((-d).getMaxAbsValueInArray(),-3.,13)
+        pass
     
     pass
 
index d7a5df027012fafbfa051a658ca88f0eb76cae5b..c7ba467a731175611c4387831624faf9655c3892 100644 (file)
@@ -916,9 +916,8 @@ namespace MEDCoupling
     void checkNoNullValues() const throw(INTERP_KERNEL::Exception);
     DataArrayDouble *computeBBoxPerTuple(double epsilon=0.0) const throw(INTERP_KERNEL::Exception);
     void recenterForMaxPrecision(double eps) throw(INTERP_KERNEL::Exception);
-    double getMaxValue(int& tupleId) const throw(INTERP_KERNEL::Exception);
     double getMaxValueInArray() const throw(INTERP_KERNEL::Exception);
-    double getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception);
+    double getMaxAbsValueInArray() const throw(INTERP_KERNEL::Exception);
     double getMinValueInArray() const throw(INTERP_KERNEL::Exception);
     int count(double value, double eps) const throw(INTERP_KERNEL::Exception);
     double getAverageValue() const throw(INTERP_KERNEL::Exception);
@@ -1293,6 +1292,16 @@ namespace MEDCoupling
         return ret;
       }
 
+         PyObject *getMaxAbsValue() const throw(INTERP_KERNEL::Exception)
+      {
+        std::size_t tmp;
+        double r1=self->getMaxAbsValue(tmp);
+        PyObject *ret=PyTuple_New(2);
+        PyTuple_SetItem(ret,0,PyFloat_FromDouble(r1));
+        PyTuple_SetItem(ret,1,PyInt_FromLong(tmp));
+        return ret;
+      }
+
       PyObject *getMaxValue2() const throw(INTERP_KERNEL::Exception)
       {
         DataArrayInt *tmp;
@@ -2313,9 +2322,8 @@ namespace MEDCoupling
     bool presenceOfValue(const std::vector<int>& vals) const throw(INTERP_KERNEL::Exception);
     int count(int value) const throw(INTERP_KERNEL::Exception);
     int accumulate(int compId) const throw(INTERP_KERNEL::Exception);
-    int getMaxValue(int& tupleId) const throw(INTERP_KERNEL::Exception);
     int getMaxValueInArray() const throw(INTERP_KERNEL::Exception);
-    int getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception);
+    int getMaxAbsValueInArray() 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);
@@ -2962,6 +2970,16 @@ namespace MEDCoupling
         PyTuple_SetItem(ret,1,PyInt_FromLong(tmp));
         return ret;
       }
+    
+      PyObject *getMaxAbsValue(std::size_t& tupleId) const throw(INTERP_KERNEL::Exception)
+      {
+       std::size_t tmp;
+        int r1=self->getMaxAbsValue(tmp);
+        PyObject *ret=PyTuple_New(2);
+        PyTuple_SetItem(ret,0,PyInt_FromLong(r1));
+        PyTuple_SetItem(ret,1,PyInt_FromLong(tmp));
+        return ret;
+      }
 
       PyObject *getMinValue() const throw(INTERP_KERNEL::Exception)
       {