linear_algebra.src.lib ====================== .. py:module:: linear_algebra.src.lib .. autoapi-nested-parse:: 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 ------- .. autoapisummary:: linear_algebra.src.lib.Matrix linear_algebra.src.lib.Vector Functions --------- .. autoapisummary:: linear_algebra.src.lib.axpy linear_algebra.src.lib.random_matrix linear_algebra.src.lib.random_vector linear_algebra.src.lib.square_zero_matrix linear_algebra.src.lib.unit_basis_vector linear_algebra.src.lib.zero_vector Module Contents --------------- .. py:class:: 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 .. py:method:: __add__(other: Matrix) -> Matrix implements matrix addition. .. py:method:: __mul__(other: float) -> Matrix __mul__(other: Vector) -> Vector implements the matrix-vector multiplication. implements the matrix-scalar multiplication .. py:method:: __str__() -> str returns a string representation of this matrix. .. py:method:: __sub__(other: Matrix) -> Matrix implements matrix subtraction. .. py:method:: change_component(x: int, y: int, value: float) -> None changes the x-y component of this matrix .. py:method:: cofactor(x: int, y: int) -> float returns the cofactor (signed minor) along (x, y) .. py:method:: component(x: int, y: int) -> float returns the specified (x,y) component .. py:method:: determinant() -> float returns the determinant of an nxn matrix using Laplace expansion .. py:method:: height() -> int getter for the height .. py:method:: minor(x: int, y: int) -> float returns the minor along (x, y) .. py:method:: width() -> int getter for the width .. py:attribute:: __height .. py:attribute:: __matrix .. py:attribute:: __width .. py:class:: 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 .. py:method:: __add__(other: Vector) -> Vector input: other vector assumes: other vector has the same size returns a new vector that represents the sum. .. py:method:: __eq__(other: object) -> bool performs the comparison between two vectors .. py:method:: __len__() -> int returns the size of the vector .. py:method:: __mul__(other: float) -> Vector __mul__(other: Vector) -> float mul implements the scalar multiplication and the dot-product .. py:method:: __str__() -> str returns a string representation of the vector .. py:method:: __sub__(other: Vector) -> Vector input: other vector assumes: other vector has the same size returns a new vector that represents the difference. .. py:method:: 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! .. py:method:: change_component(pos: int, value: float) -> None input: an index (pos) and a value changes the specified component (pos) with the 'value' .. py:method:: component(i: int) -> float input: index (0-indexed) output: the i-th component of the vector. .. py:method:: copy() -> Vector copies this vector and returns it. .. py:method:: 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 .. py:attribute:: __components .. py:function:: axpy(scalar: float, x: Vector, y: Vector) -> Vector input: a 'scalar' and two vectors 'x' and 'y' output: a vector computes the axpy operation .. py:function:: random_matrix(width: int, height: int, a: int, b: int) -> Matrix returns a random matrix WxH with integer components between 'a' and 'b' .. py:function:: 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'. .. py:function:: square_zero_matrix(n: int) -> Matrix returns a square zero-matrix of dimension NxN .. py:function:: unit_basis_vector(dimension: int, pos: int) -> Vector returns a unit basis vector with a One at index 'pos' (indexing at 0) .. py:function:: zero_vector(dimension: int) -> Vector returns a zero-vector of size 'dimension'