Skip to content

Unify Matrix and Tensor internals #24

@matajoh

Description

@matajoh

Problem

After the Tensor work lands, the project carries two parallel C implementations of "dense array of doubles": one rank-2 (Matrix), one N-D (Tensor). Every arithmetic op, every aggregate, every cross-interpreter round-trip exists twice. New work has to be added in both places, or one side drifts. This is sustainable for one release; it is not sustainable as a long-term shape for the library.

Desired functionality

The two types share a single C representation and a single set of inner loops. Matrix becomes the rank-2 face of that shared machinery. The Python-level Matrix API — constructor, methods, return types, repr, type identity — looks identical to today from the outside; existing user code, existing examples, and existing downstream C consumers keep working unmodified.

Constraints

  • Zero public API breakage. Every existing Matrix test, every example, every downstream C ABI consumer must keep working unchanged.
  • No measurable performance regression on Matrix-shaped workloads.
  • Matrix keeps its own Python type. isinstance(x, Matrix) keeps working.
  • Rank-2-only algebra (matmul, transpose, T, axis-0 / axis-1 aggregate sugar) stays on Matrix.

Out of scope

Generalising rank-2 algebra onto Tensor (matmul on Tensor, batched matmul, transpose(axes=...), swapaxes). Removing or renaming any public Matrix surface. Treating Matrix as deprecated.

Open questions

  • Whether Matrix and Tensor should be silently interchangeable in each other's arithmetic dunders, or whether the user has to step through an explicit conversion.
  • Whether to expose explicit Matrix.to_tensor() / Tensor.to_matrix() helpers regardless.

Metadata

Metadata

Assignees

No one assigned

    Labels

    mathsWork on the maths module

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions