httk.core.vectors.vector_numpy_view¶
A view presenting any vector backend as a numpy ndarray, with a selectable dtype.
This module subclasses numpy.ndarray, which happens at class-definition time, so it
cannot be imported at all unless numpy is installed. The package __init__ guards its import
with try/except ImportError accordingly.
Classes¶
A view presenting an underlying vector backend as a |
Module Contents¶
- class httk.core.vectors.vector_numpy_view.VectorNumpyView(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)¶
Bases:
httk.core.vectors.vector_view.VectorView,numpy.ndarrayA view presenting an underlying vector backend as a
numpy.ndarray.This view is a genuine ndarray, so it can be passed anywhere a numpy array is accepted. Construction has two paths:
Adoption (O(1), zero-copy). A raw base-class
numpy.ndarrayof numeric dtype (integer, float, or complex — not bool/object) is adopted directly whendtype=is omitted,None, or equal to the array’s dtype: the view shares the array’s memory and preserves its dtype, no element is scanned or converted, and bothunwrap()andunview()recover the original array object. Values that cannot enter the exact Fraction hub (non-finite floats, complex numbers) fail only if and when an exact conversion is actually requested (e.g..fractions). The adopted array is not copied, so the httk no-mutation rule applies: do not mutate it while the view is in use.Conversion. Any other input — a non-ndarray source, or an explicit
dtype=change — is built from the backend’s exactfractionsinterchange:float64by default (lossy-by-design: exact rationals become their nearest binary value), and for an integer dtype each element is first converted through the"int"leaf codec’s default (nearest, ties to even) so a value such as1/2becomes0by half-even rounding rather than being silently truncated by numpy. The original backend remains recoverable viaunwrap().
Common numpy operations shed the view: operators, ufuncs, NumPy-dispatched functions, reductions, and slicing return base-class ndarrays, so hot numeric loops carry no wrapper overhead past the first operation. A residual path that still produces a
VectorNumpyView(e.g..reshape()/.T) yields a backend-less view whose own array data is authoritative; it never falsely unwraps to the source backend and is normalized withunview(). numpy is an optional dependency (httk-core[numpy]).- Parameters:
obj – The source value to present numerically.
**hints – Backend-selection hints and an optional
dtypeconversion.
- unwrap()¶
Return the original backend value, or this view when it has no backend.
- unview()¶
Return a base-class numpy array containing this view’s data.