Skip to content

ordito.typing

Warp array type aliases, runtime rank checks, and typed vector builtins for Python wrappers.

Use these aliases in ordito Python APIs instead of wp.array2d[dtype], which static checkers treat as Warp annotation objects (no .shape / indexing).

Kernels should continue to use wp.array2d[dtype] in @wp.kernel signatures.

normalize, cross and transform_point are the Warp builtins of those names, re-exported unchanged so that a vector held in a variable resolves against them.

Functions:

Name Description
ensure_ndim

Validate the rank (and optionally the dtype) of a Warp array.

ensure_edge_pairs

Validate that arr is a rank-2 wp.int32 array with exactly two columns.

as_array2d

Validate and narrow a Warp array to the rank-2 alias for dtype.

as_array3d

Validate and narrow a Warp array to the rank-3 alias for dtype.

as_dense

Narrow a Python-scope index expression to the dense array a slice of one always is.

vec3_floats

Components of a Python-scope 3-vector, as Python floats.

has_blocks

Whether matrix is a sparse matrix whose blocks are dtype.

dtype_max

Largest representable value for a Warp scalar type.

dtype_min

Smallest representable value for a Warp scalar type.

dtype_zero

Zero value for a Warp scalar type, typed to match Python's int/float split.

sortable_dtype

Same-width dtype that warp.utils.radix_sort_pairs accepts, preserving dtype's order.

empty_1d

Allocate an uninitialized rank-1 Warp array of the given element type.

empty_2d

Allocate an uninitialized rank-2 Warp array of the given element type.

empty_3d

Allocate an uninitialized rank-3 Warp array of the given element type.

T module-attribute

T = TypeVar('T')

DType module-attribute

DType = TypeVar('DType')

NDim module-attribute

NDim = TypeVar('NDim', bound=int)

ArrayT module-attribute

ArrayT = TypeVar('ArrayT', bound='wp.array[Any, Any]')

Block module-attribute

Block = TypeVar('Block')

Kernel module-attribute

Kernel: TypeAlias = wp.Kernel | Callable[..., None]

CsrMatrix module-attribute

CsrMatrix: TypeAlias = (
    BsrMatrix[wp.float32] | BsrMatrix[wp.float64]
)

SparseMatrix module-attribute

SparseMatrix: TypeAlias = CsrMatrix | BsrMatrix[wp.mat22d]

Array1dInt32 module-attribute

Array1dInt32: TypeAlias = wp.array[wp.int32, Literal[1]]

Array1dFloat32 module-attribute

Array1dFloat32: TypeAlias = wp.array[wp.float32, Literal[1]]

Array1dFloat64 module-attribute

Array1dFloat64: TypeAlias = wp.array[wp.float64, Literal[1]]

Array2dInt32 module-attribute

Array2dInt32: TypeAlias = wp.array[wp.int32, Literal[2]]

Array2dFloat32 module-attribute

Array2dFloat32: TypeAlias = wp.array[wp.float32, Literal[2]]

Array2dFloat64 module-attribute

Array2dFloat64: TypeAlias = wp.array[wp.float64, Literal[2]]

Array2dVec3 module-attribute

Array2dVec3: TypeAlias = wp.array[wp.vec3, Literal[2]]

Array2dVec3d module-attribute

Array2dVec3d: TypeAlias = wp.array[wp.vec3d, Literal[2]]

Array3dFloat32 module-attribute

Array3dFloat32: TypeAlias = wp.array[wp.float32, Literal[3]]

Array3dFloat64 module-attribute

Array3dFloat64: TypeAlias = wp.array[wp.float64, Literal[3]]

Array3dBool module-attribute

Array3dBool: TypeAlias = wp.array[wp.bool, Literal[3]]

Array1dInt module-attribute

Array1dInt: TypeAlias = Array1dInt32

Array2dInt module-attribute

Array2dInt: TypeAlias = Array2dInt32

Array1dFloat module-attribute

Array1dFloat: TypeAlias = Array1dFloat32 | Array1dFloat64

Array2dFloat module-attribute

Array2dFloat: TypeAlias = Array2dFloat32 | Array2dFloat64

Array1dScalar module-attribute

Array1dScalar: TypeAlias = Array1dInt32 | Array1dFloat

Array2dScalar module-attribute

Array2dScalar: TypeAlias = Array2dInt32 | Array2dFloat

IntArray module-attribute

IntArray: TypeAlias = Array1dInt | Array2dInt

FloatArray module-attribute

FloatArray: TypeAlias = Array1dFloat | Array2dFloat

ScalarArray module-attribute

ScalarArray: TypeAlias = Array1dScalar | Array2dScalar

ArrayNdInt32 module-attribute

ArrayNdInt32: TypeAlias = wp.array[wp.int32, Any]

ArrayNd module-attribute

ArrayNd: TypeAlias = wp.array[Any, Any]

ArrayNdInt64 module-attribute

ArrayNdInt64: TypeAlias = wp.array[wp.int64, Any]

ArrayNdUInt32 module-attribute

ArrayNdUInt32: TypeAlias = wp.array[wp.uint32, Any]

ArrayNdUInt64 module-attribute

ArrayNdUInt64: TypeAlias = wp.array[wp.uint64, Any]

