Salome HOME
numpy bindings for dense matrix
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingCommon.i
index 8f0dfb8c9f5d7419ee1317d91e4827c17e46ac07..98c05d12b9ec93cd914922378243916abdbb03d4 100644 (file)
@@ -40,7 +40,8 @@
 #include "MEDCouplingFieldOverTime.hxx"
 #include "MEDCouplingDefinitionTime.hxx"
 #include "MEDCouplingFieldDiscretization.hxx"
-#include "MEDCouplingAHOGMesh.hxx"
+#include "MEDCouplingCartesianAMRMesh.hxx"
+#include "MEDCouplingMatrix.hxx"
 #include "MEDCouplingTypemaps.i"
 
 #include "InterpKernelAutoPtr.hxx"
@@ -311,14 +312,23 @@ using namespace INTERP_KERNEL;
 %newobject ParaMEDMEM::MEDCouplingMultiFields::New;
 %newobject ParaMEDMEM::MEDCouplingMultiFields::deepCpy;
 %newobject ParaMEDMEM::MEDCouplingFieldOverTime::New;
-%newobject ParaMEDMEM::MEDCouplingAHOGPatch::getMesh;
-%newobject ParaMEDMEM::MEDCouplingAHOGPatch::__getitem__;
-%newobject ParaMEDMEM::MEDCouplingAHOGMesh::New;
-%newobject ParaMEDMEM::MEDCouplingAHOGMesh::buildUnstructured;
-%newobject ParaMEDMEM::MEDCouplingAHOGMesh::getGodFather;
-%newobject ParaMEDMEM::MEDCouplingAHOGMesh::getFather;
-%newobject ParaMEDMEM::MEDCouplingAHOGMesh::getPatch;
-%newobject ParaMEDMEM::MEDCouplingAHOGMesh::__getitem__;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRPatch::getMesh;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRPatch::__getitem__;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::New;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::buildUnstructured;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::getGodFather;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::getFather;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::getPatch;
+%newobject ParaMEDMEM::MEDCouplingCartesianAMRMesh::__getitem__;
+%newobject ParaMEDMEM::DenseMatrix::New;
+%newobject ParaMEDMEM::DenseMatrix::deepCpy;
+%newobject ParaMEDMEM::DenseMatrix::shallowCpy;
+%newobject ParaMEDMEM::DenseMatrix::getData;
+%newobject ParaMEDMEM::DenseMatrix::matVecMult;
+%newobject ParaMEDMEM::DenseMatrix::MatVecMult;
+%newobject ParaMEDMEM::DenseMatrix::__add__;
+%newobject ParaMEDMEM::DenseMatrix::__sub__;
+%newobject ParaMEDMEM::DenseMatrix::__mul__;
 
 %feature("unref") MEDCouplingPointSet "$this->decrRef();"
 %feature("unref") MEDCouplingMesh "$this->decrRef();"
@@ -340,8 +350,9 @@ using namespace INTERP_KERNEL;
 %feature("unref") MEDCouplingMultiFields "$this->decrRef();"
 %feature("unref") MEDCouplingFieldTemplate "$this->decrRef();"
 %feature("unref") MEDCouplingMultiFields "$this->decrRef();"
-%feature("unref") MEDCouplingAHOGMesh "$this->decrRef();"
-%feature("unref") MEDCouplingAHOGPatch "$this->decrRef();"
+%feature("unref") MEDCouplingCartesianAMRMesh "$this->decrRef();"
+%feature("unref") MEDCouplingCartesianAMRPatch "$this->decrRef();"
+%feature("unref") DenseMatrix "$this->decrRef();"
 
 %rename(assign) *::operator=;
 %ignore ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationIntInfo;
@@ -2648,6 +2659,17 @@ namespace ParaMEDMEM
         return oss.str();
       }
 
