Removed SciPy imports in Manim, making it an indirect dependency #3532
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 extensionPolyhedron) for generating the graph layouts which require Scipy.Motivation and Explanation: Why and how do your changes improve the library?
manim.camera.camerascipy.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 thenndarray.item()was used to extract the only element in the generated 1x1 matrix as a float). In this scenario,pdistis unnecessary and we can just usenp.linalg.normon the distance vector between the two requested points... vector which already existed beforehand (right_vectanddown_vect).manim.utils.bezierscipy.linalg.solve_bandedwas used inget_smooth_handle_pointsfor 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_functionsThe
choosefunction calledscipy.special.chooseto calculate combinatorial coefficients. The only place wherechoosewas used was inmanim.utils.bezier, where all thechoose(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 customget_pascal_trianglefunction which calculates in a single pass a memo for the entire Pascal triangle with the coefficients up tochoose(n, n). Then, I made some slight modifications to themanim.utils.bezierfunctions which usedchoose.manim.utils.space_opsscipy.spatial.transform.Rotation.from_rotvecwas 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