Skip to content

Unable to import traits implementation from ndarray #230

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

Open
Riotpiaole opened this issue Jul 23, 2020 · 7 comments
Open

Unable to import traits implementation from ndarray #230

Riotpiaole opened this issue Jul 23, 2020 · 7 comments
Labels

Comments

@Riotpiaole
Copy link

Riotpiaole commented Jul 23, 2020

Question

  • Hi I run in a problem related with LeastSquare: I am trying to import the ndarray_linalg:: {LeastSquaresSvd, LeastSquaresSvdInto, LeastSquaresSvdInPlace} . And calling from ndarray::Array1::least_squares class.
    • However the rust compiler unable to import it.

Cargo setup

  • The OS environment is running in WSL2 in 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]>>`
@termoshtt
Copy link
Member

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.

@Riotpiaole
Copy link
Author

Thanks. So like this?

...
let result =  a.qr(&b);
...
  • but still doesn't work.

@Riotpiaole
Copy link
Author

I figured, Yeah still didn't work

let result = a.qr_square_inplace(&b);

@jturner314
Copy link
Member

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.

@Riotpiaole
Copy link
Author

  • 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

@jacobbond
Copy link

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).

@RustyBamboo
Copy link

@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"] }

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

No branches or pull requests

5 participants