Ejemplo n.º 1
0
    def __init__(self, variable):
        """Represents the absolute value squared of a variable

        Parameters
        ----------
        variable : str
            Name of variable.
        """
        assert_str(variable)
        self._variable = variable
Ejemplo n.º 2
0
 def __init__(self, scalar, variable):
     assert_is_scalar(scalar)
     assert_str(variable)
     if not isinstance(scalar, ProductOfScalars):
         raise TypeError(
             f"scalar should be ProductOfScalars, not {type(scalar)}")
     if not all(has_variable(factor, variable) for factor in scalar):
         raise ValueError("all factors should have the integration term")
     self._scalar = scalar
     self._variable = variable
Ejemplo n.º 3
0
    def __init__(self, func_name1, func_name2):
        """Represents the inner product of two functions.

        Parameters
        ----------
        func_name1 : str
            Name of first function
        func_name2 : str
            Name of second function
        """
        assert_str(func_name1)
        assert_str(func_name2)
        self._func_names = sorted([func_name1, func_name2])
Ejemplo n.º 4
0
    def __init__(self, variable, conjugate=False):
        """Represents a number as a variable

        Parameters
        ----------
        variable : str
            Name of variable.
        conjugate (optional) : bool
            If the variable is conjugated (default: `False`)
        """
        assert_str(variable)
        self._variable = variable
        self._conjugate = conjugate
Ejemplo n.º 5
0
    def __init__(self, var1, var2):
        """Delta function between two variables, e.g. d(x - y)

        Parameters
        ----------
        var1 : str
            First variable
        var2 : str
            First variable
        """
        assert_str(var1)
        assert_str(var2)
        self._assert_different(var1, var2)
        self._vars = [var1, var2]
Ejemplo n.º 6
0
    def __init__(self, func_name, variable, conjugate=False):
        """Represents a function with a single symbolic variable, e.g. f(x).

        Parameters
        ----------
        func_name : str
            Name of function.
        variable : str
            Name of variable.
        conjugate (optional) : bool
            If the functions is conjugated (default: `False`)
        """
        assert_str(func_name)
        assert_str(variable)
        self._func_name = func_name
        self._variable = variable
        self._conjugate = conjugate
Ejemplo n.º 7
0
    def __init__(self, mode, variable, creation=True):
        """
        Represents an creation/annihilation operator in a given mode with a given variable
        representing for example the frequency of the excitation.

        Parameters
        ----------

        mode: str
            The mode of the excitation
        variable: str
            The variable describing the continous variable
        creation : bool
            Whether this is a creation operator or not (annihilation).
        """
        assert_str(mode)
        assert_str(variable)
        self._mode = mode
        self._variable = variable
        self._creation = creation
Ejemplo n.º 8
0
def integrate(scalar, variable=None):
    """
    Integrates a scalar over a given variable or variables.

    Parameters
    ----------
    scalar : :class:`~.scalar.Scalar`
        The scalar to integrate.
    variable : None or str or set of str
        The variable(s) to integrate over.
        Can be:
        * `None`: Then all variables in the scalar are integrated out.
        * `str`: Then a single variable is integrated out.
        * `set` of `str`: Then all the variables in the set are integrated out.

    Returns
    -------
    :class:`~.scalar.Scalar`
        The output of the integration.
    """
    # TODO needed?
    scalar = simplify(scalar)
    if variable is None:
        return integrate(scalar, get_variables(scalar))
    if isinstance(variable, set):
        new_scalar = scalar
        for v in variable:
            new_scalar = integrate(new_scalar, v)
        return new_scalar
    assert_str(variable)
    if isinstance(scalar, SumOfScalars):
        new_scalar = sum(integrate(s, variable) for s in scalar._terms)
    # elif isinstance(scalar, ProductOfScalars):
    elif isinstance(scalar, Scalar):
        if isinstance(scalar, ProductOfScalars):
            factors = scalar._factors
        else:
            factors = [scalar]
        # Split factors based on if they contain the integration variable or not
        var_factors = []
        other_factors = []
        for factor in factors:
            if isinstance(factor, Scalar) and has_variable(factor, variable):
                var_factors.append(factor)
            else:
                other_factors.append(factor)
        if len(var_factors) > 0:
            integration_part = [
                _Integration(ProductOfScalars(var_factors), variable)
            ]
        else:
            integration_part = []

        new_scalar = ProductOfScalars(other_factors + integration_part)
    elif is_number(scalar):
        new_scalar = scalar
    else:
        raise NotImplementedError(
            f"integrate not implemented for type {type(scalar)}")

    return simplify(new_scalar)