#expression #evaluation #algebra #simplify

equation

A Rust library for mathematical expression evaluation and simplification

6 releases (1 stable)

1.0.0 Sep 19, 2023
0.3.0 Sep 19, 2023
0.2.0 Sep 16, 2023
0.1.1 Sep 15, 2023
0.0.0 Sep 12, 2023

#510 in Math

30 downloads per month

MIT/Apache

23KB
279 lines

Equation

CI

A Rust library for mathematical expression evaluation and simplification.

TODO

  • Rewrite pest parsing as PrattParser;
  • Modulo % & exponent ** support;
  • Unary minus operator support;
  • Support fractions as f32 f64;
  • Support basic triggernomic ratio functions sine, cosine & tangent as sin, cos & tan;
  • Support functions with additional arguments, atan2(y, x);
  • Support evaluate steps parameter to break evaluation steps into a Vec<&str>;
  • Support simplification & evaluation of algebraic expressions;
  • Support for vectors and basic vector arithmetic & operations;

Getting started

Add the following lines to your Cargo.toml dependencies:

[dependencies]
equation = "1.0.0"

Examples

Evaluate basic arithmetic equations and return a f64.

use equation::evaluate;

fn main() {
    let result = evaluate("(1 + 2) * (1 + 2)"); // Returns Ok(9.0)
    match result {
        Ok(val) => println!("{}", val),
        Err(e) => println!("Error: {:?}", e),
    }
}

Support

Basic arithematic

evaluate("(1 + 2) * 1 + 2"); // Returns Ok(5.0)
evaluate("4 * 3");           // Returns Ok(12.0)
evaluate("8 / 2");           // Returns Ok(4.0)
evaluate("16 - 4");          // Returns Ok(12.0)

Negative calculations

Unary operator. Returns the negation of its operand.

evaluate("-4 * 3"); // Returns Ok(-12.0)
evaluate("-8 / 2"); // Returns Ok(-4.0)
evaluate("-(4 + 4)"); // Returns Ok(-8.0)

Exponentation

Calculates the base to the exponent power, base ^ exponent.

evaluate("2 ^ 8");   // Returns Ok(256.0)
evaluate("2 exp 9"); // Returns Ok(512.0)

Modulus

Returns the integer remainder of dividing the two operands.

evaluate("10 % 2");   // Returns Ok(0.0)
evaluate("10 % 4");   // Returns Ok(2.0)
evaluate("10 mod 3"); // Returns Ok(1.0)

Triggernomic functions

Function Description Example Result
sin Sine of the input evaluate("sin(10.0)); Ok(-0.5440211108893698)
cos Cosine of the input evaluate("cos(10.0)); Ok(-0.8390715290764524)
tan Tangent of the input evaluate("tan(10.0)); Ok(0.6483608274590867)
asin Inverse sine of the input evaluate("asin(0.5)); Ok(0.5235987755982988);)
acos Inverse cosine of the input evaluate("acos(0.5)); Ok(1.0471975511965976);)
atan Inverse tangent of the input evaluate("atan(10.0)); Ok(1.4711276743037345);)
sinh Hyperbolic sine of the input evaluate("sinh(10.0)); Ok(11013.232874703393);)
cosh Hyperbolic cosine of the input evaluate("cosh(10.0)); Ok(11013.232920103323);)
tanh Hyperbolic tangent of the input evaluate("tanh(10.0)); Ok(0.9999999958776927);)
asinh Inverse hyperbolic sine of the input evaluate("asinh(10.0)); Ok(2.99822295029797);)
acosh Inverse hyperbolic cosine of the input evaluate("acosh(10.0)); Ok(2.993222846126381);)
atanh Inverse hyperbolic tangent of the input evaluate("atanh(10.0)); Ok(0.5493061443340549);)

License

Licensed under MIT license (LICENSE-MIT | https://opensource.org/licenses/MIT) or under the Apache 2.0 (LICENSE-APACHE | https://opensource.org/license/apache-2-0/).

Dependencies

~2.1–2.8MB
~57K SLoC