pub fn derive(self, wrt: char) -> Expression {
        match self {
            Expression::Lit(Term::Variable(var)) => {
                Expression::integer_expression(if var == wrt { 1 } else { 0 })
            }
            Expression::Lit(_) => Expression::integer_expression(0),
            Expression::Unary(Operator::Paren, a) => a.derive(wrt),
            Expression::Unary(Operator::Neg, a) => Expression::Variadic(
                Operator::Mul,
                vec![Expression::integer_expression(-1), a.derive(wrt)],
            ),
            Expression::Unary(Operator::Custom(f), x) if f == "ln" => {
                Expression::Binary(Operator::Div, Box::new(x.clone().derive(wrt)), x)
            }

            Expression::Unary(Operator::Custom(f), x) if f == "sin" => Expression::Variadic(
                Operator::Mul,
                vec![
                    x.clone().derive(wrt),
                    Expression::Unary(Operator::Custom(String::from("cos")), x),
                ],
            ),
            Expression::Unary(Operator::Custom(f), x) if f == "cos" => Expression::Variadic(
                Operator::Mul,
                vec![
                    x.clone().derive(wrt),
                    Expression::integer_expression(-1),
                    Expression::Unary(Operator::Custom(String::from("sin")), x),
                ],
            ),
            Expression::Unary(Operator::Custom(f), x) if f == "tan" => Expression::Binary(
                Operator::Div,
                Box::new(Expression::Unary(
                    Operator::Custom(String::from("sin")),
                    x.clone(),
                )),
                Box::new(Expression::Unary(Operator::Custom(String::from("cos")), x)),
            )
            .derive(wrt),
            Expression::Binary(Operator::Div, a, b) => {
                let a_deriv = a.clone().derive(wrt);
                let b_deriv = b.clone().derive(wrt);
                let b_squared = Box::new(Expression::Binary(
                    Operator::Exp,
                    b.clone(),
                    Box::new(Expression::integer_expression(2)),
                ));
                let b_a_deriv = Expression::Variadic(Operator::Mul, vec![*b, a_deriv]);
                let a_b_deriv = Expression::Variadic(
                    Operator::Mul,
                    vec![Expression::integer_expression(-1), *a, b_deriv],
                );
                Expression::Binary(
                    Operator::Div,
                    Box::new(Expression::Variadic(
                        Operator::Add,
                        vec![b_a_deriv, a_b_deriv],
                    )),
                    b_squared,
                )
            }
            Expression::Binary(Operator::Exp, base, exp) => Expression::Variadic(
                Operator::Mul,
                vec![
                    Expression::Binary(Operator::Exp, base.clone(), exp.clone()),
                    Expression::Variadic(
                        Operator::Mul,
                        vec![
                            *exp,
                            Expression::Unary(Operator::Custom(String::from("ln")), base),
                        ],
                    )
                    .derive(wrt),
                ],
            ),
            Expression::Variadic(_, mut exprs) if exprs.len() == 1 => exprs.remove(0).derive(wrt),
            Expression::Variadic(Operator::Add, exprs) => Expression::Variadic(
                Operator::Add,
                exprs.into_iter().map(|x| x.derive(wrt)).collect(),
            ),
            Expression::Variadic(Operator::Mul, exprs) => {
                let mut exprs_deriv = exprs
                    .clone()
                    .into_iter()
                    .map(|x| x.derive(wrt))
                    .collect::<Vec<Expression>>();
                let mut nodes = std::iter::repeat(exprs.clone())
                    .take(exprs.len())
                    .collect::<Vec<Vec<Expression>>>();
                for (i, node) in nodes.iter_mut().enumerate() {
                    node.remove(i);
                    node.insert(i, exprs_deriv.remove(0))
                }

                Expression::Variadic(
                    Operator::Add,
                    nodes
                        .into_iter()
                        .map(|exprs| Expression::Variadic(Operator::Mul, exprs))
                        .collect(),
                )
            }

            other => other,
        }
    }
