Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add .cis() methods for Float and Complex<Float>
  • Loading branch information
PaulXiCao committed Oct 26, 2025
commit 94adcd072c9ae0db1d0fc3a13bb122ee969a178f
59 changes: 59 additions & 0 deletions src/cis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Trait-based `cis` for both real and complex inputs.
//!
//! This provides a `cis()` method for both real floats and `Complex<T>` so callers can
//! uniformly write `x.cis()` whether `x` is a `T` (float) or a `Complex<T>`.

/// Compute cis(x) = exp(i*x). Implemented for both real floats and Complex<T>.
///
/// - For real `T`, `x.cis()` returns `Complex::new(x.cos(), x.sin())`.
/// - For `Complex<T>`, `z.cis()` returns `exp(i*z) = exp(-Im(z)) * cis(Re(z))`.
pub trait Cis {
type Output;
fn cis(self) -> Self::Output;
}

#[cfg(any(feature = "std", feature = "libm"))]
mod imp {
use super::Cis;
use crate::Complex;
use num_traits::Float;

impl<T: Float> Cis for T {
type Output = Complex<T>;

fn cis(self) -> Complex<T> {
// cis(x) = cos(x) + i*sin(x)
Complex::new(self.cos(), self.sin())
}
}

impl<T: Float> Cis for Complex<T> {
type Output = Complex<T>;

fn cis(self) -> Complex<T> {
// cis(a+ib) = exp(-b) * cis(a)
let scale = (-self.im).exp();
self.re.cis() * scale
}
}

#[cfg(test)]
mod tests {
use crate::Cis;
use crate::Complex;

#[test]
fn cis_real() {
let theta = 1.2345;
let expected = Complex::new(0.3299931576785677, 0.9439833239445111);
assert!((expected - theta.cis()).norm_sqr() < 1e-10);
}

#[test]
fn cis_complex() {
let z = Complex::new(-6.7890, 1.2345);
let expected = Complex::new(0.254543682056692, -0.1409858152159941);
assert!((expected - z.cis()).norm_sqr() < 1e-10);
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use num_traits::float::FloatCore;
use num_traits::float::{Float, FloatConst};

mod cast;
mod cis;
pub use cis::Cis;
mod pow;

#[cfg(any(feature = "std", feature = "libm"))]
Expand Down