Skip to main content

RuleSet

Struct RuleSet 

Source
pub struct RuleSet { /* private fields */ }
Expand description

An ordered collection of Rules.

At each node the rules are tried in order and the first one that applies wins. RuleSet is Clone + Send + Sync.

§Examples

use symplex::prelude::*;
use symplex::macros::{Rule, RuleSet};

let ctx = Context::new();
let (x, a) = (ctx.symbol("x"), ctx.symbol("a_"));
let rules = RuleSet::new()
    .with(Rule::new("cos_sq", &a.cos().powi(2), &(1 - &a.sin().powi(2))))
    .with(Rule::new("tan", &(&a.sin() / &a.cos()), &a.tan()));
assert_eq!(rules.len(), 2);
let expr = &x.cos().powi(2) + &x.sin() / &x.cos();
assert_eq!(format!("{}", expr.rewrite(&rules)), "-sin(x)^2 + tan(x) + 1");

Implementations§

Source§

impl RuleSet

Source

pub fn new() -> Self

An empty rule set.

Source

pub fn from_rules(rules: Vec<Rule>) -> Self

Build from a vector of rules (order is preserved).

Source

pub fn from_macro_rules(ctx: &Context, rules: Vec<Rule>) -> Self

Wrap arena-level rules produced by the rule! macro.

§Examples
use symplex::prelude::*;
use symplex::macros::RuleSet;

let ctx = Context::new();
let x = ctx.symbol("x");
let raw = ctx.with_arena_mut(|arena| {
    vec![
        rule!(arena, "exp_ln", exp(ln(w_)) => w_),
        rule!(arena, "sin_asin", sin(asin(w_)) => w_),
    ]
});
let rules = RuleSet::from_macro_rules(&ctx, raw);
assert_eq!(format!("{}", (&x.ln().exp() + &x.asin().sin()).rewrite(&rules)), "2*x");
Source

pub fn standard(ctx: &Context) -> Self

The built-in simplification rules used by simplify (Pythagorean identities, exp(ln x) → x, sin/cos → tan, inverse-function compositions, …), as a rule set for ctx.

Source

pub fn push(&mut self, rule: Rule)

Append a rule.

Source

pub fn with(self, rule: Rule) -> Self

Builder-style append.

Source

pub fn extend(&mut self, other: &RuleSet)

Append all rules of other.

Source

pub fn len(&self) -> usize

Number of rules.

Source

pub fn is_empty(&self) -> bool

true if the set has no rules.

Source

pub fn iter(&self) -> impl Iterator<Item = &Rule>

Iterate over the rules in order.

Source

pub fn rules(&self) -> &[Rule]

The rules as a slice.

Trait Implementations§

Source§

impl Clone for RuleSet

Source§

fn clone(&self) -> RuleSet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RuleSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RuleSet

Source§

fn default() -> RuleSet

Returns the “default value” for a type. Read more
Source§

impl From<Vec<Rule>> for RuleSet

Source§

fn from(rules: Vec<Rule>) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Rule> for RuleSet

Source§

fn from_iter<I: IntoIterator<Item = Rule>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Index<usize> for RuleSet

Source§

type Output = Rule

The returned type after indexing.
Source§

fn index(&self, i: usize) -> &Rule

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more