Skip to content

Commit 68a0c7f

Browse files
authored
Merge pull request #18 from dylannalex/develop
v1.1.0
2 parents 5e9a25f + 3f42eff commit 68a0c7f

File tree

9 files changed

+909
-277
lines changed

9 files changed

+909
-277
lines changed

README.md

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def f(x):
3333
return x**2
3434

3535

36-
plotter = curvipy.Plotter(x_axis_scale=50, y_axis_scale=20)
37-
interval = curvipy.Interval(start=-15, end=15, samples=45)
38-
plotter.plot_curve(curvipy.Function(f), interval)
39-
36+
plotter = curvipy.Plotter()
37+
interval = curvipy.Interval(start=-10, end=10, samples=45)
38+
curve = curvipy.Function(f, interval)
39+
plotter.plot_curve(curve)
4040
plotter.wait()
4141
```
4242

@@ -67,13 +67,15 @@ def m(x):
6767
return f(x + 3)
6868

6969

70-
plotter = curvipy.Plotter(x_axis_scale=50, y_axis_scale=20, plotting_speed=3)
71-
plotter.curve_color = "#FF7B61" # Red
70+
plotter = curvipy.Plotter()
71+
72+
plotter.plotting_config.curve_color = "#FF7B61" # Red
7273
interval = curvipy.Interval(start=-2, end=7.5, samples=45)
73-
plotter.plot_curve(curvipy.Function(g), interval)
74-
plotter.curve_color = "#F061FF" # Purple
74+
plotter.plot_curve(curvipy.Function(g, interval))
75+
76+
plotter.plotting_config.curve_color = "#F061FF" # Purple
7577
interval = curvipy.Interval(start=-7.5, end=2, samples=45)
76-
plotter.plot_curve(curvipy.Function(m), interval)
78+
plotter.plot_curve(curvipy.Function(m, interval))
7779

7880
plotter.wait()
7981
```
@@ -104,12 +106,14 @@ def m(x):
104106
return f(x) + 3
105107

106108

107-
plotter = curvipy.Plotter(x_axis_scale=50, y_axis_scale=20, plotting_speed=3)
109+
plotter = curvipy.Plotter()
108110
interval = curvipy.Interval(start=-5, end=5, samples=45)
109-
plotter.curve_color = "#FF7B61" # Red
110-
plotter.plot_curve(curvipy.Function(g), interval)
111-
plotter.curve_color = "#F061FF" # Purple
112-
plotter.plot_curve(curvipy.Function(m), interval)
111+
112+
plotter.plotting_config.curve_color = "#FF7B61" # Red
113+
plotter.plot_curve(curvipy.Function(g, interval))
114+
115+
plotter.plotting_config.curve_color = "#F061FF" # Purple
116+
plotter.plot_curve(curvipy.Function(m, interval))
113117

114118
plotter.wait()
115119
```
@@ -120,17 +124,23 @@ plotter.wait()
120124

121125
## Linear transformations
122126

123-
A linear transformation is a mapping $V \rightarrow W$ between two vector spaces that preserves the operations of vector addition and scalar multiplication.
127+
A linear transformation $f$ is a mapping between two vector spaces
128+
129+
$$f:\mathcal{V}\rightarrow\mathcal{W}$$
130+
131+
that preserves the operations of vector addition and scalar multiplication. If, $\vec{v_1},\vec{v_2}\in\mathcal{V}$ and, $a_1$ and $a_2$ are scalars, then:
132+
133+
$$f(a_1\vec{v_1}+a_2\vec{v_2})=a_1f(\vec{v_1})+a_2f(\vec{v_2})$$
124134

125135
Curvipy is great for visualizing how a linear transformation transform the two-dimensional space.
126136

127137
### Transformation matrix
128138

129-
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
139+
In linear algebra, linear transformations can be represented by matrices. If $T$ is a linear transformation mapping $\mathbb{R}^n$ to $\mathbb{R}^m$ and $\vec{x}$ is a column vector then
130140

131-
$T(\vec{x}) = A\vec{x}$
141+
$$T(\vec{x}) = A\vec{x}$$
132142

133-
where $A$ is an $m x n$ matrix called the *transformation matrix* of $T$.
143+
where $A$ is an $m \times n$ matrix called the *transformation matrix* of $T$.
134144

135145
With Curvipy, you can visualize how linear transformations transforms two-dimensional curves with the `curvipy.TransformedCurve` class. Let's visualize how the matrix
136146

@@ -142,7 +152,7 @@ A =
142152
\end{bmatrix}
143153
$$
144154

145-
transforms the function $f(x) =\frac{x}{2}sin(x)$.
155+
transforms the function $f(x) =\frac{x}{2}\sin(x)$.
146156

147157
```python
148158
import math
@@ -153,19 +163,19 @@ def f(x):
153163
return x / 2 * math.sin(x)
154164