+      PyObject *structurizeMe(double eps=1e-12) const throw(INTERP_KERNEL::Exception)
+      {
+        DataArrayInt *cellPerm(0),*nodePerm(0);
+        MEDCouplingCMesh *retCpp(self->structurizeMe(cellPerm,nodePerm,eps));
+        PyObject *ret(PyTuple_New(3));
+        PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(retCpp),SWIGTYPE_p_ParaMEDMEM__MEDCouplingCMesh, SWIG_POINTER_OWN | 0 ));
+        PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(cellPerm),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+        PyTuple_SetItem(ret,2,SWIG_NewPointerObj(SWIG_as_voidptr(nodePerm),SWIGTYPE_p_ParaMEDMEM__DataArrayInt, SWIG_POINTER_OWN | 0 ));
+        return ret;
+      }
+
       static MEDCoupling1SGTUMesh *Merge1SGTUMeshes(PyObject *li) throw(INTERP_KERNEL::Exception)
       {
         std::vector<const ParaMEDMEM::MEDCoupling1SGTUMesh *> tmp;
@@ -2769,6 +2791,7 @@ namespace ParaMEDMEM
     static INTERP_KERNEL::NormalizedCellType GetGeoTypeGivenMeshDimension(int meshDim) throw(INTERP_KERNEL::Exception);
     MEDCoupling1SGTUMesh *build1SGTSubLevelMesh() const throw(INTERP_KERNEL::Exception);
     static int DeduceNumberOfGivenStructure(const std::vector<int>& st) throw(INTERP_KERNEL::Exception);
+    static std::vector<int> GetSplitVectFromStruct(const std::vector<int>& strct) throw(INTERP_KERNEL::Exception);
     %extend
     {
       virtual MEDCouplingStructuredMesh *buildStructuredSubPart(PyObject *cellPart) const throw(INTERP_KERNEL::Exception)
@@ -2831,6 +2854,13 @@ namespace ParaMEDMEM
         return MEDCouplingStructuredMesh::Build1GTNodalConnectivityOfSubLevelMesh(tmp,tmp+szArr);
       }
 
+      static std::vector<int> GetDimensionsFromCompactFrmt(PyObject *partCompactFormat) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(partCompactFormat,inp);
+        return MEDCouplingStructuredMesh::GetDimensionsFromCompactFrmt(inp);
+      }
+
       static PyObject *IsPartStructured(PyObject *li, PyObject *st) throw(INTERP_KERNEL::Exception)
       {
         int szArr,sw,iTypppArr;
@@ -3027,6 +3057,13 @@ namespace ParaMEDMEM
         self->setDXYZ(originPtr,originPtr+nbTuples);
       }
 
+      static void CondenseFineToCoarse(DataArrayDouble *coarseDA, const std::vector<int>& coarseSt, const DataArrayDouble *fineDA, PyObject *fineLocInCoarse) throw(INTERP_KERNEL::Exception)
+      {
+        std::vector< std::pair<int,int> > inp;
+        convertPyToVectorPairInt(fineLocInCoarse,inp);
+        MEDCouplingIMesh::CondenseFineToCoarse(coarseDA,coarseSt,fineDA,inp);
+      }
+
       std::string __str__() const throw(INTERP_KERNEL::Exception)
       {
         return self->simpleRepr();
@@ -3265,6 +3302,7 @@ namespace ParaMEDMEM
     void setStartTime(double val, int iteration, int order) throw(INTERP_KERNEL::Exception);
     void setEndTime(double val, int iteration, int order) throw(INTERP_KERNEL::Exception);
     void applyLin(double a, double b, int compoId) throw(INTERP_KERNEL::Exception);
+    void applyLin(double a, double b) throw(INTERP_KERNEL::Exception);
     int getNumberOfComponents() const throw(INTERP_KERNEL::Exception);
     int getNumberOfTuples() const throw(INTERP_KERNEL::Exception);
     int getNumberOfValues() const throw(INTERP_KERNEL::Exception);
@@ -4562,7 +4600,9 @@ namespace ParaMEDMEM
       }
   };
 
-  class MEDCouplingAHOGPatch : public RefCountObject
+  class MEDCouplingCartesianAMRMesh;
+
+  class MEDCouplingCartesianAMRPatch : public RefCountObject
   {
   public:
     int getNumberOfCellsRecursiveWithOverlap() const throw(INTERP_KERNEL::Exception);
@@ -4577,9 +4617,9 @@ namespace ParaMEDMEM
         return convertFromVectorPairInt(ret);
       }
 
