4

I am using SWIG to pass numpy arrays from Python to C++ code:

%include "numpy.i"
%init %{
import_array();
%}

%apply (float* INPLACE_ARRAY1, int DIM1) {(float* data, int n)};

class Class 
{
  public: 
  void test(float* data, int n)
  {
    //...
  }
};

and in Python:

c = Class()
a = zeros(5)
c.test(a)

This works, but how can I pass multiple numpy arrays to the same function?

1 Answer 1

9

I found out the answer from a collegue of mine:

%apply (float* INPLACE_ARRAY1, int DIM1) {(float* data1, int n1), (float* data2, int n2)};

class Class 
{
  public: 
  void test(float* data1, int n1, float* data2, int n2)
  {
    //...
  }
};

Now two numpy arrays are passed to Class::test.

Sign up to request clarification or add additional context in comments.

1 Comment

you can shorten the apply directive a little bit by applying the typemap to both input pairs at the same time: %apply (float* INPLACE_ARRAY1, int DIM1) {(float* data1, int n1), (float* data2, int n2)};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.