Skip to content

Mat2x2

SUMMARY

A 2 × 2 matrix.

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

Parameters

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

Raises

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

Attributes

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

Methods

MethodTypeDescription
Mat2x2.coerce(value)Mat2x2Converts array-like data into a Mat2x2. Accepts a np.ndarray, list, or tuple with shape (2, 2). If value is already a Mat2x2, it is returned unchanged.
to_numpy(copy=True)np.ndarrayReturns the matrix 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 Mat2x2.
copy()Mat2x2Returns a new, independent Mat2x2 with the same data.

Operators

OperationBehavior
m1 == m2True only if other is exactly a Mat2x2 (not a subclass, not another datatype) with element-equal data. False for anything else.
len(m)Always 2 (length of axis 0, i.e. the number of rows).
np.asarray(m)Returns a copy of data as an np.ndarray; NumPy functions accept a Mat2x2 directly.

Visualization

python
import rerun as rr

# Your code block
# ....

rr.init("mat2x2_example", spawn=True)
datatypes.visualize(mat2x2, entity_path="/mat2x2", label="Mat2x2")

Example

python
"""Demonstrates the Telekinesis Mat2x2 datatype."""

import time

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

from telekinesis import datatypes

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

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

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

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

    mat2x2_copy = mat2x2.copy()
    logger.info(f"Copied Mat2x2: {mat2x2_copy}")

    mat2x2_numpy = mat2x2.to_numpy(copy=True)
    logger.info(f"NumPy Mat2x2:\n{mat2x2_numpy}")

    numpy_array = np.asarray(mat2x2)
    transposed = np.transpose(mat2x2)
    determinant = np.linalg.det(mat2x2)
    logger.info(f"NumPy array:\n{numpy_array}")
    logger.info(f"Transposed:\n{transposed}")
    logger.info(f"Determinant: {determinant}")

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

    # ======================= Serialize / Deserialize ===========================
    start = time.perf_counter()
    serialized = datatypes.serialize(mat2x2)
    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 Mat2x2: {deserialized}")
    logger.info(f"Round-trip successful: {mat2x2 == deserialized}")
    logger.info(f"Serialization time: {serialization_ms:.3f} ms")
    logger.info(f"Deserialization time: {deserialization_ms:.3f} ms")


if __name__ == "__main__":
    mat2x2_example()