Expression

Struct Expression 

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

A symbolic mathematical expression.

This type wraps egg’s RecExpr and provides a user-friendly API for symbolic computation.

Implementations§

Source§

impl Expression

Source

pub fn symbol(name: &str) -> Self

Create a new symbolic variable

§Example
use quantrs2_symengine_pure::Expression;
let x = Expression::symbol("x");
Source

pub fn int(value: i64) -> Self

Create an integer constant

Source

pub fn float(value: f64) -> SymEngineResult<Self>

Create a floating-point constant

§Errors

Returns an error if the value is NaN

Source

pub fn float_unchecked(value: f64) -> Self

Create a floating-point constant, using 0 for NaN

Source

pub fn zero() -> Self

Create the constant zero

Source

pub fn one() -> Self

Create the constant one

Source

pub fn i() -> Self

Create the imaginary unit i

Source

pub fn pi() -> Self

Create the constant π

Source

pub fn e() -> Self

Create the constant e (Euler’s number)

Source

pub fn from_complex64(c: Complex64) -> Self

Create from a complex number

If imaginary part is negligible, returns just the real part.

Source

pub fn parse(input: &str) -> SymEngineResult<Self>

Parse an expression from a string

§Errors

Returns an error if parsing fails

Source

pub fn new(input: impl AsRef<str>) -> Self

Create an expression from a string (alias for parse)

Source

pub fn is_symbol(&self) -> bool

Check if this expression is a symbol (variable or number literal)

Source

pub fn is_number(&self) -> bool

Check if this expression is a number

Source

pub fn is_zero(&self) -> bool

Check if this expression is zero

Source

pub fn is_one(&self) -> bool

Check if this expression is one

Source

pub fn as_symbol(&self) -> Option<&str>

Get the symbol name if this is a symbol

Source

pub fn to_f64(&self) -> Option<f64>

Convert to f64 if this is a numeric constant

Source

pub fn to_i64(&self) -> Option<i64>

Convert to i64 if this is an integer constant

Source

pub fn is_add(&self) -> bool

Check if this expression is an addition operation

Source

pub fn is_mul(&self) -> bool

Check if this expression is a multiplication operation

Source

pub fn is_pow(&self) -> bool

Check if this expression is a power operation

Source

pub fn is_neg(&self) -> bool

Check if this expression is a negation operation

Source

pub fn as_neg(&self) -> Option<Self>

Get the inner expression if this is a negation

Source

pub fn as_add(&self) -> Option<Vec<Self>>

Get the operands if this is an addition operation

Source

pub fn as_mul(&self) -> Option<Vec<Self>>

Get the operands if this is a multiplication operation

Source

pub fn as_pow(&self) -> Option<(Self, Self)>

Get the base and exponent if this is a power operation

Source

pub fn add(&self, other: &Self) -> Self

Add two expressions

Source

pub fn sub(&self, other: &Self) -> Self

Subtract two expressions

Source

pub fn mul(&self, other: &Self) -> Self

Multiply two expressions

Source

pub fn div(&self, other: &Self) -> Self

Divide two expressions

Source

pub fn pow(&self, exp: &Self) -> Self

Raise to a power

Source

pub fn neg(&self) -> Self

Negate the expression

Source

pub fn conjugate(&self) -> Self

Complex conjugate

Source

pub fn diff(&self, var: &Self) -> Self

Compute the derivative with respect to a variable

Source

pub fn gradient(&self, vars: &[Self]) -> Vec<Self>

Compute the gradient with respect to multiple variables

Source

pub fn hessian(&self, vars: &[Self]) -> Vec<Vec<Self>>

Compute the Hessian matrix (second derivatives)

Source

pub fn expand(&self) -> Self

Expand the expression (distribute products over sums)

Source

pub fn simplify(&self) -> Self

Simplify the expression

Source

pub fn eval(&self, values: &HashMap<String, f64>) -> SymEngineResult<f64>

Evaluate the expression with given variable values

§Errors

Returns an error if a variable is not found in the values map

Source

pub fn eval_complex( &self, values: &HashMap<String, f64>, ) -> SymEngineResult<Complex64>

Evaluate the expression to a complex number.

This can handle expressions containing the imaginary unit I, which is essential for quantum computing applications.

§Arguments
  • values - Map of variable names to real values
§Returns

The complex result of the evaluation.

§Errors

Returns an error if a variable is not found in the values map

Source

pub fn substitute(&self, var: &Self, value: &Self) -> Self

Substitute a variable with an expression

Source

pub fn substitute_many(&self, values: &HashMap<Self, Self>) -> Self

Substitute multiple variables

Trait Implementations§

Source§

impl Add for Expression

Source§

type Output = Expression

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

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 Expression

Source§

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

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

impl Display for Expression

Source§

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

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

impl Div for Expression

Source§

type Output = Expression

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl From<Complex<f64>> for Expression

Source§

fn from(c: Complex64) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Expression

Source§

fn from(f: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Expression

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Expression

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl Hash for Expression

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul for Expression

Source§

type Output = Expression

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for Expression

Source§

type Output = Expression

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Expression

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for Expression

Source§

type Output = Expression

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Eq for Expression

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

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

Source§

fn vzip(self) -> V