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.
- 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.
- 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