Tensors — Mapanare

N-dimensional arrays with compile-time shape validation. No more runtime shape mismatches.

Overview

Tensors in Mapanare are first-class types with compile-time shape verification. The compiler rejects shape mismatches before your code ever runs.

Declaration

let v: Tensor<Float>[3] = [1.0, 2.0, 3.0]        // 1D vector
let m: Tensor<Float>[2, 3] = [[1.0, 2.0, 3.0],
                               [4.0, 5.0, 6.0]]   // 2D matrix

Compile-Time Shape Checking

let a: Tensor<Float>[3] = [1.0, 2.0, 3.0]
let b: Tensor<Float>[4] = [1.0, 2.0, 3.0, 4.0]
let c = a + b   // COMPILE ERROR: shape mismatch [3] vs [4]

Matrix Multiplication

The @ operator performs matrix multiplication with dimensional compatibility verified at compile time:

let a: Tensor<Float>[3, 3] = identity(3)
let b: Tensor<Float>[3, 4] = zeros(3, 4)
let c = a @ b  // Result: Tensor<Float>[3, 4]

Built-in Operations

Arithmetic: +, -, * (element-wise), @ (matrix multiply). Constructors: zeros(), ones(), identity().

Why This Matters

Python/NumPy: shape errors throw at runtime. Mapanare: shape errors are caught at compile time. AI-generated code is correct by construction.