-// Copyright (C) 2007-2016 CEA/DEN, EDF R&D
+// Copyright (C) 2007-2017 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
}
/*!
- * This method is an extention of PySlice_GetIndices but less
+ * This method is an extension of PySlice_GetIndices but less
* open than PySlice_GetIndicesEx that accepts too many situations.
*/
void GetIndicesOfSlice(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure)
/*
* Don't allow infinite chains of views, always set the base
- * to the first owner of the data.
- * That is, either the first object which isn't an array,
+ * to the first owner of the data.
+ * That is, either the first object which isn't an array,
* or the first object which owns its own data.
*/
while (PyArray_Check(obj) && (PyObject *)arr != obj) {
PyArrayObject *obj_arr = (PyArrayObject *)obj;
PyObject *tmp;
-
+
/* If this array owns its own data, stop collapsing */
- if (PyArray_CHKFLAGS(obj_arr, MED_NUMPY_OWNDATA )) {
+ if (PyArray_CHKFLAGS(obj_arr, MED_NUMPY_OWNDATA )) {
break;
- }
+ }
tmp = PyArray_BASE(obj_arr);
/* If there's no base, stop collapsing */
if (tmp == NULL) {
break;
}
- /* Stop the collapse new base when the would not be of the same
+ /* Stop the collapse new base when the would not be of the same
* type (i.e. different subclass).
*/
if (Py_TYPE(tmp) != Py_TYPE(arr)) {
MEDCoupling::MemArray<T>& mem=self->accessToMemArray();
if(nbComp==0)
{
- std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !";
+ std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
int nbDims=nbComp==1?1:2;
if(msg)
oss << msg;
else
- oss << "PyWrap convertPyObjectToStr : expect a sting like py object !";
+ oss << "PyWrap convertPyObjectToStr : expect a string like py object !";
throw INTERP_KERNEL::Exception(oss.str());
}
return ret;
}
status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0);
if(SWIG_IsOK(status))
- {
+ {
daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp);
sw=4;
return ;
}
status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0);
if(SWIG_IsOK(status))
- {
+ {
MEDCoupling::DataArrayIntTuple *daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp);
sw=4; sz=daIntTuple->getNumberOfCompo();
return daIntTuple->getConstPointer();
void *argp;
int status=SWIG_ConvertPtr(value,&argp,ti_da,0|0);
if(SWIG_IsOK(status))
- {
+ {
d=reinterpret_cast< typename MEDCoupling::Traits<T>::ArrayType * >(argp);
sw=2;
return ;
}
status=SWIG_ConvertPtr(value,&argp,ti_tuple,0|0);
if(SWIG_IsOK(status))
- {
+ {
e=reinterpret_cast< typename MEDCoupling::Traits<T>::ArrayTuple * >(argp);
sw=3;
return ;
convertFPStarLikePyObjToCpp_2<double>(value,sw,val,d,e,f,SWIGTYPE_p_MEDCoupling__DataArrayDouble,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple);
}
+/*!
+ * if value int -> cpp val sw=1
+ * if value double -> cpp val sw=1
+ * if value DataArrayDouble -> cpp DataArrayDouble sw=2
+ * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
+ * if value list[int,double] -> cpp std::vector<double> sw=4
+ * if value tuple[int,double] -> cpp std::vector<double> sw=4
+ */
+static void convertFloatStarLikePyObjToCpp_2(PyObject *value, int& sw, float& val, MEDCoupling::DataArrayFloat *&d, MEDCoupling::DataArrayFloatTuple *&e, std::vector<float>& f)
+{
+ convertFPStarLikePyObjToCpp_2<float>(value,sw,val,d,e,f,SWIGTYPE_p_MEDCoupling__DataArrayFloat,SWIGTYPE_p_MEDCoupling__DataArrayFloatTuple);
+}
+
/*!
* if python int -> cpp int sw=1
* if python list[int] -> cpp vector<int> sw=2
sw=1;
if(nbTuplesExpected*nbCompExpected!=1)
{
- std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
+ std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
return &val;
sw=1;
if(nbTuplesExpected*nbCompExpected!=1)
{
- std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
+ std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
return &val;
void *argp;
int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
if(SWIG_IsOK(status))
- {
+ {
d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
sw=2;
if(d)
}
status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
if(SWIG_IsOK(status))
- {
+ {
e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
sw=3;
if(e->getNumberOfCompo()==nbCompExpected)
return e->getConstPointer();
else
{
- std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !";
+ std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by construction !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
}
sw=1;
if(nbCompExpected!=1)
{
- std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
+ std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
nbTuples=1;
sw=1;
if(nbCompExpected!=1)
{
- std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
+ std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
nbTuples=1;
sw=4;
if(size%nbCompExpected!=0)
{
- std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
+ std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
nbTuples=size/nbCompExpected;
sw=4;
if(size%nbCompExpected!=0)
{
- std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
+ std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
throw INTERP_KERNEL::Exception(oss.str().c_str());
}
nbTuples=size/nbCompExpected;
void *argp;
int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
if(SWIG_IsOK(status))
- {
+ {
d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
sw=2;
if(d)
}
status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
if(SWIG_IsOK(status))
- {
+ {
e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
sw=3;
if(e)
void *argp;
int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
if(SWIG_IsOK(status))
- {
+ {
d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
sw=2;
if(d)
}
status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
if(SWIG_IsOK(status))
- {
+ {
e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
sw=3;
if(e)
return reinterpret_cast< MEDCoupling::DataArray * >(aBasePtrVS);
}
-static PyObject *NewMethWrapCallInitOnlyIfEmptyDictInInput(PyObject *cls, PyObject *args, const char *clsName)
-{
- if(!PyTuple_Check(args))
- {
- std::ostringstream oss; oss << clsName << ".__new__ : the args in input is expected to be a tuple !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- PyObject *builtinsd(PyEval_GetBuiltins());//borrowed
- PyObject *obj(PyDict_GetItemString(builtinsd,"object"));//borrowed
- PyObject *selfMeth(PyObject_GetAttrString(obj,"__new__"));
- //
- PyObject *tmp0(PyTuple_New(1));
- PyTuple_SetItem(tmp0,0,cls); Py_XINCREF(cls);
- PyObject *instance(PyObject_CallObject(selfMeth,tmp0));
- Py_DECREF(tmp0);
- Py_DECREF(selfMeth);
- if(PyTuple_Size(args)==2 && PyDict_Check(PyTuple_GetItem(args,1)) && PyDict_Size(PyTuple_GetItem(args,1))==0 )
- {// NOT general case. only true if in unpickeling context ! call __init__. Because for all other cases, __init__ is called right after __new__ !
- PyObject *initMeth(PyObject_GetAttrString(instance,"__init__"));
- PyObject *tmp3(PyTuple_New(0));
- PyObject *tmp2(PyObject_CallObject(initMeth,tmp3));
- Py_XDECREF(tmp2);
- Py_DECREF(tmp3);
- Py_DECREF(initMeth);
- }
- return instance;
-}
-
-template<class T>
-static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInputGeneral(PyObject *cls, PyObject *args, const char *clsName)
-{
- if(!PyTuple_Check(args))
- {
- std::ostringstream oss; oss << clsName << ".__new__ : the args in input is expected to be a tuple !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- PyObject *builtinsd(PyEval_GetBuiltins());//borrowed
- PyObject *obj(PyDict_GetItemString(builtinsd,"object"));//borrowed
- PyObject *selfMeth(PyObject_GetAttrString(obj,"__new__"));
- //
- PyObject *tmp0(PyTuple_New(1));
- PyTuple_SetItem(tmp0,0,cls); Py_XINCREF(cls);
- PyObject *instance(PyObject_CallObject(selfMeth,tmp0));
- Py_DECREF(tmp0);
- Py_DECREF(selfMeth);
- if(PyTuple_Size(args)==2 && PyDict_Check(PyTuple_GetItem(args,1)) && PyDict_Size(PyTuple_GetItem(args,1))==1 )
- {// NOT general case. only true if in unpickeling context ! call __init__. Because for all other cases, __init__ is called right after __new__ !
- PyObject *initMeth(PyObject_GetAttrString(instance,"__init__"));
- PyObject *zeNumpyRepr(0);
- {
- PyObject *tmp1(PyInt_FromLong(0));
- zeNumpyRepr=PyDict_GetItem(PyTuple_GetItem(args,1),tmp1);//borrowed
- Py_DECREF(tmp1);
- }
- if(!zeNumpyRepr)
- {
- std::ostringstream oss; oss << clsName << ".__new__ : the args in input is expected to be a tuple !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- T tt;
- {
- PyObject *tmp3(0);
- try
- {
- tmp3=tt(zeNumpyRepr);
- }
- catch(INTERP_KERNEL::Exception& e)
- {
- std::ostringstream oss; oss << clsName << ".__new__ : Invalid type in input " << " : " << e.what();
- throw INTERP_KERNEL::Exception(oss.str());
- }
- {
- PyObject *tmp2(PyObject_CallObject(initMeth,tmp3));
- Py_XDECREF(tmp2);
- }
- Py_DECREF(tmp3);
- }
- Py_DECREF(initMeth);
- }
- return instance;
-}
-
-struct SinglePyObjToBePutInATuple
-{
- PyObject *operator()(PyObject *zeNumpyRepr)
- {
- PyObject *tmp3(PyTuple_New(1));
- PyTuple_SetItem(tmp3,0,zeNumpyRepr); Py_XINCREF(zeNumpyRepr);
- return tmp3;
- }
-};
-
-struct SinglePyObjExpectToBeAListOfSz2
-{
- PyObject *operator()(PyObject *uniqueElt)
- {
- if(!PyTuple_Check(uniqueElt) || PyTuple_Size(uniqueElt)!=2)
- throw INTERP_KERNEL::Exception("Not a tuple of size 2 !");
- Py_XINCREF(uniqueElt);
- return uniqueElt;
- }
-};
-
-static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(PyObject *cls, PyObject *args, const char *clsName)
-{
- return NewMethWrapCallInitOnlyIfDictWithSingleEltInInputGeneral<SinglePyObjToBePutInATuple>(cls,args,clsName);
-}
-
static PyObject *convertPartDefinition(MEDCoupling::PartDefinition *pd, int owner)
{
PyObject *ret=0;
}
}
+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)
{
throw INTERP_KERNEL::Exception(msg);
}
}
-
+
template<class T>
PyObject *DataArrayT_isub__internal(PyObject *trueSelf, PyObject *obj, typename MEDCoupling::Traits<T>::ArrayType *self, swig_type_info *ti_da, swig_type_info *ti_tuple)
{
swig_type_info *SWIGTITraits<float>::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe()
swig_type_info *SWIGTITraits<int>::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe()
+#ifdef WITH_NUMPY
PyTypeObject *NPYTraits<double>::NPYFunc=&PyCallBackDataArrayDouble_RefType;
PyTypeObject *NPYTraits<float>::NPYFunc=&PyCallBackDataArrayFloat_RefType;
+#endif
template<class T>
typename MEDCoupling::Traits<T>::ArrayType *DataArrayT__setitem__(typename MEDCoupling::Traits<T>::ArrayType *self, PyObject *obj, PyObject *value)
return DataArrayT_isub__internal<T>(trueSelf,obj,self,SWIGTITraits<T>::TI,SWIGTITraits<T>::TI_TUPLE);
}
+template<class T>
+typename MEDCoupling::Traits<T>::ArrayType *DataArrayFPT_rmul(typename MEDCoupling::Traits<T>::ArrayType *self, PyObject *obj)
+{
+ const char msg[]="Unexpected situation in __rmul__ !";
+ T val;
+ typename MEDCoupling::Traits<T>::ArrayType *a;
+ typename MEDCoupling::Traits<T>::ArrayTuple *aa;
+ std::vector<T> bb;
+ int sw;
+ convertFPStarLikePyObjToCpp_2<T>(obj,sw,val,a,aa,bb,SWIGTITraits<T>::TI,SWIGTITraits<T>::TI_TUPLE);
+ switch(sw)
+ {
+ case 1:
+ {
+ typename MEDCoupling::MCAuto<typename MEDCoupling::Traits<T>::ArrayType> ret(self->deepCopy());
+ ret->applyLin(val,0.);
+ return ret.retn();
+ }
+ case 3:
+ {
+ typename MEDCoupling::MCAuto<typename MEDCoupling::Traits<T>::ArrayType> aaa(aa->buildDA(1,self->getNumberOfComponents()));
+ return MEDCoupling::Traits<T>::ArrayType::Multiply(self,aaa);
+ }
+ case 4:
+ {
+ typename MEDCoupling::MCAuto<typename MEDCoupling::Traits<T>::ArrayType> aaa(MEDCoupling::Traits<T>::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size());
+ return MEDCoupling::Traits<T>::ArrayType::Multiply(self,aaa);
+ }
+ default:
+ throw INTERP_KERNEL::Exception(msg);
+ }
+}
+
#endif