-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dylannalex/develop
Bump to 1.0.0
- Loading branch information
Showing
27 changed files
with
1,204 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# .readthedocs.yaml | ||
# Read the Docs configuration file | ||
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details | ||
|
||
# Required | ||
version: 2 | ||
|
||
# Set the version of Python and other tools you might need | ||
build: | ||
os: ubuntu-20.04 | ||
tools: | ||
python: "3.9" | ||
|
||
# Build documentation in the docs/ directory with Sphinx | ||
sphinx: | ||
configuration: docs/conf.py | ||
|
||
# Optionally declare the Python requirements required to build your docs | ||
python: | ||
install: | ||
- requirements: docs/requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,260 @@ | ||
# curvipy :triangular_ruler: | ||
|
||
[![Downloads](https://pepy.tech/badge/curvipy)](https://pepy.tech/project/curvipy) | ||
![licence](https://img.shields.io/github/license/dylannalex/curvipy?color=blue) | ||
|
||
curvipy is a Python package which aims to provide simple tools to teach the fundamentals of linear | ||
algebra (vectors operations, matrices and linear transformations) and the basics of functions and | ||
parametric curves. | ||
# What is Curvipy? | ||
|
||
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. | ||
|
||
# Installation | ||
|
||
You can start using Curvipy by installing it via pip. | ||
|
||
``` | ||
$ pip install curvipy | ||
``` | ||
|
||
# Usage Example | ||
|
||
Curvipy is a great tool for learning and teaching math with animations. In this section you can find different topics being explained with Curvipy. | ||
|
||
## Functions Translations | ||
|
||
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 $f(x) = x^{2}$, where $x \in [-15, 15]$. | ||
|
||
```python | ||
import curvipy | ||
import turtle | ||
|
||
|
||
def f(x): | ||
return x**2 | ||
|
||
|
||
plotter = curvipy.Plotter(x_axis_scale=50, y_axis_scale=20) | ||
interval = curvipy.Interval(start=-15, end=15, samples=45) | ||
plotter.plot_curve(curvipy.Function(f), interval) | ||
|
||
turtle.exitonclick() | ||
``` | ||
|
||
<p align="center"> | ||
<img width="400" height="400" src="../media/demo.gif?raw=true"> | ||
<img width="500" height="500" src="docs/source/img/function_x_squared.png"> | ||
</p> | ||
|
||
_**NOTE:**_ curvipy is not meant to be a precise graphing calculator, but a package for making | ||
animations in a few lines of code. | ||
### Horizontal Translation | ||
|
||
In a horizontal translation, the function is moved along the x-axis. | ||
|
||
```python | ||
import curvipy | ||
import turtle | ||
|
||
|
||
def f(x): | ||
return x**2 | ||
|
||
## :pencil2: Installation | ||
|
||
Install curvipy package with pip: | ||
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(x_axis_scale=50, y_axis_scale=20) | ||
interval = curvipy.Interval(start=-15, end=15, samples=45) | ||
plotter.curve_color = "#FF7B61" # Red | ||
plotter.plot_curve(curvipy.Function(g), interval) | ||
plotter.curve_color = "#F061FF" # Purple | ||
plotter.plot_curve(curvipy.Function(m), interval) | ||
|
||
turtle.exitonclick() | ||
``` | ||
$ pip install curvipy | ||
|
||
<p align="center"> | ||
<img width="500" height="500" src="docs/source/img/horizontal_translation.png"> | ||
</p> | ||
|
||
### Vertical Translation | ||
|
||
In a horizontal translation, the function is moved along the y-axis. | ||
|
||
```python | ||
import curvipy | ||
import turtle | ||
|
||
|
||
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(x_axis_scale=50, y_axis_scale=20) | ||
interval = curvipy.Interval(start=-15, end=15, samples=45) | ||
plotter.curve_color = "#FF7B61" # Red | ||
plotter.plot_curve(curvipy.Function(g), interval) | ||
plotter.curve_color = "#F061FF" # Purple | ||
plotter.plot_curve(curvipy.Function(m), interval) | ||
|
||
turtle.exitonclick() | ||
``` | ||
or clone the repository: | ||
|
||
<p align="center"> | ||
<img width="500" height="500" src="docs/source/img/vertical_translation.png"> | ||
</p> | ||
|
||
## Linear transformations | ||
|
||
A linear transformation is a mapping $V \rightarrow W$ between two vector spaces that preserves the operations of vector addition and scalar multiplication. | ||
|
||
Curvipy is great for visualizing how a linear transformation transform the two-dimensional space. | ||
|
||
### Transformation matrix | ||
|
||
In linear algebra, linear transformations can be represented by matrices. If $T$ is a linear transformation mapping $R^n$ to $R^m$ and $\vec{x}$ is a column vector then | ||
|
||
$T(\vec{x}) = A\vec{x}$ | ||
|
||
where $A$ is an $m x n$ matrix called the *transformation matrix* of $T$. | ||
|
||
With Curvipy, you can visualize how linear transformations transforms two-dimensional curves with the `curvipy.TransformedCurve` class. Let's visualize how the matrix | ||
|
||
$$ | ||
A = | ||
\begin{bmatrix} | ||
0 & -1\\ | ||
1 & 0 | ||
\end{bmatrix} | ||
$$ | ||
|
||
transforms the function $f(x) = sin(x)$. | ||
|
||
```python | ||
import math | ||
import curvipy | ||
import turtle | ||
|
||
|
||
curve = curvipy.Function(math.sin) | ||
interval = curvipy.Interval(-10, 10, 50) | ||
A = ((0, -1), (1, 0)) | ||
plotter = curvipy.Plotter(x_axis_scale=25, y_axis_scale=25) | ||
|
||
# Plot curve: | ||
plotter.curve_color = "#FF7B61" # Red | ||
plotter.plot_curve(curve, interval) | ||
# Plot transformed curve: | ||
plotter.curve_color = "#457B9D" # Blue | ||
plotter.plot_curve(curvipy.TransformedCurve(A, curve), interval) | ||
|
||
turtle.exitonclick() # Exits plotter on click | ||
``` | ||
$ git clone https://github.com/dylannalex/curvipy.git | ||
|
||
<p align="center"> | ||
<img width="500" height="500" src="docs/source/img/transformation_matrix.png"> | ||
</p> | ||
|
||
As you can see above, the matrix $A$ rotates the function $f(x)$ 90° anticlockwise. | ||
|
||
**Note:** `curvipy.TransformedCurve` | ||
matrix parameter has the same format as numpy arrays. In fact, you can directly use a numpy array. | ||
|
||
### Matrix multiplication commutative property | ||
|
||
For matrix multiplication, the commutative property of multiplication does not hold. This means that, given two matrices $A$ and $B$, generally $AB {\neq} BA$. | ||
|
||
To prove this, let's define the matrices | ||
|
||
$$ | ||
A = | ||
\begin{bmatrix} | ||
0 & -1\\ | ||
1 & 0 | ||
\end{bmatrix} | ||
$$ | ||
|
||
$$ | ||
B = | ||
\begin{bmatrix} | ||
1 & 1\\ | ||
0 & 1 | ||
\end{bmatrix} | ||
$$ | ||
|
||
|
||
and see how they transform the curve $f(x) = x^{3}$. | ||
|
||
```python | ||
import curvipy | ||
import turtle | ||
|
||
|
||
def f(x): | ||
return x**3 | ||
|
||
|
||
curve = curvipy.Function(f) | ||
interval = curvipy.Interval(-10, 10, 70) | ||
plotter = curvipy.Plotter(x_axis_scale=25, y_axis_scale=25, curve_width=6) | ||
|
||
A = ((0, -1), (1, 0)) | ||
B = ((1, 1), (0, 1)) | ||
|
||
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 Orange: | ||
plotter.curve_color = "#FFC947" # Yellow | ||
plotter.plot_curve(curve, interval) | ||
|
||
# Plot AB transformed curve in Red: | ||
plotter.curve_color = "#FF7B61" # Red | ||
plotter.plot_curve(AB_transformed_curve, interval) | ||
|
||
# Plot BA transformed curve in Blue: | ||
plotter.curve_color = "#457B9D" # Blue | ||
plotter.plot_curve(BA_transformed_curve, interval) | ||
|
||
turtle.exitonclick() # Exits plotter on click | ||
``` | ||
|
||
## :pencil2: Usage | ||
<p align="center"> | ||
<img width="500" height="500" src="docs/source/img/mat_multiplication_commutative_property.png"> | ||
</p> | ||
|
||
As you can see above, transforming $f(x)$ with the matrix $AB$ gives a different result as transforming $f(x)$ with the matrix $BA$. | ||
|
||
**Tip:** you can also use numpy arrays to define `AB_transformed_curve` and `BA_transformed_curve` curves, as shown below. | ||
|
||
```python | ||
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) | ||
``` | ||
|
||
One of the main goals of curvpy is to be simple to use. You can check out the curvipy use guide [HERE](/docs/introduction.md). | ||
You can learn more about Curvipy by visiting the Documentation Page. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
from curvipy.graphing_calculator import GraphingCalculator | ||
from curvipy import lintrans | ||
from .plotter import * | ||
from .curve import * | ||
from .interval import * | ||
from .vector import * |
Oops, something went wrong.