Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing vector<complex<float>> to function argument #286

Closed
gillesdegottex opened this issue Aug 31, 2023 · 3 comments
Closed

Passing vector<complex<float>> to function argument #286

gillesdegottex opened this issue Aug 31, 2023 · 3 comments

Comments

@gillesdegottex
Copy link
Contributor

Hi,

I struggle in passing a vector of complex values to a function.

For example, the following works as expected for float type:

#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <iostream>

namespace nb = nanobind;

NB_MODULE(pybar, m) {
    m.def("foo", [](std::vector<float>& x){
        std::cout << "x[0]=" << x[0] << std::endl;
    });
}

leads to:

> python3 -c 'import pybar; import numpy as np; x = np.array([1],dtype=np.single); pybar.foo(x)'
x[0]=1

However, if using std::complex<float> type instead of float:

#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <iostream>

namespace nb = nanobind;

NB_MODULE(pybar, m) {
    m.def("foo", [](std::vector<std::complex<float>>& x){
        std::cout << "x[0]=" << x[0] << std::endl;
    });
}

leads to:

> python3 -c 'import pybar; import numpy as np; x = np.array([1+1j],dtype=np.csingle); pybar.foo(x)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: foo(): incompatible function arguments. The following argument types are supported:
    1. foo(arg: list[std::complex<float>], /) -> None

Invoked with types: ndarray

I did try to add an explicit type_caster in stl/vector.h (defined before the generic typecaster<Type>):

template <typename Type, typename Alloc> struct type_caster<std::vector<std::complex<Type>, Alloc>>
 : list_caster<std::vector<std::complex<Type>, Alloc>, std::complex<Type>> { };

which leads to the exact same error as above.

Any idea how this should be done in nanobind?

(thanks to this new C++/Python binder!)

@gillesdegottex gillesdegottex changed the title Passing vector<std::complex<float>> to function argument Passing std::vector<std::complex<float>> to function argument Aug 31, 2023
@gillesdegottex gillesdegottex changed the title Passing std::vector<std::complex<float>> to function argument Passing vector<complex<float>> to function argument Aug 31, 2023
@wjakob
Copy link
Owner

wjakob commented Aug 31, 2023

Nanobind currently lacks a type caster for `std::complex``. Help on this front is appreciated. This is not an issue but a feature request, so I am closing the ticket.

@wjakob wjakob closed this as completed Aug 31, 2023
@gillesdegottex
Copy link
Contributor Author

Ok!

A few questions:

  • Where can I open a feature request? For follow up on this subject.
  • To add support for std::complex, I guess I should add a stl/complex.h, and maybe dig into bn_cast.h, right? Am I in the proper direction?

@wjakob
Copy link
Owner

wjakob commented Aug 31, 2023

This project does not accept feature requests, though you are welcome to submit a PR. And yes, that's right, the type caster would go to stl/complex.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants