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)
class: Matrix |
|
This class represents a vector of arbitrary size. |
|
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' |
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
implements the matrix-vector multiplication. implements the matrix-scalar multiplication
returns a string representation of this matrix.
changes the x-y component of this matrix
returns the cofactor (signed minor) along (x, y)
returns the specified (x,y) component
returns the determinant of an nxn matrix using Laplace expansion
getter for the height
returns the minor along (x, y)
getter for the width
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
input: other vector assumes: other vector has the same size returns a new vector that represents the sum.
performs the comparison between two vectors
returns the size of the vector
mul implements the scalar multiplication and the dot-product
returns a string representation of the vector
input: other vector assumes: other vector has the same size returns a new vector that represents the difference.
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!
input: an index (pos) and a value changes the specified component (pos) with the ‘value’
input: index (0-indexed) output: the i-th component of the vector.
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
input: a ‘scalar’ and two vectors ‘x’ and ‘y’ output: a vector computes the axpy operation
returns a random matrix WxH with integer components between ‘a’ and ‘b’
random range (a,b)
random integer components between ‘a’ and ‘b’.
returns a square zero-matrix of dimension NxN