Skip to main content

fpm_rs/
complex.rs

1use num_complex::Complex64;
2
3use crate::{Array2, Result};
4
5pub type Complex = Complex64;
6pub type ComplexArray = Array2<Complex64>;
7
8pub fn amplitude(field: &ComplexArray) -> Array2<f64> {
9    field.map(|value| value.norm())
10}
11
12pub fn phase(field: &ComplexArray) -> Array2<f64> {
13    field.map(|value| value.arg())
14}
15
16pub fn from_amplitude_phase(amplitude: &Array2<f64>, phase: &Array2<f64>) -> Result<ComplexArray> {
17    if amplitude.shape() != phase.shape() {
18        return Err(crate::Error::InvalidShape(format!(
19            "amplitude {:?} and phase {:?} differ",
20            amplitude.shape(),
21            phase.shape()
22        )));
23    }
24    ComplexArray::from_vec(
25        amplitude.shape(),
26        amplitude
27            .as_slice()
28            .iter()
29            .zip(phase.as_slice())
30            .map(|(&amplitude, &phase)| Complex64::from_polar(amplitude, phase))
31            .collect(),
32    )
33}