Unable to import traits implementation from ndarray
Question
- Hi I run in a problem related with
LeastSquare: I am trying to import thendarray_linalg:: {LeastSquaresSvd, LeastSquaresSvdInto, LeastSquaresSvdInPlace}. And calling fromndarray::Array1::least_squaresclass.- However the rust compiler unable to import it.
Cargo setup
- The OS environment is running in
WSL2in windows 10
#Cargo.toml
...
[dependencies]
openblas-src = { version = "*", features = ["static"] }
ndarray = "0.12"
ndarray-linalg = { version = "0.12.0", features = ["openblas"] }
ndarray-stats = "0.3"
...
- code snippets to
// mod.rs
extern crate ndarray_linalg;
extern crate ndarray;
extern crate openblas_src;
pub mod foo.
...
// foo.rs
use ndarray
use ndarray_linalg::{LeastSquaresSvd, LeastSquaresSvdInto, LeastSquaresSvdInPlace};
let a= array![
[1., 1., 1.],
[2., 3., 4.],
[3., 5., 2.],
[4., 2., 5.],
[5., 4., 3.]
];
// solving for a single right-hand
let b = array![-10., 12., 14., 16., 18.];
// let res = a.least_squares(&b);
// using `least_squares_in_place` which overwrites its arguments
// let mut a_3 = a.clone();
// let mut b_3 = b.clone();
let (m, n) = (a.shape()[0], a.shape()[1]);
let solution = b.slice(s![0..n]).to_owned();
let result = a.least_squares(&b); // unable to find least_squares
...
- The error outputs
--> keyboard-analyzer/src/models/structural_break_analysis/chow.rs:19:22
|
19 | use ndarray_linalg::{LeastSquaresSvd, LeastSquaresSvdInto, LeastSquaresSvdInPlace};
....
method not found in `ndarray::ArrayBase<models::structural_break_analysis::ndarray::OwnedRepr<{float}>,
models::structural_break_analysis::ndarray::Dim<[usize; 2]>>`
Can you call other methods, e.g. qr?
ndarray-linalg = { version = "0.12.0", features = ["openblas"] }
least_squares is added in 0.12.1. Try cargo update
If you cannot call least_squares while can call qr on 0.12.1, it may be fixed by #227. I will release this fix as 0.12.2.
Thanks. So like this?
...
let result = a.qr(&b);
...
- but still doesn't work.
I figured, Yeah still didn't work
let result = a.qr_square_inplace(&b);
The paths in the type ndarray::ArrayBase<models::structural_break_analysis::ndarray::OwnedRepr<{float}>, models::structural_break_analysis::ndarray::Dim<[usize; 2]>> in the error message are a little weird because ndarray is referenced in two different ways: ndarray and models::structural_break_analysis::ndarray. It's also surprising that the error message identifies 19 | use ndarray_linalg::{LeastSquaresSvd, LeastSquaresSvdInto, LeastSquaresSvdInPlace}; as the problematic line, instead of the line where .least_squares() is being called.
Are those extern crate lines in a module within the crate (not the top-level lib.rs or main.rs)? I don't think I've seen anyone do that before. It may be worth updating to Rust 2018 edition and removing the extern crate lines.
- The problem is i am not sure which implementation is correct.
- I am struggling to figure out a way make sure some trait implementations works by
use
I seem to be having a similar issue, even when pulling the current code from GitHub (which it pulls as ndarray-linalg v0.13.0-alpha.0):
Cargo.toml:
... ndarray = "0.14" ndarray-linalg = { git = "https://github.com/rust-ndarray/ndarray-linalg", features = ["openblas"] } ...
main.rs:
use ndarray::{array, Array1, Array2}; use ndarray_linalg::{LeastSquaresSvd, LeastSquaresSvdInto, LeastSquaresSvdInPlace};
fn main() { ... let result = a.least_squares(&b); }
My error is:
error[E0599]: no method named least_squares found for struct ndarray::ArrayBase<ndarray::OwnedRepr<f64>, ndarray::Dim<[usize; 2]>> in the current scope
--> src/main.rs:23:20
|
23 | let result = a.least_squares(&b).unwrap();
| ^^^^^^^^^^^^^ method not found in ndarray::ArrayBase<ndarray::OwnedRepr<f64>, ndarray::Dim<[usize; 2]>>
I get this also when trying a.qr(&b).
@jacobbond I had the same issue. It seems to because of mismatching (out-of-date) dependencies between the ndarray-* crates. For now, setting my dependencies in Cargo.toml to following fixed the issue for me:
[dependencies]
num-complex = "0.2.4"
approx = {version = "0.3.2", features = ["num-complex"]}
ndarray = {version = "0.13", features = ["approx"]}
ndarray-linalg = { version = "0.12", features = ["openblas-static"] }