ArrayNdFloat32 module-attribute

ArrayNdFloat32: TypeAlias = wp.array[wp.float32, Any]

ArrayNdFloat64 module-attribute

ArrayNdFloat64: TypeAlias = wp.array[wp.float64, Any]

ArrayNdFloat module-attribute

ArrayNdFloat: TypeAlias = ArrayNdFloat32 | ArrayNdFloat64

ArrayNdInt module-attribute

ArrayNdInt: TypeAlias = (
    ArrayNdInt32
    | ArrayNdInt64
    | ArrayNdUInt32
    | ArrayNdUInt64
)

ArrayNdScalar module-attribute

ArrayNdScalar: TypeAlias = ArrayNdInt | ArrayNdFloat

SortableDType module-attribute

SortableDType: TypeAlias = (
    type[wp.int32]
    | type[wp.int64]
    | type[wp.uint32]
    | type[wp.uint64]
    | type[wp.float32]
    | type[wp.float64]
)

normalize module-attribute

normalize = cast('_Normalize', wp.normalize)

cross module-attribute

cross = cast('_Cross', wp.cross)

dot module-attribute

dot = cast('_Dot', wp.dot)

transform_point module-attribute

transform_point = cast(
    "_TransformPoint", wp.transform_point
)

bsr_matrix_t module-attribute

bsr_matrix_t = cast('_BsrMatrixT', wps.bsr_matrix_t)

bsr_diag module-attribute

bsr_diag = cast('_BsrDiag', wps.bsr_diag)

bsr_identity module-attribute

bsr_identity = cast('_BsrIdentity', wps.bsr_identity)

bsr_axpy module-attribute

bsr_axpy = cast('_BsrAxpy', wps.bsr_axpy)

bsr_mm module-attribute

bsr_mm = cast('_BsrMm', wps.bsr_mm)

bsr_mv module-attribute

bsr_mv = cast('_BsrMv', wps.bsr_mv)

bsr_copy module-attribute

bsr_copy = cast('_BsrUnary', wps.bsr_copy)

bsr_transposed module-attribute

bsr_transposed = cast('_BsrUnary', wps.bsr_transposed)

bsr_get_diag module-attribute

bsr_get_diag = cast('_BsrGetDiag', wps.bsr_get_diag)

bsr_compress module-attribute

bsr_compress = cast('_BsrCompress', wps.bsr_compress)

__all__ module-attribute

__all__ = [
    "Array1dFloat",
    "Array1dFloat32",
    "Array1dFloat64",
    "Array1dInt",
    "Array1dInt32",
    "Array1dScalar",
    "Array2dFloat",
    "Array2dFloat32",
    "Array2dFloat64",
    "Array2dInt",
    "Array2dInt32",
    "Array2dScalar",
    "Array2dVec3",
    "Array3dBool",
    "Array3dFloat32",
    "Array3dFloat64",
    "ArrayNd",
    "ArrayNdFloat",
    "ArrayNdFloat32",
    "ArrayNdFloat64",
    "ArrayNdInt",
    "ArrayNdInt32",
    "ArrayNdInt64",
    "ArrayNdScalar",
    "ArrayNdUInt32",
    "ArrayNdUInt64",
    "CsrMatrix",
    "FloatArray",
    "IntArray",
    "Kernel",
    "ScalarArray",
    "SortableDType",
    "SparseMatrix",
    "as_array2d",
    "as_array3d",
    "as_dense",
    "dtype_max",
    "dtype_min",
    "dtype_zero",
    "empty_1d",
    "empty_2d",
    "empty_3d",
    "ensure_edge_pairs",
    "ensure_ndim",
    "has_blocks",
    "sortable_dtype",
    "vec3_floats",
]

BsrMatrix

Bases: BsrMatrix[Block]

nrow instance-attribute

nrow: int

ncol instance-attribute

ncol: int

nnz instance-attribute

nnz: int

offsets instance-attribute

offsets: array[int32]

columns instance-attribute

columns: array[int32]

row_counts instance-attribute

row_counts: array[int32] | None

values instance-attribute

values: array[Block]

ensure_ndim

ensure_ndim(
    arr: ArrayT, ndim: int, *, dtype: type | None = None
) -> ArrayT

Validate the rank (and optionally the dtype) of a Warp array.

The runtime counterpart to the aliases in this module: isinstance(x, wp.array2d) is always False, so rank is checked here instead.

Parameters:

Name Type Description Default
arr ArrayT

Array to validate.

required
ndim int

Required rank.

required
dtype type | None

When given, the required element dtype.

None

Returns:

Type Description
array

arr unchanged, so this can wrap an argument in place.

Raises:

Type Description
TypeError

If the rank or dtype does not match.

Examples:

import warp as wp

import ordito.typing as odt

points = wp.zeros((4, 3), dtype=wp.float32)
print(odt.ensure_ndim(points, 2, dtype=wp.float32).shape)  # (4, 3)
try:
    odt.ensure_ndim(points, 1)
except TypeError as error:
    message = str(error)
