Skip to content
Snippets Groups Projects
core_operations.py 48.2 KiB
Newer Older
"""
B-ASIC Core Operations Module.

Contains some of the most commonly used mathematical operations.
Oscar Gustafsson's avatar
Oscar Gustafsson committed
from typing import Dict, Optional

from numpy import abs as np_abs
from numpy import conjugate, sqrt
from b_asic.graph_component import Name, TypeName
Oscar Gustafsson's avatar
Oscar Gustafsson committed
from b_asic.operation import AbstractOperation
from b_asic.port import SignalSourceProvider
Frans Skarman's avatar
Frans Skarman committed
from b_asic.types import Num


class Constant(AbstractOperation):
    Constant value operation.

    Gives a specified value that remains constant for every iteration.

    .. math:: y = \text{value}

    Parameters
    ==========

    value : Number, default: 0
        The constant value.
    name : Name, optional
        Operation name.
    __slots__ = ("_value", "_name")
    _value: Num
    _name: Name

Andreas Bolin's avatar
Andreas Bolin committed
    _execution_time = 0
Oscar Gustafsson's avatar
Oscar Gustafsson committed
    is_linear = True
    is_constant = True
Andreas Bolin's avatar
Andreas Bolin committed

Frans Skarman's avatar
Frans Skarman committed
    def __init__(self, value: Num = 0, name: Name = ""):
        """Construct a Constant operation with the given value."""
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        super().__init__(
            input_count=0,
            output_count=1,
Frans Skarman's avatar
Frans Skarman committed
            name=name,
Oscar Gustafsson's avatar
Oscar Gustafsson committed
            latency_offsets={"out0": 0},
        )
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        return TypeName("c")

    def evaluate(self):
        return self.param("value")

    @property
Frans Skarman's avatar
Frans Skarman committed
    def value(self) -> Num:
        """Get the constant value of this operation."""
        return self.param("value")

    @value.setter
Frans Skarman's avatar
Frans Skarman committed
    def value(self, value: Num) -> None:
        """Set the constant value of this operation."""
        self.set_param("value", value)
    @property
    def latency(self) -> int:
        return self.latency_offsets["out0"]

    def __repr__(self) -> str:
        return f"Constant({self.value})"

    def __str__(self) -> str:
        return f"{self.value}"

class Addition(AbstractOperation):
    """
    Binary addition operation.
    .. math:: y = x_0 + x_1

    Parameters
    ==========

    src0, src1 : SignalSourceProvider, optional
        The two signals to add.
    name : Name, optional
        Operation name.
    latency : int, optional
        Operation latency (delay from input to output in time units).
    latency_offsets : dict[str, int], optional
        Used if inputs have different arrival times, e.g.,
        ``{"in0": 0, "in1": 1}`` which corresponds to *src1* arriving one
        time unit later than *src0*. If not provided and *latency* is
        provided, set to zero if not explicitly provided. So the previous
        example can be written as ``{"in1": 1}`` only.
    execution_time : int, optional
        Operation execution time (time units before operator can be
        reused).

    See also
    ========
    AddSub
    __slots__ = (
        "_src0",
        "_src1",
        "_name",
        "_latency",
        "_latency_offsets",
        "_execution_time",
    )
    _src0: Optional[SignalSourceProvider]
    _src1: Optional[SignalSourceProvider]
    _name: Name
    _latency: Optional[int]
    _latency_offsets: Optional[Dict[str, int]]
    _execution_time: Optional[int]

Oscar Gustafsson's avatar
Oscar Gustafsson committed
    is_linear = True
Oscar Gustafsson's avatar
Oscar Gustafsson committed

Oscar Gustafsson's avatar
Oscar Gustafsson committed
    def __init__(
        self,
        src0: Optional[SignalSourceProvider] = None,
        src1: Optional[SignalSourceProvider] = None,
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        name: Name = Name(""),
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        latency: Optional[int] = None,
        latency_offsets: Optional[Dict[str, int]] = None,
        execution_time: Optional[int] = None,
Oscar Gustafsson's avatar
Oscar Gustafsson committed
    ):
        """
        Construct an Addition operation.
        """
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        super().__init__(
            input_count=2,
            output_count=1,
Oscar Gustafsson's avatar
Oscar Gustafsson committed
            name=Name(name),
Oscar Gustafsson's avatar
Oscar Gustafsson committed
            input_sources=[src0, src1],
            latency=latency,
            latency_offsets=latency_offsets,
            execution_time=execution_time,
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        )
Oscar Gustafsson's avatar
Oscar Gustafsson committed
        return TypeName("add")

    def evaluate(self, a, b):
        return a + b


class Subtraction(AbstractOperation):
    """
    Binary subtraction operation.

    Gives the result of subtracting the second input from the first one.

    .. math:: y = x_0 - x_1

    Parameters
    ==========

    src0, src1 : SignalSourceProvider, optional
        The two signals to subtract.
    name : Name, optional
        Operation name.
    latency : int, optional
        Operation latency (delay from input to output in time units).
    latency_offsets : dict[str, int], optional
        Used if inputs have different arrival times, e.g.,
        ``{"in0": 0, "in1": 1}`` which corresponds to *src1* arriving one
        time unit later than *src0*. If not provided and *latency* is
        provided, set to zero if not explicitly provided. So the previous
        example can be written as ``{"in1": 1}`` only.
    execution_time : int, optional
        Operation execution time (time units before operator can be
        reused).

    See also
    ========
    AddSub
Oscar Gustafsson's avatar
Oscar Gustafsson committed
    is_linear = True

    __slots__ = (
        "_src0",
        "_src1",
        "_name",
        "_latency",
        "_latency_offsets",
Loading
Loading full blame...