Curvipy is a Python library for making math animations in a few lines of code.
Curvipy is inspired on manim, an animation engine for making math videos. Despite manim being an exceptionally tool, it is quite hard to learn and use, specially for simple animations. Curvipy solves this issue by providing a less powerful but much easier to use package, with which you can plot two-dimensional curves and vectors.
You can start using Curvipy by installing it via pip.
$ pip install curvipy
Curvipy is a great tool for learning and teaching math with animations. In this section you can find different topics being explained with Curvipy.
A function has been translated when it has been moved in a way that does not change its shape or rotate it in any way. A function can be translated either vertically, horizontally, or both.
To visualize translations, we will use the function
import curvipy
def f(x):
return x**2
plotter = curvipy.Plotter()
interval = curvipy.Interval(start=-10, end=10, samples=45)
curve = curvipy.Function(f, interval)
plotter.plot_curve(curve)
plotter.wait()
In a horizontal translation, the function is moved along the x-axis.
import curvipy
def f(x):
return x**2
def g(x):
"""f(x) moved 3 units to the right."""
return f(x - 3)
def m(x):
"""f(x) moved 3 units to the left."""
return f(x + 3)
plotter = curvipy.Plotter()
plotter.plotting_config.curve_color = "#FF7B61" # Red
interval = curvipy.Interval(start=-2, end=7.5, samples=45)
plotter.plot_curve(curvipy.Function(g, interval))
plotter.plotting_config.curve_color = "#F061FF" # Purple
interval = curvipy.Interval(start=-7.5, end=2, samples=45)
plotter.plot_curve(curvipy.Function(m, interval))
plotter.wait()
In a horizontal translation, the function is moved along the y-axis.
import curvipy
def f(x):
return x**2
def g(x):
"""f(x) moved 3 units down."""
return f(x) - 3
def m(x):
"""f(x) moved 3 units up."""
return f(x) + 3
plotter = curvipy.Plotter()
interval = curvipy.Interval(start=-5, end=5, samples=45)
plotter.plotting_config.curve_color = "#FF7B61" # Red
plotter.plot_curve(curvipy.Function(g, interval))
plotter.plotting_config.curve_color = "#F061FF" # Purple
plotter.plot_curve(curvipy.Function(m, interval))
plotter.wait()
A linear transformation
that preserves the operations of vector addition and scalar multiplication. If,
Curvipy is great for visualizing how a linear transformation transform the two-dimensional space.
In linear algebra, linear transformations can be represented by matrices. If
where
With Curvipy, you can visualize how linear transformations transforms two-dimensional curves with the curvipy.TransformedCurve
class. Let's visualize how the matrix
transforms the function
import math
import curvipy
def f(x):
return x / 2 * math.sin(x)
plotter = curvipy.Plotter()
interval = curvipy.Interval(-10, 10, 200)
# Plot curve f(x) = x/2 * sin(x):
plotter.plotting_config.curve_color = "#FF7B61" # Red
curve = curvipy.Function(f, interval)
plotter.plot_curve(curve)
# Plot transformed curve:
plotter.plotting_config.curve_color = "#457B9D" # Blue
A = ((0, -1), (1, 0))
transformed_curve = curvipy.TransformedCurve(A, curve)
plotter.plot_curve(transformed_curve)
plotter.wait()
As you can see above, the matrix
Note: curvipy.TransformedCurve
matrix parameter has the same format as numpy arrays. In fact, you can directly use a numpy array.
For matrix multiplication, the commutative property of multiplication does not hold. This means that, given two matrices
To prove this, let's define the matrices
and see how they transform the curve
import curvipy
def f(x):
return x**3
plotter = curvipy.Plotter()
interval = curvipy.Interval(-2.5, 2.5, 70)
# Define curves
A = ((0, -1), (1, 0))
B = ((1, 1), (0, 1))
curve = curvipy.Function(f, interval)
AB_transformed_curve = curvipy.TransformedCurve(A, curvipy.TransformedCurve(B, curve))
BA_transformed_curve = curvipy.TransformedCurve(B, curvipy.TransformedCurve(A, curve))
# Plot f(x) = x^3 in Yellow:
plotter.plotting_config.curve_color = "#FFC947" # Yellow
plotter.plot_curve(curve)
# Plot AB transformed curve in Red:
plotter.plotting_config.curve_color = "#FF7B61" # Red
plotter.plot_curve(AB_transformed_curve)
# Plot BA transformed curve in Blue:
plotter.plotting_config.curve_color = "#457B9D" # Blue
plotter.plot_curve(BA_transformed_curve)
plotter.wait()
As you can see above, transforming
You can also use numpy arrays to define AB_transformed_curve and BA_transformed_curve curves:
import numpy as np
import curvipy
A = np.array(((0, -1), (1, 0)))
B = np.array(((1, 1), (0, 1)))
AB = np.matmul(A, B)
BA = np.matmul(B, A)
AB_transformed_curve = curvipy.TransformedCurve(AB, curve)
BA_transformed_curve = curvipy.TransformedCurve(BA, curve)
Check out Curvipy documentation to learn more!