Skip to content

reduce

triwarp.reduce

Global reductions on Warp arrays.

min

min(
    array: Array1dInt32 | Array2dInt32, *, axis: None = ...
) -> int
min(
    array: Array1dFloat | Array2dFloat, *, axis: None = ...
) -> float
min(
    array: Array2dScalar, *, axis: Literal[0, 1]
) -> twt.Array1dScalar
min(
    array: ScalarArray, *, axis: Literal[0, 1] | None = None
) -> float | int | twt.Array1dScalar

Minimum of array.

With axis=None (default), reduces every element to one Python scalar using tiled kernels (wp.tile_load + wp.tile_min + wp.atomic_min).

With axis=0 or axis=1 on a rank-2 input, reduces along that axis to a 1D wp.array of the same dtype — one plain thread per output element when the reduced extent is narrower than a tile, a tiled block reduction per output otherwise.

Parameters:

Name Type Description Default
array ScalarArray

Rank-1 (n,) or rank-2 (n, m) scalar Warp array. Must be non-empty.

required
axis Literal[0, 1] | None

None for a global scalar result. 0 or 1 for a per-axis 1D result (rank-2 input only).

None

Returns:

Type Description
float | int | array

Global scalar when axis=None; 1D array of length n (axis=1) or m (axis=0) otherwise.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, axis is not None for a rank-1 input, or axis is not 0, 1, or None for a rank-2 input.

Source code in triwarp/reduce.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def min(
    array: twt.ScalarArray, *, axis: Literal[0, 1] | None = None
) -> float | int | twt.Array1dScalar:
    """
    Minimum of ``array``.

    With ``axis=None`` (default), reduces every element to one Python scalar using
    tiled kernels (``wp.tile_load`` + ``wp.tile_min`` + ``wp.atomic_min``).

    With ``axis=0`` or ``axis=1`` on a rank-2 input, reduces along that axis to a
    1D ``wp.array`` of the same dtype — one plain thread per output element when the
    reduced extent is narrower than a tile, a tiled block reduction per output otherwise.

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` scalar Warp array. Must be non-empty.
    axis
        ``None`` for a global scalar result. ``0`` or ``1`` for a per-axis 1D
        result (rank-2 input only).

    Returns
    -------
    float | int | wp.array
        Global scalar when ``axis=None``; 1D array of length ``n`` (``axis=1``) or
        ``m`` (``axis=0``) otherwise.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, ``axis`` is not ``None`` for a
        rank-1 input, or ``axis`` is not ``0``, ``1``, or ``None`` for a rank-2 input.
    """
    return cast(float | int | twt.Array1dScalar, _reduce_scalar(array, axis, _SCALAR_REDUCE["min"]))

max

max(
    array: Array1dInt32 | Array2dInt32, *, axis: None = ...
) -> int
max(
    array: Array1dFloat | Array2dFloat, *, axis: None = ...
) -> float
max(
    array: Array2dScalar, *, axis: Literal[0, 1]
) -> twt.Array1dScalar
max(
    array: ScalarArray, *, axis: Literal[0, 1] | None = None
) -> float | int | twt.Array1dScalar

Maximum of array.

With axis=None (default), reduces every element to one Python scalar using tiled kernels (wp.tile_load + wp.tile_max + wp.atomic_max).

With axis=0 or axis=1 on a rank-2 input, reduces along that axis to a 1D wp.array of the same dtype — one plain thread per output element when the reduced extent is narrower than a tile, a tiled block reduction per output otherwise.

Parameters:

Name Type Description Default
array ScalarArray

Rank-1 (n,) or rank-2 (n, m) scalar Warp array. Must be non-empty.

required
axis Literal[0, 1] | None

None for a global scalar result. 0 or 1 for a per-axis 1D result (rank-2 input only).

None

Returns:

Type Description
float | int | array

Global scalar when axis=None; 1D array of length n (axis=1) or m (axis=0) otherwise.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, axis is not None for a rank-1 input, or axis is not 0, 1, or None for a rank-2 input.

