Skip to content

Variables

SymbolWrapper(symbol, description='')

Bases: ABC

A class storing a sympy.Symbol instance with other attributes.

PARAMETER DESCRIPTION
symbol

the symbol to wrap

TYPE: str | Symbol

description

an optional description of the container contents

TYPE: str DEFAULT: ''

Source code in psymple/abstract.py
def __init__(self, symbol: str | Symbol, description: str = ""):
    """
    Create a SymbolWrapper instance.

    Args:
        symbol: the symbol to wrap
        description: an optional description of the container contents
    """
    if isinstance(symbol, str):
        symbol = Symbol(symbol)
    self.symbol = symbol
    self.description = description

Variable(symbol, description='')

Bases: SymbolWrapper

A variable is a SymbolWrapper instance together with a description.

Source code in psymple/abstract.py
def __init__(self, symbol: str | Symbol, description: str = ""):
    """
    Create a SymbolWrapper instance.

    Args:
        symbol: the symbol to wrap
        description: an optional description of the container contents
    """
    if isinstance(symbol, str):
        symbol = Symbol(symbol)
    self.symbol = symbol
    self.description = description

Parameter(symbol, value, description='')

Bases: SymbolWrapper

A parameter is a SymbolWrapper instance together with an equivalent value and description.

PARAMETER DESCRIPTION
symbol

the symbol to wrap.

TYPE: str | Symbol

value

the value represented by symbol.

TYPE: str | float | Basic

description

an optional description of the container contents.

TYPE: str DEFAULT: ''

Source code in psymple/variables.py
def __init__(self, symbol: str | Symbol, value: str | float | Basic, description: str = ""):
    """
    Create a Parameter instance.

    Args:
        symbol: the symbol to wrap.
        value: the value represented by `symbol`.
        description: an optional description of the container contents.
    """
    super().__init__(symbol, description)
    self.value = value

ExpressionWrapper(expression, parsing_locals={})

Bases: ABC

A class storing a sympy.Basic object: anything returned by sympy.sympify.

PARAMETER DESCRIPTION
expression

the expression to wrap. If it is not a sympy object, it is parsed into one first.

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, expression: Basic | str | float | int, parsing_locals: dict = {}):
    """
    Create an ExpressionWrapper instance

    Args:
        expression: the expression to wrap. If it is not a `sympy` object, it is 
            parsed into one first.
        parsing_locals: a dictionary mapping strings to sympy objects.
    """
    if expression is not None:
        if isinstance(expression, str):
            expression = parse_expr(expression, local_dict=parsing_locals)
        elif isinstance(expression, (int, float)):
            expression = Number(expression)
        if not isinstance(expression, Basic):
            raise ParsingError(f"Expression {expression} of type {type(expression)} is not an accepted type.")
    self.expression = expression

UpdateRule(expression=0, variables=set(), parameters=set(), description='')

Bases: ExpressionWrapper

An update rule is an ExpressionWrapper that is attached to a Variable or Parameter instance. It stores how the variable or parameter will evolve over time. An update rule tracks the other variables or parameters which appear in its expression, so that it is fully aware of its system dependencies.

METHOD DESCRIPTION
sub_symbols

substitute the symbols contained inside self.

PARAMETER DESCRIPTION
expression

the expression to wrap. If it is not a sympy object, it is parsed into one first.

TYPE: Basic | str | float | int DEFAULT: 0

variables

a set of variables of which the variables in the expression are a subset

TYPE: set DEFAULT: set()

parameters

a set of parameters of which the parameters in the expression are a subset

TYPE: set DEFAULT: set()

description

description of the rule

TYPE: str DEFAULT: ''

Source code in psymple/variables.py
def __init__(
    self,
    expression: Basic | str | float | int = 0,
    variables: set = set(),
    parameters: set = set(),
    description: str = "",
):
    """
    Create an UpdateRule instance.

    Args:
        expression: the expression to wrap. If it is not a `sympy` object, it is 
            parsed into one first.
        variables: a set of variables of which the variables in the expression are a subset
        parameters: a set of parameters of which the parameters in the expression are a subset
        description: description of the rule
    """
    super().__init__(expression)
    self._initialise_dependencies(variables, parameters)
    self.description = description
    self._equation_lambdified = None

sub_symbols(vars_dict, pars_dict)

Substitute the variables and parameters of self according to dictionary mappings.

PARAMETER DESCRIPTION
vars_dict

a dictionary providing mappings between sympy.Symbol objects

TYPE: dict

pars_dict

a dictionary providing mappings between sympy.Symbol objects

TYPE: dict

Source code in psymple/variables.py
def sub_symbols(self, vars_dict: dict, pars_dict: dict):
    """
    Substitute the variables and parameters of `self` according to dictionary mappings.

    Args:
        vars_dict: a dictionary providing mappings between `sympy.Symbol` objects
        pars_dict: a dictionary providing mappings between `sympy.Symbol` objects
    """
    vars_dict_filter = {symbol: vars_dict[symbol] for symbol in self.variables}
    pars_dict_filter = {symbol: pars_dict[symbol] for symbol in self.parameters}
    subbed_vars = {vars_dict[v] for v in self.variables}
    subbed_pars = {pars_dict[p] for p in self.parameters}
    subbed_expr = self.expression.subs(vars_dict_filter | pars_dict_filter)
    self.variables = subbed_vars
    self.parameters = subbed_pars
    self.expression = subbed_expr