Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Slight performance improvement for :class:`.ArrowVectorField` and Bezier curve computation (#2727)

Tristan Schulz2dba4856c87b
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index d7bb73134..e4289853e 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -1102,7 +1102,7 @@ class VMobject(Mobject):
curve = self.get_nth_curve_function(n)
points = np.array([curve(a) for a in np.linspace(0, 1, sample_points)])
diffs = points[1:] - points[:-1]
- norms = np.apply_along_axis(np.linalg.norm, 1, diffs)
+ norms = np.linalg.norm(diffs, axis=1)
return norms
diff --git a/manim/mobject/vector_field.py b/manim/mobject/vector_field.py
index 2c09edcb3..dd48c039a 100644
--- a/manim/mobject/vector_field.py
+++ b/manim/mobject/vector_field.py
@@ -575,8 +575,12 @@ class ArrowVectorField(VectorField):
x_range = np.arange(*self.x_range)
y_range = np.arange(*self.y_range)
z_range = np.arange(*self.z_range)
- for x, y, z in it.product(x_range, y_range, z_range):
- self.add(self.get_vector(x * RIGHT + y * UP + z * OUT))
+ self.add(
+ *[
+ self.get_vector(x * RIGHT + y * UP + z * OUT)
+ for x, y, z in it.product(x_range, y_range, z_range)
+ ]
+ )
self.set_opacity(self.opacity)
def get_vector(self, point: np.ndarray):
@@ -594,7 +598,7 @@ class ArrowVectorField(VectorField):
Additional arguments to be passed to the :class:`~.Vector` constructor
"""
- output = np.array(self.func(point))
+ output = np.asarray(self.func(point))
norm = np.linalg.norm(output)
if norm != 0:
output *= self.length_func(norm) / norm
diff --git a/manim/utils/bezier.py b/manim/utils/bezier.py
index d96b6af62..6f8187eef 100644
--- a/manim/utils/bezier.py
+++ b/manim/utils/bezier.py
@@ -46,6 +46,23 @@ def bezier(
function describing the bezier curve.
"""
n = len(points) - 1
+
+ # Cubic Bezier curve
+ if n == 3:
+ return (
+ lambda t: (1 - t) ** 3 * points[0]
+ + 3 * t * (1 - t) ** 2 * points[1]
+ + 3 * (1 - t) * t**2 * points[2]
+ + t**3 * points[3]
+ )
+ # Quadratic Bezier curve
+ if n == 2:
+ return (
+ lambda t: (1 - t) ** 2 * points[0]
+ + 2 * t * (1 - t) * points[1]
+ + t**2 * points[2]
+ )
+
return lambda t: sum(
((1 - t) ** (n - k)) * (t**k) * choose(n, k) * point
for k, point in enumerate(points)
diff --git a/manim/utils/iterables.py b/manim/utils/iterables.py
index 51a9a6ff2..afcf6d992 100644
--- a/manim/utils/iterables.py
+++ b/manim/utils/iterables.py
@@ -182,10 +182,12 @@ def make_even(iterable_1: Iterable, iterable_2: Iterable) -> tuple[list, list]:
# ([1, 1, 1, 2, 2], [3, 4, 5, 6, 7])
"""
list_1, list_2 = list(iterable_1), list(iterable_2)
- length = max(len(list_1), len(list_2))
+ len_list_1 = len(list_1)
+ len_list_2 = len(list_2)
+ length = max(len_list_1, len_list_2)
return (
- [list_1[(n * len(list_1)) // length] for n in range(length)],
- [list_2[(n * len(list_2)) // length] for n in range(length)],
+ [list_1[(n * len_list_1) // length] for n in range(length)],
+ [list_2[(n * len_list_2) // length] for n in range(length)],
)