Source code in triwarp/reduce.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def max(
    array: twt.ScalarArray, *, axis: Literal[0, 1] | None = None
) -> float | int | twt.Array1dScalar:
    """
    Maximum of ``array``.

    With ``axis=None`` (default), reduces every element to one Python scalar using
    tiled kernels (``wp.tile_load`` + ``wp.tile_max`` + ``wp.atomic_max``).

    With ``axis=0`` or ``axis=1`` on a rank-2 input, reduces along that axis to a
    1D ``wp.array`` of the same dtype — one plain thread per output element when the
    reduced extent is narrower than a tile, a tiled block reduction per output otherwise.

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` scalar Warp array. Must be non-empty.
    axis
        ``None`` for a global scalar result. ``0`` or ``1`` for a per-axis 1D
        result (rank-2 input only).

    Returns
    -------
    float | int | wp.array
        Global scalar when ``axis=None``; 1D array of length ``n`` (``axis=1``) or
        ``m`` (``axis=0``) otherwise.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, ``axis`` is not ``None`` for a
        rank-1 input, or ``axis`` is not ``0``, ``1``, or ``None`` for a rank-2 input.
    """
    return cast(float | int | twt.Array1dScalar, _reduce_scalar(array, axis, _SCALAR_REDUCE["max"]))

minmax

minmax(
    array: Array1dInt32 | Array2dInt32, *, axis: None = ...
) -> tuple[int, int]
minmax(
    array: Array1dFloat | Array2dFloat, *, axis: None = ...
) -> tuple[float, float]
minmax(
    array: array[vec3], *, axis: None = ...
) -> tuple[wp.vec3, wp.vec3]
minmax(
    array: Array2dScalar, *, axis: Literal[0, 1]
) -> tuple[twt.Array1dScalar, twt.Array1dScalar]
minmax(
    array: ScalarArray | array[vec3],
    *,
    axis: Literal[0, 1] | None = None,
) -> (
    tuple[float, float]
    | tuple[int, int]
    | tuple[wp.vec3, wp.vec3]
    | tuple[twt.Array1dScalar, twt.Array1dScalar]
)

Minimum and maximum of array.

With axis=None (default), reduces every element to two Python scalars using tiled kernels (wp.tile_load + wp.tile_min/tile_max + wp.atomic_min/atomic_max). Either direction alone would be one launch, one buffer and one readback too, so asking for both costs nothing extra.

A rank-1 wp.vec3 array reduces component-wise to a (wp.vec3, wp.vec3) corner pair — one chunked kernel into a single six-slot buffer (the upper corner negated so one inf fill seeds both ends) and one readback, which is what keeps aabb host-latency-bound and nothing more.

With axis=0 or axis=1 on a rank-2 input, reduces along that axis to a pair of 1D wp.array buffers (min, max) of the same dtype.

Parameters:

Name Type Description Default
array ScalarArray | array[vec3]

Rank-1 (n,) or rank-2 (n, m) scalar Warp array, or a rank-1 wp.vec3 array. Must be non-empty.

required
axis Literal[0, 1] | None

None for a global scalar result. 0 or 1 for per-axis 1D results (rank-2 scalar input only).

None

Returns:

Type Description
tuple[float, float] | tuple[int, int] | tuple[vec3, vec3] | tuple[array, array]

(min, max) as Python scalars — or wp.vec3 corners for a wp.vec3 input — when axis=None; pair of 1D arrays of length n (axis=1) or m (axis=0) otherwise.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, axis is not None for a rank-1 or wp.vec3 input, or axis is not 0, 1, or None for a rank-2 input.

See Also

aabb The mesh-facing spelling of the wp.vec3 reduction, with the empty-input (+inf, -inf) convention instead of a raise.

