complex_algebra/
lib.rs

1//! # complex_algebra
2//!
3//! This crate intends to support complex numbers and its standard algebraic operations.
4//! To construct a complex number with real part `u` and imaginary part `v` you can do
5//! ```
6//! use complex_algebra::c;
7//! let u = 2.0;
8//! let v = 3.0;
9//! let z = c(u, v);
10//! ```
11//!
12//! `u`, `v` can be any types like `i32`, `u32`, `f64`, ... that implement at the very minimum
13//! the traits `Copy` and `PartialEq`.
14//!
15//! Depending on the chosen type and its support for various algebraic operators,
16//! the following binary and unary functions are implemented:
17//!
18//! z1 + z2
19//!
20//! z1 - z2
21//!
22//! z1 * z2
23//!
24//! z1 / z2
25//!
26//! -z
27//!
28//! Moreover, all these binary operations do work when the r.h.s is being replaced with
29//! a 'real' number.
30//!
31//! ## Example:
32//! ```
33//! use complex_algebra::c;
34//! let z1 = c(2, 3);
35//! let z2 = c(1, 1);
36//!
37//! assert_eq!(&z1 + &z2, c(3, 4));
38//! assert_eq!(z1 * 2, c(4, 6));
39//!
40//! ```
41
42mod complex;
43pub use complex::*;
44use std::ops::Sub;
45
46/// Takes a real and transforms it to a number of type `c`.
47/// Corresponds to the embedding of a real number into a complex.
48///
49/// ```
50/// use complex_algebra::{c, re};
51/// assert_eq!(re(3), c(3, 0))
52/// ```
53pub fn re<T: Copy + PartialEq + Sub<Output = T>>(r: T) -> c<T> {
54    c(r, r - r)
55}