0

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

cafce25
  • 15,907
  • 4
  • 25
  • 31
Mikhail
  • 48
  • 5

1 Answers1

1

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).
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • Ah, I see what did I miss. When run `cargo add norman` it downloads v0.0.4, which does depend on v0.12. But when I looked for a needed `ndarray` version, I looked at the last version on [norman gitlab](https://gitlab.com/Nuramon27/norman/-/blob/v0.0.7/Cargo.toml?ref_type=tags), and saw a v0.15.4 there. – Mikhail Apr 10 '23 at 05:59
  • 1
    You could also use the crate directly from git like that: `norman = { git = "https://gitlab.com/Nuramon27/norman", tag = "v0.0.7" }`. – frankenapps Apr 10 '23 at 06:48