//The reason all this other code exists. Recursively produces the derivate of a given expression with respect to its supplied argument
The reason all this other code exists. Recursively produces the derivate of a given expression with respect to its supplied argument
The reason all this other code exists . Recursively produces the derivate of a given expression with respect to its supplied argument
pub fn derive(self, wrt: char) -> Expression {
        match self {
            Expression::Lit(Term::Variable(var)) => {
                Expression::integer_expression(if var == wrt { 1 } else { 0 })
            }
            Expression::Lit(_) => Expression::integer_expression(0),
            Expression::Unary(Operator::Paren, a) => a.derive(wrt),
            Expression::Unary(Operator::Neg, a) => Expression::Variadic(
                Operator::Mul,
                vec![Expression::integer_expression(-1), a.derive(wrt)],
            ),
            Expression::Unary(Operator::Custom(f), x) if f == "ln" => {
                Expression::Binary(Operator::Div, Box::new(x.clone().derive(wrt)), x)
            }
            Expression::Unary(Operator::Custom(f), x) if f == "sin" => Expression::Variadic(
                Operator::Mul,
                vec![
                    x.clone().derive(wrt),
                    Expression::Unary(Operator::Custom(String::from("cos")), x),
                ],
            ),
            Expression::Unary(Operator::Custom(f), x) if f == "cos" => Expression::Variadic(
                Operator::Mul,
                vec![
                    x.clone().derive(wrt),
                    Expression::integer_expression(-1),
                    Expression::Unary(Operator::Custom(String::from("sin")), x),
                ],
            ),
            Expression::Unary(Operator::Custom(f), x) if f == "tan" => Expression::Binary(
                Operator::Div,
                Box::new(Expression::Unary(
                    Operator::Custom(String::from("sin")),
                    x.clone(),
                )),
                Box::new(Expression::Unary(Operator::Custom(String::from("cos")), x)),
            )
            .derive(wrt),
            Expression::Binary(Operator::Div, a, b) => {
                let a_deriv = a.clone().derive(wrt);
                let b_deriv = b.clone().derive(wrt);
                let b_squared = Box::new(Expression::Binary(
                    Operator::Exp,
                    b.clone(),
                    Box::new(Expression::integer_expression(2)),
                ));
                let b_a_deriv = Expression::Variadic(Operator::Mul, vec![*b, a_deriv]);
                let a_b_deriv = Expression::Variadic(
                    Operator::Mul,
                    vec![Expression::integer_expression(-1), *a, b_deriv],
                );
                Expression::Binary(
                    Operator::Div,
                    Box::new(Expression::Variadic(
                        Operator::Add,
                        vec![b_a_deriv, a_b_deriv],
                    )),
                    b_squared,
                )
            }
            Expression::Binary(Operator::Exp, base, exp) => Expression::Variadic(
                Operator::Mul,
                vec![
                    Expression::Binary(Operator::Exp, base.clone(), exp.clone()),
                    Expression::Variadic(
                        Operator::Mul,
                        vec![
                            *exp,
                            Expression::Unary(Operator::Custom(String::from("ln")), base),
                        ],
                    )
                    .derive(wrt),
                ],
            ),
            Expression::Variadic(_, mut exprs) if exprs.len() == 1 => exprs.remove(0).derive(wrt),
            Expression::Variadic(Operator::Add, exprs) => Expression::Variadic(
                Operator::Add,
                exprs.into_iter().map(|x| x.derive(wrt)).collect(),
            ),
            Expression::Variadic(Operator::Mul, exprs) => {
                let mut exprs_deriv = exprs
                    .clone()
                    .into_iter()
                    .map(|x| x.derive(wrt))
                    .collect::<Vec<Expression>>();
                let mut nodes = std::iter::repeat(exprs.clone())
                    .take(exprs.len())
                    .collect::<Vec<Vec<Expression>>>();
                for (i, node) in nodes.iter_mut().enumerate() {
                    node.remove(i);
                    node.insert(i, exprs_deriv.remove(0))
                }
                Expression::Variadic(
                    Operator::Add,
                    nodes
                        .into_iter()
                        .map(|exprs| Expression::Variadic(Operator::Mul, exprs))
                        .collect(),
                )
            }
            other => other,
        }
    }
