Vector

Module contents

class matrixflow.Vector(entries: Sequence[int | float | Fraction])

An implementation for a mathematical vector

Parameters:

entries (Sequence[int | float | Fraction]) – The raw entries to initialize the vector with

classmethod zero(n: int) Self

Creates the zero vector of length n: \(\vec{0}\)

Parameters:

n (int) – The size of the vector to create

Returns:

The created zero vector: \(\vec{0}\)

Return type:

Self

classmethod from_polar(r: int | float | Fraction, theta: int | float | Fraction) Self

Creates a vector of length 2: \(\begin{pmatrix}x\\y\end{pmatrix}\) based on the provided polar coordinates: \(\left(r,\theta\right)\)

Parameters:
  • r (int | float | Fraction) – The radial distance: the magnitude of the vector

  • theta (int | float | Fraction) – The polar angle: the angle away from the x-axis

Returns:

The created \(\mathbb{R}^2\) cartesian vector

Return type:

Self

classmethod from_cylindrical(r: int | float | Fraction, theta: int | float | Fraction, z: int | float | Fraction) Self

Creates a vector of length 3: \(\begin{pmatrix}x\\y\\z\end{pmatrix}\) based on the provided cylindrical coordinates: \(\left(r,\theta,z\right)\)

Parameters:
  • r (int | float | Fraction) – The euclidean distance from the z-axis to the vector

  • theta (int | float | Fraction) – The polar angle: the angle away from the x-axis

  • z (int | float | Fraction) – The axial coordinate: the z-coordinate, distance away from the xy-plane

Returns:

The created \(\mathbb{R}^3\) cartesian vector

Return type:

Self

classmethod from_spherical(rho: int | float | Fraction, theta: int | float | Fraction, phi: int | float | Fraction) Self

Creates a vector of length 3: \(\begin{pmatrix}x\\y\\z\end{pmatrix}\) based on the provided spherical coordinates: \(\left(\rho,\theta,\phi\right)\)

Parameters:
  • rho (int | float | Fraction) – The radial distance: the magnitude of the vector

  • theta (int | float | Fraction) – The polar angle: the angle away from the z-axis

  • phi (int | float | Fraction) – The azimuthal angle: the angle away from the x-axis

Returns:

The created \(\mathbb{R}^3\) cartesian vector

Return type:

Self

property length: int

The number of elements in this vector

property inner: list[Fraction]

Returns a reference to the internal list representation of this vector

to_polar() tuple[float, float]

For \(\mathbb{R}^2\) vectors: converts this vector from \(\begin{pmatrix}x\\y\end{pmatrix}\) to polar coordinates \(\left(r,\theta\right)\)

Returns:

This vector in polar coordinates

Return type:

tuple[float, float]

to_cylindrical() tuple[float, float, float]

For \(\mathbb{R}^3\) vectors: converts this vector from \(\begin{pmatrix}x\\y\\z\end{pmatrix}\) to cylindrical coordinates \(\left(r,\theta,z\right)\)

Returns:

This vector in cylindrical coordinates

Return type:

tuple[float, float, float]

to_spherical() tuple[float, float, float]

For \(\mathbb{R}^3\) vectors: converts this vector from \(\begin{pmatrix}x\\y\\z\end{pmatrix}\) to spherical coordinates \(\left(\rho,\theta,\phi\right)\)

Returns:

This vector in polar coordinates

Return type:

tuple[float, float, float]

is_same_order(other: Vector) bool

Returns True if this vector is of the same order as other, else False

Parameters:

other (Vector) – The other vector to compare to

Returns:

Whether or not the vectors are of the same order

Return type:

bool

norm(p: int) float

Computes the p-th norm of this vector \(\vec{a}\)

\(|\vec{a}|_p\equiv\left(\displaystyle\sum_i{|\vec{a}_i|^p}\right)^{\frac{1}{p}}\)

Parameters:

p (int) – The degree of the vector norm

Returns:

The p-th norm of this vector

Return type:

float

infinity_norm() Fraction

Computes the infinity norm of this vector \(\vec{a}\)

This is also simply the magnitude of its largest entry: \(|\vec{a}|_\infty\equiv\max_i{|\vec{a}_i|}\)

Returns:

The infinity norm of this vector

Return type:

Fraction

unit() Vector

Returns the unit vector \(\hat{a}\) that is in this vector \(\vec{a}\)’s direction

Returns:

The unit vector

Return type:

Vector

magnitude() float

Computes the magnitude of this vector \(\vec{a}\)

This is also the second norm: \(|\vec{a}|_2\equiv\sqrt{\displaystyle\sum_i{{\vec{a}_i}^2}}\)

Returns:

The magnitude of this vector: \(\|\vec{a}\|\)

Return type:

float

project(other: Vector) Vector

The vector projection \(\vec{a_1}\) of this vector \(\vec{a}\) onto other \(\vec{b}\)

