Skip to content

collections

Modules:

Classes:

Functions:

ArrayPrimitiveTypeSpec dataclass

ArrayPrimitiveTypeSpec(
    name: str,
    type_id: int,
    dtype_token: int,
    element_size_bytes: int | None,
    buffer_view_compatible: bool,
)

NDArrayLayout dataclass

NDArrayLayout(
    shape: tuple[int, ...],
    strides: tuple[int, ...],
    offset_bytes: int = 0,
)

Represent the logical rank, shape, strides, and byte offset of one NDArray value without duplicating the lower-level storage machinery. attributes: shape: type: tuple[int, Ellipsis] strides: type: tuple[int, Ellipsis] offset_bytes: type: int

Attributes:

ndim property

ndim: int

NDArrayOrder

Bases: str, Enum

list_element_type

list_element_type(
    type_: DataType | None,
) -> DataType | None
Source code in src/irx/builtins/collections/list.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@typechecked
def list_element_type(type_: astx.DataType | None) -> astx.DataType | None:
    """
    title: Return one concrete list element type when available.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: astx.DataType | None
    """
    if not isinstance(type_, astx.ListType):
        return None
    if len(type_.element_types) != 1:
        return None
    element_type = type_.element_types[0]
    return element_type if isinstance(element_type, astx.DataType) else None

list_has_concrete_element_type

