Skip to content

Commit

Permalink
Fix d128 support, except for the Real and SubsetOf traits.
Browse files Browse the repository at this point in the history
The Real trait requires some work on the `decimal` trait first.
Related to #49.
  • Loading branch information
sebcrozet committed Sep 20, 2018
1 parent fe25a8d commit 010a2af
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 40 deletions.
10 changes: 6 additions & 4 deletions alga/src/general/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::cmp::{Ordering, PartialOrd};
use std::fmt;
use std::marker::PhantomData;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign};
#[cfg(feature = "decimal")]
use decimal::d128;

use num::{Num, One, Zero};

Expand Down Expand Up @@ -32,12 +34,12 @@ pub trait Identity<O: Operator> {

impl_ident!(Additive; 0; u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
impl_ident!(Additive; 0.; f32, f64);
#[cfg(decimal)]
impl_ident!(Additive; 0.; decimal::d128);
#[cfg(feature = "decimal")]
impl_ident!(Additive; d128!(0.); d128);
impl_ident!(Multiplicative; 1; u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
impl_ident!(Multiplicative; 1.; f32, f64);
#[cfg(decimal)]
impl_ident!(Multiplicative; 1.; decimal::d128);
#[cfg(feature = "decimal")]
impl_ident!(Multiplicative; d128!(1.); d128);

impl<N: Identity<Additive>> Identity<Additive> for Complex<N> {
#[inline]
Expand Down
6 changes: 4 additions & 2 deletions alga/src/general/lattice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::cmp::{Ordering, PartialOrd};
#[cfg(feature = "decimal")]
use decimal::d128;

/// A set where every two elements have an infimum (i.e. greatest lower bound).
pub trait MeetSemilattice: Sized {
Expand Down Expand Up @@ -118,5 +120,5 @@ macro_rules! impl_lattice(
);

impl_lattice!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);
#[cfg(decimal)]
impl_lattice!(decimal::d128);
#[cfg(feature = "decimal")]
impl_lattice!(d128);
10 changes: 6 additions & 4 deletions alga/src/general/one_operator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use num::Num;
use num_complex::Complex;
use std::ops::{Add, Mul};
#[cfg(feature = "decimal")]
use decimal::d128;

use approx::RelativeEq;

Expand Down Expand Up @@ -372,11 +374,11 @@ macro_rules! impl_magma(
);

impl_magma!(Additive; add; u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);
#[cfg(decimal)]
impl_ident!(Additive; add; decimal::d128);
#[cfg(feature = "decimal")]
impl_magma!(Additive; add; d128);
impl_magma!(Multiplicative; mul; u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);
#[cfg(decimal)]
impl_ident!(Multiplicative; mul; decimal::d128);
#[cfg(feature = "decimal")]
impl_magma!(Multiplicative; mul; d128);

impl_monoid!(<Additive> for u8; u16; u32; u64; usize);
impl_monoid!(<Multiplicative> for u8; u16; u32; u64; usize);
Expand Down
12 changes: 7 additions & 5 deletions alga/src/general/operator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Operators traits and structures.
pub use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
#[cfg(feature = "decimal")]
use decimal::d128;

use num::Num;
use num_complex::Complex;
Expand Down Expand Up @@ -74,8 +76,8 @@ macro_rules! impl_additive_inverse(
);

impl_additive_inverse!(i8, i16, i32, i64, isize, f32, f64);
#[cfg(decimal)]
impl_additive_inverse!(decimal::d128);
#[cfg(feature = "decimal")]
impl_additive_inverse!(d128);

impl<N: Inverse<Additive>> Inverse<Additive> for Complex<N> {
#[inline]
Expand All @@ -101,10 +103,10 @@ impl Inverse<Multiplicative> for f64 {
}
}

#[cfg(decimal)]
impl Inverse<Multiplicative> for decimal::d128 {
#[cfg(feature = "decimal")]
impl Inverse<Multiplicative> for d128 {
#[inline]
fn inverse(&self) -> decimal::d128 {
fn inverse(&self) -> d128 {
d128!(1.0) / self
}
}
Expand Down
6 changes: 4 additions & 2 deletions alga/src/general/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use libm::F32Ext;
use libm::F64Ext;
#[cfg(not(feature = "std"))]
use num;
//#[cfg(feature = "decimal")]
//use decimal::d128;

#[allow(missing_docs)]

Expand Down Expand Up @@ -418,5 +420,5 @@ macro_rules! impl_real(
impl_real!(f32,f32,F32Ext; f64,f64,F64Ext);
#[cfg(feature = "std")]
impl_real!(f32,f32,f32; f64,f64,f64);
#[cfg(decimal)]
impl_real!(decimal::d128, decimal::d128);
//#[cfg(feature = "decimal")]
//impl_real!(d128, d128, d128);
42 changes: 22 additions & 20 deletions alga/src/general/subset.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use num::Zero;
use num_complex::Complex;
#[cfg(feature = "decimal")]
use decimal::d128;

/// Nested sets and conversions between them (using an injective mapping). Useful to work with
/// substructures. In generic code, it is preferable to use `SupersetOf` as trait bound whenever
Expand Down Expand Up @@ -143,24 +145,24 @@ impl_subset!(
f32 as f32, f64;
f64 as f32, f64;
);
#[cfg(decimal)]
impl_subset!(
u8 as decimal::d128;
u16 as decimal::d128;
u32 as decimal::d128;
u64 as decimal::d128;
usize as decimal::d128;

i8 as decimal::d128;
i16 as decimal::d128;
i32 as decimal::d128;
i64 as decimal::d128;
isize as decimal::d128;

f32 as decimal::d128;
f64 as decimal::d128;
decimal::d128 as decimal::d128;
);
//#[cfg(feature = "decimal")]
//impl_subset!(
// u8 as d128;
// u16 as d128;
// u32 as d128;
// u64 as d128;
// usize as d128;
//
// i8 as d128;
// i16 as d128;
// i32 as d128;
// i64 as d128;
// isize as d128;
//
// f32 as d128;
// f64 as d128;
// d128 as d128;
//);

impl<N1, N2: SupersetOf<N1>> SubsetOf<Complex<N2>> for Complex<N1> {
#[inline]
Expand Down Expand Up @@ -210,5 +212,5 @@ macro_rules! impl_scalar_subset_of_complex(
);

impl_scalar_subset_of_complex!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);
#[cfg(decimal)]
impl_scalar_subset_of_complex!(decimal::d128);
#[cfg(feature = "decimal")]
impl_scalar_subset_of_complex!(d128);
6 changes: 4 additions & 2 deletions alga/src/general/two_operators.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use approx::RelativeEq;
use num::Num;
use num_complex::Complex;
#[cfg(feature = "decimal")]
use decimal::d128;

use general::wrapper::Wrapper as W;
use general::{
Expand Down Expand Up @@ -258,8 +260,8 @@ macro_rules! impl_field(
*/
impl_ring_commutative!(<Additive, Multiplicative> for i8; i16; i32; i64; isize);
impl_field!(<Additive, Multiplicative> for f32; f64);
#[cfg(decimal)]
impl_field!(<Additive, Multiplicative> for decimal::d128);
#[cfg(feature = "decimal")]
impl_field!(<Additive, Multiplicative> for d128);

impl<N: Num + Clone + ClosedNeg + AbstractRing> AbstractRing for Complex<N> {}
impl<N: Num + Clone + ClosedNeg + AbstractRingCommutative> AbstractRingCommutative for Complex<N> {}
Expand Down
2 changes: 1 addition & 1 deletion alga/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#[macro_use]
extern crate approx;
#[cfg(decimal)]
#[cfg(feature = "decimal")]
extern crate decimal;
#[cfg(not(feature = "std"))]
extern crate libm;
Expand Down

0 comments on commit 010a2af

Please sign in to comment.