Struct rug::complex::SmallComplex [] [src]

#[repr(C)]
pub struct SmallComplex { /* fields omitted */ }

A small complex number that does not require any memory allocation.

This can be useful when you have real and imaginary numbers that are primitive integers or floats and you need a reference to a Complex. The SmallComplex will have a precision according to the type of the primitive used to set its value.

  • i8, u8: the SmallComplex will have eight bits of precision.
  • i16, u16: the SmallComplex will have 16 bits of precision.
  • i32, u32: the SmallComplex will have 32 bits of precision.
  • i64, u64: the SmallComplex will have 64 bits of precision.
  • f32: the SmallComplex will have 24 bits of precision.
  • f64: the SmallComplex will have 53 bits of precision.

The SmallComplex type can be coerced to a Complex, as it implements Deref with a Complex target.

Examples

use rug::Complex;
use rug::complex::SmallComplex;
// `a` requires a heap allocation
let mut a = Complex::with_val(53, (1, 2));
// `b` can reside on the stack
let b = SmallComplex::from((-10f64, -20.5f64));
a += &*b;
assert_eq!(*a.real(), -9);
assert_eq!(*a.imag(), -18.5);

Methods

impl SmallComplex
[src]

Creates a SmallComplex with value 0.

Methods from Deref<Target = Complex>

Returns the precision of the real and imaginary parts.

Examples

use rug::Complex;
let r = Complex::new((24, 53));
assert_eq!(r.prec(), (24, 53));

Returns a string representation of the value for the specified radix rounding to the nearest.

The exponent is encoded in decimal. If the number of digits is not specified, the output string will have enough precision such that reading it again will give the exact same number.

Examples

use rug::Complex;
let c1 = Complex::with_val(53, 0);
assert_eq!(c1.to_string_radix(10, None), "(0.0 0.0)");
let c2 = Complex::with_val(12, (15, 5));
assert_eq!(c2.to_string_radix(16, None), "(f.000 5.000)");
let c3 = Complex::with_val(53, (10, -4));
assert_eq!(c3.to_string_radix(10, Some(3)), "(1.00e1 -4.00)");
assert_eq!(c3.to_string_radix(5, Some(3)), "(2.00e1 -4.00)");

Panics

Panics if radix is less than 2 or greater than 36.

Returns a string representation of the value for the specified radix applying the specified rounding method.

The exponent is encoded in decimal. If the number of digits is not specified, the output string will have enough precision such that reading it again will give the exact same number.

Examples

use rug::Complex;
use rug::float::Round;
let c = Complex::with_val(10, 10.4);
let down = (Round::Down, Round::Down);
let nearest = (Round::Nearest, Round::Nearest);
let up = (Round::Up, Round::Up);
let nd = c.to_string_radix_round(10, None, down);
assert_eq!(nd, "(1.0406e1 0.0)");
let nu = c.to_string_radix_round(10, None, up);
assert_eq!(nu, "(1.0407e1 0.0)");
let sd = c.to_string_radix_round(10, Some(2), down);
assert_eq!(sd, "(1.0e1 0.0)");
let sn = c.to_string_radix_round(10, Some(2), nearest);
assert_eq!(sn, "(1.0e1 0.0)");
let su = c.to_string_radix_round(10, Some(2), up);
assert_eq!(su, "(1.1e1 0.0)");

Panics

Panics if radix is less than 2 or greater than 36.

Borrows the real part as a Float.

Examples

use rug::Complex;
let c = Complex::with_val(53, (12.5, -20.75));
assert_eq!(*c.real(), 12.5)

Borrows the imaginary part as a Float.

Examples

use rug::Complex;
let c = Complex::with_val(53, (12.5, -20.75));
assert_eq!(*c.imag(), -20.75)

Borrows the real and imaginary parts.

Examples

use rug::Complex;
let c = Complex::with_val(53, (12.5, -20.75));
assert_eq!(c, (12.5, -20.75));
let (re, im) = c.as_real_imag();
assert_eq!(*re, 12.5);
assert_eq!(*im, -20.75);

Consumes and converts the value into real and imaginary Float values.

This function reuses the allocated memory and does not allocate any new memory.

use rug::Complex;
let c = Complex::with_val(53, (12.5, -20.75));
let (real, imag) = c.into_real_imag();
assert_eq!(real, 12.5);
assert_eq!(imag, -20.75);

Computes a projection onto the Riemann sphere, rounding to the nearest.

If no parts of the number are infinite, the result is unchanged. If any part is infinite, the real part of the result is set to +∞ and the imaginary part of the result is set to 0 with the same sign as the imaginary part of the input.

Examples

use rug::Complex;
use std::f64;
let c1 = Complex::with_val(53, (1.5, 2.5));
let proj1 = c1.proj();
assert_eq!(proj1, (1.5, 2.5));
let c2 = Complex::with_val(53, (f64::NAN, f64::NEG_INFINITY));
let proj2 = c2.proj();
assert_eq!(proj2, (f64::INFINITY, 0.0));
// imaginary was negative, so now it is minus zero
assert!(proj2.imag().get_sign());

Computes the projection onto the Riemann sphere.

If no parts of the number are infinite, the result is unchanged. If any part is infinite, the real part of the result is set to +∞ and the imaginary part of the result is set to 0 with the same sign as the imaginary part of the input.

Examples

use rug::Complex;
use std::f64;
let c1 = Complex::with_val(53, (f64::INFINITY, 50));
let proj1 = Complex::with_val(53, c1.proj_ref());
assert_eq!(proj1, (f64::INFINITY, 0.0));
let c2 = Complex::with_val(53, (f64::NAN, f64::NEG_INFINITY));
let proj2 = Complex::with_val(53, c2.proj_ref());
assert_eq!(proj2, (f64::INFINITY, 0.0));
// imaginary was negative, so now it is minus zero
assert!(proj2.imag().get_sign());

Computes the square, rounding to the nearest.

Examples

use rug::Complex;
let c = Complex::with_val(53, (1, -2));
// (1 - 2i) squared is (-3 - 4i)
let square = c.square();
assert_eq!(square, (-3, -4));

Computes the square.

Examples

use rug::Complex;
use rug::float::Round;
use std::cmp::Ordering;
let c = Complex::with_val(53, (1.25, 1.25));
// (1.25 + 1.25i) squared is (0 + 3.125i).
let r = c.square_ref();
// With 4 bits of precision, 3.125 is rounded down to 3.
let round = (Round::Down, Round::Down);
let (square, dir) = Complex::with_val_round(4, r, round);
assert_eq!(square, (0, 3));
assert_eq!(dir, (Ordering::Equal, Ordering::Less));

Computes the square root, rounding to the nearest.

Examples

use rug::Complex;
let c = Complex::with_val(53, (-1, 0));
// square root of (-1 + 0i) is (0 + i)
let sqrt = c.sqrt();
assert_eq!(sqrt, (0, 1));

Computes the square root.

Examples

use rug::Complex;
use std::cmp::Ordering;
let c = Complex::with_val(53, (2, 2.25));
// Square root of (2 + 2.25i) is (1.5828 + 0.7108i).
let r = c.sqrt_ref();
// Nearest with 4 bits of precision: (1.625 + 0.6875i)
let (sqrt, dir) = Complex::with_val_round(4, r, Default::default());
assert_eq!(sqrt, (1.625, 0.6875));
assert_eq!(dir, (Ordering::Greater, Ordering::Less));

Computes the complex conjugate.

Examples

use rug::Complex;
let c = Complex::with_val(53, (1.5, 2.5));
let conj = c.conj();
assert_eq!(conj, (1.5, -2.5));

Computes the complex conjugate.

Examples

use rug::Complex;
let c = Complex::with_val(53, (1.5, 2.5));
let conj = Complex::with_val(53, c.conj_ref());
assert_eq!(conj, (1.5, -2.5));

Computes the absolute value and returns it as a Float with the precision of the real part.

Examples

use rug::Complex;
let c = Complex::with_val(53, (30, 40));
let f = c.abs();
assert_eq!(f, 50);

Computes the absolute value.

Examples

use rug::{Complex, Float};
let c = Complex::with_val(53, (30, 40));
let f = Float::with_val(53, c.abs_ref());
assert_eq!(f, 50);

Computes the argument, rounding to the nearest.

The argument is returned as a Float with the precision of the real part.

Examples

use rug::Complex;
let c = Complex::with_val(53, (4, 3));
let f = c.arg();
assert_eq!(f, 0.75_f64.atan());

Special values are handled like atan2 in IEEE 754-2008.

use rug::{Assign, Complex, Float};
use rug::float::Special;
use std::f64;
// f has precision 53, just like f64, so PI constants match.
let mut arg = Float::new(53);
let mut zero = Complex::new(53);
zero.assign((Special::Zero, Special::Zero));
arg.assign(zero.arg_ref());
assert!(arg.is_zero() && !arg.get_sign());
zero.assign((Special::Zero, Special::MinusZero));
arg.assign(zero.arg_ref());
assert!(arg.is_zero() && arg.get_sign());
zero.assign((Special::MinusZero, Special::Zero));
arg.assign(zero.arg_ref());
assert_eq!(arg, f64::consts::PI);
zero.assign((Special::MinusZero, Special::MinusZero));
arg.assign(zero.arg_ref());
assert_eq!(arg, -f64::consts::PI);

Computes the argument.

Examples

use rug::{Assign, Complex, Float};
use std::f64;
// f has precision 53, just like f64, so PI constants match.
let mut arg = Float::new(53);
let c_pos = Complex::with_val(53, 1);
arg.assign(c_pos.arg_ref());
assert!(arg.is_zero());
let c_neg = Complex::with_val(53, -1.3);
arg.assign(c_neg.arg_ref());
assert_eq!(arg, f64::consts::PI);
let c_pi_4 = Complex::with_val(53, (1.333, 1.333));
arg.assign(c_pi_4.arg_ref());
assert_eq!(arg, f64::consts::FRAC_PI_4);