print(message)  # expected 1D array, got ndim=2
Source code in ordito/typing.py
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
def ensure_ndim(arr: ArrayT, ndim: int, *, dtype: type | None = None) -> ArrayT:
    """
    Validate the rank (and optionally the dtype) of a Warp array.

    The runtime counterpart to the aliases in this module: `isinstance(x, wp.array2d)` is always
    ``False``, so rank is checked here instead.

    Parameters
    ----------
    arr
        Array to validate.
    ndim
        Required rank.
    dtype
        When given, the required element dtype.

    Returns
    -------
    wp.array
        ``arr`` unchanged, so this can wrap an argument in place.

    Raises
    ------
    TypeError
        If the rank or dtype does not match.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    points = wp.zeros((4, 3), dtype=wp.float32)
    print(odt.ensure_ndim(points, 2, dtype=wp.float32).shape)  # (4, 3)
    try:
        odt.ensure_ndim(points, 1)
    except TypeError as error:
        message = str(error)
    print(message)  # expected 1D array, got ndim=2
    ```
    """
    if int(arr.ndim) != ndim:
        raise TypeError(f"expected {ndim}D array, got ndim={arr.ndim}")
    if dtype is not None and arr.dtype != dtype:
        raise TypeError(f"expected dtype {dtype}, got {arr.dtype}")
    return arr

ensure_edge_pairs

ensure_edge_pairs(arr: array[T, Any], name: str) -> None

Validate that arr is a rank-2 wp.int32 array with exactly two columns.

The shared check every (k, 2) edge-list argument in the package runs before use.

Parameters:

Name Type Description Default
arr array[T, Any]

Array to validate.

required
name str

Parameter name to use in the raised message.

required

Raises:

Type Description
TypeError

If arr is not a rank-2 wp.int32 array.

ValueError

If arr does not have exactly two columns.

Examples:

import warp as wp

import ordito.typing as odt

edges = wp.array([[0, 1], [1, 2], [2, 0]], dtype=wp.int32)
odt.ensure_edge_pairs(edges, "edges")  # passes silently
try:
    odt.ensure_edge_pairs(wp.zeros((3, 3), dtype=wp.int32), "edges")
except ValueError as error:
    message = str(error)
print(message)  # edges must have shape (k, 2), got (3, 3)
Source code in ordito/typing.py
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
def ensure_edge_pairs(arr: wp.array[T, Any], name: str) -> None:
    """
    Validate that ``arr`` is a rank-2 ``wp.int32`` array with exactly two columns.

    The shared check every ``(k, 2)`` edge-list argument in the package runs before use.

    Parameters
    ----------
    arr
        Array to validate.
    name
        Parameter name to use in the raised message.

    Raises
    ------
    TypeError
        If ``arr`` is not a rank-2 ``wp.int32`` array.
    ValueError
        If ``arr`` does not have exactly two columns.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    edges = wp.array([[0, 1], [1, 2], [2, 0]], dtype=wp.int32)
    odt.ensure_edge_pairs(edges, "edges")  # passes silently
    try:
        odt.ensure_edge_pairs(wp.zeros((3, 3), dtype=wp.int32), "edges")
    except ValueError as error:
        message = str(error)
    print(message)  # edges must have shape (k, 2), got (3, 3)
    ```
    """
    ensure_ndim(arr, 2, dtype=wp.int32)
    if int(arr.shape[1]) != 2:
        raise ValueError(f"{name} must have shape (k, 2), got {arr.shape}")

as_array2d

as_array2d(
    arr: array[T, Any], dtype: type[DType]
) -> wp.array[DType, Literal[2]]

Validate and narrow a Warp array to the rank-2 alias for dtype.

One function rather than a per-dtype family: the dtype argument carries the element type into the return, so a call site keeps the narrow type it had -- as_array2d(x, wp.int32) is an Array2dInt32, not a union.

Parameters:

Name Type Description Default
arr array[T, Any]

Warp array expected to be rank-2 with element type dtype.

required
dtype type[DType]

Expected element type.

required

Returns:

Type Description
array

arr unchanged, narrowed to wp.array[dtype, Literal[2]].

Raises:

Type Description
TypeError

If arr is not rank-2 with element type dtype.

Examples:

import warp as wp

import ordito.typing as odt

table = odt.as_array2d(wp.zeros((5, 2), dtype=wp.int32), wp.int32)
print(table.shape)  # (5, 2)
try:
    odt.as_array2d(wp.zeros(5, dtype=wp.int32), wp.int32)
except TypeError as error:
    message = str(error)
print(message)  # expected 2D array, got ndim=1
Source code in ordito/typing.py
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
def as_array2d(arr: wp.array[T, Any], dtype: type[DType]) -> wp.array[DType, Literal[2]]:
    """
    Validate and narrow a Warp array to the rank-2 alias for ``dtype``.

    One function rather than a per-dtype family: the ``dtype`` argument carries the element type
    into the return, so a call site keeps the narrow type it had -- ``as_array2d(x, wp.int32)`` is
    an [`Array2dInt32`][ordito.typing.Array2dInt32], not a union.

    Parameters
    ----------
    arr
        Warp array expected to be rank-2 with element type ``dtype``.
    dtype
        Expected element type.

    Returns
    -------
    wp.array
        ``arr`` unchanged, narrowed to ``wp.array[dtype, Literal[2]]``.

    Raises
    ------
    TypeError
        If ``arr`` is not rank-2 with element type ``dtype``.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    table = odt.as_array2d(wp.zeros((5, 2), dtype=wp.int32), wp.int32)
    print(table.shape)  # (5, 2)
    try:
        odt.as_array2d(wp.zeros(5, dtype=wp.int32), wp.int32)
    except TypeError as error:
        message = str(error)
    print(message)  # expected 2D array, got ndim=1
    ```
    """
    return _as_ranked(arr, 2, dtype)

