From: ageay Date: Tue, 9 Apr 2013 10:56:32 +0000 (+0000) Subject: Extend operator __add__ of MEDCouplingFieldDouble X-Git-Tag: V6_main_FINAL~179 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d77c2f0661492030fd3788571fed9209bdf8dd6d;p=tools%2Fmedcoupling.git Extend operator __add__ of MEDCouplingFieldDouble --- diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index af38ebbc9..00914dc7a 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -11964,6 +11964,40 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(NodeField.getArray().isUniform(0.,1e-12)) pass + def testSwigFieldOperationOpen1(self): + m=MEDCouplingDataForTest.build2DTargetMesh_1() + f=MEDCouplingFieldDouble(ON_CELLS) + f.setMesh(m) + arr=DataArrayDouble(5,2) + arr[:,0]=range(5) ; arr[:,1]=2*arr[:,0] + f2=f.clone(True) + self.assertRaises(InterpKernelException,f.__add__,2) + self.assertRaises(InterpKernelException,f.__add__,range(5)) + self.assertRaises(InterpKernelException,f.__add__,arr) + self.assertRaises(InterpKernelException,f.__add__,f2) + f.setArray(DataArrayDouble()) + self.assertRaises(InterpKernelException,f.__add__,2) + self.assertRaises(InterpKernelException,f.__add__,range(5)) + self.assertRaises(InterpKernelException,f.__add__,arr) + self.assertRaises(InterpKernelException,f.__add__,f2) + self.assertRaises(InterpKernelException,f.__getitem__,(slice(None),0)) + f.getArray().alloc(5,2) + f.getArray()[:,0]=range(5) ; f.getArray()[:,1]=f.getArray()[:,0]+7 + ff=f+2 + ff.checkCoherency() + self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(2, 9), (3, 10), (4, 11), (5, 12), (6, 13)]),1e-12)) + ff=f+arr + ff.checkCoherency() + self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(0, 7), (2, 10), (4, 13), (6, 16), (8, 19)]),1e-12)) + self.assertRaises(InterpKernelException,f.__add__,f2) + f2.setArray(arr) + ff=f+f2 + ff.checkCoherency() + self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(0, 7), (2, 10), (4, 13), (6, 16), (8, 19)]),1e-12)) + ff=f+[5,8] + self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]),1e-12)) + pass + def setUp(self): pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 5395c22c4..602816a5f 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -132,7 +132,7 @@ using namespace INTERP_KERNEL; %newobject ParaMEDMEM::MEDCouplingFieldDouble::getIdsInRange; %newobject ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart; %newobject ParaMEDMEM::MEDCouplingFieldDouble::__getitem__; -%newobject ParaMEDMEM::MEDCouplingFieldDouble::operator+; +%newobject ParaMEDMEM::MEDCouplingFieldDouble::__add__; %newobject ParaMEDMEM::MEDCouplingFieldDouble::operator-; %newobject ParaMEDMEM::MEDCouplingFieldDouble::operator*; %newobject ParaMEDMEM::MEDCouplingFieldDouble::operator/; @@ -2945,7 +2945,6 @@ namespace ParaMEDMEM static MEDCouplingFieldDouble *MultiplyFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception); static MEDCouplingFieldDouble *DivideFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception); MEDCouplingFieldDouble *min(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception); - MEDCouplingFieldDouble *operator+(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception); MEDCouplingFieldDouble *operator-(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception); MEDCouplingFieldDouble *operator*(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception); MEDCouplingFieldDouble *operator/(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception); @@ -3339,6 +3338,73 @@ namespace ParaMEDMEM return self->extractSlice3D(orig,vect,eps); } + MEDCouplingFieldDouble *__add__(PyObject *obj) throw(INTERP_KERNEL::Exception) + { + const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__add__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double."; + const char msg2[]="in MEDCouplingFieldDouble.__add__ : self field has no Array of values set !"; + void *argp; + // + if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,0|0))) + { + MEDCouplingFieldDouble *other=reinterpret_cast< ParaMEDMEM::MEDCouplingFieldDouble * >(argp); + if(other) + return (*self)+(*other); + else + throw INTERP_KERNEL::Exception(msg); + } + // + double val; + DataArrayDouble *a; + DataArrayDoubleTuple *aa; + std::vector bb; + int sw; + convertObjToPossibleCpp5(obj,sw,val,a,aa,bb); + switch(sw) + { + case 1: + { + if(!self->getArray()) + throw INTERP_KERNEL::Exception(msg2); + MEDCouplingAutoRefCountObjectPtr ret=self->getArray()->deepCpy(); + ret->applyLin(1.,val); + MEDCouplingAutoRefCountObjectPtr ret2=self->clone(false); + ret2->setArray(ret); + return ret2.retn(); + } + case 2: + { + if(!self->getArray()) + throw INTERP_KERNEL::Exception(msg2); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::Add(self->getArray(),a); + MEDCouplingAutoRefCountObjectPtr ret2=self->clone(false); + ret2->setArray(ret); + return ret2.retn(); + } + case 3: + { + if(!self->getArray()) + throw INTERP_KERNEL::Exception(msg2); + MEDCouplingAutoRefCountObjectPtr aaa=aa->buildDADouble(1,self->getNumberOfComponents()); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::Add(self->getArray(),aaa); + MEDCouplingAutoRefCountObjectPtr ret2=self->clone(false); + ret2->setArray(ret); + return ret2.retn(); + } + case 4: + { + if(!self->getArray()) + throw INTERP_KERNEL::Exception(msg2); + MEDCouplingAutoRefCountObjectPtr aaa=DataArrayDouble::New(); aaa->useArray(&bb[0],false,CPP_DEALLOC,1,(int)bb.size()); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayDouble::Add(self->getArray(),aaa); + MEDCouplingAutoRefCountObjectPtr ret2=self->clone(false); + ret2->setArray(ret); + return ret2.retn(); + } + default: + { throw INTERP_KERNEL::Exception(msg); } + } + } + PyObject *___iadd___(PyObject *trueSelf, const MEDCouplingFieldDouble& other) throw(INTERP_KERNEL::Exception) { *self+=other; diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index ad08183b3..6c6f1a0bf 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -209,7 +209,7 @@ namespace ParaMEDMEM static DataArrayDouble *New(PyObject *elt0, PyObject *nbOfTuples=0, PyObject *elt2=0) throw(INTERP_KERNEL::Exception) { - 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) !"; + 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([1.,3.,4.,5.,7,8.],3,2)\n-DataArrayDouble.New(5)\n-DataArrayDouble.New(5,2) !"; if(PyList_Check(elt0) || PyTuple_Check(elt0)) { if(nbOfTuples) @@ -2213,7 +2213,7 @@ namespace ParaMEDMEM static DataArrayInt *New(PyObject *elt0, PyObject *nbOfTuples=0, PyObject *nbOfComp=0) throw(INTERP_KERNEL::Exception) { - 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) !"; + 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([1,3,4,5,7,8],3,2)\n-DataArrayInt.New(5)\n-DataArrayInt.New(5,2) !"; if(PyList_Check(elt0) || PyTuple_Check(elt0)) { if(nbOfTuples)