list_has_concrete_element_type(
    type_: DataType | None,
) -> bool
Source code in src/irx/builtins/collections/list.py
43
44
45
46
47
48
49
50
51
52
53
@typechecked
def list_has_concrete_element_type(type_: astx.DataType | None) -> bool:
    """
    title: Return whether one list type has a single concrete element type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return list_element_type(type_) is not None

ndarray_byte_bounds

ndarray_byte_bounds(
    layout: NDArrayLayout,
) -> tuple[int, int] | None

The result is relative to the underlying data pointer. None means the logical layout has zero extent and therefore addresses no elements. parameters: layout: type: NDArrayLayout returns: type: tuple[int, int] | None

Source code in src/irx/builtins/collections/array.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
@public
@typechecked
def ndarray_byte_bounds(
    layout: NDArrayLayout,
) -> tuple[int, int] | None:
    """
    title: Return the minimum and maximum element-start byte offsets.
    summary: >-
      The result is relative to the underlying data pointer. None means the
      logical layout has zero extent and therefore addresses no elements.
    parameters:
      layout:
        type: NDArrayLayout
    returns:
      type: tuple[int, int] | None
    """
    if ndarray_element_count(layout) == 0:
        return None

    minimum = layout.offset_bytes
    maximum = layout.offset_bytes

    for dim, stride in zip(layout.shape, layout.strides, strict=True):
        if dim <= 1:
            continue
        span = (dim - 1) * stride
        if span < 0:
            minimum += span
        else:
            maximum += span

    return minimum, maximum

ndarray_default_strides

ndarray_default_strides(
    shape: tuple[int, ...],
    item_size_bytes: int,
    *,
    order: NDArrayOrder = C,
) -> tuple[int, ...]
Source code in src/irx/builtins/collections/array.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@public
@typechecked
def ndarray_default_strides(
    shape: tuple[int, ...],
    item_size_bytes: int,
    *,
    order: NDArrayOrder = NDArrayOrder.C,
) -> tuple[int, ...]:
    """
    title: Return canonical byte strides for one contiguous NDArray shape.
    parameters:
      shape:
        type: tuple[int, Ellipsis]
      item_size_bytes:
        type: int
      order:
        type: NDArrayOrder
    returns:
      type: tuple[int, Ellipsis]
    """
    if item_size_bytes <= 0:
        raise ValueError("ndarray item_size_bytes must be positive")
    if not shape:
        return ()

    strides = [0] * len(shape)
    stride = item_size_bytes

    if order is NDArrayOrder.C:
        indices = range(len(shape) - 1, -1, -1)
    else:
        indices = range(len(shape))

    for axis in indices:
        strides[axis] = stride
        stride *= max(shape[axis], 1)

    return tuple(strides)

ndarray_element_count

ndarray_element_count(layout: NDArrayLayout) -> int
Source code in src/irx/builtins/collections/array.py
100
101
102
103
104
105
106
107
108
109
110
111
@public
@typechecked
def ndarray_element_count(layout: NDArrayLayout) -> int:
    """
    title: Return the logical element count for one layout.
    parameters:
      layout:
        type: NDArrayLayout
    returns:
      type: int
    """
    return _shape_extent(layout.shape)

ndarray_element_size_bytes

ndarray_element_size_bytes(
    type_: DataType | None,
) -> int | None
Source code in src/irx/builtins/collections/array.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
@public
@typechecked
def ndarray_element_size_bytes(type_: astx.DataType | None) -> int | None:
    """
    title: Return the byte width for one NDArray element type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: int | None
    """
    primitive_name = ndarray_primitive_type_name(type_)
    if primitive_name is None:
        return None
    spec = ARRAY_PRIMITIVE_TYPE_SPECS.get(primitive_name)
    if spec is None:
        return None
    return spec.element_size_bytes

ndarray_element_size_bytes_from_dtype

ndarray_element_size_bytes_from_dtype(
    dtype: BufferHandle,
) -> int | None
Source code in src/irx/builtins/collections/array.py
392
393
394
395
396
397
398
399
400
401
402
403
404
@typechecked
def ndarray_element_size_bytes_from_dtype(dtype: BufferHandle) -> int | None:
    """
    title: Return the byte width for one canonical primitive dtype handle.
    parameters:
      dtype:
        type: BufferHandle
    returns:
      type: int | None
    """
    if dtype.is_null:
        return None
    return _DTYPE_ELEMENT_SIZE_BYTES.get(dtype)

ndarray_is_c_contiguous

ndarray_is_c_contiguous(
    layout: NDArrayLayout, item_size_bytes: int
) -> bool
Source code in src/irx/builtins/collections/array.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@public
@typechecked
def ndarray_is_c_contiguous(
    layout: NDArrayLayout,
    item_size_bytes: int,
) -> bool:
    """
    title: Return whether one layout matches canonical C-order strides.
    parameters:
      layout:
        type: NDArrayLayout
      item_size_bytes:
        type: int
    returns:
      type: bool
    """
    return layout.strides == ndarray_default_strides(
        layout.shape,
        item_size_bytes,
        order=NDArrayOrder.C,
    )

ndarray_is_f_contiguous

ndarray_is_f_contiguous(
    layout: NDArrayLayout, item_size_bytes: int
) -> bool
Source code in src/irx/builtins/collections/array.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
@public
@typechecked
def ndarray_is_f_contiguous(
    layout: NDArrayLayout,
    item_size_bytes: int,
) -> bool:
    """
    title: Return whether one layout matches canonical Fortran-order strides.
    parameters:
      layout:
        type: NDArrayLayout
      item_size_bytes:
        type: int
    returns:
      type: bool
    """
    return layout.strides == ndarray_default_strides(
        layout.shape,
        item_size_bytes,
        order=NDArrayOrder.F,
    )

ndarray_primitive_type_name

ndarray_primitive_type_name(
    type_: DataType | None,
) -> str | None
Source code in src/irx/builtins/collections/array.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
@public
@typechecked
def ndarray_primitive_type_name(type_: astx.DataType | None) -> str | None:
    """
    title: Return the builtin primitive storage name for one NDArray element.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: str | None
    """
    if isinstance(type_, astx.Boolean):
        return "bool"
    if isinstance(type_, astx.Int8):
        return "int8"
    if isinstance(type_, astx.Int16):
        return "int16"
    if isinstance(type_, astx.Int32):
        return "int32"
    if isinstance(type_, astx.Int64):
        return "int64"
    if isinstance(type_, astx.UInt8):
        return "uint8"
    if isinstance(type_, astx.UInt16):
        return "uint16"
    if isinstance(type_, astx.UInt32):
        return "uint32"
    if isinstance(type_, astx.UInt64):
        return "uint64"
    if isinstance(type_, astx.Float32):
        return "float32"
    if isinstance(type_, astx.Float64):
        return "float64"
    return None

validate_ndarray_layout

validate_ndarray_layout(
    layout: NDArrayLayout,
) -> tuple[str, ...]
Source code in src/irx/builtins/collections/array.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
@public
@typechecked
def validate_ndarray_layout(
    layout: NDArrayLayout,
) -> tuple[str, ...]:
    """
    title: Validate one static NDArray layout.
    parameters:
      layout:
        type: NDArrayLayout
    returns:
      type: tuple[str, Ellipsis]
    """
    errors: list[str] = []

    if len(layout.strides) != layout.ndim:
        errors.append("ndarray stride length must match ndim")
    if any(dim < 0 for dim in layout.shape):
        errors.append("ndarray shape dimensions must be non-negative")
    if layout.offset_bytes < 0:
        errors.append("ndarray offset_bytes must be non-negative")

    return tuple(errors)