\(\vec{a_1}=\left(\|\vec{a}\|\cos\theta\right)\hat{b}={\frac{\vec{a}\cdot\vec{b}}{\|\vec{b}\|}}\hat{b}\)

The magnitude of the projection vector is the scalar projection, which is also the dot product scaled down by the magnitude of \(\vec{b}\)

Parameters:

other (Vector) – The vector to project this vector onto

Returns:

The projection vector: \(\vec{a_1}\)

Return type:

Vector

reject(other: Vector) Vector

The vector rejection \(\vec{a_2}\) of this vector \(\vec{a}\) onto other \(\vec{b}\)

\(\vec{a_2}=\vec{a}-\vec{a_1}\) where \(\vec{a_1}\) is the projection vector

Parameters:

other (Vector) – The vector to reject this vector onto

Returns:

The rejection vector: \(\vec{a_2}\)

Return type:

Vector

angle_between(other: Vector) float

Computes the angle between this vector and other: \(\theta\)

Parameters:

other (Vector) – The other vector to form the angle with

Returns:

The angle, in radians

Return type:

float

area_between(other: Vector) float

Computes the area formed by the triangle bounded by this vector and other

This is simply half of the determinant of the matrix formed by the 2 vectors, which is the area of the parallelogram formed by the 2 vectors, which is also the magnitude of the cross product vector: \(\vec{a}\times\vec{b}\)

\(A=\frac{1}{2}\|\vec{a}\times\vec{b}\|=\frac{1}{2}\|a\|\|b\|\sin\theta=\frac{1}{2}\begin{vmatrix}a_x&b_x\\a_y&b_y\\\vdots&\vdots\end{vmatrix}\)

Parameters:

other (Vector) – The other matrix to compute the area between

Returns:

The area between the 2 vectors

Return type:

float

dot(other: Vector) Fraction

Computes the scalar dot product of this vector \(\vec{a}\) and other \(\vec{b}\)

\(\vec{a}\cdot\vec{b}=\|\vec{a}\|\|\vec{b}\|\cos\theta\)

which is also the scalar projection of this vector times the magnitude of \(\vec{b}\)

Parameters:

other (Vector) – The vector to perform the dot product with

Returns:

The scalar dot product: \(\vec{a}\cdot\vec{b}\)

Return type:

Fraction

cross(other: Vector) Vector

Computes the orthogonal cross product vector of this vector \(\vec{a}\) and other \(\vec{b}\)

Parameters:

other (Vector) – The vector to perform the cross product with

Returns:

The orthogonal cross product vector: \(\vec{a}\times\vec{b}\)

Return type:

Vector

Raises:

AssertionError – Attemped cross product with either empty vectors or vectors of dimensionality greater than \(\mathbb{R}^3\)

add_entry(entry: int | float | Fraction) None

Appends an element entry to the end of this vector

Parameters:

entry (int | float | Fraction) – The entry to append

Return type:

None

map(f: Callable[[int], None]) None

Maps a function over all of this vector’s elements

Parameters:

f (Callable[[int], None]) – The function to apply over the elements It will take in the current element i and should not return anything as it maps in place

Return type:

None

copy() Self

Makes a copy of this vector and its elements

Returns:

The copied vector

Return type:

Self

display() str

Returns a formatted, displayable string representation of this vector

Returns:

The formatted string

Return type:

str

__add__(other: Vector) Vector

Computes a new vector that is the sum of this vector \(\vec{a}\) and other \(\vec{b}\)

Parameters:

other (Vector) – The vector to perform the addition with

Returns:

The sum vector: \(\vec{a}+\vec{b}\)

Return type:

Vector

Raises:

AssertionError – attempted to add vectors of different lengths

__iadd__(other: Vector) Self

Adds other \(\vec{b}\) onto this vector \(\vec{a}\) (in place)

Parameters:

other (Vector) – The vector to perform the addition with

Returns:

The sum vector: \(\vec{a}+\vec{b}\)

Return type:

Vector

Raises:

AssertionError – attempted to add vectors of different lengths

__sub__(other: Vector) Vector

Computes a new vector that is the difference of this vector \(\vec{a}\) and other \(\vec{b}\)

Parameters:

other (Vector) – The vector to perform the addition with

Returns:

The difference vector: \(\vec{a}-\vec{b}\)

Return type:

Vector

Raises:

AssertionError – attempted to subtract vectors of different lengths

__isub__(other: Vector) Self

Subtracts other \(\vec{b}\) from this vector \(\vec{a}\) (in place)

Parameters:

other (Vector) – The vector to perform the addition with

Returns:

The difference vector: \(\vec{a}-\vec{b}\)

Return type:

Vector

Raises:

AssertionError – attempted to subtract vectors of different lengths

__rmul__(other: Vector) Fraction
__rmul__(other: int | float | Fraction) Self

Overloaded method:

see: __mul__()

Parameters:

other (int | float | Fraction | Vector) – The right operand in the scalar multiplication | scalar dot product

Returns:

