- int nbCompo=getNumberOfComponents();
- int nbOfTuples=getNumberOfTuples();
- int sz=(int)std::distance(bgOfIndex,endOfIndex);
- if(sz<1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : invalid size of input index array !");
- sz--;
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(sz,nbCompo);
- const int *w=bgOfIndex;
- if(*w<0 || *w>=nbOfTuples)
- throw INTERP_KERNEL::Exception("DataArrayDouble::accumulatePerChunck : The first element of the input index not in [0,nbOfTuples) !");
- const double *srcPt=begin()+(*w)*nbCompo;
- double *tmp=ret->getPointer();
- for(int i=0;i<sz;i++,tmp+=nbCompo,w++)
- {
- std::fill(tmp,tmp+nbCompo,0.);
- if(w[1]>=w[0])
- {
- for(int j=w[0];j<w[1];j++,srcPt+=nbCompo)
- {
- if(j>=0 && j<nbOfTuples)
- std::transform(srcPt,srcPt+nbCompo,tmp,tmp,std::plus<double>());
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::accumulatePerChunck : At rank #" << i << " the input index array points to id " << j << " should be in [0," << nbOfTuples << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::accumulatePerChunck : At rank #" << i << " the input index array is not in ascendingly sorted.";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Converts each 2D point defined by the tuple of \a this array from the Polar to the
- * Cartesian coordinate system. The two components of the tuple of \a this array are
- * considered to contain (1) radius and (2) angle of the point in the Polar CS.
- * \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
- * contains X and Y coordinates of the point in the Cartesian CS. The caller
- * is to delete this array using decrRef() as it is no more needed. The array
- * does not contain any textual info on components.
- * \throw If \a this->getNumberOfComponents() != 2.
- */
-DataArrayDouble *DataArrayDouble::fromPolarToCart() const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents());
- if(nbOfComp!=2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::fromPolarToCart : must be an array with exactly 2 components !");
- int nbOfTuple(getNumberOfTuples());
- DataArrayDouble *ret(DataArrayDouble::New());
- ret->alloc(nbOfTuple,2);
- double *w(ret->getPointer());
- const double *wIn(getConstPointer());
- for(int i=0;i<nbOfTuple;i++,w+=2,wIn+=2)
- {
- w[0]=wIn[0]*cos(wIn[1]);
- w[1]=wIn[0]*sin(wIn[1]);
- }
- return ret;
-}
-
-/*!
- * Converts each 3D point defined by the tuple of \a this array from the Cylindrical to
- * the Cartesian coordinate system. The three components of the tuple of \a this array
- * are considered to contain (1) radius, (2) azimuth and (3) altitude of the point in
- * the Cylindrical CS.
- * \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
- * contains X, Y and Z coordinates of the point in the Cartesian CS. The info
- * on the third component is copied from \a this array. The caller
- * is to delete this array using decrRef() as it is no more needed.
- * \throw If \a this->getNumberOfComponents() != 3.
- */
-DataArrayDouble *DataArrayDouble::fromCylToCart() const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents());
- if(nbOfComp!=3)
- throw INTERP_KERNEL::Exception("DataArrayDouble::fromCylToCart : must be an array with exactly 3 components !");
- int nbOfTuple(getNumberOfTuples());
- DataArrayDouble *ret(DataArrayDouble::New());
- ret->alloc(getNumberOfTuples(),3);
- double *w(ret->getPointer());
- const double *wIn(getConstPointer());
- for(int i=0;i<nbOfTuple;i++,w+=3,wIn+=3)
- {
- w[0]=wIn[0]*cos(wIn[1]);
- w[1]=wIn[0]*sin(wIn[1]);
- w[2]=wIn[2];
- }
- ret->setInfoOnComponent(2,getInfoOnComponent(2));
- return ret;
-}
-
-/*!
- * Converts each 3D point defined by the tuple of \a this array from the Spherical to
- * the Cartesian coordinate system. The three components of the tuple of \a this array
- * are considered to contain (1) radius, (2) polar angle and (3) azimuthal angle of the
- * point in the Cylindrical CS.
- * \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
- * contains X, Y and Z coordinates of the point in the Cartesian CS. The info
- * on the third component is copied from \a this array. The caller
- * is to delete this array using decrRef() as it is no more needed.
- * \throw If \a this->getNumberOfComponents() != 3.
- */
-DataArrayDouble *DataArrayDouble::fromSpherToCart() const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents());
- if(nbOfComp!=3)
- throw INTERP_KERNEL::Exception("DataArrayDouble::fromSpherToCart : must be an array with exactly 3 components !");
- int nbOfTuple(getNumberOfTuples());
- DataArrayDouble *ret(DataArrayDouble::New());
- ret->alloc(getNumberOfTuples(),3);
- double *w(ret->getPointer());
- const double *wIn(getConstPointer());
- for(int i=0;i<nbOfTuple;i++,w+=3,wIn+=3)
- {
- w[0]=wIn[0]*cos(wIn[2])*sin(wIn[1]);
- w[1]=wIn[0]*sin(wIn[2])*sin(wIn[1]);
- w[2]=wIn[0]*cos(wIn[1]);
- }
- return ret;
-}
-
-/*!
- * This method returns a new array containing the same number of tuples than \a this. To do this, this method needs \a at parameter to specify the convention of \a this.
- * All the tuples of the returned array will be in cartesian sense. So if \a at equals to AX_CART the returned array is basically a deep copy of \a this.
- * If \a at equals to AX_CYL the returned array will be the result of operation cylindric to cartesian of \a this...
- *
- * \param [in] atOfThis - The axis type of \a this.
- * \return DataArrayDouble * - the new instance of DataArrayDouble (that must be dealed by caller) containing the result of the cartesianizification of \a this.
- */
-DataArrayDouble *DataArrayDouble::cartesianize(MEDCouplingAxisType atOfThis) const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents());
- MCAuto<DataArrayDouble> ret;
- switch(atOfThis)
- {
- case AX_CART:
- ret=deepCopy();
- case AX_CYL:
- if(nbOfComp==3)
- {
- ret=fromCylToCart();
- break;
- }
- if(nbOfComp==2)
- {
- ret=fromPolarToCart();
- break;
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayDouble::cartesianize : For AX_CYL, number of components must be in [2,3] !");
- case AX_SPHER:
- if(nbOfComp==3)
- {
- ret=fromSpherToCart();
- break;
- }
- if(nbOfComp==2)
- {
- ret=fromPolarToCart();
- break;
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayDouble::cartesianize : For AX_CYL, number of components must be in [2,3] !");
- default:
- throw INTERP_KERNEL::Exception("DataArrayDouble::cartesianize : not recognized axis type ! Only AX_CART, AX_CYL and AX_SPHER supported !");
- }
- ret->copyStringInfoFrom(*this);
- return ret.retn();
-}
-
-/*!
- * Computes the doubly contracted product of every tensor defined by the tuple of \a this
- * array contating 6 components.
- * \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
- * is calculated from the tuple <em>(t)</em> of \a this array as follows:
- * \f$ t[0]^2+t[1]^2+t[2]^2+2*t[3]^2+2*t[4]^2+2*t[5]^2\f$.
- * The caller is to delete this result array using decrRef() as it is no more needed.
- * \throw If \a this->getNumberOfComponents() != 6.
- */
-DataArrayDouble *DataArrayDouble::doublyContractedProduct() const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents());
- if(nbOfComp!=6)
- throw INTERP_KERNEL::Exception("DataArrayDouble::doublyContractedProduct : must be an array with exactly 6 components !");
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,1);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
- *dest=src[0]*src[0]+src[1]*src[1]+src[2]*src[2]+2.*src[3]*src[3]+2.*src[4]*src[4]+2.*src[5]*src[5];
- return ret;
-}
-
-/*!
- * Computes the determinant of every square matrix defined by the tuple of \a this
- * array, which contains either 4, 6 or 9 components. The case of 6 components
- * corresponds to that of the upper triangular matrix.
- * \return DataArrayDouble * - the new instance of DataArrayDouble, whose each tuple
- * is the determinant of matrix of the corresponding tuple of \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() is not in [4,6,9].
- */
-DataArrayDouble *DataArrayDouble::determinant() const
-{
- checkAllocated();
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,1);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- switch(getNumberOfComponents())
- {
- case 6:
- for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
- *dest=src[0]*src[1]*src[2]+2.*src[4]*src[5]*src[3]-src[0]*src[4]*src[4]-src[2]*src[3]*src[3]-src[1]*src[5]*src[5];
- return ret;
- case 4:
- for(int i=0;i<nbOfTuple;i++,dest++,src+=4)
- *dest=src[0]*src[3]-src[1]*src[2];
- return ret;
- case 9:
- for(int i=0;i<nbOfTuple;i++,dest++,src+=9)
- *dest=src[0]*src[4]*src[8]+src[1]*src[5]*src[6]+src[2]*src[3]*src[7]-src[0]*src[5]*src[7]-src[1]*src[3]*src[8]-src[2]*src[4]*src[6];
- return ret;
- default:
- ret->decrRef();
- throw INTERP_KERNEL::Exception("DataArrayDouble::determinant : Invalid number of components ! must be in 4,6,9 !");
- }
-}
-
-/*!
- * Computes 3 eigenvalues of every upper triangular matrix defined by the tuple of
- * \a this array, which contains 6 components.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing 3
- * components, whose each tuple contains the eigenvalues of the matrix of
- * corresponding tuple of \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() != 6.
- */
-DataArrayDouble *DataArrayDouble::eigenValues() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- if(nbOfComp!=6)
- throw INTERP_KERNEL::Exception("DataArrayDouble::eigenValues : must be an array with exactly 6 components !");
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,3);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,dest+=3,src+=6)
- INTERP_KERNEL::computeEigenValues6(src,dest);
- return ret;
-}
-
-/*!
- * Computes 3 eigenvectors of every upper triangular matrix defined by the tuple of
- * \a this array, which contains 6 components.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing 9
- * components, whose each tuple contains 3 eigenvectors of the matrix of
- * corresponding tuple of \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() != 6.
- */
-DataArrayDouble *DataArrayDouble::eigenVectors() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- if(nbOfComp!=6)
- throw INTERP_KERNEL::Exception("DataArrayDouble::eigenVectors : must be an array with exactly 6 components !");
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,9);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,src+=6)
- {
- double tmp[3];
- INTERP_KERNEL::computeEigenValues6(src,tmp);
- for(int j=0;j<3;j++,dest+=3)
- INTERP_KERNEL::computeEigenVectorForEigenValue6(src,tmp[j],1e-12,dest);
- }
- return ret;
-}
-
-/*!
- * Computes the inverse matrix of every matrix defined by the tuple of \a this
- * array, which contains either 4, 6 or 9 components. The case of 6 components
- * corresponds to that of the upper triangular matrix.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of components as \a this one, whose each tuple is the inverse
- * matrix of the matrix of corresponding tuple of \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() is not in [4,6,9].
- */
-DataArrayDouble *DataArrayDouble::inverse() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- if(nbOfComp!=6 && nbOfComp!=9 && nbOfComp!=4)
- throw INTERP_KERNEL::Exception("DataArrayDouble::inversion : must be an array with 4,6 or 9 components !");
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,nbOfComp);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- if(nbOfComp==6)
- for(int i=0;i<nbOfTuple;i++,dest+=6,src+=6)
- {
- double det=src[0]*src[1]*src[2]+2.*src[4]*src[5]*src[3]-src[0]*src[4]*src[4]-src[2]*src[3]*src[3]-src[1]*src[5]*src[5];
- dest[0]=(src[1]*src[2]-src[4]*src[4])/det;
- dest[1]=(src[0]*src[2]-src[5]*src[5])/det;
- dest[2]=(src[0]*src[1]-src[3]*src[3])/det;
- dest[3]=(src[5]*src[4]-src[3]*src[2])/det;
- dest[4]=(src[5]*src[3]-src[0]*src[4])/det;
- dest[5]=(src[3]*src[4]-src[1]*src[5])/det;
- }
- else if(nbOfComp==4)
- for(int i=0;i<nbOfTuple;i++,dest+=4,src+=4)
- {
- double det=src[0]*src[3]-src[1]*src[2];
- dest[0]=src[3]/det;
- dest[1]=-src[1]/det;
- dest[2]=-src[2]/det;
- dest[3]=src[0]/det;
- }
- else
- for(int i=0;i<nbOfTuple;i++,dest+=9,src+=9)
- {
- double det=src[0]*src[4]*src[8]+src[1]*src[5]*src[6]+src[2]*src[3]*src[7]-src[0]*src[5]*src[7]-src[1]*src[3]*src[8]-src[2]*src[4]*src[6];
- dest[0]=(src[4]*src[8]-src[7]*src[5])/det;
- dest[1]=(src[7]*src[2]-src[1]*src[8])/det;
- dest[2]=(src[1]*src[5]-src[4]*src[2])/det;
- dest[3]=(src[6]*src[5]-src[3]*src[8])/det;
- dest[4]=(src[0]*src[8]-src[6]*src[2])/det;
- dest[5]=(src[2]*src[3]-src[0]*src[5])/det;
- dest[6]=(src[3]*src[7]-src[6]*src[4])/det;
- dest[7]=(src[6]*src[1]-src[0]*src[7])/det;
- dest[8]=(src[0]*src[4]-src[1]*src[3])/det;
- }
- return ret;
-}
-
-/*!
- * Computes the trace of every matrix defined by the tuple of \a this
- * array, which contains either 4, 6 or 9 components. The case of 6 components
- * corresponds to that of the upper triangular matrix.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing
- * 1 component, whose each tuple is the trace of
- * the matrix of corresponding tuple of \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() is not in [4,6,9].
- */
-DataArrayDouble *DataArrayDouble::trace() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- if(nbOfComp!=6 && nbOfComp!=9 && nbOfComp!=4)
- throw INTERP_KERNEL::Exception("DataArrayDouble::trace : must be an array with 4,6 or 9 components !");
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,1);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- if(nbOfComp==6)
- for(int i=0;i<nbOfTuple;i++,dest++,src+=6)
- *dest=src[0]+src[1]+src[2];
- else if(nbOfComp==4)
- for(int i=0;i<nbOfTuple;i++,dest++,src+=4)
- *dest=src[0]+src[3];
- else
- for(int i=0;i<nbOfTuple;i++,dest++,src+=9)
- *dest=src[0]+src[4]+src[8];
- return ret;
-}
-
-/*!
- * Computes the stress deviator tensor of every stress tensor defined by the tuple of
- * \a this array, which contains 6 components.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of components and tuples as \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() != 6.
- */
-DataArrayDouble *DataArrayDouble::deviator() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- if(nbOfComp!=6)
- throw INTERP_KERNEL::Exception("DataArrayDouble::deviator : must be an array with exactly 6 components !");
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,6);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,dest+=6,src+=6)
- {
- double tr=(src[0]+src[1]+src[2])/3.;
- dest[0]=src[0]-tr;
- dest[1]=src[1]-tr;
- dest[2]=src[2]-tr;
- dest[3]=src[3];
- dest[4]=src[4];
- dest[5]=src[5];
- }
- return ret;
-}
-
-/*!
- * Computes the magnitude of every vector defined by the tuple of
- * \a this array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array and one component.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this is not allocated.
- */
-DataArrayDouble *DataArrayDouble::magnitude() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- DataArrayDouble *ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,1);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,dest++)
- {
- double sum=0.;
- for(int j=0;j<nbOfComp;j++,src++)
- sum+=(*src)*(*src);
- *dest=sqrt(sum);
- }
- return ret;
-}
-
-/*!
- * Computes for each tuple the sum of number of components values in the tuple and return it.
- *
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array and one component.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this is not allocated.
- */
-DataArrayDouble *DataArrayDouble::sumPerTuple() const
-{
- checkAllocated();
- int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
- MCAuto<DataArrayDouble> ret(DataArrayDouble::New());
- ret->alloc(nbOfTuple,1);
- const double *src(getConstPointer());
- double *dest(ret->getPointer());
- for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
- *dest=std::accumulate(src,src+nbOfComp,0.);
- return ret.retn();
-}
-
-/*!
- * Computes the maximal value within every tuple of \a this array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array and one component.
- * 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::maxPerTupleWithCompoId
- */
-DataArrayDouble *DataArrayDouble::maxPerTuple() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- int nbOfTuple=getNumberOfTuples();
- ret->alloc(nbOfTuple,1);
- const double *src=getConstPointer();
- double *dest=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,dest++,src+=nbOfComp)
- *dest=*std::max_element(src,src+nbOfComp);
- return ret.retn();
-}
-
-/*!
- * Computes the maximal value within every tuple of \a this array and it returns the first component
- * id for each tuple that corresponds to the maximal value within the tuple.
- *
- * \param [out] compoIdOfMaxPerTuple - the new new instance of DataArrayInt containing the
- * same number of tuples and only one component.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array and one component.
- * 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::maxPerTuple
- */
-DataArrayDouble *DataArrayDouble::maxPerTupleWithCompoId(DataArrayInt* &compoIdOfMaxPerTuple) const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- MCAuto<DataArrayDouble> ret0=DataArrayDouble::New();
- MCAuto<DataArrayInt> ret1=DataArrayInt::New();
- int nbOfTuple=getNumberOfTuples();
- ret0->alloc(nbOfTuple,1); ret1->alloc(nbOfTuple,1);
- const double *src=getConstPointer();
- double *dest=ret0->getPointer(); int *dest1=ret1->getPointer();
- for(int i=0;i<nbOfTuple;i++,dest++,dest1++,src+=nbOfComp)
- {
- const double *loc=std::max_element(src,src+nbOfComp);
- *dest=*loc;
- *dest1=(int)std::distance(src,loc);
- }
- compoIdOfMaxPerTuple=ret1.retn();
- return ret0.retn();
-}
-
-/*!
- * This method returns a newly allocated DataArrayDouble instance having one component and \c this->getNumberOfTuples() * \c this->getNumberOfTuples() tuples.
- * \n This returned array contains the euclidian distance for each tuple in \a this.
- * \n So the returned array can be seen as a dense symmetrical matrix whose diagonal elements are equal to 0.
- * \n The returned array has only one component (and **not** \c this->getNumberOfTuples() components to avoid the useless memory consumption due to components info in returned DataArrayDouble)
- *
- * \warning use this method with care because it can leads to big amount of consumed memory !
- *
- * \return A newly allocated (huge) MEDCoupling::DataArrayDouble instance that the caller should deal with.
- *
- * \throw If \a this is not allocated.
- *
- * \sa DataArrayDouble::buildEuclidianDistanceDenseMatrixWith
- */
-DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrix() const
-{
- checkAllocated();
- int nbOfComp=getNumberOfComponents();
- int nbOfTuples=getNumberOfTuples();
- const double *inData=getConstPointer();
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuples*nbOfTuples,1);
- double *outData=ret->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- {
- outData[i*nbOfTuples+i]=0.;
- for(int j=i+1;j<nbOfTuples;j++)
- {
- double dist=0.;
- for(int k=0;k<nbOfComp;k++)
- { double delta=inData[i*nbOfComp+k]-inData[j*nbOfComp+k]; dist+=delta*delta; }
- dist=sqrt(dist);
- outData[i*nbOfTuples+j]=dist;
- outData[j*nbOfTuples+i]=dist;
- }
- }
- return ret.retn();
-}
-
-/*!
- * This method returns a newly allocated DataArrayDouble instance having one component and \c this->getNumberOfTuples() * \c other->getNumberOfTuples() tuples.
- * \n This returned array contains the euclidian distance for each tuple in \a other with each tuple in \a this.
- * \n So the returned array can be seen as a dense rectangular matrix with \c other->getNumberOfTuples() rows and \c this->getNumberOfTuples() columns.
- * \n Output rectangular matrix is sorted along rows.
- * \n The returned array has only one component (and **not** \c this->getNumberOfTuples() components to avoid the useless memory consumption due to components info in returned DataArrayDouble)
- *
- * \warning use this method with care because it can leads to big amount of consumed memory !
- *
- * \param [in] other DataArrayDouble instance having same number of components than \a this.
- * \return A newly allocated (huge) MEDCoupling::DataArrayDouble instance that the caller should deal with.
- *
- * \throw If \a this is not allocated, or if \a other is null or if \a other is not allocated, or if number of components of \a other and \a this differs.
- *
- * \sa DataArrayDouble::buildEuclidianDistanceDenseMatrix
- */
-DataArrayDouble *DataArrayDouble::buildEuclidianDistanceDenseMatrixWith(const DataArrayDouble *other) const
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::buildEuclidianDistanceDenseMatrixWith : input parameter is null !");
- checkAllocated();
- other->checkAllocated();
- int nbOfComp=getNumberOfComponents();
- int otherNbOfComp=other->getNumberOfComponents();
- if(nbOfComp!=otherNbOfComp)
- {
- std::ostringstream oss; oss << "DataArrayDouble::buildEuclidianDistanceDenseMatrixWith : this nb of compo=" << nbOfComp << " and other nb of compo=" << otherNbOfComp << ". It should match !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- int nbOfTuples=getNumberOfTuples();
- int otherNbOfTuples=other->getNumberOfTuples();
- const double *inData=getConstPointer();
- const double *inDataOther=other->getConstPointer();
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(otherNbOfTuples*nbOfTuples,1);
- double *outData=ret->getPointer();
- for(int i=0;i<otherNbOfTuples;i++,inDataOther+=nbOfComp)
- {
- for(int j=0;j<nbOfTuples;j++)
- {
- double dist=0.;
- for(int k=0;k<nbOfComp;k++)
- { double delta=inDataOther[k]-inData[j*nbOfComp+k]; dist+=delta*delta; }
- dist=sqrt(dist);
- outData[i*nbOfTuples+j]=dist;
- }
- }
- return ret.retn();
-}
-
-/*!
- * Sorts value within every tuple of \a this array.
- * \param [in] asc - if \a true, the values are sorted in ascending order, else,
- * in descending order.
- * \throw If \a this is not allocated.
- */
-void DataArrayDouble::sortPerTuple(bool asc)
-{
- checkAllocated();
- double *pt=getPointer();
- int nbOfTuple=getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- if(asc)
- for(int i=0;i<nbOfTuple;i++,pt+=nbOfComp)
- std::sort(pt,pt+nbOfComp);
- else
- for(int i=0;i<nbOfTuple;i++,pt+=nbOfComp)
- std::sort(pt,pt+nbOfComp,std::greater<double>());
- declareAsNew();
-}
-
-/*!
- * Converts every value of \a this array to its absolute value.
- * \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());
- 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 linear function to a given component of \a this array, so that
- * an array element <em>(x)</em> becomes \f$ a * x + b \f$.
- * \param [in] a - the first coefficient of the function.
- * \param [in] b - the second coefficient of the function.
- * \param [in] compoId - the index of component to modify.
- * \throw If \a this is not allocated, or \a compoId is not in [0,\c this->getNumberOfComponents() ).
- */
-void DataArrayDouble::applyLin(double a, double b, int compoId)
-{
- checkAllocated();
- double *ptr(getPointer()+compoId);
- int nbOfComp(getNumberOfComponents()),nbOfTuple(getNumberOfTuples());
- if(compoId<0 || compoId>=nbOfComp)
- {
- std::ostringstream oss; oss << "DataArrayDouble::applyLin : The compoId requested (" << compoId << ") is not valid ! Must be in [0," << nbOfComp << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
- *ptr=a*(*ptr)+b;
- declareAsNew();
-}
-
-/*!
- * Apply a linear function to all elements of \a this array, so that
- * an element _x_ becomes \f$ a * x + b \f$.
- * \param [in] a - the first coefficient of the function.
- * \param [in] b - the second coefficient of the function.
- * \throw If \a this is not allocated.
- */
-void DataArrayDouble::applyLin(double a, double b)
-{
- checkAllocated();
- double *ptr=getPointer();
- std::size_t nbOfElems=getNbOfElems();
- for(std::size_t i=0;i<nbOfElems;i++,ptr++)
- *ptr=a*(*ptr)+b;
- declareAsNew();
-}
-
-/*!
- * Modify all elements of \a this array, so that
- * an element _x_ becomes \f$ numerator / x \f$.
- * \warning If an exception is thrown because of presence of 0.0 element in \a this
- * array, all elements processed before detection of the zero element remain
- * modified.
- * \param [in] numerator - the numerator used to modify array elements.
- * \throw If \a this is not allocated.
- * \throw If there is an element equal to 0.0 in \a this array.
- */
-void DataArrayDouble::applyInv(double numerator)
-{
- checkAllocated();
- double *ptr=getPointer();
- std::size_t nbOfElems=getNbOfElems();
- for(std::size_t i=0;i<nbOfElems;i++,ptr++)
- {
- if(std::abs(*ptr)>std::numeric_limits<double>::min())
- {
- *ptr=numerator/(*ptr);
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::applyInv : presence of null value in tuple #" << i/getNumberOfComponents() << " component #" << i%getNumberOfComponents();
- oss << " !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- declareAsNew();
-}
-
-/*!
- * Returns a full copy of \a this array except that sign of all elements is reversed.
- * \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.
- */
-DataArrayDouble *DataArrayDouble::negate() const
-{
- checkAllocated();
- DataArrayDouble *newArr=DataArrayDouble::New();
- int nbOfTuples=getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- newArr->alloc(nbOfTuples,nbOfComp);
- const double *cptr=getConstPointer();
- std::transform(cptr,cptr+nbOfTuples*nbOfComp,newArr->getPointer(),std::negate<double>());
- newArr->copyStringInfoFrom(*this);
- return newArr;
-}
-
-/*!
- * Modify all elements of \a this array, so that
- * an element _x_ becomes <em> val ^ x </em>. Contrary to DataArrayInt::applyPow
- * all values in \a this have to be >= 0 if val is \b not integer.
- * \param [in] val - the value used to apply pow on all array elements.
- * \throw If \a this is not allocated.
- * \warning If an exception is thrown because of presence of 0 element in \a this
- * array and \a val is \b not integer, all elements processed before detection of the zero element remain
- * modified.
- */
-void DataArrayDouble::applyPow(double val)
-{
- checkAllocated();
- double *ptr=getPointer();
- std::size_t nbOfElems=getNbOfElems();
- int val2=(int)val;
- bool isInt=((double)val2)==val;
- if(!isInt)
- {
- for(std::size_t i=0;i<nbOfElems;i++,ptr++)
- {
- if(*ptr>=0)
- *ptr=pow(*ptr,val);
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::applyPow (double) : At elem # " << i << " value is " << *ptr << " ! must be >=0. !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- else
- {
- for(std::size_t i=0;i<nbOfElems;i++,ptr++)
- *ptr=pow(*ptr,val2);
- }
- declareAsNew();
-}
-
-/*!
- * Modify all elements of \a this array, so that
- * an element _x_ becomes \f$ val ^ x \f$.
- * \param [in] val - the value used to apply pow on all array elements.
- * \throw If \a this is not allocated.
- * \throw If \a val < 0.
- * \warning If an exception is thrown because of presence of 0 element in \a this
- * array, all elements processed before detection of the zero element remain
- * modified.
- */
-void DataArrayDouble::applyRPow(double val)
-{
- checkAllocated();
- if(val<0.)
- throw INTERP_KERNEL::Exception("DataArrayDouble::applyRPow : the input value has to be >= 0 !");
- double *ptr=getPointer();
- std::size_t nbOfElems=getNbOfElems();
- for(std::size_t i=0;i<nbOfElems;i++,ptr++)
- *ptr=pow(val,*ptr);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying \a
- * FunctionToEvaluate to every tuple of \a this array. Textual data is not copied.
- * For more info see \ref MEDCouplingArrayApplyFunc
- * \param [in] nbOfComp - number of components in the result array.
- * \param [in] func - the \a FunctionToEvaluate declared as
- * \c bool (*\a func)(\c const \c double *\a pos, \c double *\a res),
- * where \a pos points to the first component of a tuple of \a this array
- * and \a res points to the first component of a tuple of the result array.
- * Note that length (number of components) of \a pos can differ from
- * that of \a res.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples 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.
- * \throw If \a func returns \a false.
- */
-DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, FunctionToEvaluate func) const
-{
- checkAllocated();
- DataArrayDouble *newArr=DataArrayDouble::New();
- int nbOfTuples=getNumberOfTuples();
- int oldNbOfComp=getNumberOfComponents();
- newArr->alloc(nbOfTuples,nbOfComp);
- const double *ptr=getConstPointer();
- double *ptrToFill=newArr->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- {
- if(!func(ptr+i*oldNbOfComp,ptrToFill+i*nbOfComp))
- {
- std::ostringstream oss; oss << "For tuple # " << i << " with value (";
- std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
- oss << ") : Evaluation of function failed !";
- newArr->decrRef();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return newArr;
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying a function to every
- * tuple of \a this array. Textual data is not copied.
- * For more info see \ref MEDCouplingArrayApplyFunc1.
- * \param [in] nbOfComp - number of components in the result array.
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
- * If false the computation is carried on without any notification. When false the evaluation is a little faster.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples as \a this array and \a nbOfComp components.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this is not allocated.
- * \throw If computing \a func fails.
- */
-DataArrayDouble *DataArrayDouble::applyFunc(int nbOfComp, const std::string& func, bool isSafe) const
-{
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- std::set<std::string> vars;
- expr.getTrueSetOfVars(vars);
- std::vector<std::string> varsV(vars.begin(),vars.end());
- return applyFuncNamedCompo(nbOfComp,varsV,func,isSafe);
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying a function to every
- * tuple of \a this array. Textual data is not copied. This method works by tuples (whatever its size).
- * If \a this is a one component array, call applyFuncOnThis instead that performs the same work faster.
- *
- * For more info see \ref MEDCouplingArrayApplyFunc0.
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
- * If false the computation is carried on without any notification. When false the evaluation is a little faster.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples and components as \a this array.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \sa applyFuncOnThis
- * \throw If \a this is not allocated.
- * \throw If computing \a func fails.
- */
-DataArrayDouble *DataArrayDouble::applyFunc(const std::string& func, bool isSafe) const
-{
- int nbOfComp(getNumberOfComponents());
- if(nbOfComp<=0)
- throw INTERP_KERNEL::Exception("DataArrayDouble::applyFunc : output number of component must be > 0 !");
- checkAllocated();
- int nbOfTuples(getNumberOfTuples());
- MCAuto<DataArrayDouble> newArr(DataArrayDouble::New());
- newArr->alloc(nbOfTuples,nbOfComp);
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- std::set<std::string> vars;
- expr.getTrueSetOfVars(vars);
- if((int)vars.size()>1)
- {
- std::ostringstream oss; oss << "DataArrayDouble::applyFunc : this method works only with at most one var func expression ! If you need to map comps on variables please use applyFuncCompo or applyFuncNamedCompo instead ! Vars in expr are : ";
- std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- if(vars.empty())
- {
- expr.prepareFastEvaluator();
- newArr->rearrange(1);
- newArr->fillWithValue(expr.evaluateDouble());
- newArr->rearrange(nbOfComp);
- return newArr.retn();
- }
- std::vector<std::string> vars2(vars.begin(),vars.end());
- double buff,*ptrToFill(newArr->getPointer());
- const double *ptr(begin());
- std::vector<double> stck;
- expr.prepareExprEvaluationDouble(vars2,1,1,0,&buff,&buff+1);
- expr.prepareFastEvaluator();
- if(!isSafe)
- {
- for(int i=0;i<nbOfTuples;i++)
- {
- for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
- {
- buff=*ptr;
- expr.evaluateDoubleInternal(stck);
- *ptrToFill=stck.back();
- stck.pop_back();
- }
- }
- }
- else
- {
- for(int i=0;i<nbOfTuples;i++)
- {
- for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
- {
- buff=*ptr;
- try
- {
- expr.evaluateDoubleInternalSafe(stck);
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << "For tuple # " << i << " component # " << iComp << " with value (";
- oss << buff;
- oss << ") : Evaluation of function failed !" << e.what();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- *ptrToFill=stck.back();
- stck.pop_back();
- }
- }
- }
- return newArr.retn();
-}
-
-/*!
- * This method is a non const method that modify the array in \a this.
- * This method only works on one component array. It means that function \a func must
- * contain at most one variable.
- * This method is a specialization of applyFunc method with one parameter on one component array.
- *
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
- * If false the computation is carried on without any notification. When false the evaluation is a little faster.
- *
- * \sa applyFunc
- */
-void DataArrayDouble::applyFuncOnThis(const std::string& func, bool isSafe)
-{
- int nbOfComp(getNumberOfComponents());
- if(nbOfComp<=0)
- throw INTERP_KERNEL::Exception("DataArrayDouble::applyFuncOnThis : output number of component must be > 0 !");
- checkAllocated();
- int nbOfTuples(getNumberOfTuples());
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- std::set<std::string> vars;
- expr.getTrueSetOfVars(vars);
- if((int)vars.size()>1)
- {
- std::ostringstream oss; oss << "DataArrayDouble::applyFuncOnThis : this method works only with at most one var func expression ! If you need to map comps on variables please use applyFuncCompo or applyFuncNamedCompo instead ! Vars in expr are : ";
- std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- if(vars.empty())
- {
- expr.prepareFastEvaluator();
- std::vector<std::string> compInfo(getInfoOnComponents());
- rearrange(1);
- fillWithValue(expr.evaluateDouble());
- rearrange(nbOfComp);
- setInfoOnComponents(compInfo);
- return ;
- }
- std::vector<std::string> vars2(vars.begin(),vars.end());
- double buff,*ptrToFill(getPointer());
- const double *ptr(begin());
- std::vector<double> stck;
- expr.prepareExprEvaluationDouble(vars2,1,1,0,&buff,&buff+1);
- expr.prepareFastEvaluator();
- if(!isSafe)
- {
- for(int i=0;i<nbOfTuples;i++)
- {
- for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
- {
- buff=*ptr;
- expr.evaluateDoubleInternal(stck);
- *ptrToFill=stck.back();
- stck.pop_back();
- }
- }
- }
- else
- {
- for(int i=0;i<nbOfTuples;i++)
- {
- for(int iComp=0;iComp<nbOfComp;iComp++,ptr++,ptrToFill++)
- {
- buff=*ptr;
- try
- {
- expr.evaluateDoubleInternalSafe(stck);
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << "For tuple # " << i << " component # " << iComp << " with value (";
- oss << buff;
- oss << ") : Evaluation of function failed !" << e.what();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- *ptrToFill=stck.back();
- stck.pop_back();
- }
- }
- }
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying a function to every
- * tuple of \a this array. Textual data is not copied.
- * For more info see \ref MEDCouplingArrayApplyFunc2.
- * \param [in] nbOfComp - number of components in the result array.
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
- * If false the computation is carried on without any notification. When false the evaluation is a little faster.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples 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.
- * \throw If \a func contains vars that are not in \a this->getInfoOnComponent().
- * \throw If computing \a func fails.
- */
-DataArrayDouble *DataArrayDouble::applyFuncCompo(int nbOfComp, const std::string& func, bool isSafe) const
-{
- return applyFuncNamedCompo(nbOfComp,getVarsOnComponent(),func,isSafe);
-}
-
-/*!
- * Returns a new DataArrayDouble created from \a this one by applying a function to every
- * tuple of \a this array. Textual data is not copied.
- * For more info see \ref MEDCouplingArrayApplyFunc3.
- * \param [in] nbOfComp - number of components in the result array.
- * \param [in] varsOrder - sequence of vars defining their order.
- * \param [in] func - the expression defining how to transform a tuple of \a this array.
- * Supported expressions are described \ref MEDCouplingArrayApplyFuncExpr "here".
- * \param [in] isSafe - By default true. If true invalid operation (division by 0. acos of value > 1. ...) leads to a throw of an exception.
- * If false the computation is carried on without any notification. When false the evaluation is a little faster.
- * \return DataArrayDouble * - the new instance of DataArrayDouble containing the
- * same number of tuples 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.
- * \throw If \a func contains vars not in \a varsOrder.
- * \throw If computing \a func fails.
- */
-DataArrayDouble *DataArrayDouble::applyFuncNamedCompo(int nbOfComp, const std::vector<std::string>& varsOrder, const std::string& func, bool isSafe) const
-{
- if(nbOfComp<=0)
- throw INTERP_KERNEL::Exception("DataArrayDouble::applyFuncNamedCompo : output number of component must be > 0 !");
- std::vector<std::string> varsOrder2(varsOrder);
- int oldNbOfComp(getNumberOfComponents());
- for(int i=(int)varsOrder.size();i<oldNbOfComp;i++)
- varsOrder2.push_back(std::string());
- checkAllocated();
- int nbOfTuples(getNumberOfTuples());
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- std::set<std::string> vars;
- expr.getTrueSetOfVars(vars);
- if((int)vars.size()>oldNbOfComp)
- {
- std::ostringstream oss; oss << "The field has " << oldNbOfComp << " components and there are ";
- oss << vars.size() << " variables : ";
- std::copy(vars.begin(),vars.end(),std::ostream_iterator<std::string>(oss," "));
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- MCAuto<DataArrayDouble> newArr(DataArrayDouble::New());
- newArr->alloc(nbOfTuples,nbOfComp);
- INTERP_KERNEL::AutoPtr<double> buff(new double[oldNbOfComp]);
- double *buffPtr(buff),*ptrToFill;
- std::vector<double> stck;
- for(int iComp=0;iComp<nbOfComp;iComp++)
- {
- expr.prepareExprEvaluationDouble(varsOrder2,oldNbOfComp,nbOfComp,iComp,buffPtr,buffPtr+oldNbOfComp);
- expr.prepareFastEvaluator();
- const double *ptr(getConstPointer());
- ptrToFill=newArr->getPointer()+iComp;
- if(!isSafe)
- {
- for(int i=0;i<nbOfTuples;i++,ptrToFill+=nbOfComp,ptr+=oldNbOfComp)
- {
- std::copy(ptr,ptr+oldNbOfComp,buffPtr);
- expr.evaluateDoubleInternal(stck);
- *ptrToFill=stck.back();
- stck.pop_back();
- }
- }
- else
- {
- for(int i=0;i<nbOfTuples;i++,ptrToFill+=nbOfComp,ptr+=oldNbOfComp)
- {
- std::copy(ptr,ptr+oldNbOfComp,buffPtr);
- try
- {
- expr.evaluateDoubleInternalSafe(stck);
- *ptrToFill=stck.back();
- stck.pop_back();
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << "For tuple # " << i << " with value (";
- std::copy(ptr+oldNbOfComp*i,ptr+oldNbOfComp*(i+1),std::ostream_iterator<double>(oss,", "));
- oss << ") : Evaluation of function failed !" << e.what();
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- }
- return newArr.retn();
-}
-
-void DataArrayDouble::applyFuncFast32(const std::string& func)
-{
- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- char *funcStr=expr.compileX86();
- MYFUNCPTR funcPtr;
- *((void **)&funcPtr)=funcStr;//he he...
- //
- double *ptr=getPointer();
- int nbOfComp=getNumberOfComponents();
- int nbOfTuples=getNumberOfTuples();
- int nbOfElems=nbOfTuples*nbOfComp;
- for(int i=0;i<nbOfElems;i++,ptr++)
- *ptr=funcPtr(*ptr);
- declareAsNew();
-}
-
-void DataArrayDouble::applyFuncFast64(const std::string& func)
-{
- checkAllocated();
- INTERP_KERNEL::ExprParser expr(func);
- expr.parse();
- char *funcStr=expr.compileX86_64();
- MYFUNCPTR funcPtr;
- *((void **)&funcPtr)=funcStr;//he he...
- //
- double *ptr=getPointer();
- int nbOfComp=getNumberOfComponents();
- int nbOfTuples=getNumberOfTuples();
- int nbOfElems=nbOfTuples*nbOfComp;
- for(int i=0;i<nbOfElems;i++,ptr++)
- *ptr=funcPtr(*ptr);
- declareAsNew();
-}
-
-DataArrayDoubleIterator *DataArrayDouble::iterator()
-{
- return new DataArrayDoubleIterator(this);
-}
-
-/*!
- * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
- * array whose values are within a given range. Textual data is not copied.
- * \param [in] vmin - a lowest acceptable value (included).
- * \param [in] vmax - a greatest acceptable value (included).
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() != 1.
- *
- * \sa DataArrayDouble::findIdsNotInRange
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarraydouble_getidsinrange "Here is a C++ example".<br>
- * \ref py_mcdataarraydouble_getidsinrange "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayDouble::findIdsInRange(double vmin, double vmax) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::findIdsInRange : this must have exactly one component !");
- const double *cptr(begin());
- MCAuto<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
- int nbOfTuples(getNumberOfTuples());
- for(int i=0;i<nbOfTuples;i++,cptr++)
- if(*cptr>=vmin && *cptr<=vmax)
- ret->pushBackSilent(i);
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayInt contating indices of tuples of \a this one-dimensional
- * array whose values are not within a given range. Textual data is not copied.
- * \param [in] vmin - a lowest not acceptable value (excluded).
- * \param [in] vmax - a greatest not acceptable value (excluded).
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() != 1.
- *
- * \sa DataArrayDouble::findIdsInRange
- */
-DataArrayInt *DataArrayDouble::findIdsNotInRange(double vmin, double vmax) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::findIdsNotInRange : this must have exactly one component !");
- const double *cptr(begin());
- MCAuto<DataArrayInt> ret(DataArrayInt::New()); ret->alloc(0,1);
- int nbOfTuples(getNumberOfTuples());
- for(int i=0;i<nbOfTuples;i++,cptr++)
- if(*cptr<vmin || *cptr>vmax)
- ret->pushBackSilent(i);
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayDouble by concatenating two given arrays, so that (1) the number
- * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
- * the number of component in the result array is same as that of each of given arrays.
- * Info on components is copied from the first of the given arrays. Number of components
- * in the given arrays must be the same.
- * \param [in] a1 - an array to include in the result array.
- * \param [in] a2 - another array to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If both \a a1 and \a a2 are NULL.
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents().
- */
-DataArrayDouble *DataArrayDouble::Aggregate(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- std::vector<const DataArrayDouble *> tmp(2);
- tmp[0]=a1; tmp[1]=a2;
- return Aggregate(tmp);
-}
-
-/*!
- * Returns a new DataArrayDouble by concatenating all given arrays, so that (1) the number
- * of tuples in the result array is a sum of the number of tuples of given arrays and (2)
- * the number of component in the result array is same as that of each of given arrays.
- * Info on components is copied from the first of the given arrays. Number of components
- * in the given arrays must be the same.
- * If the number of non null of elements in \a arr is equal to one the returned object is a copy of it
- * not the object itself.
- * \param [in] arr - a sequence of arrays to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If all arrays within \a arr are NULL.
- * \throw If getNumberOfComponents() of arrays within \a arr.
- */
-DataArrayDouble *DataArrayDouble::Aggregate(const std::vector<const DataArrayDouble *>& arr)
-{
- std::vector<const DataArrayDouble *> a;
- for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
- if(*it4)
- a.push_back(*it4);
- if(a.empty())
- throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : input list must contain at least one NON EMPTY DataArrayDouble !");
- std::vector<const DataArrayDouble *>::const_iterator it=a.begin();
- int nbOfComp=(*it)->getNumberOfComponents();
- int nbt=(*it++)->getNumberOfTuples();
- for(int i=1;it!=a.end();it++,i++)
- {
- if((*it)->getNumberOfComponents()!=nbOfComp)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Aggregate : Nb of components mismatch for array aggregation !");
- nbt+=(*it)->getNumberOfTuples();
- }
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbt,nbOfComp);
- double *pt=ret->getPointer();
- for(it=a.begin();it!=a.end();it++)
- pt=std::copy((*it)->getConstPointer(),(*it)->getConstPointer()+(*it)->getNbOfElems(),pt);
- ret->copyStringInfoFrom(*(a[0]));
- return ret.retn();
-}
-
-/*!
- * Returns a new DataArrayDouble by aggregating two given arrays, so that (1) the number
- * of components in the result array is a sum of the number of components of given arrays
- * and (2) the number of tuples in the result array is same as that of each of given
- * arrays. In other words the i-th tuple of result array includes all components of
- * i-th tuples of all given arrays.
- * Number of tuples in the given arrays must be the same.
- * \param [in] a1 - an array to include in the result array.
- * \param [in] a2 - another array to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If both \a a1 and \a a2 are NULL.
- * \throw If any given array is not allocated.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- */
-DataArrayDouble *DataArrayDouble::Meld(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- std::vector<const DataArrayDouble *> arr(2);
- arr[0]=a1; arr[1]=a2;
- return Meld(arr);
-}
-
-/*!
- * Returns a new DataArrayDouble by aggregating all given arrays, so that (1) the number
- * of components in the result array is a sum of the number of components of given arrays
- * and (2) the number of tuples in the result array is same as that of each of given
- * arrays. In other words the i-th tuple of result array includes all components of
- * i-th tuples of all given arrays.
- * Number of tuples in the given arrays must be the same.
- * \param [in] arr - a sequence of arrays to include in the result array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If all arrays within \a arr are NULL.
- * \throw If any given array is not allocated.
- * \throw If getNumberOfTuples() of arrays within \a arr is different.
- */
-DataArrayDouble *DataArrayDouble::Meld(const std::vector<const DataArrayDouble *>& arr)
-{
- std::vector<const DataArrayDouble *> a;
- for(std::vector<const DataArrayDouble *>::const_iterator it4=arr.begin();it4!=arr.end();it4++)
- if(*it4)
- a.push_back(*it4);
- if(a.empty())
- throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : input list must contain at least one NON EMPTY DataArrayDouble !");
- std::vector<const DataArrayDouble *>::const_iterator it;
- for(it=a.begin();it!=a.end();it++)
- (*it)->checkAllocated();
- it=a.begin();
- int nbOfTuples=(*it)->getNumberOfTuples();
- std::vector<int> nbc(a.size());
- std::vector<const double *> pts(a.size());
- nbc[0]=(*it)->getNumberOfComponents();
- pts[0]=(*it++)->getConstPointer();
- for(int i=1;it!=a.end();it++,i++)
- {
- if(nbOfTuples!=(*it)->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("DataArrayDouble::Meld : mismatch of number of tuples !");
- nbc[i]=(*it)->getNumberOfComponents();
- pts[i]=(*it)->getConstPointer();
- }
- int totalNbOfComp=std::accumulate(nbc.begin(),nbc.end(),0);
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuples,totalNbOfComp);
- double *retPtr=ret->getPointer();
- for(int i=0;i<nbOfTuples;i++)
- for(int j=0;j<(int)a.size();j++)
- {
- retPtr=std::copy(pts[j],pts[j]+nbc[j],retPtr);
- pts[j]+=nbc[j];
- }
- int k=0;
- for(int i=0;i<(int)a.size();i++)
- for(int j=0;j<nbc[i];j++,k++)
- ret->setInfoOnComponent(k,a[i]->getInfoOnComponent(j));
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing a dot product of two given arrays, so that
- * the i-th tuple of the result array is a sum of products of j-th components of i-th
- * tuples of given arrays (\f$ a_i = \sum_{j=1}^n a1_j * a2_j \f$).
- * Info on components and name is copied from the first of the given arrays.
- * Number of tuples and components in the given arrays must be the same.
- * \param [in] a1 - a given array.
- * \param [in] a2 - another given array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If any given array is not allocated.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
- */
-DataArrayDouble *DataArrayDouble::Dot(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Dot : input DataArrayDouble instance is NULL !");
- a1->checkAllocated();
- a2->checkAllocated();
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Dot !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Dot !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,1);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- {
- double sum=0.;
- for(int j=0;j<nbOfComp;j++)
- sum+=a1Ptr[i*nbOfComp+j]*a2Ptr[i*nbOfComp+j];
- retPtr[i]=sum;
- }
- ret->setInfoOnComponent(0,a1->getInfoOnComponent(0));
- ret->setName(a1->getName());
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing a cross product of two given arrays, so that
- * the i-th tuple of the result array contains 3 components of a vector which is a cross
- * product of two vectors defined by the i-th tuples of given arrays.
- * Info on components is copied from the first of the given arrays.
- * Number of tuples in the given arrays must be the same.
- * Number of components in the given arrays must be 3.
- * \param [in] a1 - a given array.
- * \param [in] a2 - another given array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- * \throw If \a a1->getNumberOfComponents() != 3
- * \throw If \a a2->getNumberOfComponents() != 3
- */
-DataArrayDouble *DataArrayDouble::CrossProduct(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::CrossProduct : input DataArrayDouble instance is NULL !");
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array crossProduct !");
- if(nbOfComp!=3)
- throw INTERP_KERNEL::Exception("Nb of components must be equal to 3 for array crossProduct !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array crossProduct !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,3);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- {
- retPtr[3*i]=a1Ptr[3*i+1]*a2Ptr[3*i+2]-a1Ptr[3*i+2]*a2Ptr[3*i+1];
- retPtr[3*i+1]=a1Ptr[3*i+2]*a2Ptr[3*i]-a1Ptr[3*i]*a2Ptr[3*i+2];
- retPtr[3*i+2]=a1Ptr[3*i]*a2Ptr[3*i+1]-a1Ptr[3*i+1]*a2Ptr[3*i];
- }
- ret->copyStringInfoFrom(*a1);
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing maximal values of two given arrays.
- * Info on components is copied from the first of the given arrays.
- * Number of tuples and components in the given arrays must be the same.
- * \param [in] a1 - an array to compare values with another one.
- * \param [in] a2 - another array to compare values with the first one.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
- */
-DataArrayDouble *DataArrayDouble::Max(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Max : input DataArrayDouble instance is NULL !");
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Max !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Max !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- int nbElem=nbOfTuple*nbOfComp;
- for(int i=0;i<nbElem;i++)
- retPtr[i]=std::max(a1Ptr[i],a2Ptr[i]);
- ret->copyStringInfoFrom(*a1);
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble containing minimal values of two given arrays.
- * Info on components is copied from the first of the given arrays.
- * Number of tuples and components in the given arrays must be the same.
- * \param [in] a1 - an array to compare values with another one.
- * \param [in] a2 - another array to compare values with the first one.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- * \throw If \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents()
- */
-DataArrayDouble *DataArrayDouble::Min(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Min : input DataArrayDouble instance is NULL !");
- int nbOfComp=a1->getNumberOfComponents();
- if(nbOfComp!=a2->getNumberOfComponents())
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array min !");
- int nbOfTuple=a1->getNumberOfTuples();
- if(nbOfTuple!=a2->getNumberOfTuples())
- throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array min !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- double *retPtr=ret->getPointer();
- const double *a1Ptr=a1->getConstPointer();
- const double *a2Ptr=a2->getConstPointer();
- int nbElem=nbOfTuple*nbOfComp;
- for(int i=0;i<nbElem;i++)
- retPtr[i]=std::min(a1Ptr[i],a2Ptr[i]);
- ret->copyStringInfoFrom(*a1);
- return ret;
-}
-
-/*!
- * Returns a new DataArrayDouble that is a sum of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a sum of the corresponding values of \a a1 and \a a2,
- * i.e.: _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] + _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \param [in] a1 - an array to sum up.
- * \param [in] a2 - another array to sum up.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayDouble *DataArrayDouble::Add(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Add : input DataArrayDouble instance is NULL !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- MCAuto<DataArrayDouble> ret=0;
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<double>());
- ret->copyStringInfoFrom(*a1);
- }
- else
- {
- 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<nbOfTuple;i++)
- res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<double>(),aMinPtr[i]));
- ret->copyStringInfoFrom(*aMax);
- }
- else
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
- }
- }
- 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(nbOfTupleMax,nbOfComp);
- double *res=ret->getPointer();
- for(int i=0;i<nbOfTupleMax;i++)
- res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::plus<double>());
- 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 !");
- return ret.retn();
-}
-
-/*!
- * Adds values of another DataArrayDouble to values of \a this one. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a other array is added to the corresponding value of \a this array, i.e.:
- * _a_ [ i, j ] += _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _a_ [ i, j ] += _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _a_ [ i, j ] += _a2_ [ 0, j ].
- *
- * \param [in] other - an array to add to \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
- * \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
- * \a other has number of both tuples and components not equal to 1.
- */
-void DataArrayDouble::addEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::addEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::addEqual !";
- checkAllocated();
- other->checkAllocated();
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- std::transform(begin(),end(),other->begin(),getPointer(),std::plus<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::plus<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::plus<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble that is a subtraction of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a subtraction of the corresponding values of \a a1 and
- * \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] - _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \param [in] a1 - an array to subtract from.
- * \param [in] a2 - an array to subtract.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayDouble *DataArrayDouble::Substract(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Substract : input DataArrayDouble instance is NULL !");
- int nbOfTuple1=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp1=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- if(nbOfTuple2==nbOfTuple1)
- {
- if(nbOfComp1==nbOfComp2)
- {
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple2,nbOfComp1);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::minus<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else if(nbOfComp2==1)
- {
- MCAuto<DataArrayDouble> 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<nbOfTuple1;i++)
- res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::minus<double>(),a2Ptr[i]));
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- 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 !");
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
- double *pt=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::minus<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Substract !");//will always throw an exception
- return 0;
- }
-}
-
-/*!
- * Subtract values of another DataArrayDouble from values of \a this one. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a other array is subtracted from the corresponding value of \a this array, i.e.:
- * _a_ [ i, j ] -= _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _a_ [ i, j ] -= _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _a_ [ i, j ] -= _a2_ [ 0, j ].
- *
- * \param [in] other - an array to subtract from \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
- * \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
- * \a other has number of both tuples and components not equal to 1.
- */
-void DataArrayDouble::substractEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::substractEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::substractEqual !";
- checkAllocated();
- other->checkAllocated();
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- std::transform(begin(),end(),other->begin(),getPointer(),std::minus<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::minus<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::minus<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble that is a product of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a product of the corresponding values of \a a1 and
- * \a a2, i.e. _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] * _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \param [in] a1 - a factor array.
- * \param [in] a2 - another factor array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayDouble *DataArrayDouble::Multiply(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Multiply : input DataArrayDouble instance is NULL !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- MCAuto<DataArrayDouble> ret=0;
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::multiplies<double>());
- ret->copyStringInfoFrom(*a1);
- }
- else
- {
- 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<nbOfTuple;i++)
- res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::multiplies<double>(),aMinPtr[i]));
- ret->copyStringInfoFrom(*aMax);
- }
- else
- throw INTERP_KERNEL::Exception("Nb of components mismatch for array Multiply !");
- }
- }
- 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(nbOfTupleMax,nbOfComp);
- double *res=ret->getPointer();
- for(int i=0;i<nbOfTupleMax;i++)
- res=std::transform(aMaxPtr+i*nbOfComp,aMaxPtr+(i+1)*nbOfComp,aMinPtr,res,std::multiplies<double>());
- 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 !");
- return ret.retn();
-}
-
-/*!
- * Multiply values of another DataArrayDouble to values of \a this one. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a other array is multiplied to the corresponding value of \a this array, i.e.
- * _this_ [ i, j ] *= _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _this_ [ i, j ] *= _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _this_ [ i, j ] *= _a2_ [ 0, j ].
- *
- * \param [in] other - an array to multiply to \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
- * \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
- * \a other has number of both tuples and components not equal to 1.
- */
-void DataArrayDouble::multiplyEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::multiplyEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::multiplyEqual !";
- checkAllocated();
- other->checkAllocated();
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- std::transform(begin(),end(),other->begin(),getPointer(),std::multiplies<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::multiplies<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::multiplies<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble that is a division of two given arrays. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * the result array (_a_) is a division of the corresponding values of \a a1 and
- * \a a2, i.e.: _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, j ].
- * 2. The arrays have same number of tuples and one array, say _a2_, has one
- * component. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ i, 0 ].
- * 3. The arrays have same number of components and one array, say _a2_, has one
- * tuple. Then
- * _a_ [ i, j ] = _a1_ [ i, j ] / _a2_ [ 0, j ].
- *
- * Info on components is copied either from the first array (in the first case) or from
- * the array with maximal number of elements (getNbOfElems()).
- * \warning No check of division by zero is performed!
- * \param [in] a1 - a numerator array.
- * \param [in] a2 - a denominator array.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples() and
- * \a a1->getNumberOfComponents() != \a a2->getNumberOfComponents() and
- * none of them has number of tuples or components equal to 1.
- */
-DataArrayDouble *DataArrayDouble::Divide(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Divide : input DataArrayDouble instance is NULL !");
- int nbOfTuple1=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp1=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- if(nbOfTuple2==nbOfTuple1)
- {
- if(nbOfComp1==nbOfComp2)
- {
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple2,nbOfComp1);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::divides<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else if(nbOfComp2==1)
- {
- MCAuto<DataArrayDouble> 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<nbOfTuple1;i++)
- res=std::transform(a1Ptr+i*nbOfComp1,a1Ptr+(i+1)*nbOfComp1,res,std::bind2nd(std::divides<double>(),a2Ptr[i]));
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- 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 !");
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple1,nbOfComp1);
- const double *a1ptr=a1->getConstPointer(),*a2ptr=a2->getConstPointer();
- double *pt=ret->getPointer();
- for(int i=0;i<nbOfTuple1;i++)
- pt=std::transform(a1ptr+i*nbOfComp1,a1ptr+(i+1)*nbOfComp1,a2ptr,pt,std::divides<double>());
- ret->copyStringInfoFrom(*a1);
- return ret.retn();
- }
- else
- {
- a1->checkNbOfTuples(nbOfTuple2,"Nb of tuples mismatch for array Divide !");//will always throw an exception
- return 0;
- }
-}
-
-/*!
- * Divide values of \a this array by values of another DataArrayDouble. There are 3
- * valid cases.
- * 1. The arrays have same number of tuples and components. Then each value of
- * \a this array is divided by the corresponding value of \a other one, i.e.:
- * _a_ [ i, j ] /= _other_ [ i, j ].
- * 2. The arrays have same number of tuples and \a other array has one component. Then
- * _a_ [ i, j ] /= _other_ [ i, 0 ].
- * 3. The arrays have same number of components and \a other array has one tuple. Then
- * _a_ [ i, j ] /= _a2_ [ 0, j ].
- *
- * \warning No check of division by zero is performed!
- * \param [in] other - an array to divide \a this one by.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples() and
- * \a this->getNumberOfComponents() != \a other->getNumberOfComponents() and
- * \a other has number of both tuples and components not equal to 1.
- */
-void DataArrayDouble::divideEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::divideEqual : input DataArrayDouble instance is NULL !");
- const char *msg="Nb of tuples mismatch for DataArrayDouble::divideEqual !";
- checkAllocated();
- other->checkAllocated();
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple==nbOfTuple2)
- {
- if(nbOfComp==nbOfComp2)
- {
- std::transform(begin(),end(),other->begin(),getPointer(),std::divides<double>());
- }
- else if(nbOfComp2==1)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptr+i*nbOfComp,std::bind2nd(std::divides<double>(),*ptrc++));
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else if(nbOfTuple2==1)
- {
- if(nbOfComp2==nbOfComp)
- {
- double *ptr=getPointer();
- const double *ptrc=other->getConstPointer();
- for(int i=0;i<nbOfTuple;i++)
- std::transform(ptr+i*nbOfComp,ptr+(i+1)*nbOfComp,ptrc,ptr+i*nbOfComp,std::divides<double>());
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- }
- else
- throw INTERP_KERNEL::Exception(msg);
- declareAsNew();
-}
-
-/*!
- * Returns a new DataArrayDouble that is the result of pow of two given arrays. There are 3
- * valid cases.
- *
- * \param [in] a1 - an array to pow up.
- * \param [in] a2 - another array to sum up.
- * \return DataArrayDouble * - the new instance of DataArrayDouble.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If either \a a1 or \a a2 is NULL.
- * \throw If \a a1->getNumberOfTuples() != \a a2->getNumberOfTuples()
- * \throw If \a a1->getNumberOfComponents() != 1 or \a a2->getNumberOfComponents() != 1.
- * \throw If there is a negative value in \a a1.
- */
-DataArrayDouble *DataArrayDouble::Pow(const DataArrayDouble *a1, const DataArrayDouble *a2)
-{
- if(!a1 || !a2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : at least one of input instances is null !");
- int nbOfTuple=a1->getNumberOfTuples();
- int nbOfTuple2=a2->getNumberOfTuples();
- int nbOfComp=a1->getNumberOfComponents();
- int nbOfComp2=a2->getNumberOfComponents();
- if(nbOfTuple!=nbOfTuple2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of tuples mismatches !");
- if(nbOfComp!=1 || nbOfComp2!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::Pow : number of components of both arrays must be equal to 1 !");
- MCAuto<DataArrayDouble> ret=DataArrayDouble::New(); ret->alloc(nbOfTuple,1);
- const double *ptr1(a1->begin()),*ptr2(a2->begin());
- double *ptr=ret->getPointer();
- for(int i=0;i<nbOfTuple;i++,ptr1++,ptr2++,ptr++)
- {
- if(*ptr1>=0)
- {
- *ptr=pow(*ptr1,*ptr2);
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::Pow : on tuple #" << i << " of a1 value is < 0 (" << *ptr1 << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret.retn();
-}
-
-/*!
- * Apply pow on values of another DataArrayDouble to values of \a this one.
- *
- * \param [in] other - an array to pow to \a this one.
- * \throw If \a other is NULL.
- * \throw If \a this->getNumberOfTuples() != \a other->getNumberOfTuples()
- * \throw If \a this->getNumberOfComponents() != 1 or \a other->getNumberOfComponents() != 1
- * \throw If there is a negative value in \a this.
- */
-void DataArrayDouble::powEqual(const DataArrayDouble *other)
-{
- if(!other)
- throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : input instance is null !");
- int nbOfTuple=getNumberOfTuples();
- int nbOfTuple2=other->getNumberOfTuples();
- int nbOfComp=getNumberOfComponents();
- int nbOfComp2=other->getNumberOfComponents();
- if(nbOfTuple!=nbOfTuple2)
- throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of tuples mismatches !");
- if(nbOfComp!=1 || nbOfComp2!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::powEqual : number of components of both arrays must be equal to 1 !");
- double *ptr=getPointer();
- const double *ptrc=other->begin();
- for(int i=0;i<nbOfTuple;i++,ptrc++,ptr++)
- {
- if(*ptr>=0)
- *ptr=pow(*ptr,*ptrc);
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::powEqual : on tuple #" << i << " of this value is < 0 (" << *ptr << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- declareAsNew();
-}
-
-/*!
- * This method is \b NOT wrapped into python because it can be useful only for performance reasons in C++ context.
- * All values in \a this must be 0. or 1. within eps error. 0 means false, 1 means true.
- * If an another value than 0 or 1 appear (within eps precision) an INTERP_KERNEL::Exception will be thrown.
- *
- * \throw if \a this is not allocated.
- * \throw if \a this has not exactly one component.
- */
-std::vector<bool> DataArrayDouble::toVectorOfBool(double eps) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayDouble::toVectorOfBool : must be applied on single component array !");
- int nbt(getNumberOfTuples());
- std::vector<bool> ret(nbt);
- const double *pt(begin());
- for(int i=0;i<nbt;i++)
- {
- if(fabs(pt[i])<eps)
- ret[i]=false;
- else if(fabs(pt[i]-1.)<eps)
- ret[i]=true;
- else
- {
- std::ostringstream oss; oss << "DataArrayDouble::toVectorOfBool : the tuple #" << i << " has value " << pt[i] << " is invalid ! must be 0. or 1. !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret;
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- * Server side.
- */
-void DataArrayDouble::getTinySerializationIntInformation(std::vector<int>& tinyInfo) const
-{
- tinyInfo.resize(2);
- if(isAllocated())
- {
- tinyInfo[0]=getNumberOfTuples();
- tinyInfo[1]=getNumberOfComponents();
- }
- else
- {
- tinyInfo[0]=-1;
- tinyInfo[1]=-1;
- }
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- * Server side.
- */
-void DataArrayDouble::getTinySerializationStrInformation(std::vector<std::string>& tinyInfo) const
-{
- if(isAllocated())
- {
- int nbOfCompo=getNumberOfComponents();
- tinyInfo.resize(nbOfCompo+1);
- tinyInfo[0]=getName();
- for(int i=0;i<nbOfCompo;i++)
- tinyInfo[i+1]=getInfoOnComponent(i);
- }
- else
- {
- tinyInfo.resize(1);
- tinyInfo[0]=getName();
- }
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- * This method returns if a feeding is needed.
- */
-bool DataArrayDouble::resizeForUnserialization(const std::vector<int>& tinyInfoI)
-{
- int nbOfTuple=tinyInfoI[0];
- int nbOfComp=tinyInfoI[1];
- if(nbOfTuple!=-1 || nbOfComp!=-1)
- {
- alloc(nbOfTuple,nbOfComp);
- return true;
- }
- return false;
-}
-
-/*!
- * Useless method for end user. Only for MPI/Corba/File serialsation for multi arrays class.
- */
-void DataArrayDouble::finishUnserialization(const std::vector<int>& tinyInfoI, const std::vector<std::string>& tinyInfoS)
-{
- setName(tinyInfoS[0]);
- if(isAllocated())
- {
- int nbOfCompo=getNumberOfComponents();
- for(int i=0;i<nbOfCompo;i++)
- setInfoOnComponent(i,tinyInfoS[i+1]);
- }
-}
-
-DataArrayDoubleIterator::DataArrayDoubleIterator(DataArrayDouble *da):_da(da),_tuple_id(0),_nb_comp(0),_nb_tuple(0)
-{
- if(_da)
- {
- _da->incrRef();
- if(_da->isAllocated())
- {
- _nb_comp=da->getNumberOfComponents();
- _nb_tuple=da->getNumberOfTuples();
- _pt=da->getPointer();
- }
- }
-}
-
-DataArrayDoubleIterator::~DataArrayDoubleIterator()
-{
- if(_da)
- _da->decrRef();
-}
-
-DataArrayDoubleTuple *DataArrayDoubleIterator::nextt()
-{
- if(_tuple_id<_nb_tuple)
- {
- _tuple_id++;
- DataArrayDoubleTuple *ret=new DataArrayDoubleTuple(_pt,_nb_comp);
- _pt+=_nb_comp;
- return ret;
- }
- else
- return 0;
-}
-
-DataArrayDoubleTuple::DataArrayDoubleTuple(double *pt, int nbOfComp):_pt(pt),_nb_of_compo(nbOfComp)
-{
-}
-
-
-std::string DataArrayDoubleTuple::repr() const
-{
- std::ostringstream oss; oss.precision(17); oss << "(";
- for(int i=0;i<_nb_of_compo-1;i++)
- oss << _pt[i] << ", ";
- oss << _pt[_nb_of_compo-1] << ")";
- return oss.str();
-}
-
-double DataArrayDoubleTuple::doubleValue() const
-{
- if(_nb_of_compo==1)
- return *_pt;
- throw INTERP_KERNEL::Exception("DataArrayDoubleTuple::doubleValue : DataArrayDoubleTuple instance has not exactly 1 component -> Not possible to convert it into a double precision float !");
-}
-
-/*!
- * This method returns a newly allocated instance the caller should dealed with by a MEDCoupling::DataArrayDouble::decrRef.
- * This method performs \b no copy of data. The content is only referenced using MEDCoupling::DataArrayDouble::useArray with ownership set to \b false.
- * This method throws an INTERP_KERNEL::Exception is it is impossible to match sizes of \b this that is too say \b nbOfCompo=this->_nb_of_elem and \bnbOfTuples==1 or
- * \b nbOfCompo=1 and \bnbOfTuples==this->_nb_of_elem.
- */
-DataArrayDouble *DataArrayDoubleTuple::buildDADouble(int nbOfTuples, int nbOfCompo) const
-{
- if((_nb_of_compo==nbOfCompo && nbOfTuples==1) || (_nb_of_compo==nbOfTuples && nbOfCompo==1))
- {
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->useExternalArrayWithRWAccess(_pt,nbOfTuples,nbOfCompo);
- return ret;
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayDoubleTuple::buildDADouble : unable to build a requested DataArrayDouble instance with nbofTuple=" << nbOfTuples << " and nbOfCompo=" << nbOfCompo;
- oss << ".\nBecause the number of elements in this is " << _nb_of_compo << " !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
-}
-
-/*!
- * Returns a new instance of DataArrayInt. The caller is to delete this array
- * using decrRef() as it is no more needed.
- */
-DataArrayInt *DataArrayInt::New()
-{
- return new DataArrayInt;
-}
-
-/*!
- * Checks if raw data is allocated. Read more on the raw data
- * in \ref MEDCouplingArrayBasicsTuplesAndCompo "DataArrays infos" for more information.
- * \return bool - \a true if the raw data is allocated, \a false else.
- */
-bool DataArrayInt::isAllocated() const
-{
- return getConstPointer()!=0;
-}
-
-/*!
- * Checks if raw data is allocated and throws an exception if it is not the case.
- * \throw If the raw data is not allocated.
- */
-void DataArrayInt::checkAllocated() const
-{
- if(!isAllocated())
- throw INTERP_KERNEL::Exception("DataArrayInt::checkAllocated : Array is defined but not allocated ! Call alloc or setValues method first !");
-}
-
-/*!
- * This method desallocated \a this without modification of informations relative to the components.
- * After call of this method, DataArrayInt::isAllocated will return false.
- * If \a this is already not allocated, \a this is let unchanged.
- */
-void DataArrayInt::desallocate()
-{
- _mem.destroy();
-}
-
-std::size_t DataArrayInt::getHeapMemorySizeWithoutChildren() const
-{
- std::size_t sz(_mem.getNbOfElemAllocated());
- sz*=sizeof(int);
- return DataArray::getHeapMemorySizeWithoutChildren()+sz;
-}
-
-/*!
- * Returns the only one value in \a this, if and only if number of elements
- * (nb of tuples * nb of components) is equal to 1, and that \a this is allocated.
- * \return double - the sole value stored in \a this array.
- * \throw If at least one of conditions stated above is not fulfilled.
- */
-int DataArrayInt::intValue() const
-{
- if(isAllocated())
- {
- if(getNbOfElems()==1)
- {
- return *getConstPointer();
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is allocated but number of elements is not equal to 1 !");
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::intValue : DataArrayInt instance is not allocated !");
-}
-
-/*!
- * Returns an integer value characterizing \a this array, which is useful for a quick
- * comparison of many instances of DataArrayInt.
- * \return int - the hash value.
- * \throw If \a this is not allocated.
- */
-int DataArrayInt::getHashCode() const
-{
- checkAllocated();
- std::size_t nbOfElems=getNbOfElems();
- int ret=nbOfElems*65536;
- int delta=3;
- if(nbOfElems>48)
- delta=nbOfElems/8;
- int ret0=0;
- const int *pt=begin();
- for(std::size_t i=0;i<nbOfElems;i+=delta)
- ret0+=pt[i] & 0x1FFF;
- return ret+ret0;
-}
-
-/*!
- * Checks the number of tuples.
- * \return bool - \a true if getNumberOfTuples() == 0, \a false else.
- * \throw If \a this is not allocated.
- */
-bool DataArrayInt::empty() const
-{
- checkAllocated();
- return getNumberOfTuples()==0;
-}
-
-/*!
- * Returns a full copy of \a this. For more info on copying data arrays see
- * \ref MEDCouplingArrayBasicsCopyDeep.
- * \return DataArrayInt * - a new instance of DataArrayInt.
- */
-DataArrayInt *DataArrayInt::deepCopy() const
-{
- return new DataArrayInt(*this);
-}
-
-/*!
- * Returns either a \a deep or \a shallow copy of this array. For more info see
- * \ref MEDCouplingArrayBasicsCopyDeep and \ref MEDCouplingArrayBasicsCopyShallow.
- * \param [in] dCpy - if \a true, a deep copy is returned, else, a shallow one.
- * \return DataArrayInt * - either a new instance of DataArrayInt (if \a dCpy
- * == \a true) or \a this instance (if \a dCpy == \a false).
- */
-DataArrayInt *DataArrayInt::performCopyOrIncrRef(bool dCpy) const
-{
- if(dCpy)
- return deepCopy();
- else
- {
- incrRef();
- return const_cast<DataArrayInt *>(this);
- }
-}
-
-/*!
- * Copies all the data from another DataArrayInt. For more info see
- * \ref MEDCouplingArrayBasicsCopyDeepAssign.
- * \param [in] other - another instance of DataArrayInt to copy data from.
- * \throw If the \a other is not allocated.
- */
-void DataArrayInt::deepCopyFrom(const DataArrayInt& other)
-{
- other.checkAllocated();
- int nbOfTuples=other.getNumberOfTuples();
- int nbOfComp=other.getNumberOfComponents();
- allocIfNecessary(nbOfTuples,nbOfComp);
- std::size_t nbOfElems=(std::size_t)nbOfTuples*nbOfComp;
- int *pt=getPointer();
- const int *ptI=other.getConstPointer();
- for(std::size_t i=0;i<nbOfElems;i++)
- pt[i]=ptI[i];
- copyStringInfoFrom(other);
-}
-
-/*!
- * This method reserve nbOfElems elements in memory ( nbOfElems*4 bytes ) \b without impacting the number of tuples in \a this.
- * If \a this has already been allocated, this method checks that \a this has only one component. If not an INTERP_KERNEL::Exception will be thrown.
- * If \a this has not already been allocated, number of components is set to one.
- * This method allows to reduce number of reallocations on invokation of DataArrayInt::pushBackSilent and DataArrayInt::pushBackValsSilent on \a this.
- *
- * \sa DataArrayInt::pack, DataArrayInt::pushBackSilent, DataArrayInt::pushBackValsSilent
- */
-void DataArrayInt::reserve(std::size_t nbOfElems)
-{
- int nbCompo=getNumberOfComponents();
- if(nbCompo==1)
- {
- _mem.reserve(nbOfElems);
- }
- else if(nbCompo==0)
- {
- _mem.reserve(nbOfElems);
- _info_on_compo.resize(1);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::reserve : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * This method adds at the end of \a this the single value \a val. This method do \b not update its time label to avoid useless incrementation
- * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
- *
- * \param [in] val the value to be added in \a this
- * \throw If \a this has already been allocated with number of components different from one.
- * \sa DataArrayInt::pushBackValsSilent
- */
-void DataArrayInt::pushBackSilent(int val)
-{
- int nbCompo=getNumberOfComponents();
- if(nbCompo==1)
- _mem.pushBack(val);
- else if(nbCompo==0)
- {
- _info_on_compo.resize(1);
- _mem.pushBack(val);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::pushBackSilent : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * This method adds at the end of \a this a serie of values [\c valsBg,\c valsEnd). This method do \b not update its time label to avoid useless incrementation
- * of counter. So the caller is expected to call TimeLabel::declareAsNew on \a this at the end of the push session.
- *
- * \param [in] valsBg - an array of values to push at the end of \c this.
- * \param [in] valsEnd - specifies the end of the array \a valsBg, so that
- * the last value of \a valsBg is \a valsEnd[ -1 ].
- * \throw If \a this has already been allocated with number of components different from one.
- * \sa DataArrayInt::pushBackSilent
- */
-void DataArrayInt::pushBackValsSilent(const int *valsBg, const int *valsEnd)
-{
- int nbCompo=getNumberOfComponents();
- if(nbCompo==1)
- _mem.insertAtTheEnd(valsBg,valsEnd);
- else if(nbCompo==0)
- {
- _info_on_compo.resize(1);
- _mem.insertAtTheEnd(valsBg,valsEnd);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::pushBackValsSilent : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * This method returns silently ( without updating time label in \a this ) the last value, if any and suppress it.
- * \throw If \a this is already empty.
- * \throw If \a this has number of components different from one.
- */
-int DataArrayInt::popBackSilent()
-{
- if(getNumberOfComponents()==1)
- return _mem.popBack();
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::popBackSilent : not available for DataArrayInt with number of components different than 1 !");
-}
-
-/*!
- * This method \b do \b not modify content of \a this. It only modify its memory footprint if the allocated memory is to high regarding real data to store.
- *
- * \sa DataArrayInt::getHeapMemorySizeWithoutChildren, DataArrayInt::reserve
- */
-void DataArrayInt::pack() const
-{
- _mem.pack();
-}
-
-/*!
- * Allocates the raw data in memory. If exactly as same memory as needed already
- * allocated, it is not re-allocated.
- * \param [in] nbOfTuple - number of tuples of data to allocate.
- * \param [in] nbOfCompo - number of components of data to allocate.
- * \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
- */
-void DataArrayInt::allocIfNecessary(int nbOfTuple, int nbOfCompo)
-{
- if(isAllocated())
- {
- if(nbOfTuple!=getNumberOfTuples() || nbOfCompo!=getNumberOfComponents())
- alloc(nbOfTuple,nbOfCompo);
- }
- else
- alloc(nbOfTuple,nbOfCompo);
-}
-
-/*!
- * Allocates the raw data in memory. If the memory was already allocated, then it is
- * freed and re-allocated. See an example of this method use
- * \ref MEDCouplingArraySteps1WC "here".
- * \param [in] nbOfTuple - number of tuples of data to allocate.
- * \param [in] nbOfCompo - number of components of data to allocate.
- * \throw If \a nbOfTuple < 0 or \a nbOfCompo < 0.
- */
-void DataArrayInt::alloc(int nbOfTuple, int nbOfCompo)
-{
- if(nbOfTuple<0 || nbOfCompo<0)
- throw INTERP_KERNEL::Exception("DataArrayInt::alloc : request for negative length of data !");
- _info_on_compo.resize(nbOfCompo);
- _mem.alloc(nbOfCompo*(std::size_t)nbOfTuple);
- declareAsNew();
-}
-
-/*!
- * Assign zero to all values in \a this array. To know more on filling arrays see
- * \ref MEDCouplingArrayFill.
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::fillWithZero()
-{
- checkAllocated();
- _mem.fillWithValue(0);
- declareAsNew();
-}
-
-/*!
- * Assign \a val to all values in \a this array. To know more on filling arrays see
- * \ref MEDCouplingArrayFill.
- * \param [in] val - the value to fill with.
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::fillWithValue(int val)
-{
- checkAllocated();
- _mem.fillWithValue(val);
- declareAsNew();
-}
-
-/*!
- * Set all values in \a this array so that the i-th element equals to \a init + i
- * (i starts from zero). To know more on filling arrays see \ref MEDCouplingArrayFill.
- * \param [in] init - value to assign to the first element of array.
- * \throw If \a this->getNumberOfComponents() != 1
- * \throw If \a this is not allocated.
- */
-void DataArrayInt::iota(int init)
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::iota : works only for arrays with only one component, you can call 'rearrange' method before !");
- int *ptr=getPointer();
- int ntuples=getNumberOfTuples();
- for(int i=0;i<ntuples;i++)
- ptr[i]=init+i;
- declareAsNew();
-}
-
-/*!
- * Returns a textual and human readable representation of \a this instance of
- * DataArrayInt. This text is shown when a DataArrayInt is printed in Python.
- * \return std::string - text describing \a this DataArrayInt.
- *
- * \sa reprNotTooLong, reprZip
- */
-std::string DataArrayInt::repr() const
-{
- std::ostringstream ret;
- reprStream(ret);
- return ret.str();
-}
-
-std::string DataArrayInt::reprZip() const
-{
- std::ostringstream ret;
- reprZipStream(ret);
- return ret.str();
-}
-
-/*!
- * This method is close to repr method except that when \a this has more than 1000 tuples, all tuples are not
- * printed out to avoid to consume too much space in interpretor.
- * \sa repr
- */
-std::string DataArrayInt::reprNotTooLong() const
-{
- std::ostringstream ret;
- reprNotTooLongStream(ret);
- return ret.str();
-}
-
-void DataArrayInt::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const
-{
- static const char SPACE[4]={' ',' ',' ',' '};
- checkAllocated();
- std::string idt(indent,' ');
- ofs << idt << "<DataArray type=\"" << type << "\" Name=\"" << nameInFile << "\" NumberOfComponents=\"" << getNumberOfComponents() << "\"";
- if(byteArr)
- {
- ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">";
- if(std::string(type)=="Int32")
- {
- const char *data(reinterpret_cast<const char *>(begin()));
- std::size_t sz(getNbOfElems()*sizeof(int));
- byteArr->insertAtTheEnd(data,data+sz);
- byteArr->insertAtTheEnd(SPACE,SPACE+4);
- }
- else if(std::string(type)=="Int8")
- {
- INTERP_KERNEL::AutoPtr<char> tmp(new char[getNbOfElems()]);
- std::copy(begin(),end(),(char *)tmp);
- byteArr->insertAtTheEnd((char *)tmp,(char *)tmp+getNbOfElems());
- byteArr->insertAtTheEnd(SPACE,SPACE+4);
- }
- else if(std::string(type)=="UInt8")
- {
- INTERP_KERNEL::AutoPtr<unsigned char> tmp(new unsigned char[getNbOfElems()]);
- std::copy(begin(),end(),(unsigned char *)tmp);
- byteArr->insertAtTheEnd((unsigned char *)tmp,(unsigned char *)tmp+getNbOfElems());
- byteArr->insertAtTheEnd(SPACE,SPACE+4);
- }
- else
- throw INTERP_KERNEL::Exception("DataArrayInt::writeVTK : Only Int32, Int8 and UInt8 supported !");
- }
- else
- {
- ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt;
- std::copy(begin(),end(),std::ostream_iterator<int>(ofs," "));
- }
- ofs << std::endl << idt << "</DataArray>\n";
-}
-
-void DataArrayInt::reprStream(std::ostream& stream) const
-{
- stream << "Name of int array : \"" << _name << "\"\n";
- reprWithoutNameStream(stream);
-}
-
-void DataArrayInt::reprZipStream(std::ostream& stream) const
-{
- stream << "Name of int array : \"" << _name << "\"\n";
- reprZipWithoutNameStream(stream);
-}
-
-void DataArrayInt::reprNotTooLongStream(std::ostream& stream) const
-{
- stream << "Name of int array : \"" << _name << "\"\n";
- reprNotTooLongWithoutNameStream(stream);
-}
-
-void DataArrayInt::reprWithoutNameStream(std::ostream& stream) const
-{
- DataArray::reprWithoutNameStream(stream);
- _mem.repr(getNumberOfComponents(),stream);
-}
-
-void DataArrayInt::reprZipWithoutNameStream(std::ostream& stream) const
-{
- DataArray::reprWithoutNameStream(stream);
- _mem.reprZip(getNumberOfComponents(),stream);
-}
-
-void DataArrayInt::reprNotTooLongWithoutNameStream(std::ostream& stream) const
-{
- DataArray::reprWithoutNameStream(stream);
- stream.precision(17);
- _mem.reprNotTooLong(getNumberOfComponents(),stream);
-}
-
-void DataArrayInt::reprCppStream(const std::string& varName, std::ostream& stream) const
-{
- int nbTuples=getNumberOfTuples(),nbComp=getNumberOfComponents();
- const int *data=getConstPointer();
- stream << "DataArrayInt *" << varName << "=DataArrayInt::New();" << std::endl;
- if(nbTuples*nbComp>=1)
- {
- stream << "const int " << varName << "Data[" << nbTuples*nbComp << "]={";
- std::copy(data,data+nbTuples*nbComp-1,std::ostream_iterator<int>(stream,","));
- stream << data[nbTuples*nbComp-1] << "};" << std::endl;
- stream << varName << "->useArray(" << varName << "Data,false,CPP_DEALLOC," << nbTuples << "," << nbComp << ");" << std::endl;
- }
- else
- stream << varName << "->alloc(" << nbTuples << "," << nbComp << ");" << std::endl;
- stream << varName << "->setName(\"" << getName() << "\");" << std::endl;
-}
-
-/*!
- * Method that gives a quick overvien of \a this for python.
- */
-void DataArrayInt::reprQuickOverview(std::ostream& stream) const
-{
- static const std::size_t MAX_NB_OF_BYTE_IN_REPR=300;
- stream << "DataArrayInt C++ instance at " << this << ". ";
- if(isAllocated())
- {
- int nbOfCompo=(int)_info_on_compo.size();
- if(nbOfCompo>=1)
- {
- int nbOfTuples=getNumberOfTuples();
- stream << "Number of tuples : " << nbOfTuples << ". Number of components : " << nbOfCompo << "." << std::endl;
- reprQuickOverviewData(stream,MAX_NB_OF_BYTE_IN_REPR);
- }
- else
- stream << "Number of components : 0.";
- }
- else
- stream << "*** No data allocated ****";
-}
-
-void DataArrayInt::reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const
-{
- const int *data=begin();
- int nbOfTuples=getNumberOfTuples();
- int nbOfCompo=(int)_info_on_compo.size();
- std::ostringstream oss2; oss2 << "[";
- std::string oss2Str(oss2.str());
- bool isFinished=true;
- for(int i=0;i<nbOfTuples && isFinished;i++)
- {
- if(nbOfCompo>1)
- {
- oss2 << "(";
- for(int j=0;j<nbOfCompo;j++,data++)
- {
- oss2 << *data;
- if(j!=nbOfCompo-1) oss2 << ", ";
- }
- oss2 << ")";
- }
- else
- oss2 << *data++;
- if(i!=nbOfTuples-1) oss2 << ", ";
- std::string oss3Str(oss2.str());
- if(oss3Str.length()<maxNbOfByteInRepr)
- oss2Str=oss3Str;
- else
- isFinished=false;
- }
- stream << oss2Str;
- if(!isFinished)
- stream << "... ";
- stream << "]";
-}
-
-/*!
- * Modifies in place \a this one-dimensional array so that each value \a v = \a indArrBg[ \a v ],
- * i.e. a current value is used as in index to get a new value from \a indArrBg.
- * \param [in] indArrBg - pointer to the first element of array of new values to assign
- * to \a this array.
- * \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
- * the last value of \a indArrBg is \a indArrEnd[ -1 ].
- * \throw If \a this->getNumberOfComponents() != 1
- * \throw If any value of \a this can't be used as a valid index for
- * [\a indArrBg, \a indArrEnd).
- *
- * \sa changeValue
- */
-void DataArrayInt::transformWithIndArr(const int *indArrBg, const int *indArrEnd)
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("Call transformWithIndArr method on DataArrayInt with only one component, you can call 'rearrange' method before !");
- int nbElemsIn((int)std::distance(indArrBg,indArrEnd)),nbOfTuples(getNumberOfTuples()),*pt(getPointer());
- for(int i=0;i<nbOfTuples;i++,pt++)
- {
- if(*pt>=0 && *pt<nbElemsIn)
- *pt=indArrBg[*pt];
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::transformWithIndArr : error on tuple #" << i << " of this value is " << *pt << ", should be in [0," << nbElemsIn << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- declareAsNew();
-}
-
-/*!
- * Computes distribution of values of \a this one-dimensional array between given value
- * ranges (casts). This method is typically useful for entity number spliting by types,
- * for example.
- * \warning The values contained in \a arrBg should be sorted ascendently. No
- * check of this is be done. If not, the result is not warranted.
- * \param [in] arrBg - the array of ascending values defining the value ranges. The i-th
- * value of \a arrBg (\a arrBg[ i ]) gives the lowest value of the i-th range,
- * and the greatest value of the i-th range equals to \a arrBg[ i+1 ] - 1. \a
- * arrBg containing \a n values defines \a n-1 ranges. The last value of \a arrBg
- * should be more than every value in \a this array.
- * \param [in] arrEnd - specifies the end of the array \a arrBg, so that
- * the last value of \a arrBg is \a arrEnd[ -1 ].
- * \param [out] castArr - a new instance of DataArrayInt, of same size as \a this array
- * (same number of tuples and components), the caller is to delete
- * using decrRef() as it is no more needed.
- * This array contains indices of ranges for every value of \a this array. I.e.
- * the i-th value of \a castArr gives the index of range the i-th value of \a this
- * belongs to. Or, in other words, this parameter contains for each tuple in \a
- * this in which cast it holds.
- * \param [out] rankInsideCast - a new instance of DataArrayInt, of same size as \a this
- * array, the caller is to delete using decrRef() as it is no more needed.
- * This array contains ranks of values of \a this array within ranges
- * they belongs to. I.e. the i-th value of \a rankInsideCast gives the rank of
- * the i-th value of \a this array within the \a castArr[ i ]-th range, to which
- * the i-th value of \a this belongs to. Or, in other words, this param contains
- * for each tuple its rank inside its cast. The rank is computed as difference
- * between the value and the lowest value of range.
- * \param [out] castsPresent - a new instance of DataArrayInt, containing indices of
- * ranges (casts) to which at least one value of \a this array belongs.
- * Or, in other words, this param contains the casts that \a this contains.
- * The caller is to delete this array using decrRef() as it is no more needed.
- *
- * \b Example: If \a this contains [6,5,0,3,2,7,8,1,4] and \a arrBg contains [0,4,9] then
- * the output of this method will be :
- * - \a castArr : [1,1,0,0,0,1,1,0,1]
- * - \a rankInsideCast: [2,1,0,3,2,3,4,1,0]
- * - \a castsPresent : [0,1]
- *
- * I.e. values of \a this array belong to 2 ranges: #0 and #1. Value 6 belongs to the
- * range #1 and its rank within this range is 2; etc.
- *
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If \a arrEnd - arrBg < 2.
- * \throw If any value of \a this is not less than \a arrEnd[-1].
- */
-void DataArrayInt::splitByValueRange(const int *arrBg, const int *arrEnd,
- DataArrayInt *& castArr, DataArrayInt *& rankInsideCast, DataArrayInt *& castsPresent) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("Call splitByValueRange method on DataArrayInt with only one component, you can call 'rearrange' method before !");
- int nbOfTuples=getNumberOfTuples();
- std::size_t nbOfCast=std::distance(arrBg,arrEnd);
- if(nbOfCast<2)
- throw INTERP_KERNEL::Exception("DataArrayInt::splitByValueRange : The input array giving the cast range values should be of size >=2 !");
- nbOfCast--;
- const int *work=getConstPointer();
- typedef std::reverse_iterator<const int *> rintstart;
- rintstart bg(arrEnd);//OK no problem because size of 'arr' is greater or equal 2
- rintstart end2(arrBg);
- MCAuto<DataArrayInt> ret1=DataArrayInt::New();
- MCAuto<DataArrayInt> ret2=DataArrayInt::New();
- MCAuto<DataArrayInt> ret3=DataArrayInt::New();
- ret1->alloc(nbOfTuples,1);
- ret2->alloc(nbOfTuples,1);
- int *ret1Ptr=ret1->getPointer();
- int *ret2Ptr=ret2->getPointer();
- std::set<std::size_t> castsDetected;
- for(int i=0;i<nbOfTuples;i++)
- {
- rintstart res=std::find_if(bg,end2,std::bind2nd(std::less_equal<int>(), work[i]));
- std::size_t pos=std::distance(bg,res);
- std::size_t pos2=nbOfCast-pos;
- if(pos2<nbOfCast)
- {
- ret1Ptr[i]=(int)pos2;
- ret2Ptr[i]=work[i]-arrBg[pos2];
- castsDetected.insert(pos2);
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::splitByValueRange : At rank #" << i << " the value is " << work[i] << " should be in [0," << *bg << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- ret3->alloc((int)castsDetected.size(),1);
- std::copy(castsDetected.begin(),castsDetected.end(),ret3->getPointer());
- castArr=ret1.retn();
- rankInsideCast=ret2.retn();
- castsPresent=ret3.retn();
-}
-
-/*!
- * This method look at \a this if it can be considered as a range defined by the 3-tuple ( \a strt , \a sttoopp , \a stteepp ).
- * If false is returned the tuple must be ignored. If true is returned \a this can be considered by a range( \a strt , \a sttoopp , \a stteepp ).
- * This method works only if \a this is allocated and single component. If not an exception will be thrown.
- *
- * \param [out] strt - the start of the range (included) if true is returned.
- * \param [out] sttoopp - the end of the range (not included) if true is returned.
- * \param [out] stteepp - the step of the range if true is returned.
- * \return the verdict of the check.
- *
- * \sa DataArray::GetNumberOfItemGivenBES
- */
-bool DataArrayInt::isRange(int& strt, int& sttoopp, int& stteepp) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::isRange : this must be single component array !");
- int nbTuples(getNumberOfTuples());
- if(nbTuples==0)
- { strt=0; sttoopp=0; stteepp=1; return true; }
- const int *pt(begin());
- strt=*pt;
- if(nbTuples==1)
- { sttoopp=strt+1; stteepp=1; return true; }
- strt=*pt; sttoopp=pt[nbTuples-1];
- if(strt==sttoopp)
- return false;
- if(sttoopp>strt)
- {
- sttoopp++;
- int a(sttoopp-1-strt),tmp(strt);
- if(a%(nbTuples-1)!=0)
- return false;
- stteepp=a/(nbTuples-1);
- for(int i=0;i<nbTuples;i++,tmp+=stteepp)
- if(pt[i]!=tmp)
- return false;
- return true;
- }
- else
- {
- sttoopp--;
- int a(strt-sttoopp-1),tmp(strt);
- if(a%(nbTuples-1)!=0)
- return false;
- stteepp=-(a/(nbTuples-1));
- for(int i=0;i<nbTuples;i++,tmp+=stteepp)
- if(pt[i]!=tmp)
- return false;
- return true;
- }
-}
-
-/*!
- * Creates a one-dimensional DataArrayInt (\a res) whose contents are computed from
- * values of \a this (\a a) and the given (\a indArr) arrays as follows:
- * \a res[ \a indArr[ \a a[ i ]]] = i. I.e. for each value in place i \a v = \a a[ i ],
- * new value in place \a indArr[ \a v ] is i.
- * \param [in] indArrBg - the array holding indices within the result array to assign
- * indices of values of \a this array pointing to values of \a indArrBg.
- * \param [in] indArrEnd - specifies the end of the array \a indArrBg, so that
- * the last value of \a indArrBg is \a indArrEnd[ -1 ].
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- * \throw If \a this->getNumberOfComponents() != 1.
- * \throw If any value of \a this array is not a valid index for \a indArrBg array.
- * \throw If any value of \a indArrBg is not a valid index for \a this array.
- */
-DataArrayInt *DataArrayInt::transformWithIndArrR(const int *indArrBg, const int *indArrEnd) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("Call transformWithIndArrR method on DataArrayInt with only one component, you can call 'rearrange' method before !");
- int nbElemsIn=(int)std::distance(indArrBg,indArrEnd);
- int nbOfTuples=getNumberOfTuples();
- const int *pt=getConstPointer();
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuples,1);
- ret->fillWithValue(-1);
- int *tmp=ret->getPointer();
- for(int i=0;i<nbOfTuples;i++,pt++)
- {
- if(*pt>=0 && *pt<nbElemsIn)
- {
- int pos=indArrBg[*pt];
- if(pos>=0 && pos<nbOfTuples)
- tmp[pos]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value of new pos is " << pos << " ( indArrBg[" << *pt << "]) ! Should be in [0," << nbOfTuples << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::transformWithIndArrR : error on tuple #" << i << " value is " << *pt << " and indirectionnal array as a size equal to " << nbElemsIn << " !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret.retn();
-}
-
-/*!
- * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
- * from values of \a this array, which is supposed to contain a renumbering map in
- * "Old to New" mode. The result array contains a renumbering map in "New to Old" mode.
- * To know how to use the renumbering maps see \ref numbering.
- * \param [in] newNbOfElem - the number of tuples in the result array.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarrayint_invertarrayo2n2n2o "Here is a C++ example".<br>
- * \ref py_mcdataarrayint_invertarrayo2n2n2o "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayInt::invertArrayO2N2N2O(int newNbOfElem) const
-{
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(newNbOfElem,1);
- int nbOfOldNodes=getNumberOfTuples();
- const int *old2New=getConstPointer();
- int *pt=ret->getPointer();
- for(int i=0;i!=nbOfOldNodes;i++)
- {
- int newp(old2New[i]);
- if(newp!=-1)
- {
- if(newp>=0 && newp<newNbOfElem)
- pt[newp]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2O : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- return ret.retn();
-}
-
-/*!
- * This method is similar to DataArrayInt::invertArrayO2N2N2O except that
- * Example : If \a this contains [0,1,2,0,3,4,5,4,6,4] this method will return [0,1,2,4,5,6,8] whereas DataArrayInt::invertArrayO2N2N2O returns [3,1,2,4,9,6,8]
- */
-DataArrayInt *DataArrayInt::invertArrayO2N2N2OBis(int newNbOfElem) const
-{
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(newNbOfElem,1);
- int nbOfOldNodes=getNumberOfTuples();
- const int *old2New=getConstPointer();
- int *pt=ret->getPointer();
- for(int i=nbOfOldNodes-1;i>=0;i--)
- {
- int newp(old2New[i]);
- if(newp!=-1)
- {
- if(newp>=0 && newp<newNbOfElem)
- pt[newp]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::invertArrayO2N2N2OBis : At place #" << i << " the newplace is " << newp << " must be in [0," << newNbOfElem << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- }
- return ret.retn();
-}
-
-/*!
- * Creates a one-dimensional DataArrayInt of given length, whose contents are computed
- * from values of \a this array, which is supposed to contain a renumbering map in
- * "New to Old" mode. The result array contains a renumbering map in "Old to New" mode.
- * To know how to use the renumbering maps see \ref numbering.
- * \param [in] newNbOfElem - the number of tuples in the result array.
- * \return DataArrayInt * - the new instance of DataArrayInt.
- * The caller is to delete this result array using decrRef() as it is no more
- * needed.
- *
- * \if ENABLE_EXAMPLES
- * \ref cpp_mcdataarrayint_invertarrayn2o2o2n "Here is a C++ example".
- *
- * \ref py_mcdataarrayint_invertarrayn2o2o2n "Here is a Python example".
- * \endif
- */
-DataArrayInt *DataArrayInt::invertArrayN2O2O2N(int oldNbOfElem) const
-{
- checkAllocated();
- MCAuto<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(oldNbOfElem,1);
- const int *new2Old=getConstPointer();
- int *pt=ret->getPointer();
- std::fill(pt,pt+oldNbOfElem,-1);
- int nbOfNewElems=getNumberOfTuples();
- for(int i=0;i<nbOfNewElems;i++)
- {
- int v(new2Old[i]);
- if(v>=0 && v<oldNbOfElem)
- pt[v]=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::invertArrayN2O2O2N : in new id #" << i << " old value is " << v << " expected to be in [0," << oldNbOfElem << ") !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- return ret.retn();
-}
-
-/*!
- * Equivalent to DataArrayInt::isEqual except that if false the reason of
- * mismatch is given.
- *
- * \param [in] other the instance to be compared with \a this
- * \param [out] reason In case of inequality returns the reason.
- * \sa DataArrayInt::isEqual
- */
-bool DataArrayInt::isEqualIfNotWhy(const DataArrayInt& other, std::string& reason) const
-{
- if(!areInfoEqualsIfNotWhy(other,reason))
- return false;
- return _mem.isEqual(other._mem,0,reason);
-}
-
-/*!
- * Checks if \a this and another DataArrayInt are fully equal. For more info see
- * \ref MEDCouplingArrayBasicsCompare.
- * \param [in] other - an instance of DataArrayInt to compare with \a this one.
- * \return bool - \a true if the two arrays are equal, \a false else.
- */
-bool DataArrayInt::isEqual(const DataArrayInt& other) const
-{
- std::string tmp;
- return isEqualIfNotWhy(other,tmp);
-}
-
-/*!
- * Checks if values of \a this and another DataArrayInt are equal. For more info see
- * \ref MEDCouplingArrayBasicsCompare.
- * \param [in] other - an instance of DataArrayInt to compare with \a this one.
- * \return bool - \a true if the values of two arrays are equal, \a false else.
- */
-bool DataArrayInt::isEqualWithoutConsideringStr(const DataArrayInt& other) const
-{
- std::string tmp;
- return _mem.isEqual(other._mem,0,tmp);
-}
-
-/*!
- * Checks if values of \a this and another DataArrayInt are equal. Comparison is
- * performed on sorted value sequences.
- * For more info see\ref MEDCouplingArrayBasicsCompare.
- * \param [in] other - an instance of DataArrayInt to compare with \a this one.
- * \return bool - \a true if the sorted values of two arrays are equal, \a false else.
- */
-bool DataArrayInt::isEqualWithoutConsideringStrAndOrder(const DataArrayInt& other) const
-{
- MCAuto<DataArrayInt> a=deepCopy();
- MCAuto<DataArrayInt> b=other.deepCopy();
- a->sort();
- b->sort();
- return a->isEqualWithoutConsideringStr(*b);
-}
-
-/*!
- * This method compares content of input vector \a v and \a this.
- * If for each id in \a this v[id]==True and for all other ids id2 not in \a this v[id2]==False, true is returned.
- * For performance reasons \a this is expected to be sorted ascendingly. If not an exception will be thrown.
- *
- * \param [in] v - the vector of 'flags' to be compared with \a this.
- *
- * \throw If \a this is not sorted ascendingly.
- * \throw If \a this has not exactly one component.
- * \throw If \a this is not allocated.
- */
-bool DataArrayInt::isFittingWith(const std::vector<bool>& v) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::isFittingWith : number of components of this should be equal to one !");
- const int *w(begin()),*end2(end());
- int refVal=-std::numeric_limits<int>::max();
- int i=0;
- std::vector<bool>::const_iterator it(v.begin());
- for(;it!=v.end();it++,i++)
- {
- if(*it)
- {
- if(w!=end2)
- {
- if(*w++==i)
- {
- if(i>refVal)
- refVal=i;
- else
- {
- std::ostringstream oss; oss << "DataArrayInt::isFittingWith : At pos #" << std::distance(begin(),w-1) << " this is not sorted ascendingly !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- }
- else
- return false;
- }
- else
- return false;
- }
- }
- return w==end2;
-}
-
-/*!
- * This method assumes that \a this has one component and is allocated. This method scans all tuples in \a this and for all tuple equal to \a val
- * put True to the corresponding entry in \a vec.
- * \a vec is expected to be with the same size than the number of tuples of \a this.
- */
-void DataArrayInt::switchOnTupleEqualTo(int val, std::vector<bool>& vec) const
-{
- checkAllocated();
- if(getNumberOfComponents()!=1)
- throw INTERP_KERNEL::Exception("DataArrayInt::switchOnTupleEqualTo : number of components of this should be equal to one !");