Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved division to ComplexField and implemented Div for missing types. #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions faer-entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,18 @@ pub trait ComplexField:
+ core::ops::Add<Self, Output = Self>
+ core::ops::Sub<Self, Output = Self>
+ core::ops::Mul<Self, Output = Self>
+ core::ops::Div<Self, Output = Self>
+ core::ops::AddAssign<Self>
+ core::ops::SubAssign<Self>
+ core::ops::MulAssign<Self>
+ core::ops::DivAssign<Self>
{
type Real: RealField;
type Simd: SimdCtx;
type ScalarSimd: SimdCtx;
type PortableSimd: SimdCtx;

/// Converts `value` from `f64` to `Self`.
/// Converts `value` from `f64` to `Self`.
/// The conversion may be lossy when converting to a type with less precision.
fn faer_from_f64(value: f64) -> Self;

Expand All @@ -441,6 +443,8 @@ pub trait ComplexField:
fn faer_sub(self, rhs: Self) -> Self;
/// Returns `self * rhs`.
fn faer_mul(self, rhs: Self) -> Self;
/// Returns `self / rhs`.
fn faer_div(self, rhs: Self) -> Self;

/// Returns `-self`.
fn faer_neg(self) -> Self;
Expand Down Expand Up @@ -706,8 +710,6 @@ pub trait RealField:
fn faer_min_positive_sqrt() -> Self;
fn faer_min_positive_sqrt_inv() -> Self;

fn faer_div(self, rhs: Self) -> Self;

fn faer_usize_to_index(a: usize) -> IndexFor<Self>;
fn faer_index_to_usize(a: IndexFor<Self>) -> usize;
fn faer_max_index() -> IndexFor<Self>;
Expand Down Expand Up @@ -788,6 +790,11 @@ impl ComplexField for f32 {
self * rhs
}

#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
self / rhs
}

#[inline(always)]
fn faer_neg(self) -> Self {
-self
Expand Down Expand Up @@ -1117,6 +1124,11 @@ impl ComplexField for f64 {
self * rhs
}

#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
self / rhs
}

#[inline(always)]
fn faer_neg(self) -> Self {
-self
Expand Down Expand Up @@ -1429,11 +1441,6 @@ impl RealField for f32 {
Self::MIN_POSITIVE
}

#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
self / rhs
}

#[inline(always)]
fn faer_usize_to_index(a: usize) -> IndexFor<Self> {
a as _
Expand Down Expand Up @@ -1563,10 +1570,6 @@ impl RealField for f64 {
fn faer_zero_threshold() -> Self {
Self::MIN_POSITIVE
}
#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
self / rhs
}

#[inline(always)]
fn faer_usize_to_index(a: usize) -> IndexFor<Self> {
Expand Down Expand Up @@ -2159,6 +2162,18 @@ impl<E: RealField> ComplexField for Complex<E> {
}
}

#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
Self {
// (self.re * rhs.re + self.im * rhs.im)/(rhs.re^2 + rhs.im^2)
re: Self::Real::faer_div(Self::Real::faer_add(self.re.faer_mul(rhs.re), self.im.faer_mul(rhs.im)),
rhs.faer_abs2()),
// (self.im * rhs.re - self.re * rhs.im)/(rhs.re^2 + rhs.im^2)
im: Self::Real::faer_div(Self::Real::faer_sub(self.im.faer_mul(rhs.re), self.re.faer_mul(rhs.im)),
rhs.faer_abs2()),
}
}

#[inline(always)]
fn faer_neg(self) -> Self {
Self {
Expand Down Expand Up @@ -2912,10 +2927,6 @@ impl RealField for Symbolic {
Self
}

#[inline(always)]
fn faer_div(self, _rhs: Self) -> Self {
Self
}

#[inline(always)]
fn faer_usize_to_index(a: usize) -> Self::Index {
Expand Down Expand Up @@ -3069,6 +3080,11 @@ impl ComplexField for Symbolic {
Self
}

#[inline(always)]
fn faer_div(self, _rhs: Self) -> Self {
Self
}

#[inline(always)]
fn faer_neg(self) -> Self {
Self
Expand Down
8 changes: 8 additions & 0 deletions src/complex_native/c32_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ impl ComplexField for c32 {
}
}

#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
Self {
re: (self.re * rhs.re + self.im * rhs.im) / (rhs.re.powi(2) + rhs.im.powi(2)),
im: (self.im * rhs.re - self.re * rhs.im) / (rhs.re.powi(2) + rhs.im.powi(2)),
}
}

#[inline(always)]
fn faer_neg(self) -> Self {
Self {
Expand Down
7 changes: 7 additions & 0 deletions src/complex_native/c64_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,13 @@ impl ComplexField for c64 {
im: self.re * rhs.im + self.im * rhs.re,
}
}
#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
Self {
re: (self.re * rhs.re + self.im * rhs.im) / (rhs.re.powi(2) + rhs.im.powi(2)),
im: (self.im * rhs.re - self.re * rhs.im) / (rhs.re.powi(2) + rhs.im.powi(2)),
}
}

#[inline(always)]
fn faer_neg(self) -> Self {
Expand Down
17 changes: 17 additions & 0 deletions src/linalg/mat_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,17 @@ impl<E: ComplexField, LhsE: Conjugate<Canonical = E>, RhsE: Conjugate<Canonical
}
}

impl<E: ComplexField, LhsE: Conjugate<Canonical = E>, RhsE: Conjugate<Canonical = E>>
Div<Scale<RhsE>> for Scale<LhsE>
{
type Output = Scale<E>;

#[inline]
fn div(self, rhs: Scale<RhsE>) -> Self::Output {
Scale(self.0.canonicalize().faer_div(rhs.0.canonicalize()))
}
}

impl<LhsE: ComplexField, RhsE: Conjugate<Canonical = LhsE>> MulAssign<Scale<RhsE>> for Scale<LhsE> {
#[inline]
fn mul_assign(&mut self, rhs: Scale<RhsE>) {
Expand All @@ -1186,6 +1197,12 @@ impl<LhsE: ComplexField, RhsE: Conjugate<Canonical = LhsE>> SubAssign<Scale<RhsE
self.0 = self.0.faer_sub(rhs.0.canonicalize())
}
}
impl<LhsE: ComplexField, RhsE: Conjugate<Canonical = LhsE>> DivAssign<Scale<RhsE>> for Scale<LhsE> {
#[inline]
fn div_assign(&mut self, rhs: Scale<RhsE>) {
self.0 = self.0.faer_div(rhs.0.canonicalize())
}
}

impl<E: ComplexField, LhsE: Conjugate<Canonical = E>, RhsE: Conjugate<Canonical = E>>
Mul<MatRef<'_, RhsE>> for MatRef<'_, LhsE>
Expand Down
9 changes: 4 additions & 5 deletions src/sparse/linalg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,6 @@ pub(crate) mod qd {
Self::MIN_POSITIVE
}

#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
self / rhs
}

#[inline(always)]
fn faer_usize_to_index(a: usize) -> Self::Index {
a as _
Expand Down Expand Up @@ -1014,6 +1009,10 @@ pub(crate) mod qd {
fn faer_mul(self, rhs: Self) -> Self {
self * rhs
}
#[inline(always)]
fn faer_div(self, rhs: Self) -> Self {
self / rhs
}

#[inline(always)]
fn faer_neg(self) -> Self {
Expand Down
Loading