Salome HOME
CSR output matrix MEDCouplingRemapper::getCrudeCSRMatrix (without memory leaks)
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingTypemaps.i
index 60c3c86af69ddb4b3a623b008cebaca292c909a6..0ad44e81f60cc2b3a012d7a00fae06a9d92cff0d 100644 (file)
@@ -210,17 +210,11 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype
   int ndim=PyArray_NDIM(elt0);
   if(ndim!=1 && ndim!=2)
     throw INTERP_KERNEL::Exception("Input numpy array should have dimension equal to 1 or 2 !");
-  if(PyArray_ObjectType(elt0,0)!=npyObjectType)
+  if(PyArray_DESCR(elt0)->type_num != npyObjectType)
     {
-      std::ostringstream oss; oss << "Input numpy array has not of type " << msg << " at component #0 !";
+      std::ostringstream oss; oss << "Input numpy array has not the type " << msg << "!";
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
-  if(ndim==2)
-    if(PyArray_ObjectType(elt0,1)!=npyObjectType)
-      {
-        std::ostringstream oss; oss << "Input numpy array has not of type " << msg << " at component #1 !";
-        throw INTERP_KERNEL::Exception(oss.str().c_str());
-      }
   npy_intp sz0=PyArray_DIM(elt0,0);
   npy_intp sz1=ndim==2?PyArray_DIM(elt0,1):1;
   //
@@ -405,6 +399,47 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
   return ret;
 }
 
+SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self);
+SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self);
+
+PyObject *ToCSRMatrix(const std::vector<std::map<int,double> >& m, int nbCols) throw(INTERP_KERNEL::Exception)
+{
+  int nbRows((int)m.size());
+  ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayInt> indPtr(ParaMEDMEM::DataArrayInt::New()),indices(ParaMEDMEM::DataArrayInt::New());
+  ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> data(ParaMEDMEM::DataArrayDouble::New());
+  indPtr->alloc(nbRows+1,1);
+  int *intPtr_ptr(indPtr->getPointer()); intPtr_ptr[0]=0; intPtr_ptr++;
+  int sz2(0);
+  for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++,intPtr_ptr++)
+    {
+      sz2+=(int)(*it0).size();
+      *intPtr_ptr=sz2;
+    }
+  indices->alloc(sz2,1); data->alloc(sz2,1);
+  int *indices_ptr(indices->getPointer());
+  double *data_ptr(data->getPointer());
+  for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++)
+    for(std::map<int,double>::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++,indices_ptr++,data_ptr++)
+      {
+        *indices_ptr=(*it1).first;
+        *data_ptr=(*it1).second;
+      }
+  PyObject *a(ParaMEDMEM_DataArrayDouble_toNumPyArray(data)),*b(ParaMEDMEM_DataArrayInt_toNumPyArray(indices)),*c(ParaMEDMEM_DataArrayInt_toNumPyArray(indPtr));
+  //
+  PyObject *args(PyTuple_New(1)),*args0(PyTuple_New(3)),*kw(PyDict_New()),*kw1(PyTuple_New(2));
+  PyTuple_SetItem(args0,0,a); PyTuple_SetItem(args0,1,b); PyTuple_SetItem(args0,2,c); PyTuple_SetItem(args,0,args0);
+  PyTuple_SetItem(kw1,0,PyInt_FromLong(nbRows)); PyTuple_SetItem(kw1,1,PyInt_FromLong(nbCols));
+  PyObject *tmp1(PyString_FromString("shape"));
+  PyDict_SetItem(kw,tmp1,kw1); Py_DECREF(tmp1); Py_DECREF(kw1);
+  PyObject* pdict=PyDict_New();
+  PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+  PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict));
+  PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix");
+  PyObject *ret(PyObject_Call(csrMatrixCls,args,kw));
+  Py_DECREF(pdict); Py_XDECREF(tmp); Py_DECREF(args); Py_DECREF(kw);
+  return ret;
+}
+
 #endif
 
 static PyObject *convertMesh(ParaMEDMEM::MEDCouplingMesh *mesh, int owner) throw(INTERP_KERNEL::Exception)
@@ -417,6 +452,10 @@ static PyObject *convertMesh(ParaMEDMEM::MEDCouplingMesh *mesh, int owner) throw
     }
   if(dynamic_cast<ParaMEDMEM::MEDCouplingUMesh *>(mesh))
     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,owner);
+  if(dynamic_cast<ParaMEDMEM::MEDCoupling1SGTUMesh *>(mesh))
+    ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCoupling1SGTUMesh,owner);
+  if(dynamic_cast<ParaMEDMEM::MEDCoupling1DGTUMesh *>(mesh))
+    ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCoupling1DGTUMesh,owner);
   if(dynamic_cast<ParaMEDMEM::MEDCouplingExtrudedMesh *>(mesh))
     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingExtrudedMesh,owner);
   if(dynamic_cast<ParaMEDMEM::MEDCouplingCMesh *>(mesh))
@@ -1121,7 +1160,10 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
           PyObject *obj=PyList_GetItem(pyLi,i);
           int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
           if(!SWIG_IsOK(status))
-            throw INTERP_KERNEL::Exception("list must contain only MEDCouplingUMesh");
+            {
+              std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
+              throw INTERP_KERNEL::Exception(oss.str().c_str());
+            }
           T arg=reinterpret_cast< T >(argp);
           ret[i]=arg;
         }
@@ -1136,7 +1178,7 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
           int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
           if(!SWIG_IsOK(status))
             {
-              std::ostringstream oss; oss << "tuple must contain only " << typeStr;
+              std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
               throw INTERP_KERNEL::Exception(oss.str().c_str());
             }
           T arg=reinterpret_cast< T >(argp);