DataArrayDouble *DataArrayDouble::Add(const DataArrayDouble *a1, const DataArrayDouble *a2) throw(INTERP_KERNEL::Exception)
{
- int nbOfTuple=a2->getNumberOfTuples();
- int nbOfComp=a2->getNumberOfComponents();
- a1->checkNbOfTuplesAndComp(nbOfTuple,nbOfComp,"Nb of components mismatch for array Add !");
- DataArrayDouble *ret=DataArrayDouble::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<double>());
- ret->copyStringInfoFrom(*a1);
+ int nbOfTuple=a1->getNumberOfTuples();
+ int nbOfTuple2=a2->getNumberOfTuples();
+ int nbOfComp=a1->getNumberOfComponents();
+ int nbOfComp2=a2->getNumberOfComponents();
+ if(nbOfTuple!=nbOfTuple2)
+ throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
+ DataArrayDouble *ret=0;
+ if(nbOfComp==nbOfComp2)
+ {
+ ret=DataArrayDouble::New();
+ ret->alloc(nbOfTuple,nbOfComp);
+ std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<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 !");
+ }
return ret;
}
DataArrayInt *DataArrayInt::Add(const DataArrayInt *a1, const DataArrayInt *a2) throw(INTERP_KERNEL::Exception)
{
- int nbOfTuple=a2->getNumberOfTuples();
- int nbOfComp=a2->getNumberOfComponents();
- a1->checkNbOfTuplesAndComp(nbOfTuple,nbOfComp,"Nb of components mismatch for array Add !");
- DataArrayInt *ret=DataArrayInt::New();
- ret->alloc(nbOfTuple,nbOfComp);
- std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<int>());
- ret->copyStringInfoFrom(*a1);
+ int nbOfTuple=a1->getNumberOfTuples();
+ int nbOfTuple2=a2->getNumberOfTuples();
+ int nbOfComp=a1->getNumberOfComponents();
+ int nbOfComp2=a2->getNumberOfComponents();
+ if(nbOfTuple!=nbOfTuple2)
+ throw INTERP_KERNEL::Exception("Nb of tuples mismatch for array Add !");
+ DataArrayInt *ret=0;
+ if(nbOfComp==nbOfComp2)
+ {
+ ret=DataArrayInt::New();
+ ret->alloc(nbOfTuple,nbOfComp);
+ std::transform(a1->begin(),a1->end(),a2->begin(),ret->getPointer(),std::plus<int>());
+ ret->copyStringInfoFrom(*a1);
+ }
+ else
+ {
+ int nbOfCompMin,nbOfCompMax;
+ const DataArrayInt *aMin, *aMax;
+ if(nbOfComp>nbOfComp2)
+ {
+ nbOfCompMin=nbOfComp2; nbOfCompMax=nbOfComp;
+ aMin=a2; aMax=a1;
+ }
+ else
+ {
+ nbOfCompMin=nbOfComp; nbOfCompMax=nbOfComp2;
+ aMin=a1; aMax=a2;
+ }
+ if(nbOfCompMin==1)
+ {
+ ret=DataArrayInt::New();
+ ret->alloc(nbOfTuple,nbOfCompMax);
+ const int *aMinPtr=aMin->getConstPointer();
+ const int *aMaxPtr=aMax->getConstPointer();
+ int *res=ret->getPointer();
+ for(int i=0;i<nbOfTuple;i++)
+ res=std::transform(aMaxPtr+i*nbOfCompMax,aMaxPtr+(i+1)*nbOfCompMax,res,std::bind2nd(std::plus<int>(),aMinPtr[i]));
+ ret->copyStringInfoFrom(*aMax);
+ }
+ else
+ throw INTERP_KERNEL::Exception("Nb of components mismatch for array Add !");
+ }
return ret;
}
%extend ParaMEDMEM::DataArrayDouble
{
- static DataArrayDouble *New(int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception)
+ static DataArrayDouble *New(PyObject *elt0, PyObject *elt1=0, PyObject *elt2=0) throw(INTERP_KERNEL::Exception)
{
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- ret->alloc(nbOfTuples,nbOfComp);
- ret->incrRef();
- return ret;
- }
-
- static DataArrayDouble *New(PyObject *li, int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception)
- {
- if(nbOfTuples<0 || nbOfComp<0)
- throw INTERP_KERNEL::Exception("DataArrayDouble::New(PyList,nbOfTuples,nbOfComponents) : should be a positive set of allocated memory !");
- MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
- double *tmp=new double[nbOfTuples*nbOfComp];
- try
+ const char *msg="ParaMEDMEM::DataArrayDouble::New : Available API are : \n-DataArrayDouble.New()\n--DataArrayDouble.New([1.,3.,4.])\n-DataArrayDouble.New([1.,3.,4.],3)\n-DataArrayDouble.New([1.,3.,4.,5.],2,2)\n-DataArrayDouble.New(5)\n-DataArrayDouble.New(5,2) !";
+ if(PyList_Check(elt0) || PyTuple_Check(elt0))
{
- fillArrayWithPyListDbl(li,tmp,nbOfTuples*nbOfComp,0.);
+ if(elt1)
+ {
+ if(PyInt_Check(elt1))
+ {
+ int nbOfTuples=PyInt_AS_LONG(elt1);
+ if(nbOfTuples<0)
+ throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive set of allocated memory !");
+ if(elt2)
+ {
+ if(PyInt_Check(elt2))
+ {//DataArrayDouble.New([1.,3.,4.,5.],2,2)
+ int nbOfCompo=PyInt_AS_LONG(elt2);
+ if(nbOfCompo<0)
+ throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive number of components !");
+ MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
+ double *tmp=new double[nbOfTuples*nbOfCompo];
+ try { fillArrayWithPyListDbl(elt0,tmp,nbOfTuples*nbOfCompo,0.,true); }
+ catch(INTERP_KERNEL::Exception& e) { delete [] tmp; throw e; }
+ ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfCompo);
+ ret->incrRef();
+ return ret;
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ {//DataArrayDouble.New([1.,3.,4.],3)
+ MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
+ double *tmp=new double[nbOfTuples];
+ try { fillArrayWithPyListDbl(elt0,tmp,nbOfTuples,0.,true); }
+ catch(INTERP_KERNEL::Exception& e) { delete [] tmp; throw e; }
+ ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,1);
+ ret->incrRef();
+ return ret;
+ }
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ {// DataArrayDouble.New([1.,3.,4.])
+ int szz=-1;
+ if(PyList_Check(elt0))
+ szz=PyList_Size(elt0);
+ else
+ szz=PyTuple_Size(elt0);
+ MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
+ double *tmp=new double[szz];
+ try { fillArrayWithPyListDbl(elt0,tmp,szz,0.,true); }
+ catch(INTERP_KERNEL::Exception& e) { delete [] tmp; throw e; }
+ ret->useArray(tmp,true,CPP_DEALLOC,szz,1);
+ ret->incrRef();
+ return ret;
+ }
}
- catch(INTERP_KERNEL::Exception& e)
+ else if(PyInt_Check(elt0))
{
- delete [] tmp;
- throw e;
+ int nbOfTuples=PyInt_AS_LONG(elt0);
+ if(nbOfTuples<0)
+ throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive set of allocated memory !");
+ if(elt1)
+ {
+ if(!elt2)
+ {
+ if(PyInt_Check(elt1))
+ {//DataArrayDouble.New(5,2)
+ int nbOfCompo=PyInt_AS_LONG(elt1);
+ if(nbOfCompo<0)
+ throw INTERP_KERNEL::Exception("DataArrayDouble::New : should be a positive number of components !");
+ MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
+ ret->alloc(nbOfTuples,nbOfCompo);
+ ret->incrRef();
+ return ret;
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ {//DataArrayDouble.New(5)
+ MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::New();
+ ret->alloc(nbOfTuples,1);
+ ret->incrRef();
+ return ret;
+ }
}
- ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfComp);
- ret->incrRef();
- return ret;
+ else
+ throw INTERP_KERNEL::Exception(msg);
}
std::string __str__() const
double *tmp=new double[nbOfTuples*nbOfElsPerTuple];
try
{
- fillArrayWithPyListDbl(li,tmp,nbOfTuples*nbOfElsPerTuple,0.);
+ fillArrayWithPyListDbl(li,tmp,nbOfTuples*nbOfElsPerTuple,0.,false);
}
catch(INTERP_KERNEL::Exception& e)
{
%extend ParaMEDMEM::DataArrayInt
{
- static DataArrayInt *New(int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception)
+ static DataArrayInt *New(PyObject *elt0, PyObject *elt1=0, PyObject *elt2=0) throw(INTERP_KERNEL::Exception)
{
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- ret->alloc(nbOfTuples,nbOfComp);
- ret->incrRef();
- return ret;
- }
-
- static DataArrayInt *New(PyObject *li, int nbOfTuples, int nbOfComp) throw(INTERP_KERNEL::Exception)
- {
- if(nbOfTuples<0 || nbOfComp<0)
- throw INTERP_KERNEL::Exception("DataArrayDouble::New(PyList,nbOfTuples,nbOfComponents) : should be a positive set of allocated memory !");
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
- int *tmp=new int[nbOfTuples*nbOfComp];
- try
+ const char *msg="ParaMEDMEM::DataArrayInt::New : Available API are : \n-DataArrayInt.New()\n--DataArrayInt.New([1,3,4])\n-DataArrayInt.New([1,3,4],3)\n-DataArrayInt.New([1,3,4,5],2,2)\n-DataArrayInt.New(5)\n-DataArrayInt.New(5,2) !";
+ if(PyList_Check(elt0) || PyTuple_Check(elt0))
{
- fillArrayWithPyListInt(li,tmp,nbOfTuples*nbOfComp,0);
+ if(elt1)
+ {
+ if(PyInt_Check(elt1))
+ {
+ int nbOfTuples=PyInt_AS_LONG(elt1);
+ if(nbOfTuples<0)
+ throw INTERP_KERNEL::Exception("DataArrayInt::New : should be a positive set of allocated memory !");
+ if(elt2)
+ {
+ if(PyInt_Check(elt2))
+ {//DataArrayInt.New([1,3,4,5],2,2)
+ int nbOfCompo=PyInt_AS_LONG(elt2);
+ if(nbOfCompo<0)
+ throw INTERP_KERNEL::Exception("DataArrayInt::New : should be a positive number of components !");
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
+ int *tmp=new int[nbOfTuples*nbOfCompo];
+ try { fillArrayWithPyListInt(elt0,tmp,nbOfTuples*nbOfCompo,0.,true); }
+ catch(INTERP_KERNEL::Exception& e) { delete [] tmp; throw e; }
+ ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfCompo);
+ ret->incrRef();
+ return ret;
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ {//DataArrayInt.New([1,3,4],3)
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
+ int *tmp=new int[nbOfTuples];
+ try { fillArrayWithPyListInt(elt0,tmp,nbOfTuples,0.,true); }
+ catch(INTERP_KERNEL::Exception& e) { delete [] tmp; throw e; }
+ ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,1);
+ ret->incrRef();
+ return ret;
+ }
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ {// DataArrayInt.New([1,3,4])
+ int szz=-1;
+ if(PyList_Check(elt0))
+ szz=PyList_Size(elt0);
+ else
+ szz=PyTuple_Size(elt0);
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
+ int *tmp=new int[szz];
+ try { fillArrayWithPyListInt(elt0,tmp,szz,0.,true); }
+ catch(INTERP_KERNEL::Exception& e) { delete [] tmp; throw e; }
+ ret->useArray(tmp,true,CPP_DEALLOC,szz,1);
+ ret->incrRef();
+ return ret;
+ }
}
- catch(INTERP_KERNEL::Exception& e)
+ else if(PyInt_Check(elt0))
{
- delete [] tmp;
- throw e;
+ int nbOfTuples=PyInt_AS_LONG(elt0);
+ if(nbOfTuples<0)
+ throw INTERP_KERNEL::Exception("DataArrayInt::New : should be a positive set of allocated memory !");
+ if(elt1)
+ {
+ if(!elt2)
+ {
+ if(PyInt_Check(elt1))
+ {//DataArrayInt.New(5,2)
+ int nbOfCompo=PyInt_AS_LONG(elt1);
+ if(nbOfCompo<0)
+ throw INTERP_KERNEL::Exception("DataArrayInt::New : should be a positive number of components !");
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
+ ret->alloc(nbOfTuples,nbOfCompo);
+ ret->incrRef();
+ return ret;
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ throw INTERP_KERNEL::Exception(msg);
+ }
+ else
+ {//DataArrayInt.New(5)
+ MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
+ ret->alloc(nbOfTuples,1);
+ ret->incrRef();
+ return ret;
+ }
}
- ret->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfComp);
- ret->incrRef();
- return ret;
+ else
+ throw INTERP_KERNEL::Exception(msg);
}
std::string __str__() const
int *tmp=new int[nbOfTuples*nbOfElsPerTuple];
try
{
- fillArrayWithPyListInt(li,tmp,nbOfTuples*nbOfElsPerTuple,0);
+ fillArrayWithPyListInt(li,tmp,nbOfTuples*nbOfElsPerTuple,0,false);
}
catch(INTERP_KERNEL::Exception& e)
{
for i in xrange(6):
self.assertEqual(arr[5-i],a.getIJ(i,0));
pass
- a.setValues(arr,5,1);
+ a.setValues(arr[:-1],5,1);
a.reverse();
for i in xrange(5):
self.assertEqual(arr[4-i],a.getIJ(i,0));
self.assertEqual(da.getIJ(0,i),expected1[i])
pass
da=-dabis
- da/=[2,3]
+ da/=DataArrayInt.New([2,3],1,2)
self.assertEqual(12,da.getNumberOfTuples());
self.assertEqual(2,da.getNumberOfComponents());
expected2=[3,2,4,3,5,3,6,4,7,5,8,5,9,6,10,7,11,7,12,8,13,9,14,9]
pass
pass
+ def testSwigDADOp5(self):
+ da=DataArrayDouble.New([5,6,7,8,9,6,7,-2,3,9,8,10])
+ da.rearrange(3)
+ da2=DataArrayDouble.New([5.,8.,10.,12])
+ self.assertEqual(4,da2.getNumberOfTuples());
+ self.assertEqual(1,da2.getNumberOfComponents());
+ da3=da+da2
+ self.assertEqual(4,da3.getNumberOfTuples());
+ self.assertEqual(3,da3.getNumberOfComponents());
+ expected1=[10.,11.,12.,16.,17.,14.,17.,8.,13.,21.,20.,22.]
+ for i in xrange(12):
+ self.assertAlmostEqual(da3.getIJ(0,i),expected1[i],13)
+ pass
+ da3=da2+da
+ self.assertEqual(4,da3.getNumberOfTuples());
+ self.assertEqual(3,da3.getNumberOfComponents());
+ for i in xrange(12):
+ self.assertAlmostEqual(da3.getIJ(0,i),expected1[i],13)
+ pass
+ pass
+
+ def testSwigDADOp6(self):
+ da=DataArrayInt.New([5,6,7,8,9,6,7,-2,3,9,8,10])
+ da.rearrange(3)
+ da2=DataArrayInt.New([5,8,10,12])
+ self.assertEqual(4,da2.getNumberOfTuples());
+ self.assertEqual(1,da2.getNumberOfComponents());
+ da3=da+da2
+ self.assertEqual(4,da3.getNumberOfTuples());
+ self.assertEqual(3,da3.getNumberOfComponents());
+ expected1=[10,11,12,16,17,14,17,8,13,21,20,22]
+ for i in xrange(12):
+ self.assertEqual(da3.getIJ(0,i),expected1[i])
+ pass
+ da3=da2+da
+ self.assertEqual(4,da3.getNumberOfTuples());
+ self.assertEqual(3,da3.getNumberOfComponents());
+ for i in xrange(12):
+ self.assertEqual(da3.getIJ(0,i),expected1[i])
+ pass
+ pass
+
def setUp(self):
pass
pass
}
}
-static void fillArrayWithPyListInt(PyObject *pyLi, int *arrToFill, int sizeOfArray, int dftVal) throw(INTERP_KERNEL::Exception)
+static void fillArrayWithPyListInt(PyObject *pyLi, int *arrToFill, int sizeOfArray, int dftVal, bool chckSize) throw(INTERP_KERNEL::Exception)
{
if(PyList_Check(pyLi))
{
int size=PyList_Size(pyLi);
+ if(chckSize)
+ if(size!=sizeOfArray)
+ {
+ std::ostringstream oss; oss << "fillArrayWithPyListInt : List expected to be of size " << sizeOfArray << " but the size is " << size << " !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
for(int i=0;i<size;i++)
{
PyObject *o=PyList_GetItem(pyLi,i);
arrToFill[i]=val;
}
else
- {
- const char msg[]="list must contain integers only";
- PyErr_SetString(PyExc_TypeError,msg);
- throw INTERP_KERNEL::Exception(msg);
- }
+ throw INTERP_KERNEL::Exception("fillArrayWithPyListInt : List must contain integers only !");
}
for(int i=size;i<sizeOfArray;i++)
arrToFill[i]=dftVal;
else if(PyTuple_Check(pyLi))
{
int size=PyTuple_Size(pyLi);
+ if(chckSize)
+ if(size!=sizeOfArray)
+ {
+ std::ostringstream oss; oss << "fillArrayWithPyListInt : Tuple expected to be of size " << sizeOfArray << " but the size is " << size << " !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
for(int i=0;i<size;i++)
{
PyObject *o=PyTuple_GetItem(pyLi,i);
}
}
-static void fillArrayWithPyListDbl(PyObject *pyLi, double *arrToFill, int sizeOfArray, double dftVal) throw(INTERP_KERNEL::Exception)
+static void fillArrayWithPyListDbl(PyObject *pyLi, double *arrToFill, int sizeOfArray, double dftVal, bool chckSize) throw(INTERP_KERNEL::Exception)
{
if(PyList_Check(pyLi))
{
int size=PyList_Size(pyLi);
+ if(chckSize)
+ if(size!=sizeOfArray)
+ {
+ std::ostringstream oss; oss << "fillArrayWithPyListDbl : List expected to be of size " << sizeOfArray << " but the size is " << size << " !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
for(int i=0;i<size;i++)
{
PyObject *o=PyList_GetItem(pyLi,i);
else if(PyTuple_Check(pyLi))
{
int size=PyTuple_Size(pyLi);
+ if(chckSize)
+ if(size!=sizeOfArray)
+ {
+ std::ostringstream oss; oss << "fillArrayWithPyListDbl : Tuple expected to be of size " << sizeOfArray << " but the size is " << size << " !";
+ throw INTERP_KERNEL::Exception(oss.str().c_str());
+ }
for(int i=0;i<size;i++)
{
PyObject *o=PyTuple_GetItem(pyLi,i);