From: ageay Date: Thu, 28 Jun 2012 13:54:21 +0000 (+0000) Subject: Extension of operation +,-,*,/ on DataArray when mismatch of number of tuples. X-Git-Tag: V6_main_FINAL~622 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c78f0e90f83a25a23acbcab36d23b9caff13de39;p=tools%2Fmedcoupling.git Extension of operation +,-,*,/ on DataArray when mismatch of number of tuples. + debug DataArrayInt.selectByTupleId when step > 1. --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 4f96a053d..5f0967631 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -245,27 +245,17 @@ void DataArray::setInfoOnComponent(int i, const char *info) throw(INTERP_KERNEL: } } -void DataArray::checkNbOfTuplesAndComp(const DataArray& other, const char *msg) const throw(INTERP_KERNEL::Exception) +void DataArray::checkNbOfTuples(int nbOfTuples, const char *msg) const throw(INTERP_KERNEL::Exception) { - if(getNumberOfTuples()!=other.getNumberOfTuples()) - { - std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " << other.getNumberOfTuples() << " having " << getNumberOfTuples() << " !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } - if(getNumberOfComponents()!=other.getNumberOfComponents()) + if(getNumberOfTuples()!=nbOfTuples) { - std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << other.getNumberOfComponents() << " having " << getNumberOfComponents() << " !"; + std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " << nbOfTuples << " having " << getNumberOfTuples() << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } } -void DataArray::checkNbOfTuplesAndComp(int nbOfTuples, int nbOfCompo, const char *msg) const throw(INTERP_KERNEL::Exception) +void DataArray::checkNbOfComps(int nbOfCompo, const char *msg) const throw(INTERP_KERNEL::Exception) { - if(getNumberOfTuples()!=nbOfTuples) - { - std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " << nbOfTuples << " having " << getNumberOfTuples() << " !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } if(getNumberOfComponents()!=nbOfCompo) { std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << nbOfCompo << " having " << getNumberOfComponents() << " !"; @@ -282,6 +272,26 @@ void DataArray::checkNbOfElems(int nbOfElems, const char *msg) const throw(INTER } } +void DataArray::checkNbOfTuplesAndComp(const DataArray& other, const char *msg) const throw(INTERP_KERNEL::Exception) +{ + if(getNumberOfTuples()!=other.getNumberOfTuples()) + { + std::ostringstream oss; oss << msg << " : mismatch number of tuples : expected " << other.getNumberOfTuples() << " having " << getNumberOfTuples() << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + if(getNumberOfComponents()!=other.getNumberOfComponents()) + { + std::ostringstream oss; oss << msg << " : mismatch number of components : expected " << other.getNumberOfComponents() << " having " << getNumberOfComponents() << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } +} + +void DataArray::checkNbOfTuplesAndComp(int nbOfTuples, int nbOfCompo, const char *msg) const throw(INTERP_KERNEL::Exception) +{ + checkNbOfTuples(nbOfTuples,msg); + checkNbOfComps(nbOfCompo,msg); +} + /*! * Simply this method checks that \b value is in [0,\b ref). */ @@ -839,13 +849,9 @@ DataArrayDouble *DataArrayDouble::selectByTupleIdSafe(const int *new2OldBg, cons */ DataArrayDouble *DataArrayDouble::selectByTupleId2(int bg, int end2, int step) const throw(INTERP_KERNEL::Exception) { - if(end2 0 !"); MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); int nbComp=getNumberOfComponents(); - int newNbOfTuples=(end2-1-bg)/step+1; + int newNbOfTuples=GetNumberOfItemGivenBES(bg,end2,step,"DataArrayDouble::selectByTupleId2 : "); ret->alloc(newNbOfTuples,nbComp); double *pt=ret->getPointer(); const double *srcPt=getConstPointer()+bg*nbComp; @@ -2486,44 +2492,66 @@ DataArrayDouble *DataArrayDouble::Add(const DataArrayDouble *a1, const DataArray int nbOfTuple2=a2->getNumberOfTuples(); int nbOfComp=a1->getNumberOfComponents(); int nbOfComp2=a2->getNumberOfComponents(); - if(nbOfTuple!=nbOfTuple2) - throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !"); - DataArrayDouble *ret=0; - if(nbOfComp==nbOfComp2) - { - ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus()); - ret->copyStringInfoFrom(*a1); - } - else + MEDCouplingAutoRefCountObjectPtr ret=0; + if(nbOfTuple==nbOfTuple2) { - int nbOfCompMin,nbOfCompMax; - const DataArrayDouble *aMin, *aMax; - if(nbOfComp>nbOfComp2) + if(nbOfComp==nbOfComp2) { - nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; - aMin=a2; aMax=a1; + ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple,nbOfComp); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus()); + ret->copyStringInfoFrom(*a1); } else { - nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; - aMin=a1; aMax=a2; + int nbOfCompMin,nbOfCompMax; + const DataArrayDouble *aMin, *aMax; + if(nbOfComp>nbOfComp2) + { + nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; + aMin=a2; aMax=a1; + } + else + { + nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; + aMin=a1; aMax=a2; + } + if(nbOfCompMin==1) + { + ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple,nbOfCompMax); + const double *aMinPtr=aMin->getConstPointer(); + const double *aMaxPtr=aMax->getConstPointer(); + double *res=ret->getPointer(); + for(int i=0;i(),aMinPtr[i])); + ret->copyStringInfoFrom(*aMax); + } + else + throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !"); } - if(nbOfCompMin==1) + } + else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1)) + { + if(nbOfComp==nbOfComp2) { + int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2); + const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1; + const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2; + const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer(); ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfCompMax); - const double *aMinPtr=aMin->getConstPointer(); - const double *aMaxPtr=aMax->getConstPointer(); + ret->alloc(nbOfTupleMax,nbOfComp); double *res=ret->getPointer(); - for(int i=0;i(),aMinPtr[i])); + for(int i=0;i()); ret->copyStringInfoFrom(*aMax); } else throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !"); } + else + throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !"); + ret->incrRef(); return ret; } @@ -2573,14 +2601,58 @@ DataArrayDouble *DataArrayDouble::Substract(const DataArrayDouble *a1, const Dat { if(!a1 || !a2) throw INTERP_KERNEL::Exception("DataArrayDouble::Substract : input DataArrayDouble instance is NULL !"); - int nbOfTuple=a2->getNumberOfTuples(); - int nbOfComp=a2->getNumberOfComponents(); - a1->checkNbOfTuplesAndComp(nbOfTuple,nbOfComp,"Nb of components mismatch for array Substract !"); - DataArrayDouble *ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus()); - ret->copyStringInfoFrom(*a1); - return ret; + int nbOfTuple1=a1->getNumberOfTuples(); + int nbOfTuple2=a2->getNumberOfTuples(); + int nbOfComp1=a1->getNumberOfComponents(); + int nbOfComp2=a2->getNumberOfComponents(); + if(nbOfTuple2==nbOfTuple1) + { + if(nbOfComp1==nbOfComp2) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple2,nbOfComp1); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else if(nbOfComp2==1) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const double *a2Ptr=a2->getConstPointer(); + const double *a1Ptr=a1->getConstPointer(); + double *res=ret->getPointer(); + for(int i=0;i(),a2Ptr[i])); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !"); + return 0; + } + } + else if(nbOfTuple2==1) + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer(); + double *pt=ret->getPointer(); + for(int i=0;i()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception + return 0; + } } void DataArrayDouble::substractEqual(const DataArrayDouble *other) throw(INTERP_KERNEL::Exception) @@ -2633,44 +2705,66 @@ DataArrayDouble *DataArrayDouble::Multiply(const DataArrayDouble *a1, const Data int nbOfTuple2=a2->getNumberOfTuples(); int nbOfComp=a1->getNumberOfComponents(); int nbOfComp2=a2->getNumberOfComponents(); - if(nbOfTuple!=nbOfTuple2) - throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !"); - DataArrayDouble *ret=0; - if(nbOfComp==nbOfComp2) - { - ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies()); - ret->copyStringInfoFrom(*a1); - } - else + MEDCouplingAutoRefCountObjectPtr ret=0; + if(nbOfTuple==nbOfTuple2) { - int nbOfCompMin,nbOfCompMax; - const DataArrayDouble *aMin, *aMax; - if(nbOfComp>nbOfComp2) + if(nbOfComp==nbOfComp2) { - nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; - aMin=a2; aMax=a1; + ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple,nbOfComp); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies()); + ret->copyStringInfoFrom(*a1); } else { - nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; - aMin=a1; aMax=a2; + int nbOfCompMin,nbOfCompMax; + const DataArrayDouble *aMin, *aMax; + if(nbOfComp>nbOfComp2) + { + nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; + aMin=a2; aMax=a1; + } + else + { + nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; + aMin=a1; aMax=a2; + } + if(nbOfCompMin==1) + { + ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple,nbOfCompMax); + const double *aMinPtr=aMin->getConstPointer(); + const double *aMaxPtr=aMax->getConstPointer(); + double *res=ret->getPointer(); + for(int i=0;i(),aMinPtr[i])); + ret->copyStringInfoFrom(*aMax); + } + else + throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !"); } - if(nbOfCompMin==1) + } + else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1)) + { + if(nbOfComp==nbOfComp2) { + int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2); + const DataArrayDouble *aMin=nbOfTuple>nbOfTuple2?a2:a1; + const DataArrayDouble *aMax=nbOfTuple>nbOfTuple2?a1:a2; + const double *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer(); ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfCompMax); - const double *aMinPtr=aMin->getConstPointer(); - const double *aMaxPtr=aMax->getConstPointer(); + ret->alloc(nbOfTupleMax,nbOfComp); double *res=ret->getPointer(); - for(int i=0;i(),aMinPtr[i])); + for(int i=0;i()); ret->copyStringInfoFrom(*aMax); } else throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !"); } + else + throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !"); + ret->incrRef(); return ret; } @@ -2720,37 +2814,58 @@ DataArrayDouble *DataArrayDouble::Divide(const DataArrayDouble *a1, const DataAr { if(!a1 || !a2) throw INTERP_KERNEL::Exception("DataArrayDouble::Divide : input DataArrayDouble instance is NULL !"); - int nbOfTuple=a1->getNumberOfTuples(); + int nbOfTuple1=a1->getNumberOfTuples(); int nbOfTuple2=a2->getNumberOfTuples(); - int nbOfComp=a1->getNumberOfComponents(); + int nbOfComp1=a1->getNumberOfComponents(); int nbOfComp2=a2->getNumberOfComponents(); - if(nbOfTuple!=nbOfTuple2) - throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Divide !"); - DataArrayDouble *ret=0; - if(nbOfComp==nbOfComp2) - { - ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides()); - ret->copyStringInfoFrom(*a1); - } - else + if(nbOfTuple2==nbOfTuple1) { - if(nbOfComp2==1) + if(nbOfComp1==nbOfComp2) { - ret=DataArrayDouble::New(); - ret->alloc(nbOfTuple,nbOfComp); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple2,nbOfComp1); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else if(nbOfComp2==1) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple1,nbOfComp1); const double *a2Ptr=a2->getConstPointer(); const double *a1Ptr=a1->getConstPointer(); double *res=ret->getPointer(); - for(int i=0;i(),a2Ptr[i])); + for(int i=0;i(),a2Ptr[i])); ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; } else - throw INTERP_KERNEL::Exception("Nb of components mismatch for array Divide !"); + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !"); + return 0; + } + } + else if(nbOfTuple2==1) + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer(); + double *pt=ret->getPointer(); + for(int i=0;i()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception + return 0; } - return ret; } void DataArrayDouble::divideEqual(const DataArrayDouble *other) throw(INTERP_KERNEL::Exception) @@ -3545,13 +3660,9 @@ DataArrayInt *DataArrayInt::selectByTupleIdSafe(const int *new2OldBg, const int */ DataArrayInt *DataArrayInt::selectByTupleId2(int bg, int end2, int step) const throw(INTERP_KERNEL::Exception) { - if(end2 0 !"); MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); int nbComp=getNumberOfComponents(); - int newNbOfTuples=(end2+1-bg)/step-1; + int newNbOfTuples=GetNumberOfItemGivenBES(bg,end2,step,"DataArrayInt::selectByTupleId2 : "); ret->alloc(newNbOfTuples,nbComp); int *pt=ret->getPointer(); const int *srcPt=getConstPointer()+bg*nbComp; @@ -5188,44 +5299,66 @@ DataArrayInt *DataArrayInt::Add(const DataArrayInt *a1, const DataArrayInt *a2) int nbOfTuple2=a2->getNumberOfTuples(); int nbOfComp=a1->getNumberOfComponents(); int nbOfComp2=a2->getNumberOfComponents(); - if(nbOfTuple!=nbOfTuple2) - throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !"); - DataArrayInt *ret=0; - if(nbOfComp==nbOfComp2) - { - ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus()); - ret->copyStringInfoFrom(*a1); - } - else + MEDCouplingAutoRefCountObjectPtr ret=0; + if(nbOfTuple==nbOfTuple2) { - int nbOfCompMin,nbOfCompMax; - const DataArrayInt *aMin, *aMax; - if(nbOfComp>nbOfComp2) + if(nbOfComp==nbOfComp2) { - nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; - aMin=a2; aMax=a1; + ret=DataArrayInt::New(); + ret->alloc(nbOfTuple,nbOfComp); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus()); + ret->copyStringInfoFrom(*a1); } else { - nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; - aMin=a1; aMax=a2; + int nbOfCompMin,nbOfCompMax; + const DataArrayInt *aMin, *aMax; + if(nbOfComp>nbOfComp2) + { + nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; + aMin=a2; aMax=a1; + } + else + { + nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; + aMin=a1; aMax=a2; + } + if(nbOfCompMin==1) + { + ret=DataArrayInt::New(); + ret->alloc(nbOfTuple,nbOfCompMax); + const int *aMinPtr=aMin->getConstPointer(); + const int *aMaxPtr=aMax->getConstPointer(); + int *res=ret->getPointer(); + for(int i=0;i(),aMinPtr[i])); + ret->copyStringInfoFrom(*aMax); + } + else + throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !"); } - if(nbOfCompMin==1) + } + else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1)) + { + if(nbOfComp==nbOfComp2) { + int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2); + const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1; + const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2; + const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer(); ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfCompMax); - const int *aMinPtr=aMin->getConstPointer(); - const int *aMaxPtr=aMax->getConstPointer(); + ret->alloc(nbOfTupleMax,nbOfComp); int *res=ret->getPointer(); - for(int i=0;i(),aMinPtr[i])); + for(int i=0;i()); ret->copyStringInfoFrom(*aMax); } else throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !"); } + else + throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !"); + ret->incrRef(); return ret; } @@ -5275,14 +5408,58 @@ DataArrayInt *DataArrayInt::Substract(const DataArrayInt *a1, const DataArrayInt { if(!a1 || !a2) throw INTERP_KERNEL::Exception("DataArrayInt::Substract : input DataArrayInt instance is NULL !"); - int nbOfTuple=a2->getNumberOfTuples(); - int nbOfComp=a2->getNumberOfComponents(); - a1->checkNbOfTuplesAndComp(nbOfTuple,nbOfComp,"Nb of components mismatch for array Substract !"); - DataArrayInt *ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus()); - ret->copyStringInfoFrom(*a1); - return ret; + int nbOfTuple1=a1->getNumberOfTuples(); + int nbOfTuple2=a2->getNumberOfTuples(); + int nbOfComp1=a1->getNumberOfComponents(); + int nbOfComp2=a2->getNumberOfComponents(); + if(nbOfTuple2==nbOfTuple1) + { + if(nbOfComp1==nbOfComp2) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple2,nbOfComp1); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else if(nbOfComp2==1) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const int *a2Ptr=a2->getConstPointer(); + const int *a1Ptr=a1->getConstPointer(); + int *res=ret->getPointer(); + for(int i=0;i(),a2Ptr[i])); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !"); + return 0; + } + } + else if(nbOfTuple2==1) + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Substract !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer(); + int *pt=ret->getPointer(); + for(int i=0;i()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception + return 0; + } } void DataArrayInt::substractEqual(const DataArrayInt *other) throw(INTERP_KERNEL::Exception) @@ -5330,44 +5507,66 @@ DataArrayInt *DataArrayInt::Multiply(const DataArrayInt *a1, const DataArrayInt int nbOfTuple2=a2->getNumberOfTuples(); int nbOfComp=a1->getNumberOfComponents(); int nbOfComp2=a2->getNumberOfComponents(); - if(nbOfTuple!=nbOfTuple2) - throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !"); - DataArrayInt *ret=0; - if(nbOfComp==nbOfComp2) - { - ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies()); - ret->copyStringInfoFrom(*a1); - } - else + MEDCouplingAutoRefCountObjectPtr ret=0; + if(nbOfTuple==nbOfTuple2) { - int nbOfCompMin,nbOfCompMax; - const DataArrayInt *aMin, *aMax; - if(nbOfComp>nbOfComp2) + if(nbOfComp==nbOfComp2) { - nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; - aMin=a2; aMax=a1; + ret=DataArrayInt::New(); + ret->alloc(nbOfTuple,nbOfComp); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies()); + ret->copyStringInfoFrom(*a1); } else { - nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; - aMin=a1; aMax=a2; + int nbOfCompMin,nbOfCompMax; + const DataArrayInt *aMin, *aMax; + if(nbOfComp>nbOfComp2) + { + nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp; + aMin=a2; aMax=a1; + } + else + { + nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2; + aMin=a1; aMax=a2; + } + if(nbOfCompMin==1) + { + ret=DataArrayInt::New(); + ret->alloc(nbOfTuple,nbOfCompMax); + const int *aMinPtr=aMin->getConstPointer(); + const int *aMaxPtr=aMax->getConstPointer(); + int *res=ret->getPointer(); + for(int i=0;i(),aMinPtr[i])); + ret->copyStringInfoFrom(*aMax); + } + else + throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !"); } - if(nbOfCompMin==1) + } + else if((nbOfTuple==1 && nbOfTuple2>1) || (nbOfTuple>1 && nbOfTuple2==1)) + { + if(nbOfComp==nbOfComp2) { + int nbOfTupleMax=std::max(nbOfTuple,nbOfTuple2); + const DataArrayInt *aMin=nbOfTuple>nbOfTuple2?a2:a1; + const DataArrayInt *aMax=nbOfTuple>nbOfTuple2?a1:a2; + const int *aMinPtr=aMin->getConstPointer(),*aMaxPtr=aMax->getConstPointer(); ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfCompMax); - const int *aMinPtr=aMin->getConstPointer(); - const int *aMaxPtr=aMax->getConstPointer(); + ret->alloc(nbOfTupleMax,nbOfComp); int *res=ret->getPointer(); - for(int i=0;i(),aMinPtr[i])); + for(int i=0;i()); ret->copyStringInfoFrom(*aMax); } else throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !"); } + else + throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Multiply !"); + ret->incrRef(); return ret; } @@ -5417,37 +5616,58 @@ DataArrayInt *DataArrayInt::Divide(const DataArrayInt *a1, const DataArrayInt *a { if(!a1 || !a2) throw INTERP_KERNEL::Exception("DataArrayInt::Divide : input DataArrayInt instance is NULL !"); - int nbOfTuple=a1->getNumberOfTuples(); + int nbOfTuple1=a1->getNumberOfTuples(); int nbOfTuple2=a2->getNumberOfTuples(); - int nbOfComp=a1->getNumberOfComponents(); + int nbOfComp1=a1->getNumberOfComponents(); int nbOfComp2=a2->getNumberOfComponents(); - if(nbOfTuple!=nbOfTuple2) - throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Divide !"); - DataArrayInt *ret=0; - if(nbOfComp==nbOfComp2) - { - ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides()); - ret->copyStringInfoFrom(*a1); - } - else + if(nbOfTuple2==nbOfTuple1) { - if(nbOfComp2==1) + if(nbOfComp1==nbOfComp2) { - ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfComp); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple2,nbOfComp1); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else if(nbOfComp2==1) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple1,nbOfComp1); const int *a2Ptr=a2->getConstPointer(); const int *a1Ptr=a1->getConstPointer(); int *res=ret->getPointer(); - for(int i=0;i(),a2Ptr[i])); + for(int i=0;i(),a2Ptr[i])); ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; } else - throw INTERP_KERNEL::Exception("Nb of components mismatch for array Divide !"); + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !"); + return 0; + } + } + else if(nbOfTuple2==1) + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Divide !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer(); + int *pt=ret->getPointer(); + for(int i=0;i()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception + return 0; } - return ret; } void DataArrayInt::divideEqual(const DataArrayInt *other) throw(INTERP_KERNEL::Exception) @@ -5494,16 +5714,60 @@ void DataArrayInt::divideEqual(const DataArrayInt *other) throw(INTERP_KERNEL::E DataArrayInt *DataArrayInt::Modulus(const DataArrayInt *a1, const DataArrayInt *a2) throw(INTERP_KERNEL::Exception) { - if(!a1 || !a2) + if(!a1 || !a2) throw INTERP_KERNEL::Exception("DataArrayInt::Modulus : input DataArrayInt instance is NULL !"); - int nbOfTuple=a2->getNumberOfTuples(); - int nbOfComp=a2->getNumberOfComponents(); - a1->checkNbOfTuplesAndComp(nbOfTuple,nbOfComp,"Nb of components mismatch for array Modulus"); - DataArrayInt *ret=DataArrayInt::New(); - ret->alloc(nbOfTuple,nbOfComp); - std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::modulus()); - ret->copyStringInfoFrom(*a1); - return ret; + int nbOfTuple1=a1->getNumberOfTuples(); + int nbOfTuple2=a2->getNumberOfTuples(); + int nbOfComp1=a1->getNumberOfComponents(); + int nbOfComp2=a2->getNumberOfComponents(); + if(nbOfTuple2==nbOfTuple1) + { + if(nbOfComp1==nbOfComp2) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple2,nbOfComp1); + std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::modulus()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else if(nbOfComp2==1) + { + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const int *a2Ptr=a2->getConstPointer(); + const int *a1Ptr=a1->getConstPointer(); + int *res=ret->getPointer(); + for(int i=0;i(),a2Ptr[i])); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Modulus !"); + return 0; + } + } + else if(nbOfTuple2==1) + { + a1->checkNbOfComps(nbOfComp2,"Nb of components mismatch for array Modulus !"); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); + ret->alloc(nbOfTuple1,nbOfComp1); + const int *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer(); + int *pt=ret->getPointer(); + for(int i=0;i()); + ret->copyStringInfoFrom(*a1); + ret->incrRef(); + return ret; + } + else + { + a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Modulus !");//will always throw an exception + return 0; + } } void DataArrayInt::modulusEqual(const DataArrayInt *other) throw(INTERP_KERNEL::Exception) diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 18c83b07b..13a93f9bb 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -107,6 +107,8 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT int getNumberOfComponents() const { return (int)_info_on_compo.size(); } MEDCOUPLING_EXPORT int getNumberOfTuples() const { return _nb_of_tuples; } MEDCOUPLING_EXPORT int getNbOfElems() const { return ((int)_info_on_compo.size())*_nb_of_tuples; } + MEDCOUPLING_EXPORT void checkNbOfTuples(int nbOfTuples, const char *msg) const throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT void checkNbOfComps(int nbOfCompo, const char *msg) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void checkNbOfTuplesAndComp(const DataArray& other, const char *msg) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void checkNbOfTuplesAndComp(int nbOfTuples, int nbOfCompo, const char *msg) const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT void checkNbOfElems(int nbOfElems, const char *msg) const throw(INTERP_KERNEL::Exception); diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 7481d6809..6bbfbe1a7 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -3251,6 +3251,11 @@ namespace ParaMEDMEM MEDCouplingAutoRefCountObjectPtr aaa=aa->buildDADouble(1,self->getNumberOfComponents()); return DataArrayDouble::Add(self,aaa); } + case 4: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayDouble::New(); aaa->useArray(&bb[0],false,CPP_DEALLOC,1,(int)bb.size()); + return DataArrayDouble::Add(self,aaa); + } default: throw INTERP_KERNEL::Exception(msg); } @@ -3279,6 +3284,11 @@ namespace ParaMEDMEM MEDCouplingAutoRefCountObjectPtr aaa=aa->buildDADouble(1,self->getNumberOfComponents()); return DataArrayDouble::Add(self,aaa); } + case 4: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayDouble::New(); aaa->useArray(&bb[0],false,CPP_DEALLOC,1,(int)bb.size()); + return DataArrayDouble::Add(self,aaa); + } default: throw INTERP_KERNEL::Exception(msg); } @@ -5079,6 +5089,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Add(self,aaa); + } case 3: { return DataArrayInt::Add(self,a); @@ -5111,6 +5126,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Add(self,aaa); + } case 4: { MEDCouplingAutoRefCountObjectPtr aaaa=aaa->buildDAInt(1,self->getNumberOfComponents()); @@ -5181,6 +5201,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Substract(self,aaa); + } case 3: { return DataArrayInt::Substract(self,a); @@ -5213,6 +5238,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Substract(aaa,self); + } case 4: { MEDCouplingAutoRefCountObjectPtr aaaa=aaa->buildDAInt(1,self->getNumberOfComponents()); @@ -5283,6 +5313,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Multiply(self,aaa); + } case 3: { return DataArrayInt::Multiply(self,a); @@ -5315,6 +5350,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Multiply(self,aaa); + } case 4: { MEDCouplingAutoRefCountObjectPtr aaaa=aaa->buildDAInt(1,self->getNumberOfComponents()); @@ -5385,6 +5425,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Divide(self,aaa); + } case 3: { return DataArrayInt::Divide(self,a); @@ -5417,6 +5462,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Divide(aaa,self); + } case 4: { MEDCouplingAutoRefCountObjectPtr aaaa=aaa->buildDAInt(1,self->getNumberOfComponents()); @@ -5487,6 +5537,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Modulus(self,aaa); + } case 3: { return DataArrayInt::Modulus(self,a); @@ -5519,6 +5574,11 @@ namespace ParaMEDMEM ret->incrRef(); return ret; } + case 2: + { + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayInt::New(); aaa->useArray(&aa[0],false,CPP_DEALLOC,1,(int)aa.size()); + return DataArrayInt::Modulus(aaa,self); + } case 3: { return DataArrayInt::Modulus(a,self); diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index a84663fff..dd85deff3 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -9858,6 +9858,71 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertRaises(InterpKernelException,m.giveCellsWithType,NORM_HEXA8) pass + def testDAOp1(self): + d=DataArrayDouble.New(5,2) + d.rearrange(1) ; d.iota(2.) ; d.rearrange(2) + d.setInfoOnComponents(["X [m]","Y [m]"]) + d.setName("AName") + # + d1=d+[8,9] + self.assertTrue(d1.isEqualWithoutConsideringStr(DataArrayDouble.New([10.0,12.0,12.0,14.0,14.0,16.0,16.0,18.0,18.0,20.0]),1e-12)) + d1bis=DataArrayDouble.New([8,9],1,2)+d + self.assertTrue(d1bis.isEqual(d1,1e-12)) + d1ter=[8,9]+d + self.assertTrue(d1ter.isEqual(d1,1e-12)) + # + d2=d1-[8,9] + self.assertTrue(d2.isEqual(d,1e-12)) + self.assertRaises(InterpKernelException,d1.__rsub__,[8,9])#[8,9]-d1 + # + d3=d*[8,9] + self.assertTrue(d3.isEqualWithoutConsideringStr(DataArrayDouble.New([16.0,27.0,32.0,45.0,48.0,63.0,64.0,81.0,80.0,99.0]),1e-12)) + d3bis=DataArrayDouble.New([8,9],1,2)*d + self.assertTrue(d3bis.isEqual(d3,1e-12)) + d3ter=[8,9]*d + self.assertTrue(d3ter.isEqual(d3,1e-12)) + # + d4=d3/[8,9] + self.assertTrue(d4.isEqual(d,1e-12)) + # + d=DataArrayInt.New(5,2) + d.rearrange(1) ; d.iota(2) ; d.rearrange(2) + d.setInfoOnComponents(["X [m]","Y [m]"]) + d.setName("AName") + # + d1=d+[8,9] + self.assertEqual(d1.getValues(),[10,12,12,14,14,16,16,18,18,20]) + d1bis=DataArrayInt.New([8,9],1,2)+d + self.assertTrue(d1bis.isEqual(d1)) + d1ter=[8,9]+d + self.assertTrue(d1ter.isEqual(d1)) + # + d2=d1-[8,9] + self.assertTrue(d2.isEqual(d)) + self.assertRaises(InterpKernelException,d1.__rsub__,[8,9]) + # + d3=d*[8,9] + self.assertEqual(d3.getValues(),[16,27,32,45,48,63,64,81,80,99]) + d3bis=DataArrayInt.New([8,9],1,2)*d + self.assertTrue(d3bis.isEqual(d3)) + d3ter=[8,9]*d + self.assertTrue(d3ter.isEqual(d3)) + # + d4=d3/[8,9] + self.assertTrue(d4.isEqual(d)) + # + d5=d%[4,5] + self.assertEqual(d5.getValues(),[2,3,0,0,2,2,0,4,2,1]) + pass + + def testSelectTupleId2DAIBug1(self): + da=DataArrayInt.New([0,1,2,3,12,13,4,5,6,7,14,15,8,9,10,11,16,17]) + self.assertEqual([2,6,10],da[2::6].getValues()) + self.assertEqual([0,4,8],da[::6].getValues()) + self.assertEqual([5,9],da[7::6].getValues()) + self.assertEqual([5],da[7:-5:6].getValues()) + pass + def setUp(self): pass pass