Source code in triwarp/reduce.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def minmax(
    array: twt.ScalarArray | wp.array[wp.vec3], *, axis: Literal[0, 1] | None = None
) -> (
    tuple[float, float]
    | tuple[int, int]
    | tuple[wp.vec3, wp.vec3]
    | tuple[twt.Array1dScalar, twt.Array1dScalar]
):
    """
    Minimum and maximum of ``array``.

    With ``axis=None`` (default), reduces every element to two Python scalars using
    tiled kernels (``wp.tile_load`` + ``wp.tile_min``/``tile_max`` +
    ``wp.atomic_min``/``atomic_max``). Either direction alone would be one launch, one
    buffer and one readback too, so asking for both costs nothing extra.

    A rank-1 ``wp.vec3`` array reduces component-wise to a ``(wp.vec3, wp.vec3)`` corner
    pair — one chunked kernel into a single six-slot buffer (the upper corner negated so
    one ``inf`` fill seeds both ends) and one readback, which is what keeps
    [`aabb`][triwarp.bounds.aabb] host-latency-bound and nothing more.

    With ``axis=0`` or ``axis=1`` on a rank-2 input, reduces along that axis to a
    pair of 1D ``wp.array`` buffers (min, max) of the same dtype.

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` scalar Warp array, or a rank-1 ``wp.vec3``
        array. Must be non-empty.
    axis
        ``None`` for a global scalar result. ``0`` or ``1`` for per-axis 1D
        results (rank-2 scalar input only).

    Returns
    -------
    tuple[float, float] | tuple[int, int] | tuple[wp.vec3, wp.vec3] | tuple[wp.array, wp.array]
        ``(min, max)`` as Python scalars — or ``wp.vec3`` corners for a ``wp.vec3`` input —
        when ``axis=None``; pair of 1D arrays of length ``n`` (``axis=1``) or ``m``
        (``axis=0``) otherwise.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, ``axis`` is not ``None`` for a
        rank-1 or ``wp.vec3`` input, or ``axis`` is not ``0``, ``1``, or ``None`` for a
        rank-2 input.

    See Also
    --------
    [`aabb`][triwarp.bounds.aabb]
        The mesh-facing spelling of the ``wp.vec3`` reduction, with the empty-input
        ``(+inf, -inf)`` convention instead of a raise.
    """
    if array.dtype == wp.vec3:
        if axis is not None:
            raise ValueError("minmax requires axis=None for a wp.vec3 array.")
        return _launch_global_vec3_minmax(cast("wp.array[wp.vec3]", array))
    return cast(
        tuple[float, float] | tuple[int, int] | tuple[twt.Array1dScalar, twt.Array1dScalar],
        _reduce_scalar(cast(twt.ScalarArray, array), axis, _SCALAR_REDUCE["minmax"]),
    )

any

any(
    array: array[bool], *, axis: Literal[0, 1] | None = None
) -> wp.array[wp.bool] | bool

Reduce a boolean array with logical OR.

For rank-1 input, or rank-2 input with axis=None, reduce all elements to one Python bool. For rank-2 input with an explicit axis, reduce along that axis to a 1D wp.bool array.

Parameters:

Name Type Description Default
array array[bool]

Rank-1 (n,) or rank-2 (n, m) wp.bool array. Must be non-empty.

required
axis Literal[0, 1] | None

0, 1, or None. For rank-2 input, None reduces all elements to a single scalar. Ignored for rank-1 input.

None

Returns:

Type Description
bool | array[bool]

Python bool for rank-1 input or rank-2 with axis=None. Length array.shape[1] when axis=0, else array.shape[0], for rank-2 input with an explicit axis.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, or axis is not 0, 1, or None for rank-2 input.

Source code in triwarp/reduce.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def any(array: wp.array[wp.bool], *, axis: Literal[0, 1] | None = None) -> wp.array[wp.bool] | bool:
    """
    Reduce a boolean array with logical OR.

    For rank-1 input, or rank-2 input with ``axis=None``, reduce all elements
    to one Python ``bool``. For rank-2 input with an explicit axis, reduce along
    that axis to a 1D ``wp.bool`` array.

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` ``wp.bool`` array. Must be non-empty.
    axis
        ``0``, ``1``, or ``None``. For rank-2 input, ``None`` reduces all elements
        to a single scalar. Ignored for rank-1 input.

    Returns
    -------
    bool | wp.array[wp.bool]
        Python ``bool`` for rank-1 input or rank-2 with ``axis=None``.
        Length ``array.shape[1]`` when ``axis=0``, else ``array.shape[0]``,
        for rank-2 input with an explicit axis.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, or ``axis`` is not
        ``0``, ``1``, or ``None`` for rank-2 input.
    """
    return _reduce_bool(array, axis, _BOOL_REDUCE["any"])

sum

sum(array: array[vec3], *, axis: None = ...) -> wp.vec3
sum(
    array: Array1dInt32 | Array2dInt32, *, axis: None = ...
) -> int
sum(
    array: Array1dFloat | Array2dFloat, *, axis: None = ...
) -> float
sum(
    array: Array2dScalar, *, axis: Literal[0, 1]
) -> twt.Array1dScalar
sum(array: array[bool], *, axis: None = ...) -> int
sum(
    array: array[bool], *, axis: Literal[0, 1]
) -> twt.Array1dInt32
sum(
    array: ScalarArray | array[bool] | array[vec3],
    *,
    axis: Literal[0, 1] | None = None,
) -> (
    float
    | int
    | twt.Array1dScalar
    | twt.Array1dInt32
    | wp.vec3
)

Sum of array.

With axis=None (default), reduces every element to one Python scalar using tiled kernels (wp.tile_load + wp.tile_sum + wp.atomic_add).

With axis=0 or axis=1 on a rank-2 input, reduces along that axis to a 1D wp.array of the same dtype — one plain thread per output element when the reduced extent is narrower than a tile, a tiled block reduction per output otherwise.

For wp.bool input, counts True values and returns int (global) or wp.int32 (per-axis).

Parameters:

Name Type Description Default
array ScalarArray | array[bool] | array[vec3]

Rank-1 (n,) or rank-2 (n, m) scalar or wp.bool Warp array. Must be non-empty.

required
axis Literal[0, 1] | None

None for a global scalar result. 0 or 1 for a per-axis 1D result (rank-2 input only).

None

Returns:

Type Description
float | int | array

Global scalar when axis=None; 1D array of length n (axis=1) or m (axis=0) otherwise.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, axis is not None for a rank-1 input, or axis is not 0, 1, or None for a rank-2 input.

Source code in triwarp/reduce.py
222
223
224
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
def sum(
    array: twt.ScalarArray | wp.array[wp.bool] | wp.array[wp.vec3],
    *,
    axis: Literal[0, 1] | None = None,
) -> float | int | twt.Array1dScalar | twt.Array1dInt32 | wp.vec3:
    """
    Sum of ``array``.

    With ``axis=None`` (default), reduces every element to one Python scalar using
    tiled kernels (``wp.tile_load`` + ``wp.tile_sum`` + ``wp.atomic_add``).

    With ``axis=0`` or ``axis=1`` on a rank-2 input, reduces along that axis to a
    1D ``wp.array`` of the same dtype — one plain thread per output element when the
    reduced extent is narrower than a tile, a tiled block reduction per output otherwise.

    For ``wp.bool`` input, counts ``True`` values and returns ``int`` (global) or
    ``wp.int32`` (per-axis).

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` scalar or ``wp.bool`` Warp array.
        Must be non-empty.
    axis
        ``None`` for a global scalar result. ``0`` or ``1`` for a per-axis 1D
        result (rank-2 input only).

    Returns
    -------
    float | int | wp.array
        Global scalar when ``axis=None``; 1D array of length ``n`` (``axis=1``) or
        ``m`` (``axis=0``) otherwise.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, ``axis`` is not ``None`` for a
        rank-1 input, or ``axis`` is not ``0``, ``1``, or ``None`` for a rank-2 input.
    """
    if array.dtype == wp.vec3:
        if axis is not None:
            raise ValueError("sum over a vec3 array supports only axis=None.")
        if array.ndim != 1:
            raise ValueError("sum over a vec3 array requires a 1D array.")
        n = int(array.shape[0])
        if n == 0:
            raise ValueError("sum requires a non-empty array.")
        return _launch_vec3_tiled_sum(kernel_reduce.sum_vec3_1d_tiled, n, array.device, [array])
    if array.dtype == wp.bool:
        mask = cast(wp.array[wp.bool], array)
        mask_i32 = astype(mask, wp.int32)
        if mask.ndim == 2 and axis is None:
            mask_i32 = mask_i32.flatten()
        result = _reduce_scalar(cast(twt.ScalarArray, mask_i32), axis, _SCALAR_REDUCE["sum"])
        if axis is None:
            return int(cast(int, result))
        return cast(twt.Array1dInt32, result)
    return cast(
        float | int | twt.Array1dScalar,
        _reduce_scalar(cast(twt.ScalarArray, array), axis, _SCALAR_REDUCE["sum"]),
    )

mean

mean(array: array[vec3], *, axis: None = ...) -> wp.vec3
mean(
    array: ScalarArray | array[bool], *, axis: None = ...
) -> float
mean(
    array: Array2dScalar | array[bool],
    *,
    axis: Literal[0, 1],
) -> twt.Array1dFloat32
mean(
    array: ScalarArray | array[bool] | array[vec3],
    *,
    axis: Literal[0, 1] | None = None,
) -> float | twt.Array1dFloat32 | wp.vec3

Arithmetic mean of array.

Delegates the reduction to the tiled sum, then divides by the element count. With axis=None (default), returns one Python float. With axis=0 or axis=1 on a rank-2 input, returns a 1D wp.float32 array.

The result is always floating point regardless of input dtype. For wp.bool input, the mean is the fraction of True values. For a 1D wp.vec3 input the mean is the component-wise average wp.vec3 (axis=None only).

Parameters:

Name Type Description Default
array ScalarArray | array[bool] | array[vec3]

Rank-1 (n,) or rank-2 (n, m) scalar or wp.bool Warp array, or a rank-1 (n,) wp.vec3 array. Must be non-empty.

required
axis Literal[0, 1] | None

None for a global scalar result. 0 or 1 for a per-axis 1D result (rank-2 input only).

None

Returns:

Type Description
float | vec3 | array

Global float (scalar/wp.bool input) or wp.vec3 (wp.vec3 input) when axis=None; 1D wp.float32 array of length n (axis=1) or m (axis=0) otherwise.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, axis is not None for a rank-1 (or wp.vec3) input, or axis is not 0, 1, or None for a rank-2 input.

Source code in triwarp/reduce.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
def mean(
    array: twt.ScalarArray | wp.array[wp.bool] | wp.array[wp.vec3],
    *,
    axis: Literal[0, 1] | None = None,
) -> float | twt.Array1dFloat32 | wp.vec3:
    """
    Arithmetic mean of ``array``.

    Delegates the reduction to the tiled [`sum`][triwarp.reduce.sum], then divides by the
    element count. With ``axis=None`` (default), returns one Python ``float``. With
    ``axis=0`` or ``axis=1`` on a rank-2 input, returns a 1D ``wp.float32`` array.

    The result is always floating point regardless of input dtype. For ``wp.bool``
    input, the mean is the fraction of ``True`` values. For a 1D ``wp.vec3`` input
    the mean is the component-wise average ``wp.vec3`` (``axis=None`` only).

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` scalar or ``wp.bool`` Warp array, or
        a rank-1 ``(n,)`` ``wp.vec3`` array. Must be non-empty.
    axis
        ``None`` for a global scalar result. ``0`` or ``1`` for a per-axis 1D
        result (rank-2 input only).

    Returns
    -------
    float | wp.vec3 | wp.array
        Global ``float`` (scalar/``wp.bool`` input) or ``wp.vec3`` (``wp.vec3``
        input) when ``axis=None``; 1D ``wp.float32`` array of length ``n``
        (``axis=1``) or ``m`` (``axis=0``) otherwise.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, ``axis`` is not ``None`` for a
        rank-1 (or ``wp.vec3``) input, or ``axis`` is not ``0``, ``1``, or ``None`` for a
        rank-2 input.
    """
    if array.dtype == wp.vec3:
        return cast(wp.vec3, sum(array, axis=axis)) / float(int(array.size))
    total = sum(array, axis=axis)
    if axis is None:
        return float(total) / float(int(array.size))
    sums = cast(twt.Array1dScalar, total)
    out = astype(sums, wp.float32)
    wp.map(wp.div, out, wp.float32(array.shape[axis]), out=out)
    return cast(twt.Array1dFloat32, out)

