# pypdevsbbl.generic.math module

Set of PythonPDEVS building blocks for doing math.

The core of this module is the `Equation` block, which allows a user to execute a function on a set of inputs.

Additionally, some few blocks with a variable set of inputs (like the sum and the product) will provide the remaining functionality.

class `pypdevsbbl.generic.math.``_SP`(name, n)

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Superclass for the `Adder` and `Multiplier` blocks.

  Warning

  This class should not be used as a block by itself.

  Parameters:

  - name (str) – The name of the block.
  - n (int) – The amount of input ports the block must have. These ports are accessible from the `inputs` list, using a 0-based index.

  State:

  - result (numeric) – The computed result of the block.
  - input-n (numeric) – The value of the n th input port.

  Input Ports: | inputs (list) – The list of all the input ports.
  Output Ports: | result (numeric) – The result of the computation.

  `operation`()

    The operation that needs to be computed upon arrival of any value.

  `timeAdvance`()
  `outputFnc`()
  `intTransition`()
  `extTransition`(inputs)

class `pypdevsbbl.generic.math.``Adder`(name, n)

  Bases: `pypdevsbbl.generic.math._SP`

  Computes the sum of a set of inputs.

  Parameters:

  - name (str) – The name of the block.
  - n (int) – The amount of input ports the block must have. These ports are accessible from the `inputs` list, using a 0-based index.

  State:

  - result (numeric) – The computed result of the block.
  - input-n (numeric) – The value of the n th input port.

  Input Ports: | inputs (list) – The list of all the input ports.
  Output Ports: | result (numeric) – The result of the computation.

  `operation`()

    The actual sum operation.

class `pypdevsbbl.generic.math.``Multiplier`(name, n)

  Bases: `pypdevsbbl.generic.math._SP`

  Computes the product of a set of inputs.

  Parameters:

  - name (str) – The name of the block.
  - n (int) – The amount of input ports the block must have. These ports are accessible from the `inputs` list, using a 0-based index.

  State:

  - result (numeric) – The computed result of the block.
  - input-n (numeric) – The value of the n th input port.

  Input Ports: | inputs (list) – The list of all the input ports.
  Output Ports: | result (numeric) – The result of the computation.

  `operation`()

    The actual product operation.

class `pypdevsbbl.generic.math.``Equation`(name, function)

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Execute an equation on a set of inputs.

  The input ports of this block are determined by the arguments of the passed `function`. Whenever a change happens on these ports and both have values, their result is outputted on the result port.

  Warning

  - The `function` cannot have a `result` parameter to prevent ambiguity with the output port.
  - The `function` must not change the values of the inputs.
  - This block is optimized for numerical equations, yet it is (because of Python) perfectly possible to use some other attributes.

  See also

  `pypdevsbbl.extra.equations` for a set of predefined equations which can be used as a function without too much thought.

  Parameters:

  - name (str) – The name of the block.
  - function (def) – The function that needs to be executed. Note that defaults can be used, but the `*args` and `**kwargs` information will be ignored.

  Input Ports: | Depends on the `function` that was passed. Say the `function` has two arguments: `a` and `b`, the input ports are also a and b.
  Output Ports: | result (any) – The output of the provided equation.

  `timeAdvance`()
  `outputFnc`()
  `intTransition`()
  `extTransition`(inputs)

class `pypdevsbbl.generic.math.``_DI`(name, ic=0)

  Bases: `pypdevs.DEVS.AtomicDEVS`

  Superclass of the `Differentiator` and `Integrator` blocks.

  Warning

  This class should not be used as a block by itself.

  Parameters:

  - name (str) – The name of the block.
  - ic (numeric) – The initial condition of the block. Defaults to 0.

  State:

  - prev (numeric) – The previous value. Defaults to the initial condition.
  - v (numeric) – The current value.

  Input Ports: | input (numeric) – The new value.
  Output Ports: | result (numeric) – The differential.

  `timeAdvance`()
  `outputFnc`()
  `intTransition`()
  `extTransition`(inputs)

