Skip to content
Symi Docs
Python
On this page

Expression methods

Single-subject operations (transforms, calculus, solving, output) are also available directly as methods on Expression — e.simplify(), e.integrate("x"), e.solve("x"), e.to_latex(), … — delegating to the context the expression belongs to. The entries in the operation chapters document parameters and semantics; the method form drops the leading expression argument. Exact input conversion and context ownership are specified in Conversions and collections and Context safety.

is_stale#

Expression method

is_stale: bool

Reports whether resetting the owning context invalidated the expression. All other operations reject a stale receiver with an actionable error.

Arithmetic#

Python uses the native operators + - * / ** -; operands may be expressions, integers, or exact fractions. JavaScript uses the equivalent named methods and accepts facade expressions, bigint, and safe integral number values. All route through the auto-simplifying builders.

add#

Not exposed by the Python bindings. Available as Expression.add in WASM / JavaScript, UniffiExpression.add in Kotlin, UniffiExpression.add in Swift, api::Expression::add in Rust.

subtract#

Not exposed by the Python bindings. Available as Expression.subtract in WASM / JavaScript, UniffiExpression.subtract in Kotlin, UniffiExpression.subtract in Swift, api::Expression::subtract in Rust.

multiply#

Not exposed by the Python bindings. Available as Expression.multiply in WASM / JavaScript, UniffiExpression.multiply in Kotlin, UniffiExpression.multiply in Swift, api::Expression::multiply in Rust.

divide#

Not exposed by the Python bindings. Available as Expression.divide in WASM / JavaScript, UniffiExpression.divide in Kotlin, UniffiExpression.divide in Swift, api::Expression::divide in Rust.

power#

Not exposed by the Python bindings. Available as Expression.power in WASM / JavaScript, UniffiExpression.power in Kotlin, UniffiExpression.power in Swift, api::Expression::power in Rust.

negate#

Not exposed by the Python bindings. Available as Expression.negate in WASM / JavaScript, UniffiExpression.negate in Kotlin, UniffiExpression.negate in Swift, api::Expression::negate in Rust.

Substitution and structure#

substitute#

Expression method

substitute(variable: VariableLike, value: ExpressionLike) -> Expression

Replace every free occurrence of the named symbol with value, re-running auto-simplification.

rewrite_in_terms_of#

Default context

rewrite_in_terms_of(
    target: ExpressionLike,
    source: ExpressionLike,
    replacement: ExpressionLike,
) -> Expression
Calling forms

Explicit context: Context.rewrite_in_terms_of

rewrite_in_terms_of(
    target: ExpressionLike,
    source: ExpressionLike,
    replacement: ExpressionLike,
) -> Expression

Expression method: Expression.rewrite_in_terms_of

rewrite_in_terms_of(
    source: ExpressionLike,
    replacement: ExpressionLike,
) -> Expression

Rewrite the target in terms of replacement under the caller-supplied formal relation replacement = source. Exact subtrees, canonical additive and multiplicative extractions, integer exponential families, and positive same-base power families are supported. Every inferred match is accepted only when substituting source back exactly reconstructs the matched target. Fractional exponential relations and branch-sensitive power identities conservatively remain unchanged. To introduce a displayed name such as z, pass symbol("z") as replacement; assigning the source expression to a host-language variable named z does not create a symbolic name.

free_variables#

Expression method

free_variables() -> list[str]

Sorted names of the free symbols. Bound binders (integration variables, image-set parameters) are excluded; the constants pi/e are not symbols.

expression_type#

Expression method

expression_type() -> str

Top-level node kind: one of integer, rational, symbol, constant, sum, product, power, factorial, function_call, derivative, integral, summation, laplace_transform, inverse_laplace_transform, fourier_transform, inverse_fourier_transform, piecewise, ordinary_differential_equation, recurrence_equation, interval, finite_set, set_union, set_intersection, set_complement, image_set, condition_set.

node_count#

Expression method

node_count() -> int

Number of nodes in the expression tree — the size measure used by the simplifier; useful for comparing alternative forms.

duplicate#

Expression method

duplicate() -> Expression

An independent handle to the same underlying expression. The recommended JavaScript facade duplicates values before passing them through ownership-transferring raw WASM arrays, so ordinary facade calls are non-destructive. Callers using facade.raw directly must still duplicate a handle they intend to retain. Python arguments are borrowed.

Equality#

