]> SALOME platform Git repositories - modules/med.git/commitdiff
Salome HOME
CSR output matrix MEDCouplingRemapper::getCrudeCSRMatrix
authorageay <ageay>
Mon, 23 Sep 2013 09:05:01 +0000 (09:05 +0000)
committerageay <ageay>
Mon, 23 Sep 2013 09:05:01 +0000 (09:05 +0000)
src/MEDCoupling/MEDCouplingRemapper.cxx
src/MEDCoupling/MEDCouplingRemapper.hxx
src/MEDCoupling_Swig/MEDCouplingRemapper.i
src/MEDCoupling_Swig/MEDCouplingTypemaps.i

index 2412dddb08cb0cfd4b5cd7ddb394cf06549c4e29..dd837acda957070eaf5bc73c5d1d0b7bd4912782 100644 (file)
@@ -1147,6 +1147,14 @@ const std::vector<std::map<int,double> >& MEDCouplingRemapper::getCrudeMatrix()
   return _matrix;
 }
 
+/*!
+ * Returns the number of columns of matrix returned by MEDCouplingRemapper::getCrudeMatrix method.
+ */
+int MEDCouplingRemapper::getNumberOfColsOfMatrix() const
+{
+  return (int)_deno_reverse_multiply.size();
+}
+
 /*!
  * This method is supposed to be called , if needed, right after MEDCouplingRemapper::prepare or MEDCouplingRemapper::prepareEx.
  * If not the behaviour is unpredictable.
index 5bc58e86db563167e54690726d7f11d8c8d2bee4..36cb6236990071dcb1aa33dfcb38aee847f4c232 100644 (file)
@@ -72,6 +72,7 @@ namespace ParaMEDMEM
     MEDCOUPLINGREMAPPER_EXPORT double getMaxValueInCrudeMatrix() const;
   public:
     MEDCOUPLINGREMAPPER_EXPORT const std::vector<std::map<int,double> >& getCrudeMatrix() const;
+    MEDCOUPLINGREMAPPER_EXPORT int getNumberOfColsOfMatrix() const;
     MEDCOUPLINGREMAPPER_EXPORT static void PrintMatrix(const std::vector<std::map<int,double> >& m);
   private:
     int prepareInterpKernelOnly();
index cfb1129e1c57d82e1d0bb013c7614270b2e88542..5578506acb061ae04d04e807e48780458dbc43b4 100644 (file)
@@ -73,6 +73,7 @@ namespace ParaMEDMEM
       int nullifiedTinyCoeffInCrudeMatrixAbs(double maxValAbs) throw(INTERP_KERNEL::Exception);
       int nullifiedTinyCoeffInCrudeMatrix(double scaleFactor) throw(INTERP_KERNEL::Exception);
       double getMaxValueInCrudeMatrix() const throw(INTERP_KERNEL::Exception);
+      int getNumberOfColsOfMatrix() const throw(INTERP_KERNEL::Exception);
       %extend
          {
            PyObject *getCrudeMatrix() const throw(INTERP_KERNEL::Exception)
@@ -90,6 +91,12 @@ namespace ParaMEDMEM
                }
              return ret;
            }
+#ifdef WITH_NUMPY
+           PyObject *getCrudeCSRMatrix() const throw(INTERP_KERNEL::Exception)
+           {
+             return ToCSRMatrix(self->getCrudeMatrix(),self->getNumberOfColsOfMatrix());
+           }
+#endif
          }
     };
 }
index dd61b8ee1ef0552ffe72a2409a08767f7ae7982b..201bf6c0b3b3be04088686ee148b32a18de4710f 100644 (file)
@@ -399,6 +399,46 @@ 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));
+  PyDict_SetItem(kw,PyString_FromString("shape"),kw1);
+  PyObject* pdict=PyDict_New();
+  PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+  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);
+  return ret;
+}
+
 #endif
 
 static PyObject *convertMesh(ParaMEDMEM::MEDCouplingMesh *mesh, int owner) throw(INTERP_KERNEL::Exception)