-      const MEDCouplingAHOGMesh *getMesh() const throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRMesh *getMesh() const throw(INTERP_KERNEL::Exception)
       {
-        MEDCouplingAHOGMesh *ret(const_cast<MEDCouplingAHOGMesh *>(self->getMesh()));
+        MEDCouplingCartesianAMRMesh *ret(const_cast<MEDCouplingCartesianAMRMesh *>(self->getMesh()));
         if(ret)
           ret->incrRef();
         return ret;
@@ -4592,11 +4632,11 @@ namespace ParaMEDMEM
         self->addPatch(inp,factor);
       }
 
-      MEDCouplingAHOGPatch *__getitem__(int patchId) const throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRPatch *__getitem__(int patchId) const throw(INTERP_KERNEL::Exception)
       {
-        const MEDCouplingAHOGMesh *mesh(self->getMesh());
+        const MEDCouplingCartesianAMRMesh *mesh(self->getMesh());
         if(!mesh)
-          throw INTERP_KERNEL::Exception("wrap MEDCouplingAHOGPatch.__getitem__ : no underlying mesh !");
+          throw INTERP_KERNEL::Exception("wrap MEDCouplingCartesianAMRPatch.__getitem__ : no underlying mesh !");
         if(patchId==mesh->getNumberOfPatches())
           {
             std::ostringstream oss;
@@ -4604,23 +4644,31 @@ namespace ParaMEDMEM
             PyErr_SetString(PyExc_StopIteration,oss.str().c_str());
             return 0;
           }
-        MEDCouplingAHOGPatch *ret(const_cast<MEDCouplingAHOGPatch *>(mesh->getPatch(patchId)));
+        MEDCouplingCartesianAMRPatch *ret(const_cast<MEDCouplingCartesianAMRPatch *>(mesh->getPatch(patchId)));
         if(ret)
           ret->incrRef();
         return ret;
       }
 
+      void __delitem__(int patchId) throw(INTERP_KERNEL::Exception)
+      {
+        MEDCouplingCartesianAMRMesh *mesh(const_cast<MEDCouplingCartesianAMRMesh *>(self->getMesh()));
+        if(!mesh)
+          throw INTERP_KERNEL::Exception("wrap MEDCouplingCartesianAMRPatch.__delitem__ : no underlying mesh !");
+        mesh->removePatch(patchId);
+      }
+
       int __len__() const throw(INTERP_KERNEL::Exception)
       {
-        const MEDCouplingAHOGMesh *mesh(self->getMesh());
+        const MEDCouplingCartesianAMRMesh *mesh(self->getMesh());
         if(!mesh)
-          throw INTERP_KERNEL::Exception("wrap MEDCouplingAHOGPatch.__len__ : no underlying mesh !");
+          throw INTERP_KERNEL::Exception("wrap MEDCouplingCartesianAMRPatch.__len__ : no underlying mesh !");
         return mesh->getNumberOfPatches();
       }
     }
   };
   
