pub struct Complex<T> {
pub real: T,
pub imag: T,
}
Fields§
§real: T
Real part of the complex number
imag: T
Imaginary part of the complex number
Implementations§
source§impl Complex<f64>
impl Complex<f64>
sourcepub fn pow(&self, w: &Complex<f64>) -> Complex<f64>
pub fn pow(&self, w: &Complex<f64>) -> Complex<f64>
Return the complex number z raised to the power of a complex number w ( z^w )
sourcepub fn powf(&self, x: f64) -> Complex<f64>
pub fn powf(&self, x: f64) -> Complex<f64>
Return complex number z raised to the power of a real number x ( z^x )
sourcepub fn exp(&self) -> Complex<f64>
pub fn exp(&self) -> Complex<f64>
Return the complex exponential of the complex number z ( exp(z) )
sourcepub fn ln(&self) -> Complex<f64>
pub fn ln(&self) -> Complex<f64>
Return the complex logarithm of the complex number z ( ln(z) )
source§impl Complex<f64>
impl Complex<f64>
sourcepub fn sinh(&self) -> Complex<f64>
pub fn sinh(&self) -> Complex<f64>
Return the hyperbolic sine of a complex number z ( sinh(z) )
sourcepub fn cosh(&self) -> Complex<f64>
pub fn cosh(&self) -> Complex<f64>
Return the hyperbolic cosine of a complex number z ( cosh(z) )
sourcepub fn tanh(&self) -> Complex<f64>
pub fn tanh(&self) -> Complex<f64>
Return the hyperbolic tangent of a complex number z ( tanh(z) )
sourcepub fn sech(&self) -> Complex<f64>
pub fn sech(&self) -> Complex<f64>
Return the hyperbolic secant of a complex number z ( sech(z) )
sourcepub fn csch(&self) -> Complex<f64>
pub fn csch(&self) -> Complex<f64>
Return the hyperbolic cosecant of a complex number z ( csch(z) )
sourcepub fn coth(&self) -> Complex<f64>
pub fn coth(&self) -> Complex<f64>
Return the hyperbolic cotangent of a complex number z ( coth(z) )
sourcepub fn asinh(&self) -> Complex<f64>
pub fn asinh(&self) -> Complex<f64>
Return the inverse hyperbolic sine of a complex number z ( asinh(z) )
sourcepub fn acosh(&self) -> Complex<f64>
pub fn acosh(&self) -> Complex<f64>
Return the inverse hyperbolic cosine of a complex number z ( acosh(z) )
sourcepub fn atanh(&self) -> Complex<f64>
pub fn atanh(&self) -> Complex<f64>
Return the inverse hyperbolic tangent of a complex number z ( atanh(z) )
sourcepub fn asech(&self) -> Complex<f64>
pub fn asech(&self) -> Complex<f64>
Return the inverse hyperbolic secant of a complex number z ( asech(z) )
source§impl<T> Complex<T>
impl<T> Complex<T>
sourcepub const fn new(real: T, imag: T) -> Self
pub const fn new(real: T, imag: T) -> Self
Create a new complex number ( z = x + iy )
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("----- Complex numbers -----");
// Create a new complex number
let z = Cmplx::new( 1.0, -1.0 );
println!( " * z = {}", z );
println!( " * Re[z] = {}", z.real );
println!( " * Im[z] = {}", z.imag );
// Take the conjugate
let zbar = z.conj();
println!(" * zbar = {}", zbar );
// Modulus and arguement
println!( " * |z| = {}", z.abs() );
println!( " * arg(z) = {}", z.arg() );
// Arithmetic
let z1 = Complex::<f64>::new( 2.0, 1.0 );
let z2 = z;
println!( " * z1 = {}", z1 );
println!( " * z2 = {}", z2 );
println!( " * z1 + z2 = {}", z1 + z2 );
println!( " * z1 - z2 = {}", z1 - z2 );
println!( " * z1 * z2 = {}", z1 * z2 );
println!( " * z1 / z2 = {}", z1 / z2 );
let real: f64 = 2.0;
println!( " * z1 + 2 = {}", z1 + real );
println!( " * z1 - 2 = {}", z1 - real );
println!( " * z1 * 2 = {}", z1 * real );
println!( " * z1 / 2 = {}", z1 / real );
println!( " * z1 + i = {}", z1 + I );
println!( "--- FINISHED ---" );
}
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
println!( "------------------ Linear system ------------------" );
println!( " * Solving the linear system Ax = b, for x, where" );
let mut a = Mat64::new( 3, 3, 0.0 );
a[0][0] = 1.0; a[0][1] = 1.0; a[0][2] = 1.0;
a[1][0] = 0.0; a[1][1] = 2.0; a[1][2] = 5.0;
a[2][0] = 2.0; a[2][1] = 5.0; a[2][2] = - 1.0;
println!( " * A ={}", a );
let b = Vec64::create( vec![ 6.0, -4.0, 27.0 ] );
println!( " * b^T ={}", b );
let x = a.clone().solve_basic( b );
println!( " * gives the solution vector");
println!( " * x^T ={}", x );
println!( "---------------------------------------------------" );
println!( " * Lets solve the complex linear system Cy = d where" );
let mut c = Matrix::<Cmplx>::new( 2, 2, Cmplx::new( 1.0, 1.0 ) );
c[0][1] = Cmplx::new( -1.0, 0.0 );
c[1][0] = Cmplx::new( 1.0, -1.0 );
println!( " * C ={}", c );
let mut d = Vector::<Cmplx>::new( 2, Cmplx::new( 0.0, 1.0 ) );
d[1] = Cmplx::new( 1.0, 0.0 );
println!( " * d^T ={}", d );
println!( " * Using LU decomposition we find that ");
let y = c.clone().solve_lu( d );
println!( " * y^T ={}", y );
println!( "---------------------------------------------------" );
println!( " * We may also find the determinant of a matrix" );
println!( " * |A| = {}", a.clone().determinant() );
println!( " * |C| = {}", c.clone().determinant() );
println!( " * or the inverse of a matrix" );
let inverse = a.clone().inverse();
println!( " * A^-1 =\n{}", inverse );
println!( " * C^-1 =\n{}", c.clone().inverse() );
println!( " * We can check this by multiplication" );
println!( " * I = A * A^-1 =\n{}", a * inverse );
println!( " * which is sufficiently accurate for our purposes.");
println!( "-------------------- FINISHED ---------------------" );
}
source§impl<T: Clone + Signed> Complex<T>
impl<T: Clone + Signed> Complex<T>
sourcepub fn conj(&self) -> Self
pub fn conj(&self) -> Self
Return the complex conjugate ( z.conj() = x - iy )
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("----- Complex numbers -----");
// Create a new complex number
let z = Cmplx::new( 1.0, -1.0 );
println!( " * z = {}", z );
println!( " * Re[z] = {}", z.real );
println!( " * Im[z] = {}", z.imag );
// Take the conjugate
let zbar = z.conj();
println!(" * zbar = {}", zbar );
// Modulus and arguement
println!( " * |z| = {}", z.abs() );
println!( " * arg(z) = {}", z.arg() );
// Arithmetic
let z1 = Complex::<f64>::new( 2.0, 1.0 );
let z2 = z;
println!( " * z1 = {}", z1 );
println!( " * z2 = {}", z2 );
println!( " * z1 + z2 = {}", z1 + z2 );
println!( " * z1 - z2 = {}", z1 - z2 );
println!( " * z1 * z2 = {}", z1 * z2 );
println!( " * z1 / z2 = {}", z1 / z2 );
let real: f64 = 2.0;
println!( " * z1 + 2 = {}", z1 + real );
println!( " * z1 - 2 = {}", z1 - real );
println!( " * z1 * 2 = {}", z1 * real );
println!( " * z1 / 2 = {}", z1 / real );
println!( " * z1 + i = {}", z1 + I );
println!( "--- FINISHED ---" );
}
source§impl Complex<f64>
impl Complex<f64>
sourcepub fn abs(&self) -> f64
pub fn abs(&self) -> f64
Return the absolute value ( |z| = sqrt( z * z.conj() ) )
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("----- Complex numbers -----");
// Create a new complex number
let z = Cmplx::new( 1.0, -1.0 );
println!( " * z = {}", z );
println!( " * Re[z] = {}", z.real );
println!( " * Im[z] = {}", z.imag );
// Take the conjugate
let zbar = z.conj();
println!(" * zbar = {}", zbar );
// Modulus and arguement
println!( " * |z| = {}", z.abs() );
println!( " * arg(z) = {}", z.arg() );
// Arithmetic
let z1 = Complex::<f64>::new( 2.0, 1.0 );
let z2 = z;
println!( " * z1 = {}", z1 );
println!( " * z2 = {}", z2 );
println!( " * z1 + z2 = {}", z1 + z2 );
println!( " * z1 - z2 = {}", z1 - z2 );
println!( " * z1 * z2 = {}", z1 * z2 );
println!( " * z1 / z2 = {}", z1 / z2 );
let real: f64 = 2.0;
println!( " * z1 + 2 = {}", z1 + real );
println!( " * z1 - 2 = {}", z1 - real );
println!( " * z1 * 2 = {}", z1 * real );
println!( " * z1 / 2 = {}", z1 / real );
println!( " * z1 + i = {}", z1 + I );
println!( "--- FINISHED ---" );
}
source§impl Complex<f64>
impl Complex<f64>
sourcepub fn arg(&self) -> f64
pub fn arg(&self) -> f64
Return the phase angle in radians
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
println!("----- Complex numbers -----");
// Create a new complex number
let z = Cmplx::new( 1.0, -1.0 );
println!( " * z = {}", z );
println!( " * Re[z] = {}", z.real );
println!( " * Im[z] = {}", z.imag );
// Take the conjugate
let zbar = z.conj();
println!(" * zbar = {}", zbar );
// Modulus and arguement
println!( " * |z| = {}", z.abs() );
println!( " * arg(z) = {}", z.arg() );
// Arithmetic
let z1 = Complex::<f64>::new( 2.0, 1.0 );
let z2 = z;
println!( " * z1 = {}", z1 );
println!( " * z2 = {}", z2 );
println!( " * z1 + z2 = {}", z1 + z2 );
println!( " * z1 - z2 = {}", z1 - z2 );
println!( " * z1 * z2 = {}", z1 * z2 );
println!( " * z1 / z2 = {}", z1 / z2 );
let real: f64 = 2.0;
println!( " * z1 + 2 = {}", z1 + real );
println!( " * z1 - 2 = {}", z1 - real );
println!( " * z1 * 2 = {}", z1 * real );
println!( " * z1 / 2 = {}", z1 / real );
println!( " * z1 + i = {}", z1 + I );
println!( "--- FINISHED ---" );
}
Trait Implementations§
source§impl<T: Number> AddAssign<T> for Complex<T>
impl<T: Number> AddAssign<T> for Complex<T>
source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
Add a real number to a mutable complex variable and assign the result to that variable ( += )
source§impl<T: Number> AddAssign for Complex<T>
impl<T: Number> AddAssign for Complex<T>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Add a complex number to a mutable complex variable and assign the result to that variable ( += )
source§impl<T: Clone + Number> DivAssign<T> for Complex<T>
impl<T: Clone + Number> DivAssign<T> for Complex<T>
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Divide a mutable complex variable by a real number and assign the result to that variable ( /= )
source§impl<T: Clone + Number> DivAssign for Complex<T>
impl<T: Clone + Number> DivAssign for Complex<T>
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Divide a mutable complex variable by a complex number and assign the result to that variable ( *= )
source§impl<T: Clone + Number> MulAssign<T> for Complex<T>
impl<T: Clone + Number> MulAssign<T> for Complex<T>
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Multiply a mutable complex variable by a real number and assign the result to that variable ( *= )
source§impl<T: Clone + Number> MulAssign for Complex<T>
impl<T: Clone + Number> MulAssign for Complex<T>
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Multipy a mutable complex variable by a complex number and assign the result to that variable ( *= )
source§impl<T: Clone + Number + PartialOrd> PartialOrd for Complex<T>
impl<T: Clone + Number + PartialOrd> PartialOrd for Complex<T>
source§impl<T: Number> SubAssign<T> for Complex<T>
impl<T: Number> SubAssign<T> for Complex<T>
source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
Subtract a real number from a mutable complex variable and assign the result to that variable ( += )
source§impl<T: Number> SubAssign for Complex<T>
impl<T: Number> SubAssign for Complex<T>
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Subtract a complex number from a mutable complex variable and assign the result to that variable ( -= )