dumb_questions
dumb_questions

Reputation: 39

What is the difference between num::Complex and num_complex::Complex?

What is the difference between num::Complex and num_complex::Complex?

I am attempting to use the rustfft crate, and get the message below.

expected struct `num::Complex`, found struct `num_complex::Complex`

In vs code i get the following hint:

note: expected mutable reference `&mut [num::Complex<f32>]`
         found mutable reference `&mut [num_complex::Complex<f32>]`
note: perhaps two different versions of crate `num_complex` are being used?

Is this error a consequence of rustfft using a different version of num::Complex than the rest of my code? If so, is it possible to typecast between the to types?

Thanks

Edit: As requested the Cargo.toml dependencies.

[dependencies]
num-traits = ""
num = ""
num-complex = "0.3"
rustfft = ""

And the code. I run it with using cargo test.

/// Create 32-bit float complex number.
#[macro_export]
macro_rules! C32F {
    ( $re:expr, $im:expr ) => {
        c_value!( $re, $im, f32 );
    };
}

macro_rules! magnitude_spectrum_calculation {
    ( $vector:expr, $T:ty ) => {
        let mut temp_vector = $vector;
        let mut planner = FftPlanner::<$T>::new();
        let size = temp_vector.len();

        let fft = planner.plan_fft_forward( size );

        fft.process( temp_vector.as_mut_slice() );
        return crate::vfunc::scale( abs(temp_vector), (1 as $T) / (size as $T) );
    };
}

/// Calculate magnitue spectrum for 32-bit complex floating point vectors, linear scale.
pub fn magnitude_spectrum( vector: Vec<Complex<f32>> ) -> Vec<f32>
{
    magnitude_spectrum_calculation!( vector, f32 );
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn cfunc_magnitude_spectrum() {
        let vec = vec![ C32F!(2,0), C32F!(1,4), C32F!(2,0), C32F!(1,4), C32F!(2,0), C32F!(1,4), C32F!(2,0), C32F!(1,4) ];
        assert_eq!( vec![ 2.5_f32, 0_f32, 0_f32, 0_f32, 2.0615528_f32, 0_f32, 0_f32, 0_f32 ], magnitude_spectrum( vec ) );
    }
}

I am quite new to Rust, so if i am doing anything odd above, please let me know :)

Upvotes: 3

Views: 567

Answers (1)

dumb_questions
dumb_questions

Reputation: 39

Removing num-traits and num-complex from the Cargo.toml file solved the problem.

The dependencies are now:

[dependencies]
num = ""
rustfft = ""

I also had to change the crate usage declaration to the following:

//use num_traits::Num;         // old
//use num_traits::Float;       // old
//use num_traits::real::Real;  // old
//use num_complex::Complex;    // old

use num::traits::Num;
use num::traits::Float;
use num::traits::real::Real;
use num::Complex;

Thanks guys!

Upvotes: 0

Related Questions