Multiplies the complex number by ±i, rounding to the nearest.

Examples

use rug::Complex;
let c = Complex::with_val(53, (13, 24));
let rot1 = c.mul_i(false);
assert_eq!(rot1, (-24, 13));
let rot2 = rot1.mul_i(false);
assert_eq!(rot2, (-13, -24));
let rot2_less1 = rot2.mul_i(true);
assert_eq!(rot2_less1, (-24, 13));

Multiplies the complex number by ±i.

Examples

use rug::Complex;
let c = Complex::with_val(53, (13, 24));
let rotated = Complex::with_val(53, c.mul_i_ref(false));
assert_eq!(rotated, (-24, 13));

Computes the reciprocal, rounding to the nearest.

Examples

use rug::Complex;
let c = Complex::with_val(53, (1, 1));
// 1/(1 + i) = (0.5 - 0.5i)
let recip = c.recip();
assert_eq!(recip, (0.5, -0.5));

Computes the reciprocal.

Examples

use rug::Complex;
let c = Complex::with_val(53, (1, 1));
// 1/(1 + i) = (0.5 - 0.5i)
let recip = Complex::with_val(53, c.recip_ref());
assert_eq!(recip, (0.5, -0.5));

Computes the norm, that is the square of the absolute value, rounding it to the nearest.

The norm is returned as a Float with the precision of the real part.

Examples

use rug::Complex;
let c = Complex::with_val(53, (3, 4));
let f = c.norm();
assert_eq!(f, 25);

Computes the norm, that is the square of the absolute value.

Examples

use rug::{Complex, Float};
let c = Complex::with_val(53, (3, 4));
let f = Float::with_val(53, c.norm_ref());
assert_eq!(f, 25);

Computes the natural logarithm, rounding to the nearest.

Computes the natural logarithm;

Computes the logarithm to base 10, rounding to the nearest.

Computes the logarithm to base 10.

Computes the exponential, rounding to the nearest.

Computes the exponential.

Computes the sine, rounding to the nearest.

Computes the sine.

Computes the cosine, rounding to the nearest.

Computes the cosine.

Computes the sine and cosine of self, rounding to the nearest.

The sine is stored in self and keeps its precision, while the cosine is stored in cos keeping its precision.

Computes the sine and cosine.

Examples

use rug::{Assign, Complex};
// sin(0.5 + 0.2i) = 0.48905 + 0.17669i
// cos(0.5 + 0.2i) = 0.89519 - 0.096526i
let angle = Complex::with_val(53, (0.5, 0.2));
let r = angle.sin_cos_ref();
// use only 10 bits of precision here to
// make comparison easier
let (mut sin, mut cos) = (Complex::new(10), Complex::new(10));
(&mut sin, &mut cos).assign(r);
assert_eq!(sin, Complex::with_val(10, (0.48905, 0.17669)));
assert_eq!(cos, Complex::with_val(10, (0.89519, -0.096526)));

Computes the tangent, rounding to the nearest.

Computes the tangent.

Computes the hyperbolic sine, rounding to the nearest.

Computes the hyperbolic sine.

Computes the hyperbolic cosine, rounding to the nearest.

Computes the hyperbolic cosine.

Computes the hyperbolic tangent, rounding to the nearest.

Computes the hyperbolic tangent.

Computes the inverse sine, rounding to the nearest.

Computes the inverse sine.

Computes the inverse cosine, rounding to the nearest.

Computes the inverse cosine.

Computes the inverse tangent, rounding to the nearest.

Computes the inverse tangent.

Computes the inverse hyperbolic sine, rounding to the nearest.

Computes the inverse hyperboic sine.

Computes the inverse hyperbolic cosine, rounding to the nearest.

Computes the inverse hyperbolic cosine.

Computes the inverse hyperbolic tangent, rounding to the nearest.

Computes the inverse hyperbolic tangent.

Trait Implementations

impl Default for SmallComplex
[src]

Returns the "default value" for a type. Read more

impl Deref for SmallComplex
[src]

The resulting type after dereferencing

The method called to dereference a value

impl<T> From<T> for SmallComplex where
    SmallComplex: Assign<T>, 
[src]

Performs the conversion.

impl Assign<i8> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<i16> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<i32> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<i64> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<u8> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<u16> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<u32> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<u64> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i8, i8)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i16, i16)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i32, i32)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i64, i64)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i8, u8)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i16, u16)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i32, u32)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(i64, u64)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u8, i8)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u16, i16)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u32, i32)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u64, i64)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u8, u8)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u16, u16)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u32, u32)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(u64, u64)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<f32> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<f64> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(f32, f32)> for SmallComplex
[src]

Peforms the assignement. Read more

impl Assign<(f64, f64)> for SmallComplex
[src]

Peforms the assignement. Read more