as_array3d

as_array3d(
    arr: array[T, Any], dtype: type[DType]
) -> wp.array[DType, Literal[3]]

Validate and narrow a Warp array to the rank-3 alias for dtype.

The rank-3 counterpart of as_array2d.

Parameters:

Name Type Description Default
arr array[T, Any]

Warp array expected to be rank-3 with scalar type dtype.

required
dtype type[DType]

Expected scalar type.

required

Returns:

Type Description
array

arr unchanged, narrowed to wp.array[dtype, Literal[3]].

Raises:

Type Description
TypeError

If arr is not rank-3 with scalar type dtype.

Examples:

import warp as wp

import ordito.typing as odt

grid = odt.as_array3d(wp.zeros((2, 3, 4), dtype=wp.float32), wp.float32)
print(grid.shape)  # (2, 3, 4)
Source code in ordito/typing.py
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
def as_array3d(arr: wp.array[T, Any], dtype: type[DType]) -> wp.array[DType, Literal[3]]:
    """
    Validate and narrow a Warp array to the rank-3 alias for ``dtype``.

    The rank-3 counterpart of [`as_array2d`][ordito.typing.as_array2d].

    Parameters
    ----------
    arr
        Warp array expected to be rank-3 with scalar type ``dtype``.
    dtype
        Expected scalar type.

    Returns
    -------
    wp.array
        ``arr`` unchanged, narrowed to ``wp.array[dtype, Literal[3]]``.

    Raises
    ------
    TypeError
        If ``arr`` is not rank-3 with scalar type ``dtype``.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    grid = odt.as_array3d(wp.zeros((2, 3, 4), dtype=wp.float32), wp.float32)
    print(grid.shape)  # (2, 3, 4)
    ```
    """
    return _as_ranked(arr, 3, dtype)

as_dense

as_dense(
    view: array[DType, NDim] | indexedarray[DType, NDim],
) -> wp.array[DType, NDim]

Narrow a Python-scope index expression to the dense array a slice of one always is.

wp.array.__getitem__ carries no annotations, so basedpyright infers its return from the two branches of the body and every subscript comes back as indexedarray | array -- including a plain slice, which can only ever produce the dense arm (an indexedarray is what an integer-array key yields, and a slice is not one). This narrows the union back, and unlike a bare cast it checks: wp.indexedarray is not a subclass of wp.array, so the isinstance genuinely discriminates rather than restating the assumption.

Use it on a slice. A gather (src[indices]) is an indexedarray by design and is materialized with wp.copy instead, not narrowed here.

Parameters:

Name Type Description Default
view array[DType, NDim] | indexedarray[DType, NDim]

Result of a Python-scope subscript of a wp.array.

required

Returns:

Type Description
array

view unchanged, typed as the dense array it is.

Raises:

Type Description
TypeError

If view is a wp.indexedarray -- that is, if the subscript was a gather rather than a slice.

See Also

as_array2d ordito.array.gather

Examples:

A slice narrows to a dense array; a gather is refused:

import warp as wp

import ordito.typing as odt

values = wp.zeros(10, dtype=wp.float32)
print(odt.as_dense(values[2:5]).shape)  # (3,)
try:
    odt.as_dense(values[wp.array([0, 2], dtype=wp.int32)])
except TypeError as error:
    message = str(error)
print(message)  # expected a dense wp.array slice, got indexedarray
Source code in ordito/typing.py
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
def as_dense(view: wp.array[DType, NDim] | wp.indexedarray[DType, NDim]) -> wp.array[DType, NDim]:
    """
    Narrow a Python-scope index expression to the dense array a slice of one always is.

    ``wp.array.__getitem__`` carries no annotations, so basedpyright infers its return from the two
    branches of the body and every subscript comes back as ``indexedarray | array`` -- including a
    plain slice, which can only ever produce the dense arm (an ``indexedarray`` is what an
    *integer-array* key yields, and a slice is not one). This narrows the union back, and unlike a
    bare ``cast`` it checks: ``wp.indexedarray`` is not a subclass of ``wp.array``, so the
    ``isinstance`` genuinely discriminates rather than restating the assumption.

    Use it on a slice. A gather (``src[indices]``) is an ``indexedarray`` by design and is
    materialized with ``wp.copy`` instead, not narrowed here.

    Parameters
    ----------
    view
        Result of a Python-scope subscript of a ``wp.array``.

    Returns
    -------
    wp.array
        ``view`` unchanged, typed as the dense array it is.

    Raises
    ------
    TypeError
        If ``view`` is a ``wp.indexedarray`` -- that is, if the subscript was a gather rather than
        a slice.

    See Also
    --------
    [`as_array2d`][ordito.typing.as_array2d]
    [`ordito.array.gather`][]

    Examples
    --------
    A slice narrows to a dense array; a gather is refused:

    ```python
    import warp as wp

    import ordito.typing as odt

    values = wp.zeros(10, dtype=wp.float32)
    print(odt.as_dense(values[2:5]).shape)  # (3,)
    try:
        odt.as_dense(values[wp.array([0, 2], dtype=wp.int32)])
    except TypeError as error:
        message = str(error)
    print(message)  # expected a dense wp.array slice, got indexedarray
    ```
    """
    if not isinstance(view, wp.array):
        raise TypeError(f"expected a dense wp.array slice, got {type(view).__name__}")
    return view

