1 // SWIG file HMatrix.i
2 
3 %{
4 #include "openturns/HMatrix.hxx"
5 #include "openturns/PythonWrappingFunctions.hxx"
6 
7 class PythonHMatrixRealAssemblyFunction : public OT::HMatrixRealAssemblyFunction
8 {
9 public:
PythonHMatrixRealAssemblyFunction(PyObject * pyObj)10   PythonHMatrixRealAssemblyFunction(PyObject * pyObj)
11   : pyObj_(pyObj)
12   {
13     if (!PyCallable_Check(pyObj)) {
14       throw OT::InvalidArgumentException(HERE) << "Argument is not a callable object.";
15     }
16   }
17 
operator()18   virtual OT::Scalar operator() (OT::UnsignedInteger i, OT::UnsignedInteger j) const
19   {
20     OT::ScopedPyObjectPointer index1(OT::convert< OT::UnsignedInteger, OT::_PyInt_ >(i));
21     OT::ScopedPyObjectPointer index2(OT::convert< OT::UnsignedInteger, OT::_PyInt_ >(j));
22     OT::ScopedPyObjectPointer result(PyObject_CallFunctionObjArgs(pyObj_, index1.get(), index2.get(), NULL));
23     OT::Scalar value = OT::convert<OT::_PyFloat_, OT::Scalar>(result.get());
24     return value;
25   }
26 private:
27   PyObject * pyObj_;
28 };
29 
30 class PythonHMatrixTensorRealAssemblyFunction : public OT::HMatrixTensorRealAssemblyFunction
31 {
32 public:
PythonHMatrixTensorRealAssemblyFunction(PyObject * pyObj,const OT::UnsignedInteger outputDimension)33   PythonHMatrixTensorRealAssemblyFunction(PyObject * pyObj, const OT::UnsignedInteger outputDimension)
34   : HMatrixTensorRealAssemblyFunction(outputDimension), pyObj_(pyObj)
35   {
36     if (!PyCallable_Check(pyObj)) {
37       throw OT::InvalidArgumentException(HERE) << "Argument is not a callable object.";
38     }
39   }
40 
compute(OT::UnsignedInteger i,OT::UnsignedInteger j,OT::Matrix * localValues)41   virtual void compute(OT::UnsignedInteger i, OT::UnsignedInteger j, OT::Matrix* localValues) const
42   {
43     OT::ScopedPyObjectPointer index1(OT::convert< OT::UnsignedInteger, OT::_PyInt_ >(i));
44     OT::ScopedPyObjectPointer index2(OT::convert< OT::UnsignedInteger, OT::_PyInt_ >(j));
45     OT::ScopedPyObjectPointer result(PyObject_CallFunctionObjArgs(pyObj_, index1.get(), index2.get(), NULL));
46     OT::Matrix value(OT::convert<OT::_PySequence_, OT::Matrix>(result.get()));
47     *localValues = value;
48   }
49 private:
50   PyObject * pyObj_;
51 };
52 
53 %}
54 
55 %include HMatrix_doc.i
56 
57 %template(HMatrixImplementationTypedInterfaceObject) OT::TypedInterfaceObject<OT::HMatrixImplementation>;
58 %template(pairlonglong) std::pair< size_t, size_t >;
59 
60 %include openturns/HMatrix.hxx
61 
62 namespace OT {
63 
64 %extend HMatrix {
65 
HMatrix(const HMatrix & other)66   HMatrix(const HMatrix & other) { return new OT::HMatrix(other); }
67 
assembleReal(PyObject * callable,char symmetry)68   void assembleReal(PyObject * callable, char symmetry) {
69     PythonHMatrixRealAssemblyFunction f(callable);
70     self->assemble(f, symmetry);
71   }
72 
assembleTensor(PyObject * callable,const UnsignedInteger outputDimension,char symmetry)73   void assembleTensor(PyObject * callable, const UnsignedInteger outputDimension, char symmetry) {
74     PythonHMatrixTensorRealAssemblyFunction f(callable, outputDimension);
75     self->assemble(f, symmetry);
76   }
77 
78 } // HMatrix
79 } // OT
80