La Linea

1 Preface

What if dtype-next had linear algebra and complex numbers?

La Linea extends dtype-next with linear algebra and complex numbers, powered by EJML as the computational backend. The two share the same row-major double[] memory layout, enabling zero-copy interop.

General info

Website https://scicloj.github.io/lalinea/
Source (GitHub repo)
Deps Clojars Project
License MIT
Status ๐Ÿ› experimental๐Ÿ› 

Design

La Linea embraces dtype-next as the tensor layer. Matrices are backed by dtype-next tensors and interoperate with the dtype-next ecosystem โ€” Tablecloth/tech.ml.dataset datasets accept them as columns, and dtype-nextโ€™s own dtype/clone, dfn/ reductions, and protocol queries work directly on them. La Linea inherits dtype-nextโ€™s lazy, noncaching evaluation โ€” element-wise operations compose without allocating intermediate arrays. Operations that cross into EJML materialize at the boundary.

Three namespaces cover most usage:

  • t/ (scicloj.lalinea.tensor) โ€” construct tensors in either field (real or complex)
  • el/ (scicloj.lalinea.elementwise) โ€” element-wise math, polymorphic over the field
  • la/ (scicloj.lalinea.linalg) โ€” linear algebra: products, decompositions, solve

All three are polymorphic โ€” they work uniformly on both real tensors and ComplexTensors. Field-aware operations like el/re, el/im, el/conj are identity on reals and meaningful on complex.

Supporting namespaces: tape/ (computation recording), grad/ (autodiff), ft/ (FFT bridge), vis/ (visualization helpers).

Features

Real matrices

  • Construction โ€” matrix, eye, zeros, ones, diag, column, row, submatrix
  • Arithmetic โ€” mmul, mpow, transpose
  • Properties โ€” trace, det, norm (Frobenius), dot
  • Analysis โ€” rank, condition-number, pinv (pseudoinverse), null-space, col-space
  • Solve โ€” solve, lstsq (least squares), invert
  • Decompositions โ€” eigendecomposition, SVD, QR, Cholesky

Complex matrices

  • ComplexTensor โ€” interleaved [re im] layout sharing memory with EJMLโ€™s ZMatrixRMaj
  • Complex mmul, add, sub, scale, conjugate transpose, invert, solve
  • Complex trace, det, norm

Element-wise operations

  • scicloj.lalinea.elementwise โ€” 35+ tape-aware functions with complex dispatch
  • Arithmetic: +, -, *, /, scale
  • Complex-aware: re, im, conj
  • Powers: sq, sqrt, pow, cbrt
  • Exponential: exp, log, log10, log1p, expm1
  • Trigonometric: sin, cos, tan, asin, acos, atan
  • Hyperbolic: sinh, cosh, tanh
  • Reductions: abs, sum, prod, mean, reduce-max, reduce-min
  • Rounding: floor, ceil, round, clip
  • Comparison: >, <, >=, <=, eq, not-eq, min, max

Tagged literals

Real and complex tensors print as readable tagged literals:

#la/R [:float64 [2 2]
       [[1.000 2.000]
        [3.000 4.000]]]

#la/C [:float64 [2 2]
       [[1.000 + 5.000 i  2.000 + 6.000 i]
        [3.000 + 7.000 i  4.000 + 8.000 i]]]

Round-trip through pr-str / read-string.

Fourier transforms

  • Forward/inverse DFT bridging real signals to complex spectra, as well as complex-to-complex
  • , DCT, DST, DHT

Computation tape

  • Record t/, la/, and el/ operations as a DAG with tape/with-tape
  • Inspect memory status: :contiguous, :strided, or :lazy
  • Detect shared backing arrays between tensors
  • Visualize computation graphs as Mermaid flowcharts

Automatic differentiation

  • Reverse-mode autodiff via VJP rules on the computation tape
  • Differentiable ops: el/+, el/-, el/scale, la/mmul, la/transpose, la/trace, la/det, la/invert, la/norm, la/dot, el/*, el/sq, el/sum
  • Compute gradients of scalar functions with respect to matrix inputs

Zero-copy interop

  • dtype-next tensor <-> EJML DMatrixRMaj โ€” same double[], no copy
  • ComplexTensor <-> EJML ZMatrixRMaj โ€” same interleaved double[], no copy
  • Mutations through either view are immediately visible in the other
  • All dfn element-wise operations work directly on matrices (they are tensors)

Documentation

The La Linea book is a set of notebook-based chapters covering:

  • Getting started โ€” quickstart
  • Core concepts โ€” tensors & EJML interop, complex tensors, Fourier transforms, sharing & mutation, computation tape, automatic differentiation
  • Abstract linear algebra โ€” vectors & spaces, maps & structure, inner products & orthogonality, eigenvalues & decompositions
  • Applications โ€” linear systems, Markov chains & PageRank, image processing, fractals, decompositions in action, least squares, spectral graph theory
  • Validation โ€” algebraic identities
  • Reference โ€” API reference

Each chapter includes inline tests via Clayโ€™s test generation.

API

(require '[scicloj.lalinea.tensor :as t])           ; tensor construction, structural ops, EJML interop
(require '[scicloj.lalinea.linalg :as la])          ; arithmetic, decompositions, solve
(require '[scicloj.lalinea.elementwise :as el])    ; element-wise math, comparisons, field ops
(require '[scicloj.lalinea.transform :as ft])       ; FFT / DCT / DST / DHT bridge
(require '[scicloj.lalinea.tape :as tape])          ; computation DAG recording
(require '[scicloj.lalinea.grad :as grad])          ; reverse-mode automatic differentiation
(require '[scicloj.lalinea.vis :as vis])            ; visualization helpers

Built on

  • dtype-next โ€” array/tensor numerics
  • EJML โ€” efficient Java matrix library (real + complex)
  • fastmath โ€” transforms (FFT, DCT, DST, DHT)

The book notebooks also use tablecloth, tableplot, and kindly (included in the :dev and :test aliases).

Development

clojure -M:dev -m nrepl.cmdline   # start REPL
./run_tests.sh                     # run tests
clojure -T:build ci                # test + build JAR

License

MIT License


Part of the scicloj ecosystem for scientific computing in Clojure.

source: notebooks/index.clj