Skip to content

scicloj/lalinea

Repository files navigation

La Linea

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

  • Constructionmatrix, eye, zeros, ones, diag, column, row, submatrix
  • Arithmeticmmul, mpow, transpose
  • Propertiestrace, det, norm (Frobenius), dot
  • Analysisrank, condition-number, pinv (pseudoinverse), null-space, col-space
  • Solvesolve, lstsq (least squares), invert
  • Decompositionseigendecomposition, 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.

About

linear algebra with dtype-next tensors

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages