Struct rug::complex::MiniComplex
source · pub struct MiniComplex { /* private fields */ }
Expand description
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 MiniComplex
will have a precision according to the types of the
primitives used to set its real and imaginary parts. Note that if different
types are used to set the parts, the parts can have different precisions.
i8
,u8
: the part will have eight bits of precision.i16
,u16
: the part will have 16 bits of precision.i32
,u32
: the part will have 32 bits of precision.i64
,u64
: the part will have 64 bits of precision.i128
,u128
: the part will have 128 bits of precision.isize
,usize
: the part will have 32 or 64 bits of precision, depending on the platform.f32
: the part will have 24 bits of precision.f64
: the part will have 53 bits of precision.Special
: the part will have the minimum possible precision.
The borrow
method returns an object that can be coerced to a
Complex
, as it implements
Deref<Target = Complex>
.
Examples
use rug::complex::MiniComplex;
use rug::Complex;
// `a` requires a heap allocation
let mut a = Complex::with_val(53, (1, 2));
// `b` can reside on the stack
let b = MiniComplex::from((-10f64, -20.5f64));
a += &*b.borrow();
assert_eq!(*a.real(), -9);
assert_eq!(*a.imag(), -18.5);
Implementations§
source§impl MiniComplex
impl MiniComplex
sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a MiniComplex
with value 0 and the minimum possible
precision.
Examples
use rug::complex::MiniComplex;
let c = MiniComplex::new();
// Borrow c as if it were Complex.
assert_eq!(*c.borrow(), 0);
sourcepub unsafe fn as_nonreallocating_complex(&mut self) -> &mut Complex
pub unsafe fn as_nonreallocating_complex(&mut self) -> &mut Complex
Returns a mutable reference to a Complex
number for simple
operations that do not need to change the precision of the real or
imaginary part.
Safety
It is undefined behavior to modify the precision of the referenced
Complex
number or to swap it with another number.
Examples
use rug::complex::MiniComplex;
let mut c = MiniComplex::from((1.0f32, 3.0f32));
// rotation does not change the precision
unsafe {
c.as_nonreallocating_complex().mul_i_mut(false);
}
assert_eq!(*c.borrow(), (-3.0, 1.0));
sourcepub fn borrow(&self) -> impl Deref<Target = Complex> + '_
pub fn borrow(&self) -> impl Deref<Target = Complex> + '_
Borrows the complex number.
The returned object implements
Deref<Target = Complex>
.
The borrow lasts until the returned object exits scope. Multiple borrows can be taken at the same time.
Examples
use rug::complex::MiniComplex;
use rug::Complex;
let c = MiniComplex::from((-13f64, 5.5f64));
let b = c.borrow();
let conj = Complex::with_val(53, b.conj_ref());
assert_eq!(*conj.real(), -13);
assert_eq!(*conj.imag(), -5.5);
sourcepub fn borrow_excl(&mut self) -> &Complex
pub fn borrow_excl(&mut self) -> &Complex
Borrows the complex number exclusively.
This is similar to the borrow
method, but it requires
exclusive access to the underlying MiniComplex
; the returned
reference can however be shared. The exclusive access is required to
reduce the amount of housekeeping necessary, providing a more efficient
operation.
Examples
use rug::complex::MiniComplex;
use rug::Complex;
let mut c = MiniComplex::from((-13f64, 5.5f64));
let b = c.borrow_excl();
let conj = Complex::with_val(53, b.conj_ref());
assert_eq!(*conj.real(), -13);
assert_eq!(*conj.imag(), -5.5);
Trait Implementations§
source§impl Assign<&MiniComplex> for MiniComplex
impl Assign<&MiniComplex> for MiniComplex
source§impl<Re: ToMini> Assign<Re> for MiniComplex
impl<Re: ToMini> Assign<Re> for MiniComplex
source§impl AssignRound<&MiniComplex> for Complex
impl AssignRound<&MiniComplex> for Complex
source§impl AssignRound<MiniComplex> for Complex
impl AssignRound<MiniComplex> for Complex
source§impl Binary for MiniComplex
impl Binary for MiniComplex
source§impl Clone for MiniComplex
impl Clone for MiniComplex
source§fn clone(&self) -> MiniComplex
fn clone(&self) -> MiniComplex
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for MiniComplex
impl Debug for MiniComplex
source§impl Default for MiniComplex
impl Default for MiniComplex
source§impl Display for MiniComplex
impl Display for MiniComplex
source§impl<Re: ToMini> From<Re> for MiniComplex
impl<Re: ToMini> From<Re> for MiniComplex
source§impl LowerExp for MiniComplex
impl LowerExp for MiniComplex
source§impl LowerHex for MiniComplex
impl LowerHex for MiniComplex
source§impl Octal for MiniComplex
impl Octal for MiniComplex
source§impl PartialEq<Complex> for MiniComplex
impl PartialEq<Complex> for MiniComplex
source§impl PartialEq<MiniComplex> for Complex
impl PartialEq<MiniComplex> for Complex
source§fn eq(&self, other: &MiniComplex) -> bool
fn eq(&self, other: &MiniComplex) -> bool
self
and other
values to be equal, and is used
by ==
.