Exemple #1
0
    def _binary_simplify(self, left, right):
        """
        See :meth:`pybamm.BinaryOperator._binary_simplify()`.

        Note
        ----
        We check for scalars first, then matrices. This is because
        (Zero Matrix) - (Zero Scalar)
        should return (Zero Matrix), not -(Zero Scalar).
        """

        # anything added by a scalar zero returns the other child
        if is_scalar_zero(left):
            return -right
        if is_scalar_zero(right):
            return left
        # Check matrices after checking scalars
        if is_matrix_zero(left):
            if isinstance(right, pybamm.Scalar):
                return pybamm.Array(-right.value * np.ones(left.shape_for_testing))
            else:
                return -right
        if is_matrix_zero(right):
            if isinstance(left, pybamm.Scalar):
                return pybamm.Array(left.value * np.ones(right.shape_for_testing))
            else:
                return left

        return pybamm.simplify_addition_subtraction(self.__class__, left, right)
Exemple #2
0
    def _binary_simplify(self, left, right):
        """
        See :meth:`pybamm.BinaryOperator.simplify()`.

        Note
        ----
        We check for scalars first, then matrices. This is because
        (Zero Matrix) + (Zero Scalar)
        should return (Zero Matrix), not (Zero Scalar).
        """

        # anything added by a scalar zero returns the other child
        if is_scalar_zero(left):
            return right
        if is_scalar_zero(right):
            return left
        # Check matrices after checking scalars
        if is_matrix_zero(left):
            return right
        if is_matrix_zero(right):
            return left

        return pybamm.simplify_addition_subtraction(self.__class__, left,
                                                    right)