equals#

Expression method

equals(other: ExpressionLike) -> bool

Mathematical equality test: simplify⁡(self⁡−other⁡)=0\operatorname{simplify}(\operatorname{self} - \operatorname{other}) = 0. A False result means "could not prove equal", not a disproof.

Shape queries#

is_matrix#

Expression method

is_matrix: bool

Always False on expressions and True on Matrix objects, so mixed result streams can be discriminated.

is_equality#

Expression method

is_equality() -> bool

True for equal(a, b) relation nodes.

is_integral#

Expression method

is_integral() -> bool

True for unevaluated integral nodes. (Distinct from the assumption query is_integer.)

is_union#

Expression method

is_union() -> bool

is_image_set#

Expression method

is_image_set() -> bool

is_finite_set#

Expression method

is_finite_set() -> bool

is_interval#

Expression method

is_interval() -> bool

Assumption queries (three-valued)#

All return True/False/None (undefined in JS): a definite verdict only on proof from the structure and the symbol assumptions.

is_real#

Expression method

is_real() -> Optional[bool]

is_positive#

Expression method

is_positive() -> Optional[bool]

is_negative#

Expression method

is_negative() -> Optional[bool]

is_integer#

Expression method

is_integer() -> Optional[bool]

is_rational#

Expression method

is_rational() -> Optional[bool]

is_nonzero#

Expression method

is_nonzero() -> Optional[bool]

is_nonnegative#

Expression method

is_nonnegative() -> Optional[bool]

Answers True only on proof that the expression is greater than or equal to zero — an even power of a provably real base, a sum or product of such, or anything already provably positive. It never answers False: a failure to prove non-negativity is not a proof of negativity, so the undecided verdict is None.

is_finite#

Expression method

is_finite() -> Optional[bool]

is_defined#

Expression method

is_defined() -> Optional[bool]

Answers True only on proof that the expression denotes one scalar value at the active point, and False only on proof that it denotes none — a zero denominator, a zero logarithm argument, or a proved indeterminate extended-value combination. Definedness is pointwise: it says nothing about continuity or analyticity near the point, and a point on a chosen principal branch cut such as log(-1) is defined even though the principal logarithm is discontinuous there. The signed infinities are defined extended-real values but are not finite.

Structural accessors#

interval_lower#

Expression method

interval_lower() -> Expression

Lower endpoint of an interval node; raises on other shapes.

interval_upper#

Expression method

interval_upper() -> Expression

integral_integrand#

Expression method

integral_integrand() -> Expression

integral_variable#

Expression method

integral_variable() -> str

integral_lower_bound#

Expression method

integral_lower_bound() -> Optional[Expression]

None for indefinite integrals.

integral_upper_bound#

Expression method

integral_upper_bound() -> Optional[Expression]

union_components#

Expression method

union_components() -> list[Any]

image_set_lambda_expression#

Expression method

image_set_lambda_expression() -> Expression

image_set_variable#

Expression method

image_set_variable() -> str

image_set_domain#

Expression method

image_set_domain() -> Expression

to_list#

Expression method

to_list() -> list[Any]

Elements of a finite set; raises on other shapes. Python finite sets also support len() and iteration.

length#

Element count of a finite set (Python: len(expression)).

String conversion#

to_string#

Human-readable form; Python uses str(expression) / repr(expression).

symbol_name#

Expression method

symbol_name() -> str

Returns the name of a symbol expression and rejects every other expression kind.

numerator_denominator#

Default context

numerator_denominator(input_expression: ExpressionLike) -> tuple[Expression, Expression]
Calling forms

Explicit context: Context.numerator_denominator

numerator_denominator(input_expression: ExpressionLike) -> tuple[Expression, Expression]

Expression method: Expression.numerator_denominator

numerator_denominator() -> tuple[Expression, Expression]

Returns the exact structural numerator and denominator without combining sums or cancelling factors. Python returns a two-tuple and the JavaScript facade returns a record.

numerator#

Default context

numerator(input_expression: ExpressionLike) -> Expression
Calling forms

Explicit context: Context.numerator

numerator(input_expression: ExpressionLike) -> Expression

Expression method: Expression.numerator

numerator() -> Expression

Returns the structural numerator from numerator_denominator.

denominator#

Default context

denominator(input_expression: ExpressionLike) -> Expression
Calling forms

Explicit context: Context.denominator

