PyO3/rust-numpy

to_pyarray method not found in nalgebra's Matrix

user529481 opened this issue · 2 comments

I'm trying to return the output of some calculation from nalgebra, but I'm getting the following error:

[error[E0599]: no method named `to_pyarray` found for struct `nalgebra::Matrix` in the current scope
beta.to_pyarray_bound(py)
   |              ^^^^^^^^^^^^^^^^ method not found in `Matrix<f64, Dyn, Const<1>, VecStorage<f64, Dyn, Const<1>>>`

warning: unused import: `ToPyArray`
 --> src/lib.rs:2:26
  |
2 | use numpy::{IntoPyArray, ToPyArray, PyArrayDyn, PyReadonlyArrayDyn};
  | ](https://github.com/PyO3/pyo3/issues/4319#issue-2394157954)

This is the relevant portion of my code

...
let beta = if let Some(w) = criteria {
            ...
            xtwx.lu().solve(&xtwy).unwrap()
        } else {
            ...
            xtx.lu().solve(&xty).unwrap()
        };
beta.to_pyarray(py)

I've also tried to_pyarray_bound. From looking at the docs, there should be a method

Integration with nalgebra is provided via an implementation of ToPyArray for nalgebra::Matrix to convert nalgebra matrices into NumPy arrays as well as the PyReadonlyArray::try_as_matrix and PyReadwriteArray::try_as_matrix_mut methods to treat NumPy array as nalgebra matrix slices.

Source:
https://docs.rs/numpy/latest/numpy/index.html

Here's my cargo.toml

[package]
name = "rustlib"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "rustlib"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.21.0"
nalgebra = "0.33.0"

[dependencies.numpy]
version = "0.21"
features = ["nalgebra"]

[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"

[tool.maturin]
bindings = "cffi"
compatibility = "linux"
Icxolu commented

I think the problem here is that you have two major versions for nalgebra involved here. You have a direct dependency on version 0.33 and an indirect dependency on version 0.32 through the nalgebra feature of numpy. From Rust's perspective the types from these two versions are different, hence you get the error about the missing function. If you downgrade your direct dependency it should start working again.

That was the issue. Thanks for your help!