155165

156-
plotter = curvipy.Plotter(x_axis_scale=25, y_axis_scale=25)
157-
interval = curvipy.Interval(-15, 15, 250)
166+
plotter = curvipy.Plotter()
167+
interval = curvipy.Interval(-10, 10, 200)
158168

159169
# Plot curve f(x) = x/2 * sin(x):
160-
plotter.curve_color = "#FF7B61" # Red
161-
curve = curvipy.Function(f)
162-
plotter.plot_curve(curve, interval)
170+
plotter.plotting_config.curve_color = "#FF7B61" # Red
171+
curve = curvipy.Function(f, interval)
172+
plotter.plot_curve(curve)
163173

164174
# Plot transformed curve:
165-
plotter.curve_color = "#457B9D" # Blue
175+
plotter.plotting_config.curve_color = "#457B9D" # Blue
166176
A = ((0, -1), (1, 0))
167177
transformed_curve = curvipy.TransformedCurve(A, curve)
168-
plotter.plot_curve(transformed_curve, interval)
178+
plotter.plot_curve(transformed_curve)
169179

170180
plotter.wait()
171181
```
@@ -190,11 +200,7 @@ A =
190200
\begin{bmatrix}
191201
0 & -1\\
192202
1 & 0
193-
\end{bmatrix}
194-
$$
195-
196-
$$
197-
B =
203+
\end{bmatrix} \text{and } B =
198204
\begin{bmatrix}
199205
1 & 1\\
200206
0 & 1
@@ -211,32 +217,27 @@ def f(x):
211217
return x**3
212218

213219

214-
plotter = curvipy.Plotter(
215-
x_axis_scale=25,
216-
y_axis_scale=25,
217-
curve_width=6,
218-
plotting_speed=3,
219-
)
220-
interval = curvipy.Interval(-2.7, 2.7, 70)
220+
plotter = curvipy.Plotter()
221+
interval = curvipy.Interval(-2.5, 2.5, 70)
221222

222223
# Define curves
223224
A = ((0, -1), (1, 0))
224225
B = ((1, 1), (0, 1))
225-
curve = curvipy.Function(f)
226+
curve = curvipy.Function(f, interval)
226227
AB_transformed_curve = curvipy.TransformedCurve(A, curvipy.TransformedCurve(B, curve))
227228
BA_transformed_curve = curvipy.TransformedCurve(B, curvipy.TransformedCurve(A, curve))
228229

229-
# Plot f(x) = x^3 in Orange:
230-
plotter.curve_color = "#FFC947" # Yellow
231-
plotter.plot_curve(curve, interval)
230+
# Plot f(x) = x^3 in Yellow:
231+
plotter.plotting_config.curve_color = "#FFC947" # Yellow
232+
plotter.plot_curve(curve)
232233

233234
# Plot AB transformed curve in Red:
234-
plotter.curve_color = "#FF7B61" # Red
235-
plotter.plot_curve(AB_transformed_curve, interval)
235+
plotter.plotting_config.curve_color = "#FF7B61" # Red
236+
plotter.plot_curve(AB_transformed_curve)
236237

237238
# Plot BA transformed curve in Blue:
238-
plotter.curve_color = "#457B9D" # Blue
239-
plotter.plot_curve(BA_transformed_curve, interval)
239+
plotter.plotting_config.curve_color = "#457B9D" # Blue
240+
plotter.plot_curve(BA_transformed_curve)
240241

241242
plotter.wait()
242243
```
@@ -247,7 +248,7 @@ plotter.wait()
247248

248249
As you can see above, transforming $f(x)$ with the matrix $AB$ gives a different result as transforming $f(x)$ with the matrix $BA$.
249250

250-
**Tip:** you can also use numpy arrays to define `AB_transformed_curve` and `BA_transformed_curve` curves, as shown below.
251+
You can also use numpy arrays to define **AB_transformed_curve** and **BA_transformed_curve** curves:
251252

252253
```python
253254
import numpy as np
@@ -264,4 +265,4 @@ AB_transformed_curve = curvipy.TransformedCurve(AB, curve)
264265
BA_transformed_curve = curvipy.TransformedCurve(BA, curve)
265266
```
266267

