Repository: sympy/sympy
File: sympy/physics/continuum_mechanics/beam.py (Python)

Code before:

        if (value, start, order, end) in self._applied_loads:
            self._load -= value*SingularityFunction(x, start, order)
            self._original_load -= value*SingularityFunction(x, start, order)
            self._applied_loads.remove((value, start, order, end))
        else:
            msg = "No such load distribution exists on the beam object."
            raise ValueError(msg)

        if end!=None:
            self._handle_end(x, value, start, order, end, type="remove")

    def _handle_end(self, x, value, start, order, end, type):
        """
        This functions handles the optional `end` value in the
        `apply_load` and `remove_load` functions. When the value
        of end is not NULL, this function will be executed.
        """
        if order.is_negative:
            msg = ("If 'end' is provided the 'order' of the load cannot "
                    "be negative, i.e. 'end' is only valid for distributed "
                    "loads.")
            raise ValueError(msg)
        # NOTE : A Taylor series can be used to define the summation of
        # singularity functions that subtract from the load past the end
        # point such that it evaluates to zero past 'end'.

        f = value*(x-start)**order

        if type == "apply":
            for i in range(0, order + 1):
                coeff = f.diff(x, i).subs(x, end)
                term = coeff * SingularityFunction(x, end, i) / factorial(i)
                self._load -= term
                self._original_load -= term
        elif type == "remove":
            for i in range(0, order + 1):
                coeff = f.diff(x, i).subs(x, end)
                term = coeff * SingularityFunction(x, end, i) / factorial(i)
                self._load += term
                self._original_load += term


    @property
    def load(self):
        """
        Returns a Singularity Function expression which represents
        the load distribution curve of the Beam object.

        Examples
        ========

Review comment (refactor):
I would just remove this function. There is no point in having a function that is called in two places with a boolean like this:
```python
def func_A():
    _func(type='A')

def func_B():
    _func(type='B')

def _func(type):
    if type == 'A':
        # A code
    elif type == 'B':
        # B code
```
It would be clear to put the A code and B code in func_A and func_B.

Code after:
        -3*SingularityFunction(x, 0, -2) + 4*SingularityFunction(x, 2, -1) - 2*SingularityFunction(x, 2, 2) + 2*SingularityFunction(x, 3, 0) + 4*SingularityFunction(x, 3, 1) + 2*SingularityFunction(x, 3, 2)
        >>> b.remove_load(-2, 2, 2, end = 3)
        >>> b.load
        -3*SingularityFunction(x, 0, -2) + 4*SingularityFunction(x, 2, -1)
        """
        x = self.variable
        value = sympify(value)
        start = sympify(start)

        if (value, start, order, end) in self._applied_loads:
            self._load -= value*SingularityFunction(x, start, order)
            self._original_load -= value*SingularityFunction(x, start, order)
            self._applied_loads.remove((value, start, order, end))
        else:
            msg = "No such load distribution exists on the beam object."
            raise ValueError(msg)

        if end is not None:
            if order < 0:
                msg = ("If 'end' is provided the 'order' of the load cannot "
                       "be negative, i.e. 'end' is only valid for distributed loads.")
                raise ValueError(msg)

            f = value * (x - start)**order
            for i in range(0, order + 1):
                coeff = f.diff(x, i).subs(x, end)
                term = coeff * SingularityFunction(x, end, i) / factorial(i)
                self._load += term
                self._original_load += term

    @property
    def load(self):
        """
        Returns a Singularity Function expression which represents
        the load distribution curve of the Beam object.

        Examples
        ========
        There is a beam of length 4 meters. A moment of magnitude 3 Nm is
        applied in the clockwise direction at the starting point of the beam.
        A point load of magnitude 4 N is applied from the top of the beam at
        2 meters from the starting point and a parabolic ramp load of magnitude
        2 N/m is applied below the beam starting from 3 meters away from the
        starting point of the beam.

        >>> from sympy.physics.continuum_mechanics.beam import Beam
        >>> from sympy import symbols
        >>> E, I = symbols('E, I')
        >>> b = Beam(4, E, I)
        >>> b.apply_load(-3, 0, -2)
        >>> b.apply_load(4, 2, -1)