Skip to content

Commit

Permalink
xprs simplification docs + example
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Nov 29, 2023
1 parent 0cfa1b2 commit 89b0863
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ Note2: `sum` and `mean` can take any number of arguments (if none, returns `0` a

### Advanced examples

## Xprs simplification

You can simplify an [`Xprs`], in-place or not, for a given variable (or set of variables) using the `simplify_for` or `simplify_for_multiple` methods.

```rust
use xprs::Xprs;

fn main() {
let mut xprs = Xprs::try_from("w + sin(x + 2y) * (3 * z)").unwrap();

println!("{xprs}"); // (w + (sin((x + (2 * y))) * (3 * z)))

xprs.simplify_for_inplace(("z", 4.0));

println!("{xprs}"); // (w + (sin((x + (2 * y))) * 12))

let xprs = xprs.simplify_for_multiple(&[("x", 1.0), ("y", 2.0)]);

println!("{xprs}"); // (w + -11.507091295957661)
}
```

## Higher order functions

You can define functions in a context based on a previously parsed expression.
Expand Down
24 changes: 24 additions & 0 deletions examples/xprs_simplification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Simple example
#![allow(
clippy::print_stdout,
clippy::default_numeric_fallback,
clippy::shadow_reuse
)]
use std::error::Error;
use xprs::Xprs;

fn main() -> Result<(), Box<dyn Error>> {
let mut xprs = Xprs::try_from("w + sin(x + 2y) * (3 * z)")?;

println!("{xprs}"); // (w + (sin((x + (2 * y))) * (3 * z)))

xprs.simplify_for_inplace(("z", 4.0));

println!("{xprs}"); // (w + (sin((x + (2 * y))) * 12))

let xprs = xprs.simplify_for_multiple(&[("x", 1.0), ("y", 2.0)]);

println!("{xprs}"); // (w + -11.507091295957661)

Ok(())
}
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@
//!
//! ### Advanced examples
//!
//! ## Xprs simplification
//!
//! You can simplify an [`Xprs`], in-place or not, for a given variable (or set of variables) using the `simplify_for` or `simplify_for_multiple` methods.
//!
//! ```rust
//! use xprs::Xprs;
//!
//! fn main() {
//! let mut xprs = Xprs::try_from("w + sin(x + 2y) * (3 * z)").unwrap();
//!
//! println!("{xprs}"); // (w + (sin((x + (2 * y))) * (3 * z)))
//!
//! xprs.simplify_for_inplace(("z", 4.0));
//!
//! println!("{xprs}"); // (w + (sin((x + (2 * y))) * 12))
//!
//! let xprs = xprs.simplify_for_multiple(&[("x", 1.0), ("y", 2.0)]);
//!
//! println!("{xprs}"); // (w + -11.507091295957661)
//! }
//! ```
//!
//! ## Higher order functions
//!
//! You can define functions in a context based on a previously parsed expression.
Expand Down

0 comments on commit 89b0863

Please sign in to comment.