0

I can reproduce the problem I am currently having with Cargo.toml and lib.rs as shown below.

use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
use pyo3::prelude::{pymodule, PyModule, PyResult, Python};

#[pymodule]
fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
    // immutable example
    fn axpy(a: f64, x: ArrayViewD<'_, f64>, y: ArrayViewD<'_, f64>) -> ArrayD<f64> {
        a * &x + &y
    }

    // mutable example (no return)
    fn mult(a: f64, mut x: ArrayViewMutD<'_, f64>) {
        x *= a;
    }

    // wrapper of `axpy`
    #[pyfn(m, "axpy")]
    fn axpy_py<'py>(
        py: Python<'py>,
        a: f64,
        x: PyReadonlyArrayDyn<f64>,
        y: PyReadonlyArrayDyn<f64>,
    ) -> &'py PyArrayDyn<f64> {
        let x = x.as_array();
        let y = y.as_array();
        axpy(a, x, y).into_pyarray(py)
    }

    // wrapper of `mult`
    #[pyfn(m, "mult")]
    fn mult_py(_py: Python<'_>, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
        let x = unsafe { x.as_array_mut() };
        mult(a, x);
        Ok(())
    }

    Ok(())
}

This code is from the README.md of rust-numpy.

We also created a Cargo.toml as shown below.

[package]
name = "pyotest"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.13",features=["extension-module"] }
numpy = "0.13"
ndarray = "0.14"

[lib]
name="preprocess"
crate-type = ["cdylib"]

If you do this, two versions of ndarray will be installed, resulting in version conflicts, so it will not compile.

The error message is as follows

   Compiling pyotest v0.1.0 (/home/bokutotu/pyotest)
error[E0308]: mismatched types
  --> src/lib.rs:50:17
   |
50 |         axpy(a, x, y).into_pyarray(py)
   |                 ^ expected struct `ArrayBase`, found struct `ndarray::ArrayBase`
   |
   = note: expected struct `ArrayBase<ViewRepr<&f64>, Dim<IxDynImpl>>`
              found struct `ndarray::ArrayBase<ndarray::ViewRepr<&f64>, ndarray::dimension::dim::Dim<ndarray::dimension::dynindeximpl::IxDynImpl>>`
   = note: perhaps two different versions of crate `ndarray` are being used?

error[E0308]: mismatched types
  --> src/lib.rs:50:20
   |
50 |         axpy(a, x, y).into_pyarray(py)
   |                    ^ expected struct `ArrayBase`, found struct `ndarray::ArrayBase`
   |
   = note: expected struct `ArrayBase<ViewRepr<&f64>, Dim<IxDynImpl>>`
              found struct `ndarray::ArrayBase<ndarray::ViewRepr<&f64>, ndarray::dimension::dim::Dim<ndarray::dimension::dynindeximpl::IxDynImpl>>`
   = note: perhaps two different versions of crate `ndarray` are being used?

error[E0599]: no method named `into_pyarray` found for struct `ArrayBase<OwnedRepr<f64>, Dim<IxDynImpl>>` in the current scope
  --> src/lib.rs:50:23
   |
50 |         axpy(a, x, y).into_pyarray(py)
   |                       ^^^^^^^^^^^^ method not found in `ArrayBase<OwnedRepr<f64>, Dim<IxDynImpl>>`

error[E0308]: mismatched types
  --> src/lib.rs:57:17
   |
57 |         mult(a, x);
   |                 ^ expected struct `ArrayBase`, found struct `ndarray::ArrayBase`
   |
   = note: expected struct `ArrayBase<ViewRepr<&mut f64>, Dim<IxDynImpl>>`
              found struct `ndarray::ArrayBase<ndarray::ViewRepr<&mut f64>, ndarray::dimension::dim::Dim<ndarray::dimension::dynindeximpl::IxDynImpl>>`
   = note: perhaps two different versions of crate `ndarray` are being used?

warning: unused import: `IntoPyArray`
  --> src/lib.rs:25:13
   |
25 | use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
   |             ^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error: aborting due to 4 previous errors; 1 warning emitted

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `pyotest`

To learn more, run the command again with --verbose.

You can also check Cargo.lock to see that it is trying to install two versions of ndarray.

....
[[package]]
name = "ndarray"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c0d5c9540a691d153064dc47a4db2504587a75eae07bf1d73f7a596ebc73c04"
dependencies = [
 "matrixmultiply 0.2.4",
 "num-complex 0.3.1",
 "num-integer",
 "num-traits",
 "rawpointer",
]

...
[[package]]
name = "ndarray"
version = "0.15.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08e854964160a323e65baa19a0b1a027f76d590faba01f05c0cbc3187221a8c9"
dependencies = [
 "matrixmultiply 0.3.1",
 "num-complex 0.4.0",
 "num-integer",
 "num-traits",
 "rawpointer",
]

...

[[package]]
name = "numpy"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a996bcd58fb29bef9debf717330cd8876c3b4adbeea4939020a5326a3afad4d9"
dependencies = [
 "cfg-if 0.1.10",
 "libc",
 "ndarray 0.15.3",
 "num-complex 0.4.0",
 "num-traits",
 "pyo3",
]

As you can see, I have installed a strange version due to rust-numpy. What should I do in this case?

I want to use 0.14 for ndarray-linalg because of the version of ndarray.

musako
  • 897
  • 2
  • 10
  • 26
  • It looks like your question might be answered by the answers of [Why is a trait not implemented for a type that clearly has it implemented?](https://stackoverflow.com/q/44437123/155423); [Can I force the use of my dependencies' Cargo.lock when resolving package versions?](https://stackoverflow.com/q/49723779/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 29 '21 at 14:53
  • TL;DR, use an older version of numpy – Shepmaster Jun 29 '21 at 14:54
  • @Shepmaster I tried numpy `0.13.0`. but I have same error.And older than 0.13.0, I can't use ndarray==0.14 because of numpy dependency problems – musako Jun 29 '21 at 15:00
  • And what happens when you try the other answer? Something like `cargo update -p ndarray:0.15.3 --precise 0.14.0` – Shepmaster Jun 29 '21 at 15:10

0 Answers0