pub fn evaluate(&self, vars: &HashMap<String, f64>) -> Result<f64, EvaluationError> {
        match self {
            ExpressionNode::BinaryExprNode {
                operator,
                left_node,
                right_node,
            } => {
                let left_value = left_node.evaluate(&vars)?;
                let right_value = right_node.evaluate(&vars)?;
                match operator {
                    BinaryOperator::Addition => Ok(left_value + right_value),
                    BinaryOperator::Subtraction => Ok(left_value - right_value),
                    BinaryOperator::Multiplication => Ok(left_value * right_value),
                    BinaryOperator::Division => Ok(left_value / right_value),
                    BinaryOperator::Exponentiation => Ok(left_value.powf(right_value)),
                }
            }
            ExpressionNode::UnaryExprNode {
                operator,
                child_node,
            } => {
                let child_value = child_node.evaluate(&vars)?;
                match operator {
                    UnaryOperator::Negation => Ok(-child_value),
                    UnaryOperator::Sin => Ok(child_value.sin()),
                    UnaryOperator::Asin => Ok(child_value.asin()),
                    UnaryOperator::Cos => Ok(child_value.cos()),
                    UnaryOperator::Acos => Ok(child_value.acos()),
                    UnaryOperator::Tan => Ok(child_value.tan()),
                    UnaryOperator::Ctan => Ok(1.0 / child_value.tan()),
                    UnaryOperator::Abs => Ok(child_value.abs()),
                    UnaryOperator::Exp => Ok(child_value.exp()),
                    UnaryOperator::Log2 => Ok(child_value.log2()),
                    UnaryOperator::Log10 => Ok(child_value.log10()),
                    UnaryOperator::Ln => Ok(child_value.ln()),
                    UnaryOperator::Ceil => Ok(child_value.ceil()),
                    UnaryOperator::Floor => Ok(child_value.floor()),
                }
            }
            ExpressionNode::NaryExprNode {
                operator,
                child_nodes,
            } => {
                let child_values: Vec<f64> = child_nodes
                                                .iter()
                                                .map(|node| node.evaluate(&vars))
                                                .collect::<Result<_,_>>()?;
                match operator {
                    NaryOperator::Log => if let [a, b] = &child_values[..] {
                            Ok(a.log(*b))
                        }
                        else {
                            Err(EvaluationError::WrongNumberOfArgsError)
                        },
                }
            }
            ExpressionNode::VariableExprNode { variable_key } => match vars.get(variable_key) {
                Some(x) => Ok(*x),
                None => Err(EvaluationError::VariableNotFoundError),
            },
            ExpressionNode::ConstantExprNode { value } => Ok(*value),
        }
    }
/// Takes in an array of variables to recursively pass down to all `ExpressionNode`s until the
/// expression is evaluated. The `f64` value returned is the result of the expression tree
/// rooted at `self`.
Takes in an array of variables to recursively pass down to all `ExpressionNode`s until the
expression is evaluated. The `f64` value returned is the result of the expression tree
rooted at `self`.
Takes in an array of variables to recursively pass down to all ` ExpressionNode ` s until the expression is evaluated . The ` f64 ` value returned is the result of the expression tree rooted at ` self ` .
pub fn evaluate(&self, vars: &HashMap<String, f64>) -> Result<f64, EvaluationError> {
        match self {
            ExpressionNode::BinaryExprNode {
                operator,
                left_node,
                right_node,
            } => {
                let left_value = left_node.evaluate(&vars)?;
                let right_value = right_node.evaluate(&vars)?;
                match operator {
                    BinaryOperator::Addition => Ok(left_value + right_value),
                    BinaryOperator::Subtraction => Ok(left_value - right_value),
                    BinaryOperator::Multiplication => Ok(left_value * right_value),
                    BinaryOperator::Division => Ok(left_value / right_value),
                    BinaryOperator::Exponentiation => Ok(left_value.powf(right_value)),
                }
            }
            ExpressionNode::UnaryExprNode {
                operator,
                child_node,
            } => {
                let child_value = child_node.evaluate(&vars)?;
                match operator {
                    UnaryOperator::Negation => Ok(-child_value),
                    UnaryOperator::Sin => Ok(child_value.sin()),
                    UnaryOperator::Asin => Ok(child_value.asin()),
                    UnaryOperator::Cos => Ok(child_value.cos()),
                    UnaryOperator::Acos => Ok(child_value.acos()),
                    UnaryOperator::Tan => Ok(child_value.tan()),
                    UnaryOperator::Ctan => Ok(1.0 / child_value.tan()),
                    UnaryOperator::Abs => Ok(child_value.abs()),
                    UnaryOperator::Exp => Ok(child_value.exp()),
                    UnaryOperator::Log2 => Ok(child_value.log2()),
                    UnaryOperator::Log10 => Ok(child_value.log10()),
                    UnaryOperator::Ln => Ok(child_value.ln()),
                    UnaryOperator::Ceil => Ok(child_value.ceil()),
                    UnaryOperator::Floor => Ok(child_value.floor()),
                }
            }
            ExpressionNode::NaryExprNode {
                operator,
                child_nodes,
            } => {
                let child_values: Vec<f64> = child_nodes
                                                .iter()
                                                .map(|node| node.evaluate(&vars))
                                                .collect::<Result<_,_>>()?;
                match operator {
                    NaryOperator::Log => if let [a, b] = &child_values[..] {
                            Ok(a.log(*b))
                        }
                        else {
                            Err(EvaluationError::WrongNumberOfArgsError)
                        },
                }
            }
            ExpressionNode::VariableExprNode { variable_key } => match vars.get(variable_key) {
                Some(x) => Ok(*x),
                None => Err(EvaluationError::VariableNotFoundError),
            },
            ExpressionNode::ConstantExprNode { value } => Ok(*value),
        }
    }
