2323 */
2424
2525
26+
2627%module (package=" casadi" ,directors=1 ) casadi
2728
2829#ifdef CASADI_WITH_COPYSIGN_UNDEF
@@ -2294,6 +2295,59 @@ namespace std {
22942295 #define L_STR " str"
22952296#endif
22962297
2298+ #ifdef SWIGPYTHON
2299+ %typemap(in, doc=" memoryview(ro)" , noblock=1 , fragment=" casadi_all" ) (const double * a, casadi_int size) (Py_buffer* buffer) {
2300+ if (!PyMemoryView_Check ($input)) SWIG_exception_fail (SWIG_TypeError, " Must supply a MemoryView." );
2301+ buffer = PyMemoryView_GET_BUFFER ($input);
2302+ $1 = static_cast <double *>(buffer->buf ); // const double cast comes later
2303+ $2 = buffer->len ;
2304+ }
2305+
2306+ %typemap(in, doc=" memoryview(rw)" , noblock=1 , fragment=" casadi_all" ) (double * a, casadi_int size) (Py_buffer* buffer) {
2307+ if (!PyMemoryView_Check ($input)) SWIG_exception_fail (SWIG_TypeError, " Must supply a writable MemoryView." );
2308+ buffer = PyMemoryView_GET_BUFFER ($input);
2309+ if (buffer->readonly ) SWIG_exception_fail (SWIG_TypeError, " Must supply a writable MemoryView." );
2310+ $1 = static_cast <double *>(buffer->buf );
2311+ $2 = buffer->len ;
2312+ }
2313+
2314+ // Directorin typemap; as output
2315+ %typemap(directorin, noblock=1 , fragment=" casadi_all" ) (const double ** arg, const std::vector<casadi_int>& sizes_arg) (PyObject* my_tuple) {
2316+ PyObject * arg_tuple = PyTuple_New ($2 .size ());
2317+ for (casadi_int i=0 ;i<$2 .size ();++i) {
2318+
2319+ #ifdef WITH_PYTHON3
2320+ PyObject* buf = $1 [i] ? PyMemoryView_FromMemory (reinterpret_cast <char *>(const_cast <double *>($1 [i])), $2 [i]*sizeof (double ), PyBUF_READ) : SWIG_Py_Void ();
2321+ #else
2322+ PyObject* buf = $1 [i] ? PyBuffer_FromMemory (const_cast <double *>($1 [i]), $2 [i]*sizeof (double )) : SWIG_Py_Void ();
2323+ #endif
2324+ PyTuple_SET_ITEM (arg_tuple, i, buf);
2325+ }
2326+ $input = arg_tuple;
2327+ }
2328+
2329+ %typemap(directorin, noblock=1 , fragment=" casadi_all" ) (double ** res, const std::vector<casadi_int>& sizes_res) {
2330+ PyObject* res_tuple = PyTuple_New ($2 .size ());
2331+ for (casadi_int i=0 ;i<$2 .size ();++i) {
2332+ #ifdef WITH_PYTHON3
2333+ PyObject* buf = $1 [i] ? PyMemoryView_FromMemory (reinterpret_cast <char *>(const_cast <double *>($1 [i])), $2 [i]*sizeof (double ), PyBUF_WRITE) : SWIG_Py_Void ();
2334+ #else
2335+ PyObject* buf = $1 [i] ? PyBuffer_FromReadWriteMemory ($1 [i], $2 [i]*sizeof (double )) : SWIG_Py_Void ();
2336+ #endif
2337+ PyTuple_SET_ITEM (res_tuple, i, buf);
2338+ }
2339+ $input = res_tuple;
2340+ }
2341+
2342+ %typemap(in, doc=" void*" , noblock=1 , fragment=" casadi_all" ) void * raw {
2343+ $1 = PyCapsule_GetPointer ($input, NULL );
2344+ }
2345+
2346+ %typemap(out, doc=" void*" , noblock=1 , fragment=" casadi_all" ) void * {
2347+ $result = PyCapsule_New ($1 , NULL ,NULL );
2348+ }
2349+ #endif
2350+
22972351%casadi_typemaps(L_STR, PREC_STRING, std::string)
22982352%casadi_template(LL L_STR LR, PREC_VECTOR, std::vector<std::string>)
22992353%casadi_typemaps(" Sparsity" , PREC_SPARSITY, casadi::Sparsity)
@@ -3950,6 +4004,11 @@ def PyFunction(name, obj, inputs, outputs, opts={}):
39504004%}
39514005#endif
39524006
4007+ #ifndef SWIGPYTHON
4008+ %ignore FunctionBuffer;
4009+ %ignore _function_buffer_eval;
4010+ #endif
4011+
39534012%include <casadi/core/function.hpp >
39544013#ifdef SWIGPYTHON
39554014namespace casadi {
@@ -3971,8 +4030,21 @@ namespace casadi{
39714030 else :
39724031 # Named inputs -> return dictionary
39734032 return self.call (kwargs)
4033+
4034+ def buffer (self):
4035+ " " "
4036+ Create a FunctionBuffer object for evaluating with minimal overhead
4037+
4038+ " " "
4039+ import functools
4040+ fb = FunctionBuffer (self)
4041+ caller = functools.partial (_casadi._function_buffer_eval , fb._self ())
4042+ return (fb, caller)
39744043 %}
4044+
4045+
39754046 }
4047+
39764048}
39774049#endif // SWIGPYTHON
39784050
0 commit comments