-  class MEDCouplingAHOGMesh : public RefCountObject, public TimeLabel
+  class MEDCouplingCartesianAMRMesh : public RefCountObject, public TimeLabel
   {
   public:
     
@@ -4632,12 +4680,14 @@ namespace ParaMEDMEM
     //
     int getNumberOfPatches() const throw(INTERP_KERNEL::Exception);    
     MEDCouplingUMesh *buildUnstructured() const throw(INTERP_KERNEL::Exception);
+    void removePatch(int patchId) throw(INTERP_KERNEL::Exception);
+    void detachFromFather() throw(INTERP_KERNEL::Exception);
     %extend
     {
-      static MEDCouplingAHOGMesh *New(const std::string& meshName, int spaceDim, PyObject *nodeStrct, PyObject *origin, PyObject *dxyz) throw(INTERP_KERNEL::Exception)
+      static MEDCouplingCartesianAMRMesh *New(const std::string& meshName, int spaceDim, PyObject *nodeStrct, PyObject *origin, PyObject *dxyz) throw(INTERP_KERNEL::Exception)
       {
-        static const char msg0[]="MEDCouplingAHOGMesh::New : error on 'origin' parameter !";
-        static const char msg1[]="MEDCouplingAHOGMesh::New : error on 'dxyz' parameter !";
+        static const char msg0[]="MEDCouplingCartesianAMRMesh::New : error on 'origin' parameter !";
+        static const char msg1[]="MEDCouplingCartesianAMRMesh::New : error on 'dxyz' parameter !";
         const int *nodeStrctPtr(0);
         const double *originPtr(0),*dxyzPtr(0);
         int sw,sz,val0;
@@ -4650,12 +4700,12 @@ namespace ParaMEDMEM
         originPtr=convertObjToPossibleCpp5_SingleCompo(origin,sw,val,bb,msg0,false,sz1);
         dxyzPtr=convertObjToPossibleCpp5_SingleCompo(dxyz,sw,val2,bb2,msg1,false,sz2);
         //
-        return MEDCouplingAHOGMesh::New(meshName,spaceDim,nodeStrctPtr,nodeStrctPtr+sz,originPtr,originPtr+sz1,dxyzPtr,dxyzPtr+sz2);
+        return MEDCouplingCartesianAMRMesh::New(meshName,spaceDim,nodeStrctPtr,nodeStrctPtr+sz,originPtr,originPtr+sz1,dxyzPtr,dxyzPtr+sz2);
       }
 
-      MEDCouplingAHOGMesh(const std::string& meshName, int spaceDim, PyObject *nodeStrct, PyObject *origin, PyObject *dxyz) throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRMesh(const std::string& meshName, int spaceDim, PyObject *nodeStrct, PyObject *origin, PyObject *dxyz) throw(INTERP_KERNEL::Exception)
       {
-        return ParaMEDMEM_MEDCouplingAHOGMesh_New(meshName,spaceDim,nodeStrct,origin,dxyz);
+        return ParaMEDMEM_MEDCouplingCartesianAMRMesh_New(meshName,spaceDim,nodeStrct,origin,dxyz);
       }
 
       void addPatch(PyObject *bottomLeftTopRight, int factor) throw(INTERP_KERNEL::Exception)
@@ -4665,31 +4715,31 @@ namespace ParaMEDMEM
         self->addPatch(inp,factor);
       }
 
-      MEDCouplingAHOGMesh *getFather() const throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRMesh *getFather() const throw(INTERP_KERNEL::Exception)
       {
-        MEDCouplingAHOGMesh *ret(const_cast<MEDCouplingAHOGMesh *>(self->getFather()));
+        MEDCouplingCartesianAMRMesh *ret(const_cast<MEDCouplingCartesianAMRMesh *>(self->getFather()));
         if(ret)
           ret->incrRef();
         return ret;
       }
       
-      MEDCouplingAHOGMesh *getGodFather() const throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRMesh *getGodFather() const throw(INTERP_KERNEL::Exception)
       {
-        MEDCouplingAHOGMesh *ret(const_cast<MEDCouplingAHOGMesh *>(self->getGodFather()));
+        MEDCouplingCartesianAMRMesh *ret(const_cast<MEDCouplingCartesianAMRMesh *>(self->getGodFather()));
         if(ret)
           ret->incrRef();
         return ret;
       }
 
-      MEDCouplingAHOGPatch *getPatch(int patchId) const throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRPatch *getPatch(int patchId) const throw(INTERP_KERNEL::Exception)
       {
-        MEDCouplingAHOGPatch *ret(const_cast<MEDCouplingAHOGPatch *>(self->getPatch(patchId)));
+        MEDCouplingCartesianAMRPatch *ret(const_cast<MEDCouplingCartesianAMRPatch *>(self->getPatch(patchId)));
         if(ret)
           ret->incrRef();
         return ret;
       }
 
-      MEDCouplingAHOGPatch *__getitem__(int patchId) const throw(INTERP_KERNEL::Exception)
+      MEDCouplingCartesianAMRPatch *__getitem__(int patchId) const throw(INTERP_KERNEL::Exception)
       {
         if(patchId==self->getNumberOfPatches())
           {
@@ -4698,18 +4748,116 @@ namespace ParaMEDMEM
             PyErr_SetString(PyExc_StopIteration,oss.str().c_str());
             return 0;
           }
-        MEDCouplingAHOGPatch *ret(const_cast<MEDCouplingAHOGPatch *>(self->getPatch(patchId)));
+        MEDCouplingCartesianAMRPatch *ret(const_cast<MEDCouplingCartesianAMRPatch *>(self->getPatch(patchId)));
         if(ret)
           ret->incrRef();
         return ret;
       }
 
+      void __delitem__(int patchId) throw(INTERP_KERNEL::Exception)
+      {
+        self->removePatch(patchId);
+      }
+
       int __len__() const throw(INTERP_KERNEL::Exception)
       {
         return self->getNumberOfPatches();
       }
     }
   };
