bertiqwerty / exmex

Math parser and evaluator in Rust, capable of partial differentiation, allows the use of custom operators.
Apache License 2.0
39 stars 6 forks source link

Feature request: Apply unary and binary operations to existing expressions #40

Closed terrorfisch closed 2 years ago

terrorfisch commented 2 years ago

This is a feature I require and currently implement by formatting the expression(s) into a String with the required operator and reparsing it.

bertiqwerty commented 2 years ago

Do you mean all user defined operators for that expression? How could a call look like?

terrorfisch commented 2 years ago
let op_minus: Operator = todo!();

let diff1 = expr1.apply_binary_operator("-", expr2)?;
let diff2 = expr1.apply_binary(op_minus, expr2);
let diff3 = op_minus.apply_binary(expr1, expr2);

let neg1 = expr1.apply_unary_operator("-")?;
let neg2 = expr1.apply_unary(op_minus);
let neg3 = op_minus.apply_unary(expr1);

diff1 and neg1 would enforce that the operator is known to the OperatorFactory of the expression and is therefore the cleanest and most convenient. The other two variants are more flexible. I would be fine with all three variants.

bertiqwerty commented 2 years ago

I implemented something similar to compute partial derivatives. I will look into how to make this part of the public API. https://github.com/bertiqwerty/exmex/blob/07ce3de3dc20724f1498b4653ce0e6336d75b100/src/partial/mod.rs#L478

bertiqwerty commented 2 years ago

See https://github.com/bertiqwerty/exmex/blob/0358a04fd0e5e5c2c8cf1537ef48f8fdda711666/src/lib.rs#L276