Error: comparison operators cannot be chained "How do I fix this error?"

use std::f64::consts::PI;

use ndarray::{array, Array, Array1, ArrayBase, Dim, OwnedRepr, ShapeError};

// fn from_shape02<D>() -> Result<ArrayBase<OwnedRepr<f64>, D>, ShapeError> {
//     let _ = Array::from_shape_vec((3, 3), Array1::range(0., 9., 1.).to_vec()).expect("failed");
//     Ok((ArrayBase<OwnedRepr && OwnedRepr<f64>, D>))
// }

fn from_shape02<D>() -> Result<ArrayBase<OwnedRepr<f64>, D>, ShapeError> {
    let _ = Array::from_shape_vec((3, 3), Array1::range(0., 9., 1.).to_vec()).expect("failed");
    Ok(ArrayBase<OwnedRepr && OwnedRepr<f64>, D>()) // Change here
}

// fn from_shape02<D>() -> Result<ArrayBase<OwnedRepr<f64>, D>, ShapeError>
// where
//     D: Dimension,
// {
//     let _ = Array::from_shape_vec((3, 3), Array1::range(0., 9., 1.).to_vec());
//     // Ok(ArrayBase::<OwnedRepr<f64>, D>)
//     Ok(ArrayBase<OwnedRepr && OwnedRepr<f64>, D>)
//     // comparison operators cannot be chained
// }

fn main() {
    let a: Array1<f64> = array![0., 30., 45., 60., 90.];

    println!("angle: {:?}", &a);
    println!("sine(a): {}", (&a * PI / 180_f64).map(|x| x.sin()));

    let c = array![40., 100., 10.]; // No redundant type needed
    let d: Array1<f64> = array![20., 10., 100.];

    println!(
        "b: {}",
        format!(
            "{:?} {:?}",
            &from_shape02::<Dim<[usize; 2]>>().unwrap(),
            "{:?}"
        )
    );

    println!("a * 2: {}", (&a * 2.)); // Compatible types

    println!("c + d: {:?}", (&c + &d)); // Compatible types

    println!("c * d: {:?}", (&c * &d)); // Compatible types

    println!("average(a): {}", (a.sum() / a.len() as f64));

    println!("mean(c): {:?}", c.mean()); // Use Array::mean
}

error: comparison operators cannot be chained
  --> src/main.rs:11:40
   |
11 |     Ok(ArrayBase<OwnedRepr && OwnedRepr<f64>, D>()) // Change here
   |                                        ^   ^
   |
   = help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
   = help: or use `(...)` if you meant to specify fn arguments

Could you elaborate on what you are trying to achieve exactly? What you are presenting to us is not valid Rust syntax and I can't guess what you expect your from_shape02 function to actually do.

1 Like

OwnedRepr && OwnedRepr<f64> is not a valid way to write a Rust type. Try replacing this with just OwnedRepr<f64>.

2 Likes

Then OP would try to return this:

Ok(ArrayBase<OwnedRepr<f64>, D>())

which still isn't valid syntax, as you can't just call ArrayBase like that (even when you'd fix the misplaced generic arguments by using turbo-fish: ArrayBase::<OwnedRepr<f64>, D>>(). Incidentally, I believe what you are suggesting is what OP started out with, as it returns the following error:

error: comparison operators cannot be chained
 --> src/main.rs:8:17
  |
8 |     Ok(ArrayBase<OwnedRepr<f64>, D>())
  |                 ^         ^
  |
  = help: use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments
  = help: or use `(...)` if you meant to specify fn arguments
help: split the comparison into two
  |
8 |     Ok(ArrayBase<OwnedRepr && OwnedRepr<f64>, D>())
  |                            ++++++++++++

This error and the suggested fix are unfortunately not at all indicative of what is going wrong, as the < are interpreted as comparison operators. Hence the weird suggestion of adding && between them. Like what you were trying to do was compare ArrayBase < OwnedRepr < f64.

6 Likes
use ndarray::{Array, Array1, ShapeBuilder};

fn main() {
    let a = Array::from_shape_vec((3, 3), Array1::range(0., 9., 1.).to_vec());
    let b = Array::from_shape_vec((2, 2).strides((1, 2)), vec![1., 2., 3., 4.]).unwrap();
    println!("create array 01 bool : {:?}", a.is_ok());
    println!("create array : {:?}", b);
}
  • Result
create array 01 bool : true
create array : [[1.0, 3.0],
 [2.0, 4.0]], shape=[2, 2], strides=[1, 2], layout=Ff (0xa), const ndim=2

I think the approach is wrong from the start, I've solved it, thank you.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.