Vector Class#

class rocketpy.mathutils.vector_matrix.Vector[source]#

Pure python basic R3 vector class designed for simple operations.

Notes

Instances of the Vector class are immutable. Real and complex components are supported.

Examples

Creating a Vector instance requires passing its components as an iterable:

>>> v = Vector([1, 2, 3])
>>> v
Vector(1, 2, 3)

Vector components can be accessed by x, y and z or by indexing:

>>> v.x, v.y, v.z
(1, 2, 3)
>>> v[0], v[1], v[2]
(1, 2, 3)

Vector instances can be added, subtracted, multiplied by a scalar, divided by a scalar, negated, and cross and dot product can be computed:

>>> v + v
Vector(2, 4, 6)
>>> v - v
Vector(0, 0, 0)
>>> v * 2
Vector(2, 4, 6)
>>> v / 2
Vector(0.5, 1.0, 1.5)
>>> -v
Vector(-1, -2, -3)
>>> v @ v # Dot product
14

Cross products need to be wrapped in parentheses to ensure the ^ operator precedence:

>>> (v ^ v)
Vector(0, 0, 0)

Vector instances can be called as functions if their elements are callable:

>>> v = Vector([lambda x: x**2, lambda x: x**3, lambda x: x**4])
>>> v(2)
Vector(4, 8, 16)

Vector instances magnitudes can be accessed as its absolute value:

>>> v = Vector([1, 2, 3])
>>> abs(v)
3.7416573867739413

Vector instances can be normalized:

>>> v.unit_vector
Vector(0.2672612419124244, 0.5345224838248488, 0.8017837257372732)

Vector instances can be compared for equality:

>>> v = Vector([1, 2, 3])
>>> u = Vector([1, 2, 3])
>>> v == u
True
>>> v != u
False

And last, but not least, it is also possible to check if two vectors are parallel or orthogonal:

>>> v = Vector([1, 2, 3])
>>> u = Vector([2, 4, 6])
>>> v.is_parallel_to(u)
True
>>> v.is_orthogonal_to(u)
False
__init__(components)[source]#

Vector class constructor.

Parameters:

components (array-like, iterable) – An iterable with length equal to 3, corresponding to x, y and z components.

Examples

>>> v = Vector([1, 2, 3])
>>> v
Vector(1, 2, 3)
__getitem__(i)[source]#

Access vector components by indexing.

__iter__()[source]#

Adds support for iteration.

__call__(*args)[source]#

Adds support for calling a vector as a function, if its elements are callable.

Parameters:

args (arguments) – Arguments to be passed to the vector elements.

Returns:

Vector with the return of each element called with the given arguments.

Return type:

Vector

Examples

>>> v = Vector([lambda x: x**2, lambda x: x**3, lambda x: x**4])
>>> v(2)
Vector(4, 8, 16)
unit_vector#

R3 vector with the same direction of self, but normalized.

cross_matrix#

Skew symmetric matrix used for cross product.

Notes

The cross product between two vectors can be computed as the matrix product between the cross matrix of the first vector and the second vector.

Examples

>>> v = Vector([1, 7, 3])
>>> u = Vector([2, 5, 6])
>>> (v ^ u) == v.cross_matrix @ u
True
__abs__()[source]#

R3 vector norm, magnitude or absolute value.

__neg__()[source]#

-1 times R3 vector self.

__add__(other)[source]#

Sum two R3 vectors.

__sub__(other)[source]#

Subtract two R3 vectors.

__mul__(other)[source]#

Component wise multiplication between R3 vector and scalar other.

__rmul__(other)[source]#

Component wise multiplication between R3 vector and scalar other.

__truediv__(other)[source]#

Component wise division between R3 vector and scalar other.

__xor__(other)[source]#

Cross product between self and other.

Parameters:

other (Vector) – R3 vector to be crossed with self.

Returns:

R3 vector resulting from the cross product between self and other.

Return type:

Vector

Examples

>>> v = Vector([1, 7, 3])
>>> u = Vector([2, 5, 6])
>>> (v ^ u)
Vector(27, 0, -9)

Notes

Parameters order matters, since cross product is not commutative. Parentheses are required when using cross product with the ^ operator to avoid ambiguity with the bitwise xor operator and keep the precedence of the operators.

__matmul__(other)[source]#

Dot product between two R3 vectors.

__eq__(other)[source]#

Check if two R3 vectors are equal.

Parameters:

other (Vector) – R3 vector to be compared with self.

Returns:

True if self and other are equal. False otherwise.

Return type:

bool

Examples

>>> v = Vector([1, 7, 3])
>>> u = Vector([1, 7, 3])
>>> v == u
True

Notes

Two R3 vectors are equal if their components are equal or almost equal. Python’s cmath.isclose function is used to compare the components.

is_parallel_to(other)[source]#

Returns True if self is parallel to R3 vector other. False otherwise.

Parameters:

other (Vector) – R3 vector to be compared with self.

Returns:

True if self and other are parallel. False otherwise.

Return type:

bool

Notes

Two R3 vectors are parallel if their cross product is the zero vector. Python’s cmath.isclose function is used to assert this.

is_orthogonal_to(other)[source]#

Returns True if self is perpendicular to R3 vector other. False otherwise.

Parameters:

other (Vector) – R3 vector to be compared with self.

Returns:

True if self and other are perpendicular. False otherwise.

Return type:

bool

Notes

Two R3 vectors are perpendicular if their dot product is zero. Python’s cmath.isclose function is used to assert this with absolute tolerance of 1e-9.

element_wise(operation)[source]#

Element wise operation.

Parameters:

operation (callable) – Callable with a single argument, which should take an element and return the result of the desired operation.

Examples

>>> v = Vector([1, 7, 3])
>>> v.element_wise(lambda x: x**2)
Vector(1, 49, 9)
dot(other)[source]#

Dot product between two R3 vectors.

cross(other)[source]#

Cross product between two R3 vectors.

proj(other)[source]#

Scalar projection of R3 vector self onto R3 vector other.

Parameters:

other (Vector) – R3 vector to be projected onto.

Returns:

Scalar projection of self onto other.

Return type:

float

Examples

>>> v = Vector([1, 7, 3])
>>> u = Vector([2, 5, 6])
>>> v.proj(u)
6.821910402406465
static zeros()[source]#

Returns the zero vector.

static i()[source]#

Returns the i vector, [1, 0, 0].

static j()[source]#

Returns the j vector, [0, 1, 0].

static k()[source]#

Returns the k vector, [0, 0, 1].