From 458c594416db0b4d7cd640fbb0c3f44e684afefa Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 3 Apr 2012 12:28:15 +0000 Subject: [PATCH] Addition of abs method in DataArrays. --- src/MEDCoupling/MEDCouplingMemArray.cxx | 16 ++++++++++++++ src/MEDCoupling/MEDCouplingMemArray.hxx | 2 ++ .../Test/MEDCouplingBasicsTest5.cxx | 22 +++++++++++++++++++ .../Test/MEDCouplingBasicsTest5.hxx | 2 ++ 4 files changed, 42 insertions(+) diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 0e2f6c82a..21a682366 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -1834,6 +1834,14 @@ void DataArrayDouble::sortPerTuple(bool asc) throw(INTERP_KERNEL::Exception) declareAsNew(); } +void DataArrayDouble::abs() throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + double *ptr=getPointer(); + int nbOfElems=getNbOfElems(); + std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun(fabs)); +} + void DataArrayDouble::applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception) { checkAllocated(); @@ -4133,6 +4141,14 @@ int DataArrayInt::getMinValueInArray() const throw(INTERP_KERNEL::Exception) return *loc; } +void DataArrayInt::abs() throw(INTERP_KERNEL::Exception) +{ + checkAllocated(); + int *ptr=getPointer(); + int nbOfElems=getNbOfElems(); + std::transform(ptr,ptr+nbOfElems,ptr,std::ptr_fun(std::abs)); +} + void DataArrayInt::applyLin(int a, int b, int compoId) throw(INTERP_KERNEL::Exception) { checkAllocated(); diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index aadcca145..b830a99f3 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -231,6 +231,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT DataArrayDouble *magnitude() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayDouble *maxPerTuple() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void sortPerTuple(bool asc) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT void abs() throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyLin(double a, double b) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyInv(double numerator) throw(INTERP_KERNEL::Exception); @@ -399,6 +400,7 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT int getMaxValueInArray() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT int getMinValue(int& tupleId) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT int getMinValueInArray() const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT void abs() throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyLin(int a, int b, int compoId) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyLin(int a, int b) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void applyInv(int numerator) throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx index 79b351c66..861554d79 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx @@ -568,3 +568,25 @@ void MEDCouplingBasicsTest5::testAre2DCellsNotCorrectlyOriented1() // m1->decrRef(); } + +void MEDCouplingBasicsTest5::testDataArrayAbs1() +{ + DataArrayDouble *d1=DataArrayDouble::New(); + const double val1[12]={2.,-3.,-5.,6.,-7.,-8.,9.,10.,-11.,-12.}; + const double expected1[12]={2.,3.,5.,6.,7.,8.,9.,10.,11.,12.}; + d1->alloc(6,2); + std::copy(val1,val1+12,d1->getPointer()); + DataArrayInt *d2=d1->convertToIntArr(); + // + d1->abs(); + for(int i=0;i<12;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected1[i],d1->getIJ(0,i),1e-14); + // + const int expected2[12]={2,3,5,6,7,8,9,10,11,12}; + d2->abs(); + for(int i=0;i<12;i++) + CPPUNIT_ASSERT_EQUAL(expected2[i],d2->getIJ(0,i)); + // + d2->decrRef(); + d1->decrRef(); +} diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx index 1252226e3..8be6bfed6 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx @@ -44,6 +44,7 @@ namespace ParaMEDMEM CPPUNIT_TEST( testDataArrayIntAdvSetting1 ); CPPUNIT_TEST( testBuildDescendingConnec2Of3DMesh1 ); CPPUNIT_TEST( testAre2DCellsNotCorrectlyOriented1 ); + CPPUNIT_TEST( testDataArrayAbs1 ); CPPUNIT_TEST_SUITE_END(); public: void testUMeshTessellate2D1(); @@ -55,6 +56,7 @@ namespace ParaMEDMEM void testDataArrayIntAdvSetting1(); void testBuildDescendingConnec2Of3DMesh1(); void testAre2DCellsNotCorrectlyOriented1(); + void testDataArrayAbs1(); }; } -- 2.39.2