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

VectorNumpyView

A view presenting an underlying vector backend as a numpy.ndarray.

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.ndarray

A 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.ndarray of numeric dtype (integer, float, or complex — not bool/object) is adopted directly when dtype= 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 both unwrap() and unview() 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 exact fractions interchange: float64 by 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 as 1/2 becomes 0 by half-even rounding rather than being silently truncated by numpy. The original backend remains recoverable via unwrap().

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 with unview(). numpy is an optional dependency (httk-core[numpy]).

Parameters:
  • obj – The source value to present numerically.

  • **hints – Backend-selection hints and an optional dtype conversion.

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.