Skip to content

Vector4D

SUMMARY

A vector in 4D space.

python
from telekinesis import datatypes
vector4d = datatypes.Vector4D([1.0, 2.0, 3.0, 4.0])
API Reference
Complete API documentation for Vector4D, including parameters, attributes, and methods.
View Reference →

Parameters

ParameterTypeDefaultDescription
datanp.ndarray | list | tupleRequiredArray-like data of shape (4,).

Raises

ExceptionCondition
TypeErrordata can't be converted to a float32 array (e.g. non-numeric elements)
ValueErrordata's rank isn't 1, its shape isn't (4,), or it contains a non-finite value (NaN/Inf)

Attributes

AttributeTypeDescription
shape_specClassVar[tuple[int, ...]]Class-level shape spec, (4,).
datanp.ndarrayThe wrapped vector. Reading it returns a copy, so mutating the result doesn't affect the Vector4D; assigning a new value re-validates it the same way as construction.
shapetuple[int, ...]Always (4,).
ndimintAlways 1.
dtypenp.dtypeAlways float32.
sizeintAlways 4.

Methods

MethodTypeDescription
Vector4D.coerce(value)Vector4DConverts array-like data into a Vector4D. Accepts a np.ndarray, list, or tuple of shape (4,). If value is already a Vector4D, it is returned unchanged.
to_numpy(copy=True)np.ndarrayReturns the vector as a plain array. With the default copy=True you get an independent copy; pass copy=False to get a direct reference to the internal array instead, so mutating it also mutates the Vector4D.
copy()Vector4DReturns a new, independent Vector4D with the same data.

Operators

OperationBehavior
v1 == v2True only if other is exactly a Vector4D (not a subclass, not another datatype) with element-equal data. False for anything else.
len(v)Always 4 (length of the single axis).
np.asarray(v)Returns a copy of data as an np.ndarray; NumPy functions accept a Vector4D directly.

Visualization

python
import rerun as rr

# Your code block
# ....

rr.init("vector4d_example", spawn=True)
datatypes.visualize(vector4d, entity_path="/vector4d", label="Vector4D")

Example

python
"""Demonstrates the Telekinesis Vector4D datatype."""

import time

import numpy as np
import rerun as rr
from loguru import logger

from telekinesis import datatypes


def vector4d_example():
    """Demonstrate creation, inspection, operations, visualization, and serialization."""

    # ======================= Create ============================================
    vector4d = datatypes.Vector4D([1.0, 2.0, 3.0, 4.0])
    logger.info(f"Created Vector4D: {vector4d}")

    # ======================= Inspect ===========================================
    logger.info(f"data={vector4d.data}")
    logger.info(f"shape={vector4d.shape}")
    logger.info(f"ndim={vector4d.ndim}")
    logger.info(f"dtype={vector4d.dtype}")
    logger.info(f"size={vector4d.size}")

    # ======================= Operations =========================================
    vector4d.data = [5.0, 6.0, 7.0, 8.0]
    logger.info(f"Updated Vector4D: {vector4d}")

    vector4d_copy = vector4d.copy()
    logger.info(f"Copied Vector4D: {vector4d_copy}")

    vector4d_numpy = vector4d.to_numpy(copy=True)
    logger.info(f"NumPy Vector4D: {vector4d_numpy}")

    numpy_array = np.asarray(vector4d)
    logger.info(f"NumPy array: {numpy_array}")

    sum_with_numpy = vector4d + np.array([1.0, 1.0, 1.0, 1.0])
    logger.info(f"Sum of Vector4D with NumPy array: {sum_with_numpy}")

    norm = np.linalg.norm(vector4d)
    logger.info(f"Norm (np.linalg.norm): {norm}")

    # ======================= Visualize =========================================
    rr.init("vector4d_example", spawn=True)
    datatypes.visualize(vector4d, entity_path="/vector4d")

    # ======================= Serialize / Deserialize ===========================
    start = time.perf_counter()
    serialized = datatypes.serialize(vector4d)
    serialization_ms = (time.perf_counter() - start) * 1000

    start = time.perf_counter()
    deserialized = datatypes.deserialize(serialized)["param_0"]
    deserialization_ms = (time.perf_counter() - start) * 1000

    logger.info(f"Deserialized Vector4D: {deserialized}")
    logger.info(f"Round-trip successful: {vector4d == deserialized}")
    logger.info(f"Serialization time: {serialization_ms:.3f} ms")
    logger.info(f"Deserialization time: {deserialization_ms:.3f} ms")


if __name__ == "__main__":
    vector4d_example()

See also Vectors4D for batches of 4D vectors.