0

I have a problem where I want to build a matrix of boolean values. I first found a way to get bit vectors from integers, but I want to combine these vectors into a matrix, using (I guess) the stack function. The problem is, the docs for the stack function use slices over a list of arrays, whereas I want to generate my rows using an iterator expression, and then collect() those values into something useable. This then causes me a headache because I can't get collect() to infer or use the correct type.

So far, here is what I have tried:

use quark::BitIndex;
use ndarray::{Array1, Array2, stack};

trait BitVector {
    fn to_bit_vec(&self) -> Array1<bool>;
}

impl BitVector for u8 {
    fn to_bit_vec(&self) -> Array1<bool> {
        (0..8).map(|b| self.bit(b)).collect::<Array1<bool>>()
    }
}

fn gen_matrix() -> Array2<bool> {
    ndarray::stack::<bool, ndarray::Dim<[usize; 1]>>(
            ndarray::Axis(0),
            &((0..8).map(|x| (1u8 << x).to_bit_vec().view()).collect())
        )
        .unwrap()
}

fn main() {
    let number: u8 = 5;
    println!("{:?}", number.to_bit_vec());


}

For the details of the map, I guess it's not so important, in this case it should generate the 'identity' matrix, but all I really need this to show is that I have valid 1D arrays that I am trying to collect into a format that the stack() function will accept as something it can combine into a 2D matrix.

I have also tried a couple of different combinations of passing by reference, as well as trying to tell collect() what type to infer using the turbofish operator, but I haven't found a solution.

I have included the I/O in main to show roughly what I want a bit vector to look like. When the compiler does the 'on-the-fly' check, hovering over the map() method gives me the following error message:

a value of type [ArrayBase<ViewRepr<&bool>, Dim<[usize; 1]>>] cannot be built from an iterator over elements of type ArrayBase<ViewRepr<&bool>, Dim<[usize; 1]>> [E0277]

I feel like I am actually quite close to what I want as a result, because I have sort-of the correct type, I just can't seem to coerce collect() into converting the correct type into a list of that same type.

I am very new to Rust so sorry if I am missing something very obvious. Thanks for your patience and help:)

whatf0xx
  • 108
  • 7

0 Answers0