fail when appropriate

This commit is contained in:
shimun 2019-11-20 21:03:50 +01:00
parent f8a8727fdd
commit 8d19b238f1
Signed by: shimun
GPG Key ID: E81D8382DC2F971B

View File

@ -146,10 +146,16 @@ impl Derive for Op {
), ),
Self::Add(a, b) => Op::Add(a.derive(), b.derive()), Self::Add(a, b) => Op::Add(a.derive(), b.derive()),
Self::Sub(a, b) => Op::Sub(a.derive(), b.derive()), Self::Sub(a, b) => Op::Sub(a.derive(), b.derive()),
Self::Pow(a, b) => Op::Pow( Self::Pow(a, b) => match b {
Op::Mul(a.clone(), b.clone()).into(), Value::Eval(e) => Op::Mul(
Op::Sub(b.clone(), Value::Const(1.0)).into(), unimplemented!("Ln(x)"),
), Op::Pow(a.clone(), b.clone()).into(),
),
b => Op::Pow(
Op::Mul(a.clone(), b.clone()).into(),
Op::Sub(b.clone(), Value::Const(1.0)).into(),
),
},
} }
.into() .into()
} }
@ -206,6 +212,11 @@ fn main() {
Value::Const(9.0), Value::Const(9.0),
) )
.into(); .into();
let res = n_root(9.0, 2.0); fn pow_to_x(b: f64) -> Value {
Op::Pow(Value::Const(b), Var::val("x")).into()
}
let eq: Value = Op::Sub(Op::Add(pow_to_x(4.0), pow_to_x(6.0)).into(), pow_to_x(9.0)).into();
//let res = n_root(9.0, 2.0);
let res = newton_raphson(&eq, 1.0).last();
println!("1.0 - {}/{} ==> {}", &eq, eq.derive(), res.unwrap()); println!("1.0 - {}/{} ==> {}", &eq, eq.derive(), res.unwrap());
} }