aboutsummaryrefslogtreecommitdiff
path: root/src/function.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/function.rs')
-rw-r--r--src/function.rs42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/function.rs b/src/function.rs
index 547ae0e..442d987 100644
--- a/src/function.rs
+++ b/src/function.rs
@@ -1,17 +1,51 @@
+use crate::complex::Complex;
+
pub struct FunctionArgument {
- args : Vec<f64>,
+ args : Vec<Complex>,
}
impl FunctionArgument {
- pub fn new(args: Vec<f64>) -> Self {
+ pub fn new(args: Vec<Complex>) -> Self {
Self { args }
}
- pub fn get(&self, i : usize) -> f64 {
+ pub fn get(&self, i : usize) -> Complex {
self.args[i]
}
+
+ pub fn len(&self) -> usize {
+ self.args.len()
+ }
}
pub trait Function {
- fn eval(&self, args: FunctionArgument) -> f64;
+ fn eval(&self, args: FunctionArgument) -> Result<Complex, String>;
+}
+
+#[macro_export]
+macro_rules! functions {
+ ({$($x:expr => $y:expr), *}) => {
+ {
+ let mut h : HashMap<String, Box<dyn Function>> = HashMap::new();
+ $(
+ h.insert($x.to_string(), Box::new($y));
+ )*
+ h
+ }
+ };
+}
+
+
+// Some default implementations
+impl<T> Function for T
+where
+ T: Fn(Complex) -> Complex,
+{
+ fn eval(&self, args: FunctionArgument) -> Result<Complex, String> {
+ if args.len() == 1 {
+ Ok(self(args.get(0)))
+ } else {
+ Err("too many arguments".to_string())
+ }
+ }
}