weighted_sum

weighted_sum(
    values: Array1dFloat32, weights: Array1dFloat32
) -> float
weighted_sum(
    values: array[vec3], weights: Array1dFloat32
) -> wp.vec3
weighted_sum(
    values: Array1dFloat32 | array[vec3],
    weights: Array1dFloat32,
) -> float | wp.vec3

Weighted sum sum_i weights[i] * values[i].

Parameters:

Name Type Description Default
values Array1dFloat32 | array[vec3]

Rank-1 (n,) wp.float32 or wp.vec3 array. Must be non-empty.

required
weights Array1dFloat32

Rank-1 (n,) wp.float32 array of the same length as values.

required

Returns:

Type Description
float | vec3

Scalar weighted sum on the host (float for scalar values, wp.vec3 for wp.vec3 values).

Raises:

Type Description
ValueError

If either array is not rank-1, either is empty, or their lengths differ.

RuntimeError

If values and weights are not all on one device.

Source code in triwarp/reduce.py
347
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
395
396
397
def weighted_sum(
    values: twt.Array1dFloat32 | wp.array[wp.vec3], weights: twt.Array1dFloat32
) -> float | wp.vec3:
    """
    Weighted sum ``sum_i weights[i] * values[i]``.

    Parameters
    ----------
    values
        Rank-1 ``(n,)`` ``wp.float32`` or ``wp.vec3`` array. Must be non-empty.
    weights
        Rank-1 ``(n,)`` ``wp.float32`` array of the same length as ``values``.

    Returns
    -------
    float | wp.vec3
        Scalar weighted sum on the host (``float`` for scalar ``values``,
        ``wp.vec3`` for ``wp.vec3`` ``values``).

    Raises
    ------
    ValueError
        If either array is not rank-1, either is empty, or their lengths differ.
    RuntimeError
        If ``values`` and ``weights`` are not all on one device.
    """
    require_same_device(values=values, weights=weights)
    if values.ndim != 1 or weights.ndim != 1:
        raise ValueError("weighted_sum requires rank-1 values and weights arrays.")
    n_values = int(values.shape[0])
    n_weights = int(weights.shape[0])
    if n_values == 0 or n_weights == 0:
        raise ValueError("weighted_sum requires non-empty arrays.")
    if n_values != n_weights:
        raise ValueError("weighted_sum requires values and weights of equal length.")

    if values.dtype == wp.vec3:
        return _launch_vec3_tiled_sum(
            kernel_reduce.weighted_sum_vec3_1d_tiled, n_values, values.device, [values, weights]
        )

    n_blocks = kernel_reduce.blocks_1d(n_values)
    out = wp.zeros(1, dtype=wp.float32, device=values.device)
    wp.launch_tiled(
        kernel_reduce.weighted_sum1d_tiled,
        dim=[n_blocks],
        inputs=[values, weights, out],
        block_dim=TILE_1D,
        device=values.device,
    )
    return float(read_scalar(out, 0))

all

all(
    array: array[bool], *, axis: Literal[0, 1] | None = None
) -> wp.array[wp.bool] | bool

Reduce a boolean array with logical AND.

For rank-1 input, or rank-2 input with axis=None, reduce all elements to one Python bool. For rank-2 input with an explicit axis, reduce along that axis to a 1D wp.bool array.

Parameters:

Name Type Description Default
array array[bool]

Rank-1 (n,) or rank-2 (n, m) wp.bool array. Must be non-empty.

required
axis Literal[0, 1] | None

0, 1, or None. For rank-2 input, None reduces all elements to a single scalar. Ignored for rank-1 input.

None

Returns:

Type Description
bool | array[bool]

Python bool for rank-1 input or rank-2 with axis=None. Length array.shape[1] when axis=0, else array.shape[0], for rank-2 input with an explicit axis.

Raises:

Type Description
ValueError

If array is empty, its rank is not 1 or 2, or axis is not 0, 1, or None for rank-2 input.

Source code in triwarp/reduce.py
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
def all(array: wp.array[wp.bool], *, axis: Literal[0, 1] | None = None) -> wp.array[wp.bool] | bool:
    """
    Reduce a boolean array with logical AND.

    For rank-1 input, or rank-2 input with ``axis=None``, reduce all elements
    to one Python ``bool``. For rank-2 input with an explicit axis, reduce along
    that axis to a 1D ``wp.bool`` array.

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` or rank-2 ``(n, m)`` ``wp.bool`` array. Must be non-empty.
    axis
        ``0``, ``1``, or ``None``. For rank-2 input, ``None`` reduces all elements
        to a single scalar. Ignored for rank-1 input.

    Returns
    -------
    bool | wp.array[wp.bool]
        Python ``bool`` for rank-1 input or rank-2 with ``axis=None``.
        Length ``array.shape[1]`` when ``axis=0``, else ``array.shape[0]``,
        for rank-2 input with an explicit axis.

    Raises
    ------
    ValueError
        If ``array`` is empty, its rank is not 1 or 2, or ``axis`` is not
        ``0``, ``1``, or ``None`` for rank-2 input.
    """
    return _reduce_bool(array, axis, _BOOL_REDUCE["all"])

median

median(array: Array1dScalar) -> float

Median of a 1D scalar array (numpy.median).

Sorts a copy of the values on-device with warp.utils.radix_sort_pairs and reads the middle element (odd length) or averages the two middle elements (even length). The result is always a Python float, regardless of input dtype.

Parameters:

Name Type Description Default
array Array1dScalar

Rank-1 (n,) Warp array of any 32- or 64-bit scalar dtype accepted by warp.utils.radix_sort_pairs (wp.int32, wp.uint32, wp.float32, wp.int64, wp.uint64, wp.float64). Must be non-empty.

required

Returns:

Type Description
float

The median value on the host.

Raises:

Type Description
ValueError

If array is empty or not rank-1.

Source code in triwarp/reduce.py
432
433
434
435
436
437
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
def median(array: twt.Array1dScalar) -> float:
    """
    Median of a 1D scalar array (``numpy.median``).

    Sorts a copy of the values on-device with ``warp.utils.radix_sort_pairs`` and reads the
    middle element (odd length) or averages the two middle elements (even length). The result
    is always a Python ``float``, regardless of input dtype.

    Parameters
    ----------
    array
        Rank-1 ``(n,)`` Warp array of any 32- or 64-bit scalar dtype accepted by
        ``warp.utils.radix_sort_pairs`` (``wp.int32``, ``wp.uint32``, ``wp.float32``,
        ``wp.int64``, ``wp.uint64``, ``wp.float64``). Must be non-empty.

    Returns
    -------
    float
        The median value on the host.

    Raises
    ------
    ValueError
        If ``array`` is empty or not rank-1.
    """
    if array.ndim != 1:
        raise ValueError("median requires a 1D array.")
    n = int(array.shape[0])
    if n == 0:
        raise ValueError("median requires a non-empty array.")

    sorted_values = cast(twt.Array1dScalar, _sorted_copy(cast(twt.Array1dScalar, array)))
    if n % 2 == 1:
        return float(read_scalar(sorted_values, n // 2))
    middle = sorted_values[n // 2 - 1 : n // 2 + 1].numpy()
    return (float(middle[0]) + float(middle[1])) / 2.0