vtkmodules.numpy_interface.vtk_constant_array#
VTKConstantArray — numpy-compatible mixin for vtkConstantArray.
This module provides the VTKConstantArray mixin class and registers overrides for all vtkConstantArray instantiations so that constant arrays coming from VTK automatically have numpy-compatible operations.
Arithmetic between constant arrays produces new constant arrays; mixed operations with real arrays return VTKAOSArray results. The constant value is never materialized into a full array unless explicitly requested.
Module Contents#
Classes#
A memory-efficient array where every element has the same value. |
Functions#
Register VTKConstantArray mixin as override for all vtkConstantArray template types. |
API#
- class vtkmodules.numpy_interface.vtk_constant_array.VTKConstantArray(shape=None, value=0, **kwargs)#
Bases:
vtkmodules.numpy_interface._vtk_array_mixin.VTKDataArrayMixinA memory-efficient array where every element has the same value.
vtkConstantArraystores a single scalar value and a shape, using O(1) memory regardless of the number of elements. This Python mixin adds a numpy-compatible interface so that constant arrays can be used in arithmetic expressions, numpy functions, and VTK pipelines without ever materializing the full array into memory.Parameters
shape : int or tuple of int, optional Number of tuples (single int or 1-tuple), or
(ntuples, ncomps)for a multi-component array. When omitted, creates an empty array with zero tuples that can be resized later with the C++ API. value : scalar, optional The constant value for every element. Default is0.Construction
Create with bracket notation and positional arguments::
from vtkmodules.vtkCommonCore import vtkConstantArray # 1-component, 1 million tuples, all 5.0 a = vtkConstantArray[numpy.float64](1000000, 5.0) # 3-component, 100 tuples, all 5.0 b = vtkConstantArray[numpy.float64]((100, 3), 5.0)
Available dtypes:
'float32','float64','int8','int16','int32','int64','uint8','uint16','uint32','uint64'.Properties
value : scalar The constant value stored by the array. shape : tuple of int
(ntuples,)for 1-component,(ntuples, ncomps)otherwise. dtype : numpy.dtype The element data type. dataset : vtkDataSet or None The owning VTK dataset, when the array is attached to one. association : int or None The attribute association (POINT, CELL, etc.).Arithmetic
All element-wise operations (
+,-,*,/,**, comparisons, unary-,abs, and numpy ufuncs) operate on the scalar constant value without allocating a full array:Constant + constant returns a new
VTKConstantArray.Constant + regular array substitutes the scalar and returns a
VTKAOSArraywith metadata (dataset, association) propagated.
Reductions (
sum,mean,min,max,std,var,prod,any,all) are computed in O(1) from the constant value and array shape.NumPy Integration
Constant arrays implement the numpy array protocol:
numpy.asarray(arr)materializes the full array vianumpy.full().numpy.sum(arr),numpy.mean(arr), etc. are intercepted and computed in O(1).Ufuncs (
numpy.sin,numpy.sqrt, etc.) substitute the scalar value.Unoptimized numpy functions trigger a warning and fall back to materialization.
Indexing
Scalar indexing returns the constant value. Slices and fancy indexing return a new
VTKConstantArrayof the appropriate shape, still using O(1) memory. The array is read-only;__setitem__raisesTypeError.Dataset Integration
Constant arrays retrieved from a VTK dataset (via
data_model) automatically carrydatasetandassociationmetadata::arr = polydata.point_data["my_constant_array"] print(arr.value) # the constant print(arr.dataset) # the owning vtkPolyData
See Also
vtkConstantArray : The underlying C++ implicit array class. VTKAffineArray : Similar lazy evaluation for affine (slope*i + intercept) arrays. VTKAOSArray : Mixin for regular (array-of-structures) VTK arrays.
Initialization
Create a new constant array.
Parameters
shape : int or tuple of int, optional Number of tuples, or
(ntuples, ncomps)tuple. value : scalar, optional The constant value (default0). **kwargs Forwarded to the VTK base-class initializer (e.g. property setters likename="MyArray").- _new_like(value, dtype=None)#
Create a new VTKConstantArray of same shape with given value.
- property value#
The constant scalar value.
- property dtype#
- property nbytes#
- __array__(dtype=None, copy=None)#
Materialize the full array when numpy needs it explicitly.
- __buffer__(flags)#
Override C-level buffer protocol to materialize via numpy.full.
- __array_ufunc__(ufunc, method, *inputs, **kwargs)#
Handle numpy ufuncs by substituting the scalar constant.
When all operands are VTKConstantArrays or plain scalars, the result is wrapped back into a VTKConstantArray of the same shape. When mixed with a real numpy array, the constant is passed as a scalar and the result is wrapped as VTKAOSArray.
- __array_function__(func, types, args, kwargs)#
Dispatch numpy functions with O(1) overrides where possible.
- __getitem__(key)#
Index into the virtual constant array.
Returns a scalar for element access, or a new VTKConstantArray for slices / fancy indexing.
- __setitem__(key, value)#
- __add__(other)#
- __radd__(other)#
- __sub__(other)#
- __rsub__(other)#
- __mul__(other)#
- __rmul__(other)#
- __truediv__(other)#
- __rtruediv__(other)#
- __floordiv__(other)#
- __rfloordiv__(other)#
- __pow__(other)#
- __rpow__(other)#
- __mod__(other)#
- __rmod__(other)#
- __lt__(other)#
- __le__(other)#
- __eq__(other)#
- __ne__(other)#
- __ge__(other)#
- __gt__(other)#
- __neg__()#
- __pos__()#
- __abs__()#
- _axis_full(axis, value)#
Build a result array of the correct shape for a reduction.
- sum(axis=None, **kwargs)#
- mean(axis=None, **kwargs)#
- min(axis=None, **kwargs)#
- max(axis=None, **kwargs)#
- std(axis=None, **kwargs)#
- var(axis=None, **kwargs)#
- any(axis=None, **kwargs)#
- all(axis=None, **kwargs)#
- prod(axis=None, **kwargs)#
- astype(dtype)#
- __iter__()#
- __repr__()#
- __str__()#
- vtkmodules.numpy_interface.vtk_constant_array._constant_sum(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_mean(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_min(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_max(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_std(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_var(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_any(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_all(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._constant_prod(a, axis=None, **kwargs)#
- vtkmodules.numpy_interface.vtk_constant_array._register_constant_overrides()#
Register VTKConstantArray mixin as override for all vtkConstantArray template types.