+
+  class DenseMatrix : public RefCountObject, public TimeLabel
+  {
+  public:
+    static DenseMatrix *New(int nbRows, int nbCols) throw(INTERP_KERNEL::Exception);
+    static DenseMatrix *New(DataArrayDouble *array, int nbRows, int nbCols) throw(INTERP_KERNEL::Exception);
+    DenseMatrix *deepCpy() const throw(INTERP_KERNEL::Exception);
+    DenseMatrix *shallowCpy() const throw(INTERP_KERNEL::Exception);
+    //
+    int getNumberOfRows() const throw(INTERP_KERNEL::Exception);
+    int getNumberOfCols() const throw(INTERP_KERNEL::Exception);
+    int getNbOfElems() const throw(INTERP_KERNEL::Exception);
+    void reBuild(DataArrayDouble *array, int nbRows=-1, int nbCols=-1) throw(INTERP_KERNEL::Exception);
+    void reShape(int nbRows, int nbCols) throw(INTERP_KERNEL::Exception);
+    void transpose() throw(INTERP_KERNEL::Exception);
+    //
+    bool isEqual(const DenseMatrix& other, double eps) const throw(INTERP_KERNEL::Exception);
+    DataArrayDouble *matVecMult(const DataArrayDouble *vec) const throw(INTERP_KERNEL::Exception);
+    static DataArrayDouble *MatVecMult(const DenseMatrix *mat, const DataArrayDouble *vec) throw(INTERP_KERNEL::Exception);
+    %extend
+    {
+      DenseMatrix(int nbRows, int nbCols) throw(INTERP_KERNEL::Exception)
+      {
+        return DenseMatrix::New(nbRows,nbCols);
+      }
+
+      DenseMatrix(DataArrayDouble *array, int nbRows, int nbCols) throw(INTERP_KERNEL::Exception)
+      {
+        return DenseMatrix::New(array,nbRows,nbCols);
+      }
+
+      PyObject *isEqualIfNotWhy(const DenseMatrix& other, double eps) const throw(INTERP_KERNEL::Exception)
+      {
+        std::string ret1;
+        bool ret0=self->isEqualIfNotWhy(other,eps,ret1);
+        PyObject *ret=PyTuple_New(2);
+        PyObject *ret0Py=ret0?Py_True:Py_False;
+        Py_XINCREF(ret0Py);
+        PyTuple_SetItem(ret,0,ret0Py);
+        PyTuple_SetItem(ret,1,PyString_FromString(ret1.c_str()));
+        return ret;
+      }
+
+      DataArrayDouble *getData() throw(INTERP_KERNEL::Exception)
+      {
+        DataArrayDouble *ret(self->getData());
+        if(ret)
+          ret->incrRef();
+        return ret;
+      }
+
+      DenseMatrix *__add__(const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Add(self,other);
+      }
+
+      DenseMatrix *__sub__(const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Substract(self,other);
+      }
+
+      DenseMatrix *__mul__(const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Multiply(self,other);
+      }
+
+      DenseMatrix *__mul__(const DataArrayDouble *other) throw(INTERP_KERNEL::Exception)
+      {
+        return ParaMEDMEM::DenseMatrix::Multiply(self,other);
+      }
+
+      PyObject *___iadd___(PyObject *trueSelf, const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        self->addEqual(other);
+        Py_XINCREF(trueSelf);
+        return trueSelf;
+      }
+
+      PyObject *___isub___(PyObject *trueSelf, const DenseMatrix *other) throw(INTERP_KERNEL::Exception)
+      {
+        self->substractEqual(other);
+        Py_XINCREF(trueSelf);
+        return trueSelf;
+      }
+#ifdef WITH_NUMPY
+      PyObject *toNumPyMatrix() throw(INTERP_KERNEL::Exception) // not const. It is not a bug !
+      {
+        PyObject *obj(ToNumPyArrayUnderground<DataArrayDouble,double>(self->getData(),NPY_DOUBLE,"DataArrayDouble",self->getNumberOfRows(),self->getNumberOfCols()));
+        return obj;
+      }
+#endif
+    }
+  };
 }
 
 %pythoncode %{