linear_map
mypackage.linearmap.linear_map
This module contains the LinearMap abstract class and two subclasses: Rotation and Shear.
Examples:
We can define a rotation linear map and then apply the transformation to a vector as if it were a function:
>>> angle = 0.5
>>> map = Rotation(angle)
>>> map(Vector(1, 0))
Vector(0.8775825618903728, 0.479425538604203)
And with the inverse method, we can apply the inverse transformation
>>> shear_angle = 0.5
>>> map = Shear(shear_angle)
>>> map.inverse(Vector(1, 1))
Vector(-0.830487721712452, 1)
LinearMap(matrix)
Bases: ABC
This abstract class will serve us as a base for other linear maps that we will later especify in subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
matrix |
list[list[float]]
|
matrix of our linear map. It consists on a list of rows, each row being a list of the numbers in each column. |
required |
Attributes:
Name | Type | Description |
---|---|---|
matrix |
list[list[float]]
|
the matrix of our linear map. |
inv_matrix |
list[list[float]]
|
the matrix of the inverse of our linear map. |
Source code in mypackage/linearmap/linear_map.py
41 42 43 |
|
__call__(vector)
Apply the linear map to a vector (which translates into ordinary matrix times vector multiplication).
Note
The call method allows an instance of this class to behave as a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector |
Vector
|
Vector to map. |
required |
Returns:
Name | Type | Description |
---|---|---|
Vector |
Vector
|
Transformed vector. |
Source code in mypackage/linearmap/linear_map.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
inverse(vector)
Apply the inverse of our map to a vector.
Note
When giving a name to a method, you should take into account how would
you name an instance of this class. For example, if we create an instance
called 'rotation', it is very clear to read rotation.inverse(some_vector)
and guess that it applies the inverse rotation to the vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector |
Vector
|
Vector to transform. |
required |
Returns:
Name | Type | Description |
---|---|---|
Vector |
Vector
|
Transformed vector. |
Source code in mypackage/linearmap/linear_map.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
Rotation(angle)
Bases: LinearMap
Two dimensional rotation of a certain angle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
angle |
float
|
angle of the rotation. |
required |
Attributes:
Name | Type | Description |
---|---|---|
angle |
float
|
angle of the rotation. |
Source code in mypackage/linearmap/linear_map.py
109 110 111 112 |
|
Shear(shear_angle)
Bases: LinearMap
Shear transformation parallel to the x axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shear_angle |
float
|
angle of the shear transformation. |
required |
Attributes:
Name | Type | Description |
---|---|---|
shear_factor |
float
|
cotangent of the shear angle. |
Source code in mypackage/linearmap/linear_map.py
128 129 130 131 |
|