vec3_floats

vec3_floats(v: vec3) -> tuple[float, float, float]

Components of a Python-scope 3-vector, as Python floats.

At Python scope indexing a wp.vec3 already returns a native float, but Warp's stubs type the index as vec_t | bool | float32 | int32, so arithmetic on two components does not type-check. This names that fact once for host-side bounds arithmetic.

Parameters:

Name Type Description Default
v vec3

Vector to unpack.

required

Returns:

Type Description
tuple[float, float, float]

(v[0], v[1], v[2]).

Examples:

import warp as wp

import ordito.typing as odt

x, y, z = odt.vec3_floats(wp.vec3(1.0, 2.0, 3.5))
print(x + y + z)  # 6.5
Source code in ordito/typing.py
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
def vec3_floats(v: wp.vec3) -> tuple[float, float, float]:
    """
    Components of a Python-scope 3-vector, as Python floats.

    At Python scope indexing a ``wp.vec3`` already returns a native ``float``, but
    Warp's stubs type the index as ``vec_t | bool | float32 | int32``, so arithmetic on two
    components does not type-check. This names that fact once for host-side bounds arithmetic.

    Parameters
    ----------
    v
        Vector to unpack.

    Returns
    -------
    tuple[float, float, float]
        ``(v[0], v[1], v[2])``.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    x, y, z = odt.vec3_floats(wp.vec3(1.0, 2.0, 3.5))
    print(x + y + z)  # 6.5
    ```
    """
    return cast("tuple[float, float, float]", (v[0], v[1], v[2]))

has_blocks

has_blocks(
    matrix: object, dtype: type[Block]
) -> TypeIs[BsrMatrix[Block]]

Whether matrix is a sparse matrix whose blocks are dtype.

The runtime test a solver branches on (wp.float32 / wp.float64 CSR, wp.mat22d blocks), and it narrows a SparseMatrix on both branches: to BsrMatrix[dtype] where it holds and to the remaining block types where it does not.

Parameters:

Name Type Description Default
matrix object

Value to test.

required
dtype type[Block]

Block type to test for: the element dtype of matrix.values.

required

Returns:

Type Description
bool

True if matrix is a warp.sparse matrix and matrix.values.dtype is dtype.

Examples:

import warp as wp
import warp.sparse as wps

import ordito.typing as odt

matrix = wps.bsr_zeros(3, 3, wp.float64)
print(odt.has_blocks(matrix, wp.float64))  # True
print(odt.has_blocks(matrix, wp.float32))  # False
Source code in ordito/typing.py
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
def has_blocks(matrix: object, dtype: type[Block]) -> TypeIs[BsrMatrix[Block]]:
    """
    Whether ``matrix`` is a sparse matrix whose blocks are ``dtype``.

    The runtime test a solver branches on (``wp.float32`` / ``wp.float64`` CSR, ``wp.mat22d``
    blocks), and it narrows a [`SparseMatrix`][ordito.typing.SparseMatrix] on both branches:
    to ``BsrMatrix[dtype]`` where it holds and to the remaining block types where it does not.

    Parameters
    ----------
    matrix
        Value to test.
    dtype
        Block type to test for: the element dtype of ``matrix.values``.

    Returns
    -------
    bool
        ``True`` if ``matrix`` is a ``warp.sparse`` matrix and ``matrix.values.dtype`` is
        ``dtype``.

    Examples
    --------
    ```python
    import warp as wp
    import warp.sparse as wps

    import ordito.typing as odt

    matrix = wps.bsr_zeros(3, 3, wp.float64)
    print(odt.has_blocks(matrix, wp.float64))  # True
    print(odt.has_blocks(matrix, wp.float32))  # False
    ```
    """
    return isinstance(matrix, BsrMatrix) and matrix.values.dtype == dtype

dtype_max

dtype_max(dtype: type[Int]) -> int
dtype_max(dtype: type[Float]) -> float
dtype_max(dtype: type[Scalar]) -> int | float

Largest representable value for a Warp scalar type.

Parameters:

Name Type Description Default
dtype type[Scalar]

A Warp integer or floating-point scalar type.

required

Returns:

Type Description
int | float

float("inf") for floating-point types; the maximum representable integer for integer types.

Examples:

import warp as wp

import ordito.typing as odt

