diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/commonsense.rs | 18 | ||||
| -rw-r--r-- | src/complex.rs | 58 | ||||
| -rw-r--r-- | src/context.rs | 1 | ||||
| -rw-r--r-- | src/expression_function.rs | 3 | ||||
| -rw-r--r-- | src/function.rs | 6 |
5 files changed, 42 insertions, 44 deletions
diff --git a/src/commonsense.rs b/src/commonsense.rs index 4570109..c42da00 100644 --- a/src/commonsense.rs +++ b/src/commonsense.rs @@ -43,10 +43,10 @@ macro_rules! commonsense_functions { use std::sync::Arc; let mut h : HashMap<String, Arc<dyn Function>> = HashMap::new(); h.extend(functions!{ - "sqrt" => &crate::commonsense::sqrt, - "sin" => &crate::commonsense::sin, - "cos" => &crate::commonsense::cos, - "tan" => &crate::commonsense::tan + "sqrt" => &$crate::commonsense::sqrt, + "sin" => &$crate::commonsense::sin, + "cos" => &$crate::commonsense::cos, + "tan" => &$crate::commonsense::tan }); $( h.insert($x.to_string(), Arc::new($y)); @@ -60,11 +60,11 @@ macro_rules! commonsense_functions { macro_rules! commonsense_operations { {$($x:expr => $y:expr), *} => { vec![ - Operation::new('+', crate::commonsense::add), - Operation::new('-', crate::commonsense::sub), - Operation::new('*', crate::commonsense::mul), - Operation::new('/', crate::commonsense::div), - Operation::new('^', crate::commonsense::pow) + Operation::new('+', $crate::commonsense::add), + Operation::new('-', $crate::commonsense::sub), + Operation::new('*', $crate::commonsense::mul), + Operation::new('/', $crate::commonsense::div), + Operation::new('^', $crate::commonsense::pow) $( Operation::new($x, Box::new($y)), )*] diff --git a/src/complex.rs b/src/complex.rs index e498234..d120c29 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -67,12 +67,10 @@ impl std::fmt::Display for Complex { } else { write!(f, "{}i", self.imag) } + } else if self.imag == 1.0 { + write!(f, "{} + i", self.real) } else { - if self.imag == 1.0 { - write!(f, "{} + i", self.real) - } else { - write!(f, "{} + {}i", self.real, self.imag) - } + write!(f, "{} + {}i", self.real, self.imag) } } } @@ -101,25 +99,23 @@ impl FromStr for Complex { c.real = a.parse()?; c.imag = b[..b.len() - 1].parse()?; } - } else { - if s.contains('i') { - if s.len() == 1 { - c.imag = 1.0; - } else { - c.imag = s[..s.len() - 1].parse()?; - } + } else if s.contains('i') { + if s.len() == 1 { + c.imag = 1.0; } else { - c.real = s.parse()?; + c.imag = s[..s.len() - 1].parse()?; } + } else { + c.real = s.parse()?; } Ok(c) } } -impl Into<Complex> for f64 { - fn into(self) -> Complex { - Complex::new(self, 0.0) +impl From<f64> for Complex { + fn from(val: f64) -> Self { + Complex::new(val, 0.0) } } @@ -127,7 +123,7 @@ impl std::ops::Add for &Complex { type Output = Complex; fn add(self, rhs: Self) -> Self::Output { - Complex::new(self.real + rhs.real, self.imag + rhs.imag) + *self + *rhs } } @@ -135,7 +131,7 @@ impl std::ops::Add for Complex { type Output = Complex; fn add(self, rhs: Self) -> Self::Output { - &self + &rhs + Complex::new(self.real + rhs.real, self.imag + rhs.imag) } } @@ -143,7 +139,7 @@ impl std::ops::Sub for &Complex { type Output = Complex; fn sub(self, rhs: Self) -> Self::Output { - Complex::new(self.real - rhs.real, self.imag - rhs.imag) + *self - *rhs } } @@ -151,7 +147,7 @@ impl std::ops::Sub for Complex { type Output = Complex; fn sub(self, rhs: Self) -> Self::Output { - &self - &rhs + Complex::new(self.real - rhs.real, self.imag - rhs.imag) } } @@ -159,10 +155,7 @@ impl std::ops::Mul for &Complex { type Output = Complex; fn mul(self, rhs: Self) -> Self::Output { - Complex::new( - self.real * rhs.real - self.imag * rhs.imag, - self.real * rhs.imag + self.imag * rhs.real, - ) + *self * *rhs } } @@ -170,7 +163,10 @@ impl std::ops::Mul for Complex { type Output = Complex; fn mul(self, rhs: Self) -> Self::Output { - &self * &rhs + Complex::new( + self.real * rhs.real - self.imag * rhs.imag, + self.real * rhs.imag + self.imag * rhs.real, + ) } } @@ -178,11 +174,7 @@ impl std::ops::Div for &Complex { type Output = Complex; fn div(self, rhs: Self) -> Self::Output { - let z = rhs.real * rhs.real + rhs.imag * rhs.imag; - Complex::new( - (self.real * rhs.real + self.imag * rhs.imag) / z, - (self.imag * rhs.real - self.real * rhs.imag) / z, - ) + *self / *rhs } } @@ -190,6 +182,10 @@ impl std::ops::Div for Complex { type Output = Complex; fn div(self, rhs: Self) -> Self::Output { - &self / &rhs + let z = rhs.real * rhs.real + rhs.imag * rhs.imag; + Complex::new( + (self.real * rhs.real + self.imag * rhs.imag) / z, + (self.imag * rhs.real - self.real * rhs.imag) / z, + ) } } diff --git a/src/context.rs b/src/context.rs index 04e27f7..409600f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -47,7 +47,6 @@ impl Context { self.vars.get(name) } - #[allow(clippy::borrowed_box)] pub fn function(&self, name: &str) -> Option<&Arc<dyn Function>> { self.funcs.get(name) } diff --git a/src/expression_function.rs b/src/expression_function.rs index 138891f..8cafd76 100644 --- a/src/expression_function.rs +++ b/src/expression_function.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, iter::zip}; +use std::{iter::zip}; use std::sync::Arc; use crate::{ @@ -21,7 +21,6 @@ impl ExpressionFunction { let (name, a) = lhs.split_once('(').unwrap(); let args: Vec<String> = a[0..a.len() - 1] .split(',') - .into_iter() .map(|s| s.to_string()) .collect(); Self { diff --git a/src/function.rs b/src/function.rs index 126477c..4daebf4 100644 --- a/src/function.rs +++ b/src/function.rs @@ -16,7 +16,11 @@ impl FunctionArgument { pub fn len(&self) -> usize { self.args.len() } - + + pub fn is_empty(&self) -> bool { + self.args.len() == 0 + } + pub fn data(&self) -> &[Complex] { &self.args } |