denominator(input_expression: ExpressionLike) -> Expression

Expression method: Expression.denominator

denominator() -> Expression

Returns the structural denominator from numerator_denominator.

Example#

Inspect and rewrite an expression through its own methods.

Python
import symi

x = symi.symbol("x")
target = (x**2 - 1) / (x + 1)

print(target.cancel())
print(target.numerator_denominator())
print(target.substitute(x, 3).execute())
print((x**2 - 1).factor())

is_nonpositive#

Expression method

is_nonpositive() -> Optional[bool]

Whether the expression is known to be nonpositive. Three-valued, like the other sign predicates on this page: None means the sign was not decided from the assumptions in force, not that the expression is positive. Compare is_nonnegative.

Additional API#

Expression#

Type

class Expression

A symbolic expression owned by the context that built it.

add#

Expression method

__add__(other: Any) -> Expression

Sum self + other, returning a new expression.

bool#

Expression method

__bool__() -> bool

Always raises: symbolic truth is three-valued, so use evaluate_truth instead.

complex#

Expression method

__complex__() -> complex

The certified value as a complex number; raises unless the value is certified.

eq#

Expression method

__eq__(other: Any) -> bool

Structural equality of two expressions from the same live context; use equal to build a relation.

float#

Expression method

__float__() -> float

The certified real value as a float; raises unless the value is certified and exactly real.

getattribute#

Expression method

__getattribute__(name: str) -> Any

Reject attribute access on a stale object before delegating to the default lookup.

hash#

Expression method

__hash__() -> int

Hash consistent with structural equality, so expressions can key a dict or set.

mul#

Expression method

__mul__(other: ExpressionLike) -> Expression

Product self * other, returning a new expression. A matrix operand is left to the matrix, which scales every entry by this expression.

ne#

Expression method

__ne__(other: Any) -> bool

Structural inequality, the negation of __eq__.

neg#

Expression method

__neg__() -> Expression

Negation -self, returning a new expression.

pow#

Expression method

__pow__(exponent: Any, modulo: Optional[Any] = ...) -> Expression

Power self ** exponent; the three-argument form is rejected.

radd#

Expression method

__radd__(other: Any) -> Expression

Sum other + self, returning a new expression.

repr#

Expression method

__repr__() -> str

The repr() text, which reports staleness instead of failing.

rmul#

Expression method

__rmul__(other: ExpressionLike) -> Expression

Product other * self, returning a new expression.

rpow#

Expression method

__rpow__(base: Any, modulo: Optional[Any] = ...) -> Expression

Power base ** self; the three-argument form is rejected.

rsub#

Expression method

__rsub__(other: Any) -> Expression

Difference other - self, returning a new expression.

rtruediv#

Expression method

__rtruediv__(other: Any) -> Expression

Quotient other / self, returning a new expression.

str#

Expression method

__str__() -> str

The mathematical text of the expression.

sub#

Expression method

__sub__(other: Any) -> Expression

Difference self - other, returning a new expression.

truediv#

Expression method

__truediv__(other: Any) -> Expression

Quotient self / other, returning a new expression.

is_algebraic#

Expression method

is_algebraic() -> Optional[bool]

Whether the expression is known to be algebraic.

is_complex#

Expression method

is_complex() -> Optional[bool]

Whether the expression is known to be finite complex.

is_composite#

Expression method

is_composite() -> Optional[bool]

Whether the expression is known to be composite.

is_even#

Expression method

is_even() -> Optional[bool]

Whether the expression is known to be even.

is_extended_real#

Expression method

is_extended_real() -> Optional[bool]

Whether the expression is known to be extended-real.

is_infinite#

Expression method

is_infinite() -> Optional[bool]

Whether the expression is known to be infinite.

is_irrational#

Expression method

is_irrational() -> Optional[bool]

Whether the expression is known to be irrational.

is_natural#

Expression method

is_natural() -> Optional[bool]

Whether the expression is known to be natural.

is_odd#

Expression method

is_odd() -> Optional[bool]

Whether the expression is known to be odd.

is_positive_integer#

Expression method

is_positive_integer() -> Optional[bool]

Whether the expression is known to be a positive integer.

is_transcendental#

Expression method

is_transcendental() -> Optional[bool]

Whether the expression is known to be transcendental.

is_zero#

Expression method

is_zero() -> Optional[bool]

Whether the expression is known to be zero.

Symi documentation · Python