print(odt.dtype_max(wp.int32))  # 2147483647
print(odt.dtype_max(wp.uint8))  # 255
print(odt.dtype_max(wp.float32))  # inf
Source code in ordito/typing.py
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
def dtype_max(dtype: type[wp.Scalar]) -> int | float:
    """
    Largest representable value for a Warp scalar type.

    Parameters
    ----------
    dtype
        A Warp integer or floating-point scalar type.

    Returns
    -------
    int | float
        ``float("inf")`` for floating-point types; the maximum representable
        integer for integer types.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    print(odt.dtype_max(wp.int32))  # 2147483647
    print(odt.dtype_max(wp.uint8))  # 255
    print(odt.dtype_max(wp.float32))  # inf
    ```
    """
    if not wp.types.type_is_int(dtype):
        return float("inf")
    bits = wp.types.type_size_in_bytes(dtype) * 8
    if not dtype.__name__.lower().startswith("u"):
        bits -= 1
    return (1 << bits) - 1

dtype_min

dtype_min(dtype: type[Int]) -> int
dtype_min(dtype: type[Float]) -> float
dtype_min(dtype: type[Scalar]) -> int | float

Smallest representable value for a Warp scalar type.

Parameters:

Name Type Description Default
dtype type[Scalar]

A Warp integer or floating-point scalar type.

required

Returns:

Type Description
int | float

float("-inf") for floating-point types; the minimum representable integer for integer types.

Examples:

import warp as wp

import ordito.typing as odt

print(odt.dtype_min(wp.int32))  # -2147483648
print(odt.dtype_min(wp.uint32))  # 0
print(odt.dtype_min(wp.float64))  # -inf
Source code in ordito/typing.py
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
def dtype_min(dtype: type[wp.Scalar]) -> int | float:
    """
    Smallest representable value for a Warp scalar type.

    Parameters
    ----------
    dtype
        A Warp integer or floating-point scalar type.

    Returns
    -------
    int | float
        ``float("-inf")`` for floating-point types; the minimum representable
        integer for integer types.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    print(odt.dtype_min(wp.int32))  # -2147483648
    print(odt.dtype_min(wp.uint32))  # 0
    print(odt.dtype_min(wp.float64))  # -inf
    ```
    """
    if not wp.types.type_is_int(dtype):
        return float("-inf")
    bits = wp.types.type_size_in_bytes(dtype) * 8
    if dtype.__name__.lower().startswith("u"):
        return 0
    return -(1 << (bits - 1))

dtype_zero

dtype_zero(dtype: type[Scalar]) -> int | float

Zero value for a Warp scalar type, typed to match Python's int/float split.

Parameters:

Name Type Description Default
dtype type[Scalar]

A Warp integer or floating-point scalar type.

required

Returns:

Type Description
int | float

0 for integer types, 0.0 for floating-point types.

Examples:

import warp as wp

import ordito.typing as odt

print(odt.dtype_zero(wp.int64))  # 0
print(odt.dtype_zero(wp.float32))  # 0.0
Source code in ordito/typing.py
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
def dtype_zero(dtype: type[wp.Scalar]) -> int | float:
    """
    Zero value for a Warp scalar type, typed to match Python's ``int``/``float`` split.

    Parameters
    ----------
    dtype
        A Warp integer or floating-point scalar type.

    Returns
    -------
    int | float
        ``0`` for integer types, ``0.0`` for floating-point types.

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    print(odt.dtype_zero(wp.int64))  # 0
    print(odt.dtype_zero(wp.float32))  # 0.0
    ```
    """
    if wp.types.type_is_int(dtype):
        return 0
    return 0.0

sortable_dtype

sortable_dtype(dtype: type[Scalar]) -> SortableDType

Same-width dtype that warp.utils.radix_sort_pairs accepts, preserving dtype's order.

The single widening rule for every radix sort in this package. Sorting a sub-32-bit dtype is not supported by Warp, and sorting the reinterpreted bits of a float or an unsigned value gets the order wrong, so callers ask here rather than widening ad hoc.

The hash table works in one common signed-integer key space (see bitcast_to_int), which is fine for equality but wrong for ordering: negative floats have descending bit patterns, and a uint64 with its top bit set reads as a negative int64. Warp sorts int32 / int64 / uint32 / uint64 / float32 / float64 keys directly, so the sort is done in this dtype instead of on the reinterpreted bits. Every narrower width -- int8 / uint8 / int16 / uint16 / float16 -- raises Unsupported keys and values data types, so the widening table below has a case for each of them.

Parameters:

Name Type Description Default
dtype type[Scalar]

Any Warp scalar dtype.

required

Returns:

Type Description
SortableDType

dtype itself when Warp can already sort it, otherwise the narrowest same-signedness, same-kind dtype it can (float32 / float64, uint32 / uint64, int32 / int64), chosen by whether dtype is wider than four bytes.

See Also

sort_and_argsort bitcast_to_int

Examples:

Narrow types widen to 32 bits of the same kind; sortable types come back unchanged:

import warp as wp

import ordito.typing as odt

