copapy.tensor#

class copapy.tensor[TNum: (int, float)](values, shape=None, packed=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 (TNum | value[TNum] | vector[TNum] | tensor[TNum] | array[TNum] | TensorSequence[TNum] | Iterable[TNum | value[TNum]]) – Nested sequence of constant values or copapy values, a flat 1D iterable if shape is provided or an array (its memory is used by the tensor without copying).

  • shape (Sequence[int] | None) – Optional shape of the tensor. If not provided, inferred from values.

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

property T: tensor[TNum]#

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

property dtype: str#

‘int’, ‘float’ or ‘bool’

Type:

Type of the elements

flatten()#

Flatten the tensor to 1D.

Return type:

tensor[TNum]

Returns:

A flattened 1D tensor.

get_scalar(*key)#

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

Parameters:

*key (int) – Variable number of indices for each dimension.

Return type:

TNum | value[TNum]

Returns:

The scalar value at the specified indices.

homogenize()#

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

Return type:

tensor[TNum]

Returns:

Tensor with all elements converted to Copapy values, or the input tensor if no value is a Copapy value.

item()#

The element of a tensor with one element, e.g. the scalar of the 0-d result of 1D @ 1D.

Return type:

TNum | value[TNum]

map(func)#

Apply a function to each element.

Parameters:

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

Return type:

tensor[U]

Returns:

A new tensor with the function applied element-wise.

matmul(other)#

Matrix multiplication (@ operator). The type of the result does not depend on the shapes: it is a tensor, 0-d for 1D @ 1D (use item() for the scalar). With a vector as other, the tensor must be 2D and the result is a vector.

Overloads:
Parameters:

other (tensor[Any] | vector[Any]) – Another tensor or a vector to multiply with.

Returns:

Result of matrix multiplication.

Raises:

ValueError – If shapes are incompatible for matrix multiplication.

mean(axis=None, keepdims=False)#

Calculate mean of all elements or along specified axis/axes.

Overloads:
  • axis (None) = None, keepdims (Literal[False]) = False → float | value[float]

  • axis (None) = None, *, keepdims (Literal[True]) → tensor[float]

  • axis (int | Sequence[int]), keepdims (bool) = False → tensor[float]

  • axis (int | Sequence[int] | None) = None, keepdims (bool) = False → float | value[float] | tensor[float]

Parameters:
  • axis (int | Sequence[int] | None) – Axis or tuple of axes along which to compute mean. If None, computes overall mean.

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

Returns:

Scalar or tensor with reduced dimension(s).

ndim: int#

Number of dimensions (length of shape)

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

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[TNum]

Returns:

A new tensor with the specified shape.

shape: tuple[int, ...]#

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

size()#

Count number of elements over all dimensions.

Return type:

int

Returns:

Total number of elements.

sum(axis=None, keepdims=False)#

Sum all or along specified axis/axes. The type of the result does not depend on the shape: a scalar for axis None without keepdims, else a tensor (0-d if all axes are summed, use item() for the scalar).

Overloads:
  • axis (None) = None, keepdims (Literal[False]) = False → TNum | value[TNum]

  • axis (None) = None, *, keepdims (Literal[True]) → tensor[TNum]

  • axis (int | Sequence[int]), keepdims (bool) = False → tensor[TNum]

  • axis (int | Sequence[int] | None) = None, keepdims (bool) = False → TNum | value[TNum] | tensor[TNum]

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.

Returns:

Scalar or tensor with reduced dimension(s).

trace()#

Calculate the trace (sum of diagonal elements).

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

  • () → int | value[int]

  • () → float | value[float]

Returns:

The sum of diagonal elements.

transpose(*axes)#

Transpose the tensor.

Parameters:

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

Return type:

tensor[TNum]

Returns:

A transposed tensor.

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

Flat tuple of all elements