linear_algebra.src.lib¶
Created on Mon Feb 26 14:29:11 2018
@author: Christian Bender @license: MIT-license
This module contains some useful classes and functions for dealing with linear algebra in python.
Overview:
class Vector
function zero_vector(dimension)
function unit_basis_vector(dimension, pos)
function axpy(scalar, vector1, vector2)
function random_vector(N, a, b)
class Matrix
function square_zero_matrix(N)
function random_matrix(W, H, a, b)
Classes¶
class: Matrix |
|
This class represents a vector of arbitrary size. |
Functions¶
|
input: a 'scalar' and two vectors 'x' and 'y' |
|
returns a random matrix WxH with integer components |
|
input: size (N) of the vector. |
|
returns a square zero-matrix of dimension NxN |
|
returns a unit basis vector with a One |
|
returns a zero-vector of size 'dimension' |
Module Contents¶
- class linear_algebra.src.lib.Matrix(matrix: list[list[float]], w: int, h: int)¶
class: Matrix This class represents an arbitrary matrix.
Overview of the methods:
__init__(): __str__(): returns a string representation __add__(other: Matrix): matrix addition __sub__(other: Matrix): matrix subtraction __mul__(other: float): scalar multiplication __mul__(other: Vector): vector multiplication height() : returns height width() : returns width component(x: int, y: int): returns specified component change_component(x: int, y: int, value: float): changes specified component minor(x: int, y: int): returns minor along (x, y) cofactor(x: int, y: int): returns cofactor along (x, y) determinant() : returns determinant
- __mul__(other: float) Matrix ¶
- __mul__(other: Vector) Vector
implements the matrix-vector multiplication. implements the matrix-scalar multiplication
- __str__() str ¶
returns a string representation of this matrix.
- change_component(x: int, y: int, value: float) None ¶
changes the x-y component of this matrix
- cofactor(x: int, y: int) float ¶
returns the cofactor (signed minor) along (x, y)
- component(x: int, y: int) float ¶
returns the specified (x,y) component
- determinant() float ¶
returns the determinant of an nxn matrix using Laplace expansion
- height() int ¶
getter for the height
- minor(x: int, y: int) float ¶
returns the minor along (x, y)
- width() int ¶
getter for the width
- __height¶
- __matrix¶
- __width¶
- class linear_algebra.src.lib.Vector(components: collections.abc.Collection[float] | None = None)¶
This class represents a vector of arbitrary size. You need to give the vector components.
Overview of the methods:
__init__(components: Collection[float] | None): init the vector __len__(): gets the size of the vector (number of components) __str__(): returns a string representation __add__(other: Vector): vector addition __sub__(other: Vector): vector subtraction __mul__(other: float): scalar multiplication __mul__(other: Vector): dot product copy(): copies this vector and returns it component(i): gets the i-th component (0-indexed) change_component(pos: int, value: float): changes specified component euclidean_length(): returns the euclidean length of the vector angle(other: Vector, deg: bool): returns the angle between two vectors TODO: compare-operator
- __add__(other: Vector) Vector ¶
input: other vector assumes: other vector has the same size returns a new vector that represents the sum.
- __len__() int ¶
returns the size of the vector
- __mul__(other: float) Vector ¶
- __mul__(other: Vector) float
mul implements the scalar multiplication and the dot-product
- __str__() str ¶
returns a string representation of the vector
- __sub__(other: Vector) Vector ¶
input: other vector assumes: other vector has the same size returns a new vector that represents the difference.
- angle(other: Vector, deg: bool = False) float ¶
find angle between two Vector (self, Vector)
>>> Vector([3, 4, -1]).angle(Vector([2, -1, 1])) 1.4906464636572374 >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]), deg = True) 85.40775111366095 >>> Vector([3, 4, -1]).angle(Vector([2, -1])) Traceback (most recent call last): ... Exception: invalid operand!
- change_component(pos: int, value: float) None ¶
input: an index (pos) and a value changes the specified component (pos) with the ‘value’
- component(i: int) float ¶
input: index (0-indexed) output: the i-th component of the vector.
- euclidean_length() float ¶
returns the euclidean length of the vector
>>> Vector([2, 3, 4]).euclidean_length() 5.385164807134504 >>> Vector([1]).euclidean_length() 1.0 >>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length() 9.539392014169456 >>> Vector([]).euclidean_length() Traceback (most recent call last): ... Exception: Vector is empty
- __components¶
- linear_algebra.src.lib.axpy(scalar: float, x: Vector, y: Vector) Vector ¶
input: a ‘scalar’ and two vectors ‘x’ and ‘y’ output: a vector computes the axpy operation
- linear_algebra.src.lib.random_matrix(width: int, height: int, a: int, b: int) Matrix ¶
returns a random matrix WxH with integer components between ‘a’ and ‘b’
- linear_algebra.src.lib.random_vector(n: int, a: int, b: int) Vector ¶
- input: size (N) of the vector.
random range (a,b)
- output: returns a random vector of size N, with
random integer components between ‘a’ and ‘b’.
- linear_algebra.src.lib.square_zero_matrix(n: int) Matrix ¶
returns a square zero-matrix of dimension NxN