copapy.tensor#

class copapy.tensor(values, shape=None)#

Bases: ArrayType[TNum]

Generalized n-dimensional tensor class supporting numpy-style operations.

A tensor can have any number of dimensions and supports element-wise operations, reshaping, transposition, and various reduction operations.

Create a tensor with given values.

Parameters:
  • values – Nested iterables of constant values or copapy values. Can be a scalar, 1D iterable (vector), or n-dimensional nested structure.

  • shape – Optional shape of the tensor. If not provided, inferred from values.

property T: tensor[TNum]#

Transpose all axes (equivalent to transpose() with no args).

flatten()#

Flatten the tensor to 1D.

Return type:

tensor[TypeVar(TNum, int, float)]

Returns:

A flattened 1D tensor.

get_scalar(*key)#

Get a single scalar value from the tensor given multi-dimensional indices.

Return type:

TypeVar(TNum, int, float) | value[TypeVar(TNum, int, float)]

homogenize()#

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

Return type:

tensor[TypeVar(TNum, int, float)]

map(func)#

Apply a function to each element.

Parameters:

func (Callable[[Any], value[TypeVar(U, int, float)] | TypeVar(U, int, float)]) – Function to apply to each element.

Return type:

tensor[TypeVar(U, int, float)]

Returns:

A new tensor with the function applied element-wise.

matmul(other)#

Matrix multiplication (@ operator).

Parameters:

other (tensor[TypeVar(TNum, int, float)] | vector[TypeVar(TNum, int, float)]) – Another tensor to multiply with.

Return type:

TypeVar(TNum, int, float) | value[TypeVar(TNum, int, float)] | tensor[TypeVar(TNum, int, float)]

Returns:

Result of matrix multiplication.

Raises:

ValueError – If shapes are incompatible for matrix multiplication.

mean(axis=None)#

Calculate mean along axis or overall.

Parameters:

axis (int | None) – Axis along which to compute mean. If None, computes overall mean.

Return type:

Any

Returns:

Scalar or tensor with reduced dimension.

reshape(*new_shape)#

Reshape the tensor to a new shape.

Parameters:

*new_shape (int) – New shape dimensions. Use -1 for one dimension to infer automatically.

Return type:

tensor[TypeVar(TNum, int, float)]

Returns:

A new tensor with the specified shape.

size()#

Return total number of elements.

Return type:

int

sum(axis=None, keepdims=False)#

Sum all or along specified axis/axes.

Parameters:
  • axis (int | Sequence[int] | None) – Axis or tuple of axes along which to sum. If None, sums all elements.

  • keepdims (bool) – If True, keep reduced dimensions as size 1.

Return type:

TypeVar(TNum, int, float) | value[TypeVar(TNum, int, float)] | tensor[TypeVar(TNum, int, float)]

Returns:

Scalar or tensor with reduced dimension(s).

trace()#

Calculate the trace (sum of diagonal elements).

Return type:

Any

transpose(*axes)#

Transpose the tensor.

Parameters:

*axes (int) – Permutation of axes. If not provided, reverses all axes.

Return type:

tensor[TypeVar(TNum, int, float)]

Returns:

A transposed tensor.