From: ageay Date: Tue, 9 Apr 2013 15:29:08 +0000 (+0000) Subject: Extend operator __iadd__ of MEDCouplingFieldDouble X-Git-Tag: V6_main_FINAL~172 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=de050e89285e9f85d68760bb5a8d109f89a1c6ae;p=tools%2Fmedcoupling.git Extend operator __iadd__ of MEDCouplingFieldDouble --- diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index ad5ee146c..085f33abb 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -12123,6 +12123,35 @@ class MEDCouplingBasicsTest(unittest.TestCase): ff=f**f2 ff.checkCoherency() self.assertTrue(ff.getArray().isEqual(DataArrayDouble([2,3,64,25,1]),1e-12)) + ## MEDCouplingFieldDouble.__iadd__ + 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.__iadd__,2) + self.assertRaises(InterpKernelException,f.__iadd__,range(5)) + self.assertRaises(InterpKernelException,f.__iadd__,arr) + self.assertRaises(InterpKernelException,f.__iadd__,f2) + f.setArray(DataArrayDouble()) + self.assertRaises(InterpKernelException,f.__iadd__,2) + self.assertRaises(InterpKernelException,f.__iadd__,range(5)) + self.assertRaises(InterpKernelException,f.__iadd__,arr) + self.assertRaises(InterpKernelException,f.__iadd__,f2) + f.getArray().alloc(5,2) + f.getArray()[:,0]=range(5) ; f.getArray()[:,1]=f.getArray()[:,0]+7 + f.checkCoherency() + f+=2 + f.checkCoherency() + self.assertTrue(f.getArray().isEqual(DataArrayDouble([(2,9),(3,10),(4,11),(5,12),(6,13)]),1e-12)) + f+=arr + f.checkCoherency() + self.assertTrue(f.getArray().isEqual(DataArrayDouble([(2,9),(4,12),(6,15),(8,18),(10,21)]),1e-12)) + f2.setArray(arr) + f+=f2 + f.checkCoherency() + self.assertTrue(f.getArray().isEqual(DataArrayDouble([(2,9),(5,14),(8,19),(11,24),(14,29)]),1e-12)) pass def setUp(self): diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 6374da1f1..03d6671a9 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -3681,11 +3681,72 @@ namespace ParaMEDMEM return self->negate(); } - PyObject *___iadd___(PyObject *trueSelf, const MEDCouplingFieldDouble& other) throw(INTERP_KERNEL::Exception) + PyObject *___iadd___(PyObject *trueSelf, PyObject *obj) throw(INTERP_KERNEL::Exception) { - *self+=other; - Py_XINCREF(trueSelf); - return trueSelf; + const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__iadd__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double."; + const char msg2[]="in MEDCouplingFieldDouble.__iadd__ : 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) + { + *self+=*other; + Py_XINCREF(trueSelf); + return trueSelf; + } + 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); + self->getArray()->applyLin(1.,val); + Py_XINCREF(trueSelf); + return trueSelf; + } + case 2: + { + MEDCouplingAutoRefCountObjectPtr ret2=self->clone(false); + ret2->setArray(a); + *self+=*ret2; + Py_XINCREF(trueSelf); + return trueSelf; + } + case 3: + { + MEDCouplingAutoRefCountObjectPtr aaa=aa->buildDADouble(1,self->getNumberOfComponents()); + MEDCouplingAutoRefCountObjectPtr ret2=self->clone(false); + ret2->setArray(aaa); + *self+=*ret2; + Py_XINCREF(trueSelf); + return trueSelf; + } + 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 ret2=self->clone(false); + ret2->setArray(aaa); + *self+=*ret2; + Py_XINCREF(trueSelf); + return trueSelf; + } + default: + { throw INTERP_KERNEL::Exception(msg); } + } } PyObject *___isub___(PyObject *trueSelf, const MEDCouplingFieldDouble& other) throw(INTERP_KERNEL::Exception)