The product of the scalar multiplication: \(k\vec{a}\) | scalar dot product: \(\vec{a}\cdot\vec{b}\)

Return type:

Self | Fraction

__mul__(other: Vector) Fraction
__mul__(other: int | float | Fraction) Self

Overloaded method:

  1. Computes the scalar dot product of this vector \(\vec{a}\) and other \(\vec{b}\)

    \(\vec{a}\cdot\vec{b}=\|\vec{a}\|\|\vec{b}\|\cos\theta\)

    which is also the scalar projection of this vector times the magnitude of \(\vec{b}\)

  2. Computes a new vector that is this vector \(\vec{a}\) scaled up by a factor of other \(k\)

Parameters:

other (int | float | Fraction | Vector) – The right operand in the scalar multiplication | scalar dot product

Returns:

The product of the scalar multiplication: \(k\vec{a}\) | scalar dot product: \(\vec{a}\cdot\vec{b}\)

Return type:

Self | Fraction

__imul__(other: Vector) Fraction
__imul__(other: int | float | Fraction) Self

Overloaded method:

see: __mul__()

  1. Computes the scalar dot product of this vector \(\vec{a}\) and other \(\vec{b}\):

  2. Scales this vector \(\vec{a}\) up by a factor of other \(k\) (in place)

Parameters:

other (int | float | Fraction | Vector) – The scaled vector | scalar dot product

Returns:

The scaled vector: \(k\vec{a}\) | scalar dot product: \(\vec{a}\cdot\vec{b}\)

Return type:

Self | Fraction

__truediv__(other: int | float | Fraction) Self

Computes a new vector that is this vector \(\vec{a}\) scaled down by a factor of other \(k\)

Parameters:

other (int | float | Fraction) – The scalar to scale this vector down by

Returns:

The scaled down vector: \(\frac{1}{k}\vec{a}\)

Return type:

Vector

__itruediv__(other: int | float | Fraction) Self

Scales this vector \(\vec{a}\) down by a factor of other \(k\) (in place)

Parameters:

other (int | float | Fraction) – The scalar to scale this vector down by

Returns:

The scaled down vector: \(\frac{1}{k}\vec{a}\)

Return type:

Vector

__floordiv__(other: int | float | Fraction) Self

Computes a new vector that is this vector \(\vec{a}\) scaled down by a factor of other \(k\) and then floored

Parameters:

other (int | float | Fraction) – The scalar to scale this vector down by

Returns:

The scaled down vector: \(\lfloor\frac{1}{k}\rfloor\vec{a}\)

Return type:

Vector

__ifloordiv__(other: int | float | Fraction) Self

Scales this vector \(\vec{a}\) down by a factor of other \(k\) and then floored (in place)

Parameters:

other (int | float | Fraction) – The scalar to scale this vector down by

Returns:

The scaled down vector: \(\lfloor\frac{1}{k}\rfloor\vec{a}\)

Return type:

Vector

__matmul__(other: Vector) Vector

Computes the orthogonal cross product vector of this vector \(\vec{a}`and ``other`\) \(\vec{b}\)

see: cross()

Parameters:

other (Vector) – The vector to perform the cross product with

Returns:

The orthogonal cross product vector: \(\vec{a}\times\vec{b}\)

Return type:

Vector

Raises:

AssertionError – Attemped cross product with either empty vectors or vectors of dimensionality greater than \(\mathbb{R}^3\)

__pos__() Self

Unary plus: does nothing as it performs a scalar multiplication of all the elements by \(+1\)

Returns:

Returns itself

Return type:

Self

__neg__() Self

Negates this vector \(\vec{a}\) (switches its direction)

Returns:

Returns the negated vector: \({-\vec{a}}\)

Return type:

Self

__len__() int

Returns the length of the vector: self.length

Returns:

The length of the vector

Return type:

int

__abs__() float

Computes the magnitude of this vector

See: magnitude()

Returns:

The magnitude of this vector

Return type:

float

__getitem__(i: int) Fraction

Gets the element of this vector \(\vec{a}\) at i:

Parameters:

i (int) – The index of the element in this vector

Returns:

The element of the vector at i: \(\vec{a}_i\)

Return type:

Fraction

__contains__(target: int | float | Fraction) bool

Returns True if the value target can be found in this vector, else False

Parameters:

target (int | float | Fraction) – The target value to search for

Returns:

whether or not the value target can be found in this vector

Return type:

bool

__copy__() Self

Creates a copy of this vector and its elements

Returns:

The copied vector

Return type:

Self

__deepcopy__() Self

Creates a copy of this vector and its elements

Returns:

The copied vector

Return type:

Self

__repr__() str

Return repr(self)

Return type:

str

__str__() str

Return str(self)

Return type:

str

__eq__(other: Any) bool

Return self == other

Parameters:

other (Any)

Return type:

bool

__ne__(other: Any) bool

Return self != other

Parameters:

other (Any)

Return type:

bool