267-
You can learn more about Curvipy by going through the [Documentation](documentation.md) section or by directly visiting Curvipy on [Github](https://github.com/dylannalex/curvipy) in order to check out the source code itself.
268+
Check out [Curvipy documentation](https://curvipy.readthedocs.io/en/latest/) to learn more!

curvipy/_curve.py

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
import typing as _typing
21
from abc import ABC as _ABC
32
from abc import abstractmethod as _abstractmethod
43

5-
from ._interval import Interval as _Interval
4+
from typing import Callable as _Callable
5+
from typing import Union as _Union
66

7+
from ._interval import Interval as _Interval
78

8-
_number = int | float
9-
_vector = tuple[_number, _number]
9+
_TNumber = _Union[int, float]
10+
_TVector = tuple[_TNumber, _TNumber]
1011

1112

1213
class Curve(_ABC):
1314
"""Base class for all two-dimensional curves."""
1415

1516
@_abstractmethod
16-
def points(self, interval: _Interval) -> list[_number]:
17-
"""Returns the curve point for each value in the given interval.
18-
19-
Parameters
20-
----------
21-
interval : Interval
22-
The interval from which the curve will be plotted.
17+
def points(self) -> list[_TNumber]:
18+
"""Returns a sorted list of curve point. The position of the points \
19+
indicates the order in which they will be plotted.
2320
2421
Returns
2522
-------
2623
list[int or float]
27-
A list of curve points.
24+
A list of curve points. The position of the points indicates the \
25+
order in which they will be plotted.
2826
"""
2927
pass
3028

@@ -36,25 +34,23 @@ class Function(Curve):
3634
----------
3735
function : Callable[[int or float], int or float]
3836
Function that given an integer or float returns another integer or float.
37+
interval : Interval
38+
The interval from which the curve will be plotted.
3939
"""
4040

41-
def __init__(self, function: _typing.Callable[[_number], _number]):
41+
def __init__(self, function: _Callable[[_TNumber], _TNumber], interval: _Interval):
4242
self.function = function
43+
self.interval = interval
4344

44-
def points(self, interval: _Interval) -> list[_number]:
45+
def points(self) -> list[_TNumber]:
4546
"""Returns the function point for each value in the given interval.
4647
47-
Parameters
48-
----------
49-
interval : Interval
50-
The interval from which the function will be plotted.
51-
5248
Returns
5349
-------
5450
list[int or float]
5551
A list of function points.
5652
"""
57-
return [(x, self.function(x)) for x in interval]
53+
return [(x, self.function(x)) for x in self.interval]
5854

5955

6056
class ParametricFunction(Curve):
@@ -65,25 +61,25 @@ class ParametricFunction(Curve):
6561
function : Callable[[int or float], tuple[int or float, int or float]]
6662
Function that given an integer or float returns a tuple containing two \
6763
integers or floats.
64+
interval : Interval
65+
The interval from which the parametric function will be plotted.
6866
"""
6967

70-
def __init__(self, parametric_function: _typing.Callable[[_number], _vector]):
68+
def __init__(
69+
self, parametric_function: _Callable[[_TNumber], _TVector], interval: _Interval
70+
):
7171
self.parametric_function = parametric_function
72+
self.interval = interval
7273

73-
def points(self, interval: _Interval) -> list[_number]:
74+
def points(self) -> list[_TNumber]:
7475
"""Returns the parametric function point for each value in the given interval.
7576
76-
Parameters
77-
----------
78-
interval : Interval
79-
The interval from which the parametric function will be plotted.
80-
8177
Returns
8278
-------
8379
list[int or float]
8480
A list of parametric function points.
8581
"""
86-
return [self.parametric_function(t) for t in interval]
82+
return [self.parametric_function(t) for t in self.interval]
8783

8884

8985
class TransformedCurve(Curve):
@@ -104,29 +100,24 @@ class TransformedCurve(Curve):
104100
def __init__(
105101
self,
106102
matrix: tuple[
107-
tuple[_number, _number],
108-
tuple[_number, _number],
103+
tuple[_TNumber, _TNumber],
104+
tuple[_TNumber, _TNumber],
109105
],
110106
curve: Curve,
111107
):
112108
self.__curve = curve
113109
self.__matrix = matrix
114110

115-
def points(self, interval: _Interval) -> list[_number]:
116-
"""Returns the curve point for each value in the given interval.
117-
118-
Parameters
119-
----------
120-
interval : Interval
121-
The interval from which the transformed curve will be plotted.
111+
def points(self) -> list[_TNumber]:
112+
"""Applies a linear transformation to each point of the curve.
122113
123114
Returns
124115
-------
125116
list[int or float]
126117
A list of transformed curve points.
127118
"""
128119
points = []
129-
for point in self.__curve.points(interval):
120+
for point in self.__curve.points():
130121
points.append(
131122
[
132123
point[0] * self.__matrix[0][0] + point[1] * self.__matrix[0][1],

curvipy/_interval.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from typing import Union as _Union
2+
3+
_TNumber = _Union[int, float]
4+
5+
16
class Interval:
27
"""Interval in which a curve will be plotted.
38
@@ -16,7 +21,7 @@ class Interval:
1621
curve plot is.
1722
"""
1823

19-
def __init__(self, start: int | float, end: int | float, samples: int):
24+
def __init__(self, start: _TNumber, end: _TNumber, samples: int):
2025
dx = (end - start) / samples
2126
self.__interval = [start + dx * i for i in range(samples)]
2227

0 commit comments

Comments
 (0)