manim icon indicating copy to clipboard operation
manim copied to clipboard

Removed SciPy imports in Manim, making it an indirect dependency

Open chopan050 opened this issue 2 years ago • 1 comments

Overview: What does this pull request change?

I removed all the SciPy imports in Manim, using NumPy functions instead. Thus, Scipy would become just an indirect dependency of NetworkX, used in Graph (and by extension Polyhedron) for generating the graph layouts which require Scipy.

Motivation and Explanation: Why and how do your changes improve the library?

manim.camera.camera

scipy.spatial.distance.pdist, a function to generate a matrix of pairwise distances between two arrays of points, was imported to calculate... the distance between a single pair of points (and then ndarray.item() was used to extract the only element in the generated 1x1 matrix as a float). In this scenario, pdist is unnecessary and we can just use np.linalg.norm on the distance vector between the two requested points... vector which already existed beforehand (right_vect and down_vect).

manim.utils.bezier

scipy.linalg.solve_banded was used in get_smooth_handle_points for solving a system of equations to find the required handles for a smooth cubic spline. In PR #3281 I already rewrote that function in a way which doesn't use SciPy, so I just copy-pasted that solution.

manim.utils.simple_functions

The choose function called scipy.special.choose to calculate combinatorial coefficients. The only place where choose was used was in manim.utils.bezier, where all the choose(n, k) coefficients were requested for all k in [0, n], or sometimes even for all n in [0, num_points], wasting too many intermediate calculations. Therefore, I replaced it with a custom get_pascal_triangle function which calculates in a single pass a memo for the entire Pascal triangle with the coefficients up to choose(n, n). Then, I made some slight modifications to the manim.utils.bezier functions which used choose.

manim.utils.space_ops

scipy.spatial.transform.Rotation.from_rotvec was used to calculate a rotation matrix. I replaced it with a custom implementation, explaining all the process in the docstring.

Links to added or changed documentation pages

Further Information and Comments

Reviewer Checklist

  • [ ] The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • [ ] If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • [ ] If applicable: newly added functions and classes are tested

chopan050 avatar Dec 16 '23 20:12 chopan050

Nice, always good to reduce the amount of dependencies we have. Will review when I have some time

Viicos avatar Dec 17 '23 19:12 Viicos