Skip to content

Generate Complex Array #1014

@vmorillon

Description

@vmorillon

I am trying to generate a Complex<f64> Array using ndarray-rand.

let matrix_size = 3;
                   
let m_re = Array::<f64, _>::random((matrix_size,matrix_size), Uniform::new(-1.0, 1.0));
let m_im = Array::<f64, _>::random((matrix_size,matrix_size), Uniform::new(-1.0, 1.0));
                   
let mut m_c = Array::<Complex<f64>, _>::zeros((matrix_size,matrix_size));
                   
Zip::from(&mut m_c)
    .and(&m_re)   
    .and(&m_im)   
    .apply(|c, &re, &im| {
        *c = Complex {re, im};
    });

Is there a more direct way to generate the Complex Array? (without instantiating 2 Array<f64>)

I tried let m_c = Array::<Complex<f64>, _>::random((matrix_size,matrix_size), Uniform::new(-1.0, 1.0)); but I got

the trait `Distribution<Complex<f64>>` is not implemented for `Uniform<{float}>`
= help: the following implementations were found:
          <Uniform<X> as Distribution<X>>
= note: required by `ndarray_rand::RandomExt::random`

Cargo dependencies:

ndarray = "0.14"
ndarray-rand = "0.13"
num-complex = "0.3"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions