TorchLean API

NN.Proofs.Tensor.Basic.LinearAlgebra

Linear-algebra facts for dependent tensors.

The results here cover dot products, matrix-vector structure, and linearity facts used by autograd, runtime approximation, and model proofs.

theorem Spec.sum_spec_vec {n : } (v : TorchLean.Tensor [n]) :
v.sumSpec = i : Fin n, v.getScalar i

sumSpec on a 1D tensor equals the Finset sum of its coordinates (getScalar).

theorem Spec.getScalar_mul_spec {n : } (a b : TorchLean.Tensor [n]) (i : Fin n) :

getScalar of mulSpec is pointwise multiplication of coordinate functions.

theorem Spec.dot_vec_eq_sum {n : } (a b : TorchLean.Tensor [n]) :
dot a b = i : Fin n, a.getScalar i * b.getScalar i

Dot product of vectors is the coordinate-wise sum ∑ i, a[i] * b[i].

theorem Spec.getScalar_vec_mat_mul_spec {m n : } (v : TorchLean.Tensor [m]) (A : TorchLean.Tensor [m, n]) (j : Fin n) :
(vecMatMulSpec v A).getScalar j = i : Fin m, v.getScalar i * get2 A i j

Coordinate formula for vecMatMulSpec as a Finset sum: (v @ A)[j] = ∑ i, v[i] * A[i,j].

theorem Spec.dot_mat_linear_adjoint {inDim outDim : } (W : TorchLean.Tensor [outDim, inDim]) (dLdy : TorchLean.Tensor [outDim]) (dx : TorchLean.Tensor [inDim]) :
dot dLdy (matVecMulSpec W dx) = dot (vecMatMulSpec dLdy W) dx

Adjointness of matrix-vector and vector-matrix multiplication under the dot product: ⟪y, W x⟫ = ⟪y W, x⟫ (a.k.a. ⟪y, W x⟫ = ⟪Wᵀ y, x⟫ depending on conventions).

This is the algebraic heart of the linear-layer gradient rule.

theorem Spec.shapeOf_eq_shape {α : Type} [TorchLean.Storage α] {s : Shape} (t : TorchLean.Tensor α s) :

shapeOf recovers the shape already tracked in the tensor type.

This is a small bridge for proofs that move between value-level shape computations and type-indexed tensor operations.

theorem Spec.get_preserves_inner_shape {n : } {s : Shape} (t : TorchLean.Tensor (Shape.dim n s)) (i : Fin n) :
shapeOf (get t i) = s

Indexing the outer dimension of a tensor exposes a subtensor with the declared inner shape.

Map and elementwise operation laws #

Functor identity law for mapSpec: mapping id is a no-op.

Functor law for mapSpec: mapping g then f equals mapping f ∘ g.

theorem Spec.map_spec_add_distrib {s : Shape} (f : ) (a b : TorchLean.Tensor s) (h : ∀ (x y : ), f (x + y) = f x + f y) :

A scalar additivity law lifts pointwise through mapSpec and addSpec.

theorem Spec.map2_spec_comm {s : Shape} (f : ) (a b : TorchLean.Tensor s) (h : ∀ (x y : ), f x y = f y x) :

Commutativity transfer: if f is commutative, then map2_spec f is commutative on tensors.

Matrix and vector algebra #

Associativity of matrix-vector multiplication: A (B x) = (A B) x.

theorem Spec.get2_matrix_transpose_spec {m n : } (A : TorchLean.Tensor [m, n]) (i : Fin n) (j : Fin m) :
get2 (A.swapAdjacentAxes 0) i j = get2 A j i

Coordinate rule for the matrix transpose swapAdjacentAxes A 0: (Aᵀ)[i,j] = A[j,i].

theorem Spec.matrix_ext {m n : } {A B : TorchLean.Tensor [m, n]} :
(∀ (i : Fin m) (j : Fin n), get2 A i j = get2 B i j)A = B

Matrix extensionality: matrices are equal when all their entries are equal.

Matrix transpose is an involution.

Transpose of a product: (A ⬝ B)ᵀ = Bᵀ ⬝ Aᵀ.

theorem Spec.dot_mat_eq_sum {m n : } (A B : TorchLean.Tensor [m, n]) :
dot A B = i : Fin m, j : Fin n, get2 A i j * get2 B i j

Expand the matrix dot-product as a double sum over entries (Frobenius inner product).

Right-adjointness of matrix multiplication under the Frobenius dot-product.

Informally: ⟪A ⬝ B, C⟫ = ⟪A, C ⬝ Bᵀ⟫.

Transpose invariance of the Frobenius dot-product: ⟪Aᵀ, Bᵀ⟫ = ⟪A, B⟫.

Left-adjointness of matrix multiplication under the Frobenius dot-product.

Informally: ⟪A ⬝ B, C⟫ = ⟪B, Aᵀ ⬝ C⟫.

Outer product properties. Essential for proving weight gradient correctness.

Reductions and aggregation #