print(odt.sortable_dtype(wp.int8).__name__)  # int32
print(odt.sortable_dtype(wp.float16).__name__)  # float32
print(odt.sortable_dtype(wp.uint64).__name__)  # uint64
Source code in ordito/typing.py
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
def sortable_dtype(dtype: type[wp.Scalar]) -> SortableDType:
    """
    Same-width dtype that ``warp.utils.radix_sort_pairs`` accepts, preserving ``dtype``'s order.

    The single widening rule for every radix sort in this package. Sorting a sub-32-bit dtype is
    not supported by Warp, and sorting the reinterpreted *bits* of a float or an unsigned value
    gets the order wrong, so callers ask here rather than widening ad hoc.

    The hash table works in one common signed-integer key space (see
    [`bitcast_to_int`][ordito.array.bitcast_to_int]), which is fine for equality but wrong for
    ordering: negative floats have descending bit patterns, and a ``uint64`` with its top bit set
    reads as a negative ``int64``. Warp sorts ``int32`` / ``int64`` / ``uint32`` / ``uint64`` /
    ``float32`` / ``float64`` keys directly, so the sort is done in this dtype instead of on the
    reinterpreted bits. Every narrower width -- ``int8`` / ``uint8`` / ``int16`` / ``uint16`` /
    ``float16`` -- raises ``Unsupported keys and values data types``, so the widening table below
    has a case for each of them.

    Parameters
    ----------
    dtype
        Any Warp scalar dtype.

    Returns
    -------
    SortableDType
        ``dtype`` itself when Warp can already sort it, otherwise the narrowest same-signedness,
        same-kind dtype it can (``float32`` / ``float64``, ``uint32`` / ``uint64``, ``int32`` /
        ``int64``), chosen by whether ``dtype`` is wider than four bytes.

    See Also
    --------
    [`sort_and_argsort`][ordito.array.sort_and_argsort]
    [`bitcast_to_int`][ordito.array.bitcast_to_int]

    Examples
    --------
    Narrow types widen to 32 bits of the same kind; sortable types come back unchanged:

    ```python
    import warp as wp

    import ordito.typing as odt

    print(odt.sortable_dtype(wp.int8).__name__)  # int32
    print(odt.sortable_dtype(wp.float16).__name__)  # float32
    print(odt.sortable_dtype(wp.uint64).__name__)  # uint64
    ```
    """
    wide = wp.types.type_size_in_bytes(dtype) > 4
    if wp.types.type_is_float(dtype):
        return wp.float64 if wide else wp.float32
    if dtype.__name__.lower().startswith("u"):
        return wp.uint64 if wide else wp.uint32
    return wp.int64 if wide else wp.int32

empty_1d

empty_1d(
    n: int, dtype: type[DType], *, device: DeviceLike = None
) -> wp.array[DType, Literal[1]]

Allocate an uninitialized rank-1 Warp array of the given element type.

The rank-1 member of the empty_* family, and the checked replacement for cast(odt.Array1dFloat32, wp.empty(n, dtype=wp.float32, device=...)). wp.empty is annotated as returning warp.array -- element type unknown and rank unspecified -- and the rank is what a caller cannot recover afterwards: NDim is invariant, so wp.array[wp.float32] and Array1dFloat32 are not assignable to one another in either direction. A cast is an unchecked assertion of both; this fixes the rank by construction and carries dtype through to the return.

Prefer plain wp.empty where the surrounding annotations are wp.array[dtype], which is this package's usual rank-1 spelling and which wp.empty already satisfies -- the aliases are for the modules that carry the rank in their signatures (ordito.reduce, ordito.metrics, ordito.neighbors).

Parameters:

Name Type Description Default
n int

Length of the allocated array.

required
dtype type[DType]

Element type. Any Warp scalar, vector or matrix type; a call site holding a runtime dtype gets wp.array[Unknown, Literal[1]], which still carries the rank.

required
device DeviceLike

Target Warp device.

None

Returns:

Type Description
array

(n,) uninitialized array of element type dtype on device.

See Also

empty_2d empty_3d

Examples:

import warp as wp

import ordito.typing as odt

weights = odt.empty_1d(5, wp.float32)
print(weights.shape, weights.ndim)  # (5,) 1
Source code in ordito/typing.py
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
def empty_1d(
    n: int, dtype: type[DType], *, device: wp.DeviceLike = None
) -> wp.array[DType, Literal[1]]:
    """
    Allocate an uninitialized rank-1 Warp array of the given element type.

    The rank-1 member of the ``empty_*`` family, and the checked replacement for
    ``cast(odt.Array1dFloat32, wp.empty(n, dtype=wp.float32, device=...))``. ``wp.empty`` is
    annotated as returning ``warp.array`` -- element type unknown and rank unspecified -- and the
    rank is what a caller cannot recover afterwards: ``NDim`` is invariant, so
    ``wp.array[wp.float32]`` and [`Array1dFloat32`][ordito.typing.Array1dFloat32] are not
    assignable to one another in either direction. A ``cast`` is an unchecked assertion of both;
    this fixes the rank by construction and carries ``dtype`` through to the return.

    Prefer plain ``wp.empty`` where the surrounding annotations are ``wp.array[dtype]``, which is
    this package's usual rank-1 spelling and which ``wp.empty`` already satisfies -- the aliases
    are for the modules that carry the rank in their signatures ([`ordito.reduce`][],
    [`ordito.metrics`][], [`ordito.neighbors`][]).

    Parameters
    ----------
    n
        Length of the allocated array.
    dtype
        Element type. Any Warp scalar, vector or matrix type; a call site holding a runtime
        ``dtype`` gets ``wp.array[Unknown, Literal[1]]``, which still carries the rank.
    device
        Target Warp device.

    Returns
    -------
    wp.array
        ``(n,)`` uninitialized array of element type ``dtype`` on ``device``.

    See Also
    --------
    [`empty_2d`][ordito.typing.empty_2d]
    [`empty_3d`][ordito.typing.empty_3d]

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    weights = odt.empty_1d(5, wp.float32)
    print(weights.shape, weights.ndim)  # (5,) 1
    ```
    """
    return _empty_ranked((n,), 1, dtype, device)

