Repository: ManimCommunity/manim
File: manim/animation/speedmodifier.py (Python)

Code before:
    changed = False

    def __init__(
        self,
        anim: Animation | _AnimationBuilder,
        speedinfo: dict[float, float],
        rate_func: Callable[[float], float] | None = None,
        **kwargs,
    ) -> None:

        self.anim = self.setup(anim)
        if issubclass(type(anim), AnimationGroup):
            self.anim = AnimationGroup(
                *map(self.setup, anim.animations),
                group=anim.group,
                run_time=anim.run_time,
                rate_func=anim.rate_func,
                lag_ratio=anim.lag_ratio,
            )

        self.rate_func = self.anim.rate_func if rate_func is None else rate_func

        # A function where, f(0) = 0, f'(0) = m, f'( f-1(1) ) = n
        # m being initial speed, n being final speed
        # Following function obtained when conditions applied to vertical parabola
        self.speed_modifier = lambda x, m, n: (n * n - m * m) * x * x / 4 + m * x

        # f-1(1), returns x for which f(x) = 1 in `speed_modifier` function
        self.f_inv_1 = lambda m, n: 2 / (m + n)

        if 0 not in speedinfo:
            speedinfo[0] = 1
        if 1 not in speedinfo:
            speedinfo[1] = sorted(speedinfo.items())[-1][1]

        self.speedinfo = dict(sorted(speedinfo.items()))
        self.functions = []
        self.conditions = []

        # To the total time
        total_time = self.get_total_time()

        prevnode = 0
        m = self.speedinfo[0]
        curr_time = 0
        for node, n in list(self.speedinfo.items())[1:]:
            dur = node - prevnode
            self.conditions.append(
                lambda x, curr_time=curr_time, m=m, n=n, dur=dur: curr_time / total_time
                <= x
                <= (curr_time + self.f_inv_1(m, n) * dur) / total_time

Review comment (suggestion):
I think it would be worth it to rename m and n so that their variable names reflect their actual purpose. This makes the code more maintainable.

```suggestion
        self.speed_modifier = (
            lambda x, initial_speed, final_speed: (final_speed**2 - initial_speed**2) * x**2 / 4 
            + initial_speed * x
        )
```

Code after:

    dt = 0
    is_changing_dt = False

    def __init__(
        self,
        anim: Animation | _AnimationBuilder,
        speedinfo: dict[float, float],
        rate_func: Callable[[float], float] | None = None,
        affects_speed_updaters: bool = True,
        **kwargs,
    ) -> None:

        if issubclass(type(anim), AnimationGroup):
            self.anim = type(anim)(
                *map(self.setup, anim.animations),
                group=anim.group,
                run_time=anim.run_time,
                rate_func=anim.rate_func,
                lag_ratio=anim.lag_ratio,
            )
        else:
            self.anim = self.setup(anim)

        if affects_speed_updaters:
            assert (
                ChangeSpeed.is_changing_dt is False
            ), "Only one animation at a time can play that changes speed (dt) for ChangeSpeed updaters"
            ChangeSpeed.is_changing_dt = True
            self.t = 0
        self.affects_speed_updaters = affects_speed_updaters

        self.rate_func = self.anim.rate_func if rate_func is None else rate_func

        # A function where, f(0) = 0, f'(0) = initial speed, f'( f-1(1) ) = final speed
        # Following function obtained when conditions applied to vertical parabola
        self.speed_modifier = lambda x, init_speed, final_speed: (
            (final_speed**2 - init_speed**2) * x**2 / 4 + init_speed * x
        )

        # f-1(1), returns x for which f(x) = 1 in `speed_modifier` function
        self.f_inv_1 = lambda init_speed, final_speed: 2 / (init_speed + final_speed)

        # if speed factors for the starting node (0) and the final node (1) are
        # not set, set them to 1 and the penultimate factor, respectively
        if 0 not in speedinfo:
            speedinfo[0] = 1
        if 1 not in speedinfo:
            speedinfo[1] = sorted(speedinfo.items())[-1][1]

        self.speedinfo = dict(sorted(speedinfo.items()))