Nanobind fails converting complex np array to Eigen complex vector

99 views Asked by At

System:

Mac OSX 13.5.2
Apple Clang

Here is a test project:

complex_test/
  .git/
  nanobind/ # added as git submodule
  eigen/ # added as git submodule
  - CMakeLists.txt
  - complex_test.cpp
  - test.py

Here are the files:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.15...3.27)
project(complex_test)
find_package(Python 3.8 COMPONENTS Interpreter Development.Module REQUIRED)
add_subdirectory(${CMAKE_SOURCE_DIR}/nanobind)
nanobind_add_module(complex_test complex_test.cpp)
target_include_directories(complex_test PRIVATE ${CMAKE_SOURCE_DIR}/Eigen)

complex_test.cpp:

#include <Eigen/Core>
#include <iostream>
#include <nanobind/eigen/dense.h>
#include <nanobind/nanobind.h>

NB_MODULE(complex_test, m) {
  m.def("hello_world", []() { std::cout << "hello world\n"; });
  m.def("print_double_vector", [](Eigen::Vector2d &vec) {
    std::cout << "Double: " << vec.transpose() << std::endl;
  });
  m.def("print_complex_vector", [](Eigen::Vector2cd &vec) {
    std::cout << "Complex: " << vec.transpose() << std::endl;
  });
}

test.py:

import sys
import numpy as np
sys.path.append("build")
import complex_test  # noqa
complex_test.print_double_vector(np.zeros(2, dtype=float))
complex_test.print_complex_vector(np.zeros(2, dtype=complex))

to build and run the test:

mkdir build
cd build
cmake ..
cmake --build .
cd ..
python test.py

When run the following is the printout:

> python test.py
Double: 0 0
Traceback (most recent call last):
  File "/Users/vince/Coding/scratch/testing_nanobind/test.py", line 8, in <module>
    complex_test.print_complex_vector(np.zeros(2, dtype=complex))
TypeError: print_complex_vector(): incompatible function arguments. The following argument types are supported:
    1. print_complex_vector(arg: Eigen::Matrix<std::__1::complex<double>, 2, 1, 0, 2, 1>, /) -> None

Invoked with types: ndarray

In pybind11, this would work. I know that some things have changed. The documentation does state that "Presently supported dtypes include signed/unsigned integers, floating point values, and boolean values." I guess I was hoping that complex numbers would be included under floating, and was wondering if anyone else knew specifically what is going on here, and if I am doing something wrong, or if this is just not supported.

0

There are 0 answers