From: Anthony Geay Date: Thu, 20 Apr 2017 07:58:26 +0000 (+0200) Subject: 2 small useful methods DataArrayInt::fromLinkedListOfPairToList and DataArrayDouble... X-Git-Tag: V8_4_0a1~82 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fagy%2FGlobalNumNodeIds;p=tools%2Fmedcoupling.git 2 small useful methods DataArrayInt::fromLinkedListOfPairToList and DataArrayDouble::cumSum --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 6609e9142..4218da297 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -2055,6 +2055,27 @@ DataArrayDouble *DataArrayDouble::accumulatePerChunck(const int *bgOfIndex, cons return ret.retn(); } +/*! + * This method is close to numpy cumSum except that number of element is equal to \a this->getNumberOfTuples()+1. First element of DataArray returned is equal to 0. + * This method expects that \a this as only one component. The returned array will have \a this->getNumberOfTuples()+1 tuple with also one component. + * The ith element of returned array is equal to the sum of elements in \a this with rank strictly lower than i. + * + * \return DataArrayDouble - A newly built array containing cum sum of \a this. + */ +MCAuto DataArrayDouble::cumSum() const +{ + checkAllocated(); + checkNbOfComps(1,"DataArrayDouble::cumSum : this is expected to be single component"); + int nbOfTuple(getNumberOfTuples()); + MCAuto ret(DataArrayDouble::New()); ret->alloc(nbOfTuple+1,1); + double *ptr(ret->getPointer()); + ptr[0]=0.; + const double *thisPtr(begin()); + for(int i=0;i DataArrayInt::fromLinkedListOfPairToList() const +{ + checkAllocated(); + checkNbOfComps(2,"DataArrayInt::fromLinkedListOfPairToList : this is expected to have 2 components"); + int nbTuples(getNumberOfTuples()); + if(nbTuples<1) + throw INTERP_KERNEL::Exception("DataArrayInt::fromLinkedListOfPairToList : no tuples in this ! Not a linked list !"); + MCAuto ret(DataArrayInt::New()); ret->alloc(nbTuples+1,1); + const int *thisPtr(begin()); + int *retPtr(ret->getPointer()); + retPtr[0]=thisPtr[0]; + for(int i=0;i cumSum() const; double distanceToTuple(const double *tupleBg, const double *tupleEnd, int& tupleId) const; DataArrayDouble *fromPolarToCart() const; DataArrayDouble *fromCylToCart() const; @@ -613,6 +614,7 @@ namespace MEDCoupling DataArrayInt *findRangeIdForEachTuple(const DataArrayInt *ranges) const; DataArrayInt *findIdInRangeForEachTuple(const DataArrayInt *ranges) const; void sortEachPairToMakeALinkedList(); + MCAuto fromLinkedListOfPairToList() const; DataArrayInt *duplicateEachTupleNTimes(int nbTimes) const; DataArrayInt *getDifferentValues() const; std::vector partitionByDifferentValues(std::vector& differentIds) const; diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py index dd9a2e8b9..474c6ab7c 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest5.py @@ -23,7 +23,7 @@ import unittest from math import pi,e,sqrt,cos,sin from datetime import datetime from MEDCouplingDataForTest import MEDCouplingDataForTest -import rlcompleter,readline # this line has to be here, to ensure a usability of MEDCoupling/MEDLoader. B4 removing it please notify to anthony.geay@cea.fr +import rlcompleter,readline # this line has to be here, to ensure a usability of MEDCoupling/MEDLoader. B4 removing it please notify to anthony.geay@edf.fr class MEDCouplingBasicsTest5(unittest.TestCase): def testSwig2FieldDoubleBuildSubPartRange1(self): @@ -4491,6 +4491,33 @@ class MEDCouplingBasicsTest5(unittest.TestCase): self.assertEqual(f2.getName(),"field") self.assertEqual(f2.getTime(),[1.,2,3]) pass + + def testDADCumSum1(self): + d=DataArrayDouble([3.,2.,4.,5.]) + self.assertTrue(d.cumSum().isEqual(DataArrayDouble([0.,3.,5.,9.,14.]),1e-12)) + d2=DataArrayDouble([]) + self.assertTrue(d2.cumSum().isEqual(DataArrayDouble([0.]),1e-12)) + d.rearrange(2) + self.assertRaises(InterpKernelException,d.cumSum) + pass + + def testDAIFromLinkedListOfPairToList1(self): + d=DataArrayInt([(5,7),(7,3),(3,12),(12,17)]) + zeRes=DataArrayInt([5,7,3,12,17]) + self.assertTrue(d.fromLinkedListOfPairToList().isEqual(zeRes)) + d.rearrange(1) + self.assertRaises(InterpKernelException,d.fromLinkedListOfPairToList) + d.rearrange(2) + self.assertTrue(d.fromLinkedListOfPairToList().isEqual(zeRes)) + d2=DataArrayInt([(5,7)]) + self.assertTrue(d2.fromLinkedListOfPairToList().isEqual(DataArrayInt([5,7]))) + d3=DataArrayInt([(5,7),(7,3),(4,12),(12,17)]) + self.assertRaises(InterpKernelException,d3.fromLinkedListOfPairToList) # not a linked list of pair + d4=DataArrayInt([(5,7),(7,3),(12,3),(12,17)]) + self.assertRaises(InterpKernelException,d4.fromLinkedListOfPairToList) # not a linked list of pair, but can be repaired ! + d4.sortEachPairToMakeALinkedList() + self.assertTrue(d4.fromLinkedListOfPairToList().isEqual(zeRes)) + pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 397712f5d..e8d0d9083 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -97,6 +97,7 @@ %newobject MEDCoupling::DataArrayInt::buildIntersection; %newobject MEDCoupling::DataArrayInt::buildUnique; %newobject MEDCoupling::DataArrayInt::buildUniqueNotSorted; +%newobject MEDCoupling::DataArrayInt::fromLinkedListOfPairToList; %newobject MEDCoupling::DataArrayInt::deltaShiftIndex; %newobject MEDCoupling::DataArrayInt::buildExplicitArrByRanges; %newobject MEDCoupling::DataArrayInt::buildExplicitArrOfSliceOnScaledArr; @@ -158,6 +159,7 @@ %newobject MEDCoupling::DataArrayDouble::subArray; %newobject MEDCoupling::DataArrayDouble::changeNbOfComponents; %newobject MEDCoupling::DataArrayDouble::accumulatePerChunck; +%newobject MEDCoupling::DataArrayDouble::cumSum; %newobject MEDCoupling::DataArrayDouble::findIdsInRange; %newobject MEDCoupling::DataArrayDouble::findIdsNotInRange; %newobject MEDCoupling::DataArrayDouble::negate; @@ -883,6 +885,12 @@ namespace MEDCoupling return ret.retn(); } + DataArrayDouble *cumSum() const throw(INTERP_KERNEL::Exception) + { + MCAuto ret(self->cumSum()); + return ret.retn(); + } + void pushBackValsSilent(PyObject *li) throw(INTERP_KERNEL::Exception) { double val; @@ -3007,6 +3015,12 @@ namespace MEDCoupling return self->iterator(); } + DataArrayInt *fromLinkedListOfPairToList() const throw(INTERP_KERNEL::Exception) + { + MCAuto ret(self->fromLinkedListOfPairToList()); + return ret.retn(); + } + DataArrayInt *selectPartDef(const PartDefinition* pd) const throw(INTERP_KERNEL::Exception) { MCAuto ret(self->selectPartDef(pd));