empty_2d

empty_2d(
    shape: tuple[int, int] | list[int],
    dtype: type[DType],
    *,
    device: DeviceLike = None,
) -> wp.array[DType, Literal[2]]

Allocate an uninitialized rank-2 Warp array of the given element type.

The rank-2 member of the empty_* family; see empty_1d for why the rank has to be fixed at the allocation rather than asserted afterwards.

Parameters:

Name Type Description Default
shape tuple[int, int] | list[int]

(rows, cols) shape of the allocated array.

required
dtype type[DType]

Element type. wp.vec3 is what an (m, 2) table of segment endpoints wants -- ordito.intersection's two public segment returns are the callers.

required
device DeviceLike

Target Warp device.

None

Returns:

Type Description
array

(rows, cols) uninitialized array of element type dtype on device.

Raises:

Type Description
ValueError

If shape does not have length 2.

See Also

empty_1d empty_3d

Examples:

import warp as wp

import ordito.typing as odt

segments = odt.empty_2d((4, 2), wp.vec3)
print(segments.shape, segments.ndim)  # (4, 2) 2
Source code in ordito/typing.py
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
def empty_2d(
    shape: tuple[int, int] | list[int], dtype: type[DType], *, device: wp.DeviceLike = None
) -> wp.array[DType, Literal[2]]:
    """
    Allocate an uninitialized rank-2 Warp array of the given element type.

    The rank-2 member of the ``empty_*`` family; see [`empty_1d`][ordito.typing.empty_1d] for why
    the rank has to be fixed at the allocation rather than asserted afterwards.

    Parameters
    ----------
    shape
        ``(rows, cols)`` shape of the allocated array.
    dtype
        Element type. ``wp.vec3`` is what an ``(m, 2)`` table of segment endpoints wants --
        ``ordito.intersection``'s two public segment returns are the callers.
    device
        Target Warp device.

    Returns
    -------
    wp.array
        ``(rows, cols)`` uninitialized array of element type ``dtype`` on ``device``.

    Raises
    ------
    ValueError
        If ``shape`` does not have length 2.

    See Also
    --------
    [`empty_1d`][ordito.typing.empty_1d]
    [`empty_3d`][ordito.typing.empty_3d]

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    segments = odt.empty_2d((4, 2), wp.vec3)
    print(segments.shape, segments.ndim)  # (4, 2) 2
    ```
    """
    return _empty_ranked(shape, 2, dtype, device)

empty_3d

empty_3d(
    shape: tuple[int, int, int] | list[int],
    dtype: type[DType],
    *,
    device: DeviceLike = None,
) -> wp.array[DType, Literal[3]]

Allocate an uninitialized rank-3 Warp array of the given element type.

The rank-3 member of the empty_* family; see empty_1d for why the rank has to be fixed at the allocation rather than asserted afterwards.

Parameters:

Name Type Description Default
shape tuple[int, int, int] | list[int]

(nx, ny, nz) shape of the allocated array.

required
dtype type[DType]

Element type.

required
device DeviceLike

Target Warp device.

None

Returns:

Type Description
array

(nx, ny, nz) uninitialized array of element type dtype on device.

Raises:

Type Description
ValueError

If shape does not have length 3.

See Also

empty_1d empty_2d

Examples:

import warp as wp

import ordito.typing as odt

field = odt.empty_3d((2, 3, 4), wp.float32)
print(field.shape, field.ndim)  # (2, 3, 4) 3
Source code in ordito/typing.py
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
def empty_3d(
    shape: tuple[int, int, int] | list[int], dtype: type[DType], *, device: wp.DeviceLike = None
) -> wp.array[DType, Literal[3]]:
    """
    Allocate an uninitialized rank-3 Warp array of the given element type.

    The rank-3 member of the ``empty_*`` family; see [`empty_1d`][ordito.typing.empty_1d] for why
    the rank has to be fixed at the allocation rather than asserted afterwards.

    Parameters
    ----------
    shape
        ``(nx, ny, nz)`` shape of the allocated array.
    dtype
        Element type.
    device
        Target Warp device.

    Returns
    -------
    wp.array
        ``(nx, ny, nz)`` uninitialized array of element type ``dtype`` on ``device``.

    Raises
    ------
    ValueError
        If ``shape`` does not have length 3.

    See Also
    --------
    [`empty_1d`][ordito.typing.empty_1d]
    [`empty_2d`][ordito.typing.empty_2d]

    Examples
    --------
    ```python
    import warp as wp

    import ordito.typing as odt

    field = odt.empty_3d((2, 3, 4), wp.float32)
    print(field.shape, field.ndim)  # (2, 3, 4) 3
    ```
    """
    return _empty_ranked(shape, 3, dtype, device)