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.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 thenndarray.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 usenp.linalg.norm
on the distance vector between the two requested points... vector which already existed beforehand (right_vect
anddown_vect
).manim.utils.bezier
scipy.linalg.solve_banded
was used inget_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 calledscipy.special.choose
to calculate combinatorial coefficients. The only place wherechoose
was 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_triangle
function 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.bezier
functions which usedchoose
.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