Search code examples
rustdependenciesrust-cargorust-cratesrust-ndarray

rust crate dependencies: norman::Norm trait is not defined for ndarray::Array1


I am trying to get a P-norm for my simple 1D Vec in Rust, but I am failing with every norman example. Error states that there is no method named `norm` found for struct `ArrayBase` in the current scope.

What I did:

  1. cargo new norman-test
  2. cd norman-test
  3. cargo add ndarray
  4. cargo add norman
  5. replace main.rs with:
use ndarray::Array1;

use norman::Norm;
use norman::desc::{Sup, PNorm};

fn main() {

    let a = Array1::from(vec![2.0f32, -4.0, -2.0]);

    assert_eq!(a.norm(Sup::new()), 4.0);
    assert_eq!(a.norm(PNorm::new(2)), (2.0f32*2.0 + 4.0*4.0 + 2.0*2.0).sqrt());
    assert_eq!(a.norm(PNorm::new(1)), 2.0f32 + 4.0 + 2.0);
}

I feel like I am missing something obvious, but I cannot find any other examples or some answers on how to properly use crates with dependencies. I'm not even sure what to ask. Thanks


Solution

  • The latest version of norman, 0.0.4, depends on ndarray ^0.12.0, but your cargo add will have gotten ndarray 0.15.16, which is not a semver-compatible version, so cargo compiles the 0.12.* and 0.15.* versions separately.

    So, norman is implementing Norm for ndarray::ArrayBase version 0.12 but not for ndarray::ArrayBase version 0.15.

    Things you can do about this situation:

    • Change your dependency to ndarray = "^0.12" (edit your Cargo.toml) so you're using the same version as norman is.
    • Convince the author of norman to publish an updated version.
    • Take the source code of norman and patch it to work with ndarray ^0.15.
    • Write code for the norms you want in your own crate, or copy code from norman (license permitting).