pub fn evaluate ( & self , vars : & HashMap < String , f64 > ) -> Result < f64 , EvaluationError > { match self { ExpressionNode :: BinaryExprNode { operator , left_node , right_node , } => { let left_value = left_node . evaluate ( & vars ) ? ; let right_value = right_node . evaluate ( & vars ) ? ; match operator { BinaryOperator :: Addition => Ok ( left_value + right_value ) , BinaryOperator :: Subtraction => Ok ( left_value - right_value ) , BinaryOperator :: Multiplication => Ok ( left_value * right_value ) , BinaryOperator :: Division => Ok ( left_value / right_value ) , BinaryOperator :: Exponentiation => Ok ( left_value . powf ( right_value ) ) , } } ExpressionNode :: UnaryExprNode { operator , child_node , } => { let child_value = child_node . evaluate ( & vars ) ? ; match operator { UnaryOperator :: Negation => Ok ( - child_value ) , UnaryOperator :: Sin => Ok ( child_value . sin ( ) ) , UnaryOperator :: Asin => Ok ( child_value . asin ( ) ) , UnaryOperator :: Cos => Ok ( child_value . cos ( ) ) , UnaryOperator :: Acos => Ok ( child_value . acos ( ) ) , UnaryOperator :: Tan => Ok ( child_value . tan ( ) ) , UnaryOperator :: Ctan => Ok ( 1.0 / child_value . tan ( ) ) , UnaryOperator :: Abs => Ok ( child_value . abs ( ) ) , UnaryOperator :: Exp => Ok ( child_value . exp ( ) ) , UnaryOperator :: Log2 => Ok ( child_value . log2 ( ) ) , UnaryOperator :: Log10 => Ok ( child_value . log10 ( ) ) , UnaryOperator :: Ln => Ok ( child_value . ln ( ) ) , UnaryOperator :: Ceil => Ok ( child_value . ceil ( ) ) , UnaryOperator :: Floor => Ok ( child_value . floor ( ) ) , } } ExpressionNode :: NaryExprNode { operator , child_nodes , } => { let child_values : Vec < f64 > = child_nodes . iter ( ) . map ( | node | node . evaluate ( & vars ) ) . collect :: < Result < _ , _ > > ( ) ? ; match operator { NaryOperator :: Log => if let [ a , b ] = & child_values [ .. ] { Ok ( a . log ( * b ) ) } else { Err ( EvaluationError :: WrongNumberOfArgsError ) } , } } ExpressionNode :: VariableExprNode { variable_key } => match vars . get ( variable_key ) { Some ( x ) => Ok ( * x ) , None => Err ( EvaluationError :: VariableNotFoundError ) , } , ExpressionNode :: ConstantExprNode { value } => Ok ( * value ) , } }
Takes in an array of variables to recursively pass down to all `ExpressionNode`s until the
expression is evaluated.
Takes in an array of variables to recursively pass down to all ` ExpressionNode ` s until the expression is evaluated .
&self
vars
&HashMap<String, f64>
&self
vars
&HashMap<String, f64>