Skip to content

Assignments

Assignment(symbol, expression, parsing_locals={})

Bases: ABC

Base class for storing a symbol wrapper together with a symbolic expression. An assignment is a formal equality between the symbol wrapper and the expression.

PARAMETER DESCRIPTION
symbol

LHS of the assignment (e.g. parameter or variable). If input is a string, it is converted to a SymbolWrapper instance.

TYPE: str

expression

expression on the RHS. If input is a string or number, it is converted to a sympy expression.

TYPE: Basic | str | float | int

parsing_locals

a dictionary mapping strings to sympy objects.

TYPE: dict DEFAULT: {}

Source code in psymple/abstract.py
def __init__(self, symbol: str, expression: Basic | str | float | int, parsing_locals: dict={}):
    """
    Instantiate an Assignment.

    Args:
        symbol: LHS of the assignment (e.g. parameter or variable). If input is a string, 
            it is converted to a [SymbolWrapper][psymple.abstract.SymbolWrapper] instance.
        expression: expression on the RHS. If input is a string or number, 
            it is converted to a sympy expression.
        parsing_locals: a dictionary mapping strings to sympy objects.
    """
    self.symbol_wrapper = SymbolWrapper(symbol)
    self.expression_wrapper = ExpressionWrapper(expression, parsing_locals)

DifferentialAssignment(symbol, expression, parsing_locals={})

Bases: Assignment

Class storing an ODE of the form \( dx/dt = f(x,t,b) \) where \( b \) is a list of external dependencies.

PARAMETER DESCRIPTION
symbol

LHS of the assignment containing the variable derivative. If input is a string, it is converted to a SymbolWrapper instance.

TYPE: str

expression

function on the RHS. If input is a string or number, it is converted to a sympy expression.

TYPE: Expr | str | float | int

parsing_locals

a dictionary mapping strings to sympy objects.

TYPE: dict DEFAULT: {}

Source code in psymple/build/assignments.py
def __init__(self, symbol: str, expression: Expr | str | float | int, parsing_locals: dict={}):
    """
    Intantiate a DifferentialAssignment. 

    Args:
        symbol: LHS of the assignment containing the variable derivative. 
            If input is a string, it is converted to a [SymbolWrapper][psymple.abstract.SymbolWrapper] instance.
        expression: function on the RHS. If input is a string or number, 
            it is converted to a sympy expression.
        parsing_locals: a dictionary mapping strings to sympy objects.
    """
    super().__init__(symbol, expression, parsing_locals)
    # Coerce self.symbol_wrapper into instance of Variable.
    if type(self.symbol_wrapper) is SymbolWrapper:
        self.symbol_wrapper = Variable(
            symbol=self.symbol_wrapper.symbol,
            description=self.symbol_wrapper.description,
        )

ParameterAssignment(symbol, expression, parsing_locals={})

Bases: Assignment

Class storing an assignment of the form \( y = f(x,t,b) \), where \( b \) is a list of external dependencies.

PARAMETER DESCRIPTION
symbol

LHS of the assignment containing the parameter name. If input is a string, it is converted to a SymbolWrapper instance.

TYPE: str

expression

function on the RHS. If input is a string or number, it is converted to a sympy expression.

TYPE: Expr | str | float | int

parsing_locals

a dictionary mapping strings to sympy objects.

TYPE: dict DEFAULT: {}

Source code in psymple/build/assignments.py
def __init__(self, symbol: str, expression: Expr | str | float | int, parsing_locals: dict={}):
    """
    Instantiate a ParameterAssignment. 

    Args:
        symbol: LHS of the assignment containing the parameter name. 
            If input is a string, it is converted to a [SymbolWrapper][psymple.abstract.SymbolWrapper] instance.
        expression: function on the RHS. If input is a string or number, 
            it is converted to a sympy expression.
        parsing_locals: a dictionary mapping strings to sympy objects.
    """
    super().__init__(symbol, expression, parsing_locals)
    # We ensure we that the symbol_wrapper is instance of Parameter.
    if type(self.symbol_wrapper) is SymbolWrapper:
        self.symbol_wrapper = Parameter(
            self.symbol_wrapper.symbol,
            self.expression,
            self.symbol_wrapper.description,
        )
    # We forbid the symbol wrapper to appear in the expression eg. R=2*R unless the expression
    # and symbol are identical
    if (self.symbol in self.expression.free_symbols) and (self.symbol != self.expression):
        raise DependencyError(
            f"The symbol {self.symbol} cannot appear as both the function "
            f"value and argument of {self}."
        )

FunctionalAssignment(symbol, expression, parsing_locals={})

Bases: ParameterAssignment

A convenience class to identify parameters which have been constructed from the OutputPort of a FunctionalPortedObject. These represent the core functional building blocks of a System.

Source code in psymple/build/assignments.py
def __init__(self, symbol: str, expression: Expr | str | float | int, parsing_locals: dict={}):
    """
    Instantiate a ParameterAssignment. 

    Args:
        symbol: LHS of the assignment containing the parameter name. 
            If input is a string, it is converted to a [SymbolWrapper][psymple.abstract.SymbolWrapper] instance.
        expression: function on the RHS. If input is a string or number, 
            it is converted to a sympy expression.
        parsing_locals: a dictionary mapping strings to sympy objects.
    """
    super().__init__(symbol, expression, parsing_locals)
    # We ensure we that the symbol_wrapper is instance of Parameter.
    if type(self.symbol_wrapper) is SymbolWrapper:
        self.symbol_wrapper = Parameter(
            self.symbol_wrapper.symbol,
            self.expression,
            self.symbol_wrapper.description,
        )
    # We forbid the symbol wrapper to appear in the expression eg. R=2*R unless the expression
    # and symbol are identical
    if (self.symbol in self.expression.free_symbols) and (self.symbol != self.expression):
        raise DependencyError(
            f"The symbol {self.symbol} cannot appear as both the function "
            f"value and argument of {self}."
        )

DefaultParameterAssignment(symbol, expression, parsing_locals={})

Bases: ParameterAssignment

A convenience class to identify parameters which have been constructed from default values. These represent those system parameters which are changeable.

Source code in psymple/build/assignments.py
def __init__(self, symbol: str, expression: Expr | str | float | int, parsing_locals: dict={}):
    """
    Instantiate a ParameterAssignment. 

    Args:
        symbol: LHS of the assignment containing the parameter name. 
            If input is a string, it is converted to a [SymbolWrapper][psymple.abstract.SymbolWrapper] instance.
        expression: function on the RHS. If input is a string or number, 
            it is converted to a sympy expression.
        parsing_locals: a dictionary mapping strings to sympy objects.
    """
    super().__init__(symbol, expression, parsing_locals)
    # We ensure we that the symbol_wrapper is instance of Parameter.
    if type(self.symbol_wrapper) is SymbolWrapper:
        self.symbol_wrapper = Parameter(
            self.symbol_wrapper.symbol,
            self.expression,
            self.symbol_wrapper.description,
        )
    # We forbid the symbol wrapper to appear in the expression eg. R=2*R unless the expression
    # and symbol are identical
    if (self.symbol in self.expression.free_symbols) and (self.symbol != self.expression):
        raise DependencyError(
            f"The symbol {self.symbol} cannot appear as both the function "
            f"value and argument of {self}."
        )