diff --git a/src/lib.rs b/src/lib.rs index 7cadec5..81bab8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,11 +92,42 @@ pub struct Complex { pub im: T, } +/// Alias for a [`Complex`] pub type Complex32 = Complex; + +/// Create a new [`Complex`] with arguments that can convert [`Into`]. +/// +/// ``` +/// use num_complex::{c32, Complex32}; +/// assert_eq!(c32(1u8, 2), Complex32::new(1.0, 2.0)); +/// ``` +/// +/// Note: ambiguous integer literals in Rust will [default] to `i32`, which does **not** implement +/// `Into`, so a call like `c32(1, 2)` will result in a type error. The example above uses a +/// suffixed `1u8` to set its type, and then the `2` can be inferred as the same type. +/// +/// [default]: https://doc.rust-lang.org/reference/expressions/literal-expr.html#integer-literal-expressions +#[inline] +pub fn c32>(re: T, im: T) -> Complex32 { + Complex::new(re.into(), im.into()) +} + +/// Alias for a [`Complex`] pub type Complex64 = Complex; +/// Create a new [`Complex`] with arguments that can convert [`Into`]. +/// +/// ``` +/// use num_complex::{c64, Complex64}; +/// assert_eq!(c64(1, 2), Complex64::new(1.0, 2.0)); +/// ``` +#[inline] +pub fn c64>(re: T, im: T) -> Complex64 { + Complex::new(re.into(), im.into()) +} + impl Complex { - /// Create a new Complex + /// Create a new `Complex` #[inline] pub const fn new(re: T, im: T) -> Self { Complex { re, im }