cas_compute/funcs/
complex.rs

1//! Useful functions for complex numbers.
2
3use cas_attrs::builtin;
4use rug::{Complex, Float};
5
6/// Returns the real part of the given complex number.
7#[derive(Debug)]
8pub struct Re;
9
10#[cfg_attr(feature = "numerical", builtin)]
11impl Re {
12    pub fn eval_static(n: Complex) -> Float {
13        n.into_real_imag().0
14    }
15}
16
17/// Returns the imaginary part of the given complex number.
18#[derive(Debug)]
19pub struct Im;
20
21#[cfg_attr(feature = "numerical", builtin)]
22impl Im {
23    pub fn eval_static(n: Complex) -> Float {
24        n.into_real_imag().1
25    }
26}
27
28/// Returns the argument of the given complex number, in radians.
29#[derive(Debug)]
30pub struct Arg;
31
32#[cfg_attr(feature = "numerical", builtin(radian = output))]
33impl Arg {
34    pub fn eval_static(n: Complex) -> Float {
35        n.arg().into_real_imag().0 // `n`.arg() returns Complex interestingly
36    }
37}
38
39/// Returns the complex conjugate of the given complex number.
40#[derive(Debug)]
41pub struct Conj;
42
43#[cfg_attr(feature = "numerical", builtin)]
44impl Conj {
45    pub fn eval_static(n: Complex) -> Complex {
46        n.conj()
47    }
48}