SymTable

Struct SymTable 

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

Symbol table containing constants and functions.

The table stores mathematical constants like pi and functions like sin. Symbol lookups are case-insensitive.

§Examples

use expr_solver::SymTable;
use rust_decimal_macros::dec;

let mut table = SymTable::stdlib();
table.add_const("x", dec!(42)).unwrap();

Implementations§

Source§

impl SymTable

Source

pub fn new() -> Self

Creates an empty symbol table.

Source

pub fn stdlib() -> Self

Creates a symbol table with the standard library.

§Constants
  • pi - π (3.14159…)
  • e - Euler’s number (2.71828…)
  • tau - 2π (6.28318…)
  • ln2 - Natural logarithm of 2
  • ln10 - Natural logarithm of 10
  • sqrt2 - Square root of 2
§Fixed arity functions
  • sin(x) - Sine
  • cos(x) - Cosine
  • tan(x) - Tangent
  • asin(x) - Arcsine
  • acos(x) - Arccosine
  • atan(x) - Arctangent
  • atan2(y, x) - Two-argument arctangent
  • sinh(x) - Hyperbolic sine
  • cosh(x) - Hyperbolic cosine
  • tanh(x) - Hyperbolic tangent
  • sqrt(x) - Square root
  • cbrt(x) - Cube root
  • pow(x, y) - x raised to power y
  • log(x) - Natural logarithm
  • log2(x) - Base-2 logarithm
  • log10(x) - Base-10 logarithm
  • exp(x) - e raised to power x
  • exp2(x) - 2 raised to power x
  • abs(x) - Absolute value
  • sign(x) - Sign function (-1, 0, or 1)
  • floor(x) - Floor function
  • ceil(x) - Ceiling function
  • round(x) - Round to nearest integer
  • trunc(x) - Truncate to integer
  • fract(x) - Fractional part
  • mod(x, y) - Remainder of x/y
  • hypot(x, y) - Euclidean distance sqrt(x²+y²)
  • clamp(x, min, max) - Constrain value between bounds
§Variadic functions
  • min(x, ...) - Minimum value
  • max(x, ...) - Maximum value
  • sum(x, ...) - Sum of values
  • avg(x, ...) - Average of values
Source

pub fn add_const<S: Into<Cow<'static, str>>>( &mut self, name: S, value: Decimal, ) -> Result<&mut Self, SymbolError>

Adds a constant to the table.

Returns an error if a symbol with the same name already exists.

Source

pub fn add_func<S: Into<Cow<'static, str>>>( &mut self, name: S, args: usize, variadic: bool, callback: fn(&[Decimal]) -> Result<Decimal, FuncError>, ) -> Result<&mut Self, SymbolError>

Adds a function to the table.

§Parameters
  • name: Function name
  • args: Minimum number of arguments
  • variadic: Whether the function accepts additional arguments
  • callback: Function implementation

Returns an error if a symbol with the same name already exists.

Source

pub fn get(&self, name: &str) -> Option<&Symbol>

Looks up a symbol by name (case-insensitive).

Source

pub fn get_with_index(&self, name: &str) -> Option<(usize, &Symbol)>

Looks up a symbol by name and returns its index and reference (case-insensitive).

Source

pub fn get_by_index(&self, index: usize) -> Option<&Symbol>

Returns a symbol by index.

Source

pub fn symbols(&self) -> impl Iterator<Item = &Symbol>

Returns an iterator over all symbols in the table.

Trait Implementations§

Source§

impl Clone for SymTable

Source§

fn clone(&self) -> SymTable

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SymTable

Source§

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

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

impl Default for SymTable

Source§

fn default() -> SymTable

Returns the “default value” for a type. 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, 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.