class `pypdevsbbl.generic.math.``Differentiator`(name, ic=0)

  Bases: `pypdevsbbl.generic.math._DI`

  Differentiates the input over time.

  This block will make use of the backwards difference:

  \(\dfrac{d}{dt}f(t) = \lim_{\Delta t\to 0}\dfrac{f(t) - f(t - \Delta t)}{\Delta t} = \dfrac{x_i - x_{i-1}}{\Delta t}\)

  Parameters:

  - name (str) – The name of the block.
  - ic (numeric) – The initial condition of the block. Defaults to 0.

  State:

  - prev (numeric) – The previous value of the differential. Defaults to the initial condition.
  - v (numeric) – The current differential.

  Input Ports: | input (numeric) – The new value.
  Output Ports: | result (numeric) – The differential.

  `extTransition`(inputs)

class `pypdevsbbl.generic.math.``Integrator`(name, ic=0, method=<function Integrator.INTEGRAL_TRAPEZOID>)

  Bases: `pypdevsbbl.generic.math._DI`

  Integrates the input over time.

  \(v(t) = {\displaystyle \int} f(\tau)\ d\tau = ic + {\displaystyle \int_0^t} f(\tau)\ d\tau = v(t - \Delta t) + {\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau\)

  Parameters:

  - name (str) – The name of the block.
  - ic (numeric) – The initial condition of the block. Defaults to 0.
  - method (def) – A function to determine the computation of the integral. It’s preferrably one of `INTEGRAL_LOWER()`, `INTEGRAL_UPPER()` or `INTEGRAL_TRAPEZOID()`. Defaults to `INTEGRAL_TRAPEZOID()`.

  State:

  - prev (numeric) – The previous value of the integral. Defaults to the initial condition.
  - v (numeric) – The current integral.
  - fprev (numeric) – The previous value of the function. When `None`, the `method` is ignored.

  Input Ports: | input (numeric) – The new value.
  Output Ports: | result (numeric) – The differential.

  `INTEGRAL_LOWER`(curr)

    Computes the integral from the lower-bound.

    \({\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau = \Delta t\cdot f(t - \Delta t)\)

    Parameters: | curr (numeric) – Current input value.

  `INTEGRAL_UPPER`(curr)

    Computes the integral from the upper-bound.

    \({\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau = \Delta t\cdot f(t)\)

    Parameters: | curr (numeric) – Current input value.

  `INTEGRAL_TRAPEZOID`(curr)

    Computes the integral with the trapezoidal rule.

    \({\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau = \dfrac{\Delta t\cdot\left(f(t - \Delta t) + f(t)\right)}{2}\)

    Parameters: | curr (numeric) – Current input value.

  `extTransition`(inputs)

class `pypdevsbbl.generic.math.``Random`(name: str, seed=None, dist=<function Random.<lambda>>, args=())

  Bases: `pypdevs.DEVS.AtomicDEVS`

  `AtomicDEVS` that generates a random number every time a message arrives.

  It makes use of the RngStreams random number generator i.i.d. (0, 1). Using the `dist` argument, one can simply change the distribution over which numbers need to be generated. The `pypdevsbbl.extra.rngstreams.distributions` module provides an extensive implementation of a lot of distributions.

  See also

  - `pypdevsbbl.generic.generators.RandomNumberGenerator`
  - `pypdevsbbl.generic.generators.RandomDelayGenerator`
  - `pypdevsbbl.extra.rngstreams`
  - `pypdevsbbl.extra.rngstreams.distributions`

  Parameters:

  - name (str) – The name of the block and the RNG.
  - seed (numeric) – The seed for the generator. When `None`, no seed will be set. Defaults to `None`.
  - dist (def) – A stateless function that implements an inversive cumulative distribution function. It takes at least a single argument, which falls in (0, 1). Defaults to a uniform distribution over (0, 1).
  - args (tuple) – Optional additional arguments to add to the distribution.

  State:

  - num (numeric) – The random number that’s being/needs to be outputted.
  - item (any) – The item that’s passing through.

  Input Ports: | input (any) – A new input, indicative of an random number request.

  Output Ports:

  - random (numeric) – The random number.
  - value (any) – Outputs the input once more.

  `timeAdvance`()
  `extTransition`(inputs)
  `intTransition`()
  `outputFnc`()
  `next`()

    Obtains a random variable w.r.t. this block’s config.

    Do note that this updates the gen object, meaning it must only be called in functions that may update the state.