pub struct RComplex(/* private fields */);
Expand description
A Value pointer to a RComplex struct, Ruby’s internal representation of complex numbers.
See the ReprValue
trait for additional methods available on this type.
Implementations§
source§impl RComplex
impl RComplex
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RComplex)
if val
is a RComplex
, None
otherwise.
§Examples
use magnus::{eval, RComplex};
assert!(RComplex::from_value(eval("2+1i").unwrap()).is_some());
assert!(RComplex::from_value(eval("3").unwrap()).is_none());
sourcepub fn new<T, U>(real: T, imag: U) -> RComplex
pub fn new<T, U>(real: T, imag: U) -> RComplex
Create a new RComplex
.
§Examples
use magnus::{Integer, RComplex};
let complex = RComplex::new(Integer::from_i64(2), Integer::from_i64(1));
assert_eq!(complex.to_string(), "2+1i");
sourcepub fn polar<T, U>(real: T, imag: U) -> Result<RComplex, Error>
pub fn polar<T, U>(real: T, imag: U) -> Result<RComplex, Error>
Create a new RComplex
using polar representation.
§Examples
use magnus::{Integer, RComplex};
let complex = RComplex::polar(Integer::from_i64(2), Integer::from_i64(3)).unwrap();
assert_eq!(
complex.to_string(),
"-1.9799849932008908+0.2822400161197344i"
);
sourcepub fn real<T>(self) -> Result<T, Error>where
T: TryConvert,
pub fn real<T>(self) -> Result<T, Error>where
T: TryConvert,
Returns the real part of self
.
§Examples
use magnus::{Integer, RComplex};
let complex = RComplex::new(Integer::from_i64(9), Integer::from_i64(-4));
assert_eq!(complex.real::<i64>().unwrap(), 9);
sourcepub fn imag<T>(self) -> Result<T, Error>where
T: TryConvert,
pub fn imag<T>(self) -> Result<T, Error>where
T: TryConvert,
Returns the imaginary part of self
.
§Examples
use magnus::{Integer, RComplex};
let complex = RComplex::new(Integer::from_i64(9), Integer::from_i64(-4));
assert_eq!(complex.imag::<i64>().unwrap(), -4);
sourcepub fn conjugate(self) -> Self
pub fn conjugate(self) -> Self
Returns the complex conjugate.
§Examples
use magnus::{Integer, RComplex};
let complex = RComplex::new(Integer::from_i64(1), Integer::from_i64(2));
assert_eq!(complex.conjugate().to_string(), "1-2i");
sourcepub fn abs(self) -> f64
pub fn abs(self) -> f64
Returns the absolute (or the magnitude) of self
.
§Examples
use magnus::{Integer, RComplex};
let complex = RComplex::new(Integer::from_i64(3), Integer::from_i64(-4));
assert_eq!(complex.abs(), 5.0);
sourcepub fn arg(self) -> f64
pub fn arg(self) -> f64
Returns the argument (or the angle) of the polar form of self
.
§Examples
use std::f64::consts::PI;
use magnus::{Float, Integer, RComplex};
let complex = RComplex::polar(Integer::from_i64(3), Float::from_f64(PI / 2.0)).unwrap();
assert_eq!(complex.arg(), 1.5707963267948966);
Trait Implementations§
source§impl Numeric for RComplex
impl Numeric for RComplex
source§fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_bin<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
Apply the operator
op
with coercion. Read moresource§fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_cmp<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
Apply the operator
op
with coercion. Read moresource§fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
fn coerce_relop<T, ID, U>(self, other: T, op: ID) -> Result<U, Error>
Apply the operator
op
with coercion. Read moresource§impl ReprValue for RComplex
impl ReprValue for RComplex
source§fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
Checks for equality, delegating to the Ruby method
#==
. Read moresource§fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
Checks for equality, delegating to the Ruby method
#eql?
. Read moresource§fn hash(self) -> Result<Integer, Error>
fn hash(self) -> Result<Integer, Error>
Returns an integer non-uniquely identifying
self
. Read moresource§fn to_bool(self) -> bool
fn to_bool(self) -> bool
Convert
self
to a bool
, following Ruby’s rules of false
and nil
as boolean false
and everything else boolean true
. Read moresource§fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
source§fn block_call<M, A, R, T>(
self,
method: M,
args: A,
block: fn(_: &[Value], _: Option<Proc>) -> R
) -> Result<T, Error>
fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
source§fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
Check if
self
responds to the given Ruby method. Read moresource§fn is_kind_of<T>(self, class: T) -> bool
fn is_kind_of<T>(self, class: T) -> bool
source§fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator ⓘwhere
M: IntoSymbol,
A: ArgList,
fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator ⓘwhere
M: IntoSymbol,
A: ArgList,
source§impl TryConvert for RComplex
impl TryConvert for RComplex
impl Copy for RComplex
Auto Trait Implementations§
impl Freeze for RComplex
impl RefUnwindSafe for RComplex
impl Send for RComplex
impl Sync for RComplex
impl Unpin for RComplex
impl UnwindSafe for RComplex
Blanket Implementations§
source§impl<T> AsRawValue for Twhere
T: ReprValue,
impl<T> AsRawValue for Twhere
T: ReprValue,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more