Skip to content

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
def __init__(self, matrix: list[list[float]]) -> None:
    self.matrix = matrix
    self.inv_matrix = self._get_inverse()  # we can call an undefined abstract method

__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
def __call__(self, vector: Vector) -> 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.

    Args:
        vector (Vector): Vector to map.

    Returns:
        Vector: Transformed vector.
    """
    x = self.matrix[0][0] * vector.x + self.matrix[0][1] * vector.y
    y = self.matrix[1][0] * vector.x + self.matrix[1][1] * vector.y
    return Vector(x, y)

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
def inverse(self, vector: Vector) -> 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.

    Args:
        vector (Vector): Vector to transform.

    Returns:
        Vector: Transformed vector.
    """
    x = self.inv_matrix[0][0] * vector.x + self.inv_matrix[0][1] * vector.y
    y = self.inv_matrix[1][0] * vector.x + self.inv_matrix[1][1] * vector.y
    return Vector(x, y)

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
def __init__(self, angle: float) -> None:
    self.angle = angle
    matrix = [[cos(angle), -sin(angle)], [sin(angle), cos(angle)]]
    super().__init__(matrix)

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
def __init__(self, shear_angle: float) -> None:
    self.shear_factor = 1 / tan(shear_angle)  # shear factor is the cotangent of the shear angle
    matrix = [[1, self.shear_factor], [0, 1]]
    super().__init__(matrix)