pub fn derive ( self , wrt : char ) -> Expression { match self { Expression :: Lit ( Term :: Variable ( var ) ) => { Expression :: integer_expression ( if var == wrt { 1 } else { 0 } ) } Expression :: Lit ( _ ) => Expression :: integer_expression ( 0 ) , Expression :: Unary ( Operator :: Paren , a ) => a . derive ( wrt ) , Expression :: Unary ( Operator :: Neg , a ) => Expression :: Variadic ( Operator :: Mul , vec ! [ Expression integer_expression ( 1 ) a derive ( wrt ) ] , ) , Expression :: Unary ( Operator :: Custom ( f ) , x ) if f == " " => { Expression :: Binary ( Operator :: Div , Box :: new ( x . clone ( ) . derive ( wrt ) ) , x ) } Expression :: Unary ( Operator :: Custom ( f ) , x ) if f == " " => Expression :: Variadic ( Operator :: Mul , vec ! [ x clone ( ) derive ( wrt ) Expression Unary ( Operator Custom ( String from ( " " ) ) x ) ] , ) , Expression :: Unary ( Operator :: Custom ( f ) , x ) if f == " " => Expression :: Variadic ( Operator :: Mul , vec ! [ x clone ( ) derive ( wrt ) Expression integer_expression ( 1 ) Expression Unary ( Operator Custom ( String from ( " " ) ) x ) ] , ) , Expression :: Unary ( Operator :: Custom ( f ) , x ) if f == " " => Expression :: Binary ( Operator :: Div , Box :: new ( Expression :: Unary ( Operator :: Custom ( String :: from ( " " ) ) , x . clone ( ) , ) ) , Box :: new ( Expression :: Unary ( Operator :: Custom ( String :: from ( " " ) ) , x ) ) , ) . derive ( wrt ) , Expression :: Binary ( Operator :: Div , a , b ) => { let a_deriv = a . clone ( ) . derive ( wrt ) ; let b_deriv = b . clone ( ) . derive ( wrt ) ; let b_squared = Box :: new ( Expression :: Binary ( Operator :: Exp , b . clone ( ) , Box :: new ( Expression :: integer_expression ( 2 ) ) , ) ) ; let b_a_deriv = Expression :: Variadic ( Operator :: Mul , vec ! [ b a_deriv ] ) ; let a_b_deriv = Expression :: Variadic ( Operator :: Mul , vec ! [ Expression integer_expression ( 1 ) a b_deriv ] , ) ; Expression :: Binary ( Operator :: Div , Box :: new ( Expression :: Variadic ( Operator :: Add , vec ! [ b_a_deriv a_b_deriv ] , ) ) , b_squared , ) } Expression :: Binary ( Operator :: Exp , base , exp ) => Expression :: Variadic ( Operator :: Mul , vec ! [ Expression Binary ( Operator Exp base clone ( ) exp clone ( ) ) Expression Variadic ( Operator Mul vec [ exp Expression Unary ( Operator Custom ( String from ( " " ) ) base ) ] ) derive ( wrt ) ] , ) , Expression :: Variadic ( _ , mut exprs ) if exprs . len ( ) == 1 => exprs . remove ( 0 ) . derive ( wrt ) , Expression :: Variadic ( Operator :: Add , exprs ) => Expression :: Variadic ( Operator :: Add , exprs . into_iter ( ) . map ( | x | x . derive ( wrt ) ) . collect ( ) , ) , Expression :: Variadic ( Operator :: Mul , exprs ) => { let mut exprs_deriv = exprs . clone ( ) . into_iter ( ) . map ( | x | x . derive ( wrt ) ) . collect :: < Vec < Expression > > ( ) ; let mut nodes = std :: iter :: repeat ( exprs . clone ( ) ) . take ( exprs . len ( ) ) . collect :: < Vec < Vec < Expression > > > ( ) ; for ( i , node ) in nodes . iter_mut ( ) . enumerate ( ) { node . remove ( i ) ; node . insert ( i , exprs_deriv . remove ( 0 ) ) } Expression :: Variadic ( Operator :: Add , nodes . into_iter ( ) . map ( | exprs | Expression :: Variadic ( Operator :: Mul , exprs ) ) . collect ( ) , ) } other => other , } }
The reason all this other code exists.
The reason all this other code exists .
self
wrt
char
self
wrt
char