copapy.vector#

class copapy.vector[TNum: (int, float)](values, packed=None)#

Bases: ArrayType[TNum]

Mathematical vector class supporting basic operations and interactions with values. A vector has one dimension: ndim is 1 and shape is (number of elements,).

Create a vector with given values.

Parameters:
  • values (Iterable[TNum | value[TNum]] | array[TNum]) – iterable of constant values or a 1D array (its memory is used by the vector without copying)

  • packed (bool | None) – True to always use array stencils for this vector, False to never use them, None to decide by vector.pack_threshold.

cross(other)#

Calculate the cross product of this vector with another 3D vector.

Parameters:

other (vector[float]) – Another 3D vector.

Return type:

vector[float]

Returns:

A new vector perpendicular to both vectors.

dot(other)#

Calculate the dot product of this vector with another.

Overloads:
Parameters:

other (vector[int] | vector[float]) – Another vector of the same length.

Returns:

The dot product as a scalar value.

property dtype: str#

‘int’, ‘float’ or ‘bool’

Type:

Type of the elements

get_scalar(index)#

Get a single scalar value from the vector.

Parameters:

index (int) – The index of the element to retrieve.

Return type:

TNum | value[TNum]

Returns:

The scalar value at the specified index.

homogenize()#

Convert all elements to Copapy values if any element is a Copapy value.

Return type:

vector[TNum]

Returns:

Vector with homogeneous element type.

magnitude()#

Magnitude (length) of the vector.

Return type:

float | value[float]

Returns:

The magnitude of the vector as a scalar value.

map(func)#

Applies a function to each element of the vector and returns a new vector.

Parameters:

func (Callable[[Any], value[U] | U]) – A function that takes a single argument.

Return type:

vector[U]

Returns:

A new vector with the function applied to each element.

ndim: int#

Number of dimensions (length of shape)

normalize()#

Calculate a normalized (unit length) version of the vector.

Return type:

vector[float]

Returns:

A normalized (unit length) vector.

pack_threshold: int | None = 64#

Instances are packed if more elements than pack_threshold are copapy values or constants other than zero, None disables packing and type promotion

shape: tuple[int, ...]#

Shape of the elements, e.g. (n,) for a vector

sum()#

Sum of all vector elements.

Overloads:
  • () → int | value[int]

  • () → float | value[float]

Returns:

The sum of all vector elements as a scalar value.

property values: tuple[TNum | value[TNum], ...]#

Flat tuple of all elements