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

Add optional serde support #10

Open
wants to merge 1 commit into
base: master
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
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ categories = ["algorithms"]
license = "MIT"

[dependencies]
num-complex = { version = "0.4.0", features = ["serde", "rand"] }
num-complex = { version = "0.4.0", features = ["rand"] }
num-traits = "0.2.14"
rand = "0.8.3"
serde = "1.0.124"
serde = { version = "1.0.124", optional = true }

[features]
serde = ["dep:serde", "num-complex/serde"]

[package.metadata.release]
no-dev-version = true
85 changes: 84 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@
use num_complex::Complex;
use num_traits::{Float, FromPrimitive, NumAssign, NumCast, NumOps, ToPrimitive, Zero};
use rand::{distributions::Standard, prelude::*};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, LowerExp, UpperExp};
use std::iter::{Product, Sum};
use std::ops::Neg;

pub use num_complex::Complex32 as c32;
pub use num_complex::Complex64 as c64;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "serde")]
pub trait Scalar:
NumAssign
+ FromPrimitive
Expand Down Expand Up @@ -129,6 +132,86 @@ pub trait Scalar:
fn rand(rng: &mut impl Rng) -> Self;
}

#[cfg(not(feature = "serde"))]
pub trait Scalar:
NumAssign
+ FromPrimitive
+ NumCast
+ Neg<Output = Self>
+ Copy
+ Clone
+ Display
+ Debug
+ LowerExp
+ UpperExp
+ Sum
+ Product
+ 'static
{
type Real: Scalar<Real = Self::Real, Complex = Self::Complex>
+ NumOps<Self::Real, Self::Real>
+ Float;
type Complex: Scalar<Real = Self::Real, Complex = Self::Complex>
+ NumOps<Self::Real, Self::Complex>
+ NumOps<Self::Complex, Self::Complex>;

/// Create a new real number
fn real<T: ToPrimitive>(re: T) -> Self::Real;
/// Create a new complex number
fn complex<T: ToPrimitive>(re: T, im: T) -> Self::Complex;

fn from_real(re: Self::Real) -> Self;

fn add_real(self, re: Self::Real) -> Self;
fn sub_real(self, re: Self::Real) -> Self;
fn mul_real(self, re: Self::Real) -> Self;
fn div_real(self, re: Self::Real) -> Self;

fn add_complex(self, im: Self::Complex) -> Self::Complex;
fn sub_complex(self, im: Self::Complex) -> Self::Complex;
fn mul_complex(self, im: Self::Complex) -> Self::Complex;
fn div_complex(self, im: Self::Complex) -> Self::Complex;

fn pow(self, n: Self) -> Self;
fn powi(self, n: i32) -> Self;
fn powf(self, n: Self::Real) -> Self;
fn powc(self, n: Self::Complex) -> Self::Complex;

/// Real part
fn re(&self) -> Self::Real;
/// Imaginary part
fn im(&self) -> Self::Real;
/// As a complex number
fn as_c(&self) -> Self::Complex;
/// Complex conjugate
fn conj(&self) -> Self;

/// Absolute value
fn abs(self) -> Self::Real;
/// Sqaure of absolute value
fn square(self) -> Self::Real;

fn sqrt(self) -> Self;
fn exp(self) -> Self;
fn ln(self) -> Self;
fn sin(self) -> Self;
fn cos(self) -> Self;
fn tan(self) -> Self;
fn asin(self) -> Self;
fn acos(self) -> Self;
fn atan(self) -> Self;
fn sinh(self) -> Self;
fn cosh(self) -> Self;
fn tanh(self) -> Self;
fn asinh(self) -> Self;
fn acosh(self) -> Self;
fn atanh(self) -> Self;

/// Generate an random number from
/// [rand::distributions::Standard](https://docs.rs/rand/0.7.2/rand/distributions/struct.Standard.html)
fn rand(rng: &mut impl Rng) -> Self;
}

macro_rules! impl_float {
($name:ident) => {
#[inline]
Expand Down