copapy.array#

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

Bases: Generic[TNum]

Homogeneous n-dimensional array stored contiguously (row-major) in the target memory. Operations on arrays compile to a array stencil instead of one stencil per element, which keeps compile time and code size independent of the array size.

shape#

Size of each dimension.

dtype#

Element type (‘int’, ‘float’, ‘bool’ or ‘byte’)

net#

Underlying array net in the computation graph.

Create an array from (nested) sequences of numbers or copapy values. An array of numbers can be overwritten on the target with Target.write_value. An array containing copapy values is filled with the current values on each run (one load and store per element).

Elements of different types are promoted: the array is float if any element is float, int elements are converted (int variables by a float_int stencil). An array of only bool elements is a bool array (stored as int).

A ‘byte’ array stores numbers from 0 to 255 in one byte per element, e.g. image data written with Target.write_value. Bytes are only stored: operations convert the array to float or int, its elements are read as int values.

Parameters:
  • values (Sequence[TNum] | Sequence[Sequence[TNum]] | Sequence[Any]) – Nested sequences of int or float numbers or copapy values.

  • dtype (str | None) – Element type (‘int’, ‘float’, ‘bool’ or ‘byte’), inferred from values if omitted.

property T: array[TNum]#
argsort()#

Indices that sort a 1D array, equal elements in order of their index (like a stable sort). The execution time only depends on the number of elements, not on the values.

Return type:

array[int]

broadcast_to(shape)#

Repeat dimensions of size 1 (numpy broadcasting rules).

Return type:

array[TNum]

dot(other)#

Dot product of two arrays with the same number of elements.

Overloads:
element(flat_index)#

Element by its flat index. No code is generated, the element is read directly from the array memory. An element of a byte array is copied and converted to int.

Return type:

value[TNum]

flatten()#
Return type:

array[TNum]

static flatten_data(data)#
Return type:

list[Any]

get_values(data)#

Nest flat element data according to the array shape

Return type:

list[Any]

matmul(other)#

Matrix multiplication for 1D and 2D operands (numpy semantics).

Overloads:
max()#

Largest element.

Return type:

value[TNum]

min()#

Smallest element.

Return type:

value[TNum]

property ndim: int#
reshape(*shape)#

Same data with a new shape, a dimension of -1 is inferred.

Return type:

array[TNum]

property size: int#
sort()#

Sorted copy of a 1D array. A sorting network is used: the execution time only depends on the number of elements, not on the values.

Return type:

array[TNum]

sum(axis=None, keepdims=False)#

Sum of all elements or along the given axis or axes.

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

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

Parameters:
  • axis (int | Sequence[int] | None) – Axis or axes to sum along, all elements are summed if None.

  • keepdims (bool) – Keep the summed dimensions with size 1 (only with axis).

Returns:

Value if all axes are summed, otherwise an array with reduced dimensions.

transpose(*axes)#

Permute the axes (reverse them if no axes are given).

Return type:

array[TNum]