Skip to content

Commit d0a06ca

Browse files
bors[bot]rayhem
andauthored
Merge #101
101: Add cis function r=cuviper a=rayhem Adds a [cis function](https://en.wikipedia.org/wiki/Cis_(mathematics)) as a convenient way to construct a complex number with a specific phase/angle. Shorthand for `(Complex::i() * angle).exp()`. Co-authored-by: Connor Glosser <glosser1@gmail.com>
2 parents f92c43c + d4328d9 commit d0a06ca

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ impl<T: Clone + Signed> Complex<T> {
161161

162162
#[cfg(any(feature = "std", feature = "libm"))]
163163
impl<T: Float> Complex<T> {
164+
/// Create a new Complex with a given phase: `exp(i * phase)`.
165+
/// See [cis (mathematics)](https://en.wikipedia.org/wiki/Cis_(mathematics)).
166+
#[inline]
167+
pub fn cis(phase: T) -> Self {
168+
Self::new(phase.cos(), phase.sin())
169+
}
170+
164171
/// Calculate |self|
165172
#[inline]
166173
pub fn norm(self) -> T {
@@ -1629,6 +1636,15 @@ mod test {
16291636
use super::*;
16301637
use num_traits::{Float, Pow};
16311638

1639+
#[test]
1640+
fn test_cis() {
1641+
assert!(close(Complex::cis(0.0 * f64::consts::PI), _1_0i));
1642+
assert!(close(Complex::cis(0.5 * f64::consts::PI), _0_1i));
1643+
assert!(close(Complex::cis(1.0 * f64::consts::PI), -_1_0i));
1644+
assert!(close(Complex::cis(1.5 * f64::consts::PI), -_0_1i));
1645+
assert!(close(Complex::cis(2.0 * f64::consts::PI), _1_0i));
1646+
}
1647+
16321648
#[test]
16331649
#[cfg_attr(target_arch = "x86", ignore)]
16341650
// FIXME #7158: (maybe?) currently failing on x86.

0 commit comments

Comments
 (0)