use crate::{
core::integer::FullInt, Circle, CircleConstants, ExponentConstants, FractionConstants, Integer,
Scalar, ScalarConstants,
};
use i256::I256;
use num_traits::{AsPrimitive, WrappingAdd, WrappingMul, WrappingNeg, WrappingSub};
pub trait IntoScalars<F: Integer, E: Integer> {
fn into_scalars(&self) -> (Scalar<F, E>, Scalar<F, E>);
}
impl<F: Integer, E: Integer> IntoScalars<F, E> for Circle<F, E>
where
F: FractionConstants
+ FullInt
+ core::ops::Shl<isize, Output = F>
+ core::ops::Shr<isize, Output = F>
+ core::ops::Shl<F, Output = F>
+ core::ops::Shr<F, Output = F>
+ core::ops::Shl<E, Output = F>
+ core::ops::Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
E: ExponentConstants
+ FullInt
+ core::ops::Shl<isize, Output = E>
+ core::ops::Shr<isize, Output = E>
+ core::ops::Shl<E, Output = E>
+ core::ops::Shr<E, Output = E>
+ core::ops::Shl<F, Output = E>
+ core::ops::Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
Circle<F, E>: CircleConstants,
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<F>,
I256: From<E>,
{
fn into_scalars(&self) -> (Scalar<F, E>, Scalar<F, E>) {
let mut real = Scalar {
fraction: self.real,
exponent: self.exponent,
};
let mut imaginary = Scalar {
fraction: self.imaginary,
exponent: self.exponent,
};
if self.is_normal() {
real.normalize();
imaginary.normalize();
} else if self.exploded() {
real.normalize_exploded();
imaginary.normalize_exploded();
} else if self.vanished() {
real.normalize_vanished();
imaginary.normalize_vanished();
}
(real, imaginary)
}
}
impl<F: Integer, E: Integer> IntoScalars<F, E> for &mut Circle<F, E>
where
F: FractionConstants
+ FullInt
+ core::ops::Shl<isize, Output = F>
+ core::ops::Shr<isize, Output = F>
+ core::ops::Shl<F, Output = F>
+ core::ops::Shr<F, Output = F>
+ core::ops::Shl<E, Output = F>
+ core::ops::Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
E: ExponentConstants
+ FullInt
+ core::ops::Shl<isize, Output = E>
+ core::ops::Shr<isize, Output = E>
+ core::ops::Shl<E, Output = E>
+ core::ops::Shr<E, Output = E>
+ core::ops::Shl<F, Output = E>
+ core::ops::Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
Circle<F, E>: CircleConstants,
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<F>,
I256: From<E>,
{
fn into_scalars(&self) -> (Scalar<F, E>, Scalar<F, E>) {
let mut real = Scalar {
fraction: self.real,
exponent: self.exponent,
};
let mut imaginary = Scalar {
fraction: self.imaginary,
exponent: self.exponent,
};
if self.is_normal() {
real.normalize();
imaginary.normalize();
} else if self.exploded() {
real.normalize_exploded();
imaginary.normalize_exploded();
} else if self.vanished() {
real.normalize_vanished();
imaginary.normalize_vanished();
}
(real, imaginary)
}
}
impl<F: Integer, E: Integer> IntoScalars<F, E> for &Circle<F, E>
where
F: FractionConstants
+ FullInt
+ core::ops::Shl<isize, Output = F>
+ core::ops::Shr<isize, Output = F>
+ core::ops::Shl<F, Output = F>
+ core::ops::Shr<F, Output = F>
+ core::ops::Shl<E, Output = F>
+ core::ops::Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
E: ExponentConstants
+ FullInt
+ core::ops::Shl<isize, Output = E>
+ core::ops::Shr<isize, Output = E>
+ core::ops::Shl<E, Output = E>
+ core::ops::Shr<E, Output = E>
+ core::ops::Shl<F, Output = E>
+ core::ops::Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
Circle<F, E>: CircleConstants,
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<F>,
I256: From<E>,
{
fn into_scalars(&self) -> (Scalar<F, E>, Scalar<F, E>) {
let mut real = Scalar {
fraction: self.real,
exponent: self.exponent,
};
let mut imaginary = Scalar {
fraction: self.imaginary,
exponent: self.exponent,
};
if self.is_normal() {
real.normalize();
imaginary.normalize();
} else if self.exploded() {
real.normalize_exploded();
imaginary.normalize_exploded();
} else if self.vanished() {
real.normalize_vanished();
imaginary.normalize_vanished();
}
(real, imaginary)
}
}