pymoto.MathExpression

class pymoto.MathExpression(expression: str)

General mathematical expression module

This block can evaluate symbolic mathematical expressions. The derivatives are automatically calculated using sympy. Variables in this expression can be given by using the signal’s global name (Signal.tag), or by the signal’s position in the input signal list: "inp0" for the first input signal given, "inp1" for the second input signal, "inp2" for the third, etc

Example

Two scalars:

from pymoto import Signal, MathExpression
s1 = Signal("x", 1.3)
s2 = Signal("y", 4.8)
m = MathExpression([s1, s2], Signal("output"), "inp0*inp1")  # or "x * y" as expression
m.response()
assert (m.sig_out[0].state == 1.3*4.8)

Scalar and Vector:

s1 = Signal("x", np.array([1.3, 2.5, 9.4]))
s2 = Signal("y", 4.8)
m = MathExpression([s1, s2], Signal("output"), "sin(x)*y").response()
assert (m.sig_out[0].state.shape == (3,))
from math import sin
assert (abs(m.sig_out[0].state[0] - sin(1.3)*4.8) < 1e-10)
assert (abs(m.sig_out[0].state[1] - sin(2.5)*4.8) < 1e-10)
assert (abs(m.sig_out[0].state[2] - sin(9.4)*4.8) < 1e-10)
Input signals:

*args (float or np.ndarray): Any number of numerical inputs which match the provided expression

Output signal:

y (float or np.ndarray): Result of the mathematical operation

References

__init__(expression: str)

Initialize the MathExpression module

Parameters:

expression (str) – The mathematical expression to be evaluated

Methods

__init__(expression)

Initialize the MathExpression module

connect(sig_in[, sig_out])

Connect without automatic adding to a function network

get_input_sensitivities([as_list])

get_input_states([as_list])

get_output_sensitivities([as_list])

get_output_states([as_list])

parse_expression([n_args])

reset()

Reset the state of the sensitivities (they are set to zero or to None)

response()

Calculate the response from sig_in and output this to sig_out

sensitivity()

Calculate sensitivities using backpropagation

Attributes

n_in

Get the number of input signals

n_out

Get the number of output signals

sig_in

sig_out

parse_expression(n_args: int = None)
connect(sig_in: Signal | Iterable[Signal], sig_out: Signal | Iterable[Signal] = None)

Connect without automatic adding to a function network

get_input_sensitivities(as_list=False)
get_input_states(as_list=False)
get_output_sensitivities(as_list=False)
get_output_states(as_list=False)
property n_in: int

Get the number of input signals

property n_out: int

Get the number of output signals

Note: Cannot be used in the initial __call__()

reset()

Reset the state of the sensitivities (they are set to zero or to None)

response()

Calculate the response from sig_in and output this to sig_out

sensitivity()

Calculate sensitivities using backpropagation

Based on the sensitivity we get from sig_out, reverse the process and output the new sensitivities to sig_in

sig_in: List = None
sig_out: List = None