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::{num, SymTable};
let mut table = SymTable::stdlib();
table.add_const("x", num!(42), false).unwrap();Implementations§
Source§impl SymTable
impl SymTable
Sourcepub fn stdlib() -> Self
pub fn stdlib() -> Self
Creates a symbol table with the standard library for f64 precision.
This implementation uses standard f64 floating-point arithmetic. NaN and Inf results are allowed and not treated as errors.
§Fixed arity functions
sin(x)- Sinecos(x)- Cosinetan(x)- Tangentasin(x)- Arcsine (returns NaN for |x| > 1)acos(x)- Arccosine (returns NaN for |x| > 1)atan(x)- Arctangentatan2(y, x)- Two-argument arctangentsinh(x)- Hyperbolic sinecosh(x)- Hyperbolic cosinetanh(x)- Hyperbolic tangentsqrt(x)- Square root (returns NaN for x < 0)cbrt(x)- Cube rootpow(x, y)- x raised to power ylog(x)- Natural logarithm (returns NaN for x <= 0)log2(x)- Base-2 logarithm (returns NaN for x <= 0)log10(x)- Base-10 logarithm (returns NaN for x <= 0)exp(x)- e raised to power xexp2(x)- 2 raised to power xabs(x)- Absolute valuesign(x)- Sign function (-1, 0, or 1)floor(x)- Floor functionceil(x)- Ceiling functionround(x)- Round to nearest integertrunc(x)- Truncate to integerfract(x)- Fractional partmod(x, y)- Remainder of x/yhypot(x, y)- Euclidean distance sqrt(x²+y²)clamp(x, min, max)- Constrain value between bounds
§Variadic functions
min(x, ...)- Minimum valuemax(x, ...)- Maximum valuesum(x, ...)- Sum of valuesavg(x, ...)- Average of values
Source§impl SymTable
impl SymTable
Sourcepub fn add_const<S: Into<Cow<'static, str>>>(
&mut self,
name: S,
value: Number,
local: bool,
) -> Result<&mut Self, SymbolError>
pub fn add_const<S: Into<Cow<'static, str>>>( &mut self, name: S, value: Number, local: bool, ) -> Result<&mut Self, SymbolError>
Adds a constant to the table.
Returns an error if a symbol with the same name already exists.
§Parameters
name: Constant namevalue: Constant valuelocal: Whether this is a local constant (from let) or global (from stdlib)
Sourcepub fn add_func<S: Into<Cow<'static, str>>>(
&mut self,
name: S,
args: usize,
variadic: bool,
callback: fn(&[Number]) -> Result<Number, FuncError>,
local: bool,
) -> Result<&mut Self, SymbolError>
pub fn add_func<S: Into<Cow<'static, str>>>( &mut self, name: S, args: usize, variadic: bool, callback: fn(&[Number]) -> Result<Number, FuncError>, local: bool, ) -> Result<&mut Self, SymbolError>
Adds a function to the table.
§Parameters
name: Function nameargs: Minimum number of argumentsvariadic: Whether the function accepts additional argumentscallback: Function implementationlocal: Whether this is a local function (reserved for future) or global (from stdlib)
Returns an error if a symbol with the same name already exists.
Sourcepub fn get_by_name(&self, name: &str) -> Result<&Symbol, SymbolError>
pub fn get_by_name(&self, name: &str) -> Result<&Symbol, SymbolError>
Looks up a symbol by name (case-insensitive).
Sourcepub fn get_with_index(
&self,
name: &str,
) -> Result<(usize, &Symbol), SymbolError>
pub fn get_with_index( &self, name: &str, ) -> Result<(usize, &Symbol), SymbolError>
Looks up a symbol by name and returns its index and reference (case-insensitive).
Sourcepub fn get_by_index(&self, index: usize) -> Result<&Symbol, SymbolError>
pub fn get_by_index(&self, index: usize) -> Result<&Symbol, SymbolError>
Returns a symbol by index.
Sourcepub fn get_mut_by_index(
&mut self,
index: usize,
) -> Result<&mut Symbol, SymbolError>
pub fn get_mut_by_index( &mut self, index: usize, ) -> Result<&mut Symbol, SymbolError>
Returns a symbol by index.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SymTable
impl RefUnwindSafe for SymTable
impl Send for SymTable
impl Sync for SymTable
impl Unpin for SymTable
impl UnwindSafe for SymTable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more