Salome HOME
Merge remote-tracking branch 'origin/abn/bug_fixes' into V8_5_BR
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingDataArrayTypemaps.i
index f06995ce70285e13a3c550743d93a1624b94f9aa..6fd671e1be40c9ea81ced51d3e08a54e10be02f8 100644 (file)
@@ -3032,6 +3032,83 @@ PyObject *DataArrayT__getitem__internal(const typename MEDCoupling::Traits<T>::A
     }
 }
 
+bool isCSRMatrix(PyObject *m)
+{
+#if defined(WITH_NUMPY) && defined(WITH_SCIPY)
+  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));
+  if(!tmp)
+    throw INTERP_KERNEL::Exception("Problem during loading csr_matrix in scipy.sparse ! Is Scipy module available in present ?");
+  PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix");
+  if(!csrMatrixCls)
+    throw INTERP_KERNEL::Exception("csr_matrix not found in scipy.sparse ! Is Scipy module available in present ?");
+  bool ret(PyObject_IsInstance(m,csrMatrixCls));
+  Py_DECREF(pdict); Py_XDECREF(tmp);
+  return ret;
+#else
+  return false;
+#endif
+}
+
+void convertCSR_MCDataToVectMapIntDouble(const MEDCoupling::DataArrayInt *indptrPtr, const MEDCoupling::DataArrayInt *indicesPtr, const MEDCoupling::DataArrayDouble *dataPtr, std::vector<std::map<int,double> >& mCpp)
+{
+#if __cplusplus >= 201103L
+  auto nbOfRows(indptrPtr->getNumberOfTuples()-1);
+  if(nbOfRows<0)
+    throw INTERP_KERNEL::Exception("pywrap of MEDCouplingRemapper::setMatrix : input CSR matrix looks bad regarding indptr array !");
+  mCpp.resize(nbOfRows);
+  auto indPtrCPtr(indptrPtr->begin());
+  auto indicesCPtr(indicesPtr->begin());
+  auto dataCPtr(dataPtr->begin());
+  for(auto i=0;i<nbOfRows;i++)
+    {
+      auto& line(mCpp[i]);
+      for(auto j=indPtrCPtr[i];j<indPtrCPtr[i+1];j++)
+        {
+          line[indicesCPtr[j]]=dataCPtr[j];
+        }
+    }
+#else
+  throw INTERP_KERNEL::Exception("Breaking news : 10% off for C++11 compiler :)");
+#endif
+}
+
+void convertToVectMapIntDouble(PyObject *pyobj, std::vector<std::map<int,double> >& mCpp)
+{
+  if(!PyList_Check(pyobj))
+    throw INTERP_KERNEL::Exception("convertToVectMapIntDouble : input is not a python list !");
+  mCpp.clear();
+  Py_ssize_t sz(PyList_Size(pyobj));
+  mCpp.resize(sz);
+  for(Py_ssize_t i=0;i<sz;i++)
+    {
+      PyObject *elt(PyList_GetItem(pyobj,i));
+      if(!PyDict_Check(elt))
+        {
+          std::ostringstream oss; oss << "convertToVectMapIntDouble : at pos # " << i << " of pylist a dict is exepect !";
+          throw INTERP_KERNEL::Exception(oss.str());
+        }
+      PyObject *key, *value;
+      Py_ssize_t pos(0);
+      std::map<int,double>& mapCpp(mCpp[i]);
+      while(PyDict_Next(elt,&pos,&key,&value))
+        {
+          if(!PyInt_Check(key))
+            {
+              std::ostringstream oss; oss << "convertToVectMapIntDouble : at pos # " << i << " of pylist the dict contains at pos " << pos << " a key not mappable to pyint !";
+              throw INTERP_KERNEL::Exception(oss.str());
+            }
+          if(!PyFloat_Check(value))
+            {
+              std::ostringstream oss; oss << "convertToVectMapIntDouble : at pos # " << i << " of pylist the dict contains at pos " << pos << " the value not mappable to pyfloat !";
+              throw INTERP_KERNEL::Exception(oss.str());
+            }
+          mapCpp[(int)PyInt_AS_LONG(key)]=PyFloat_AS_DOUBLE(value);
+        }
+    }
+}
+
 template<class T>
 PyObject *DataArrayT_imul__internal(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits<T>::ArrayType *self, swig_type_info *ti_da, swig_type_info *ti_tuple)
 {