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 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)\)
- 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:
- Returns:
The created \(\mathbb{R}^3\) cartesian vector
- Return type:
- 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:
- Returns:
The created \(\mathbb{R}^3\) cartesian vector
- Return type:
- 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)\)
- 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)\)
- 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)\)
- is_same_order(other: Vector) bool¶
Returns
Trueif this vector is of the same order asother, elseFalse
- norm(p: int) float¶
Computes the
p-thnorm of this vector \(\vec{a}\)\(|\vec{a}|_p\equiv\left(\displaystyle\sum_i{|\vec{a}_i|^p}\right)^{\frac{1}{p}}\)
- 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:
- unit() Vector¶
Returns the unit vector \(\hat{a}\) that is in this vector \(\vec{a}\)’s direction
- Returns:
The unit vector
- Return type:
- 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:
- 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}\)
- 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
- area_between(other: Vector) float¶
Computes the area formed by the triangle bounded by this vector and
otherThis 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}\)
- 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}\)
- 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:
- Raises:
AssertionError – Attemped cross product with either empty vectors or vectors of dimensionality greater than \(\mathbb{R}^3\)
- display() str¶
Returns a formatted, displayable string representation of this vector
- Returns:
The formatted string
- Return type:
- __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:
- 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:
- 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:
- 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:
- Raises:
AssertionError – attempted to subtract vectors of different lengths
- __rmul__(other: Vector) Fraction¶
- __rmul__(other: int | float | Fraction) Self
Overloaded method:
see:
__mul__()
- __mul__(other: Vector) Fraction¶
- __mul__(other: int | float | Fraction) Self
Overloaded method:
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}\)
Computes a new vector that is this vector \(\vec{a}\) scaled up by a factor of
other\(k\)
- __imul__(other: Vector) Fraction¶
- __imul__(other: int | float | Fraction) Self
Overloaded method:
see:
__mul__()Computes the scalar dot product of this vector \(\vec{a}\) and
other\(\vec{b}\):Scales this vector \(\vec{a}\) up by a factor of
other\(k\) (in place)
- __truediv__(other: int | float | Fraction) Self¶
Computes a new vector that is this vector \(\vec{a}\) scaled down by a factor of
other\(k\)
- __itruediv__(other: int | float | Fraction) Self¶
Scales this vector \(\vec{a}\) down by a factor of
other\(k\) (in place)
- __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
- __ifloordiv__(other: int | float | Fraction) Self¶
Scales this vector \(\vec{a}\) down by a factor of
other\(k\) and then floored (in place)
- __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:
- 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:
- __neg__() Self¶
Negates this vector \(\vec{a}\) (switches its direction)
- Returns:
Returns the negated vector: \({-\vec{a}}\)
- Return type:
- __len__() int¶
Returns the length of the vector:
self.length- Returns:
The length of the vector
- Return type:
- __abs__() float¶
Computes the magnitude of this vector
See:
magnitude()- Returns:
The magnitude of this vector
- Return type:
- __contains__(target: int | float | Fraction) bool¶
Returns
Trueif the valuetargetcan be found in this vector, elseFalse
- __copy__() Self¶
Creates a copy of this vector and its elements
- Returns:
The copied vector
- Return type: