Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Haifengl Smile Matrix Construction

From Leeroopedia


Overview

Matrix Construction is the foundational entry point for all linear algebra operations in the Smile library. It encompasses the creation of dense matrices from raw data arrays, as well as the generation of special matrices with well-defined algebraic properties such as zero matrices, identity matrices, random matrices, and diagonal matrices.

In numerical computing, the manner in which a matrix is constructed determines its memory layout, scalar precision, and compatibility with hardware-accelerated BLAS/LAPACK routines. Smile's tensor module stores all dense matrices in column-major order (Fortran-style layout), which is the native format expected by BLAS and LAPACK. This design decision eliminates the need for layout transposition when delegating to native routines, thereby maximizing performance.

Theoretical Basis

Matrices as Linear Transformations

A matrix Am×n is a rectangular array of real numbers that represents a linear transformation T:nm. The element aij resides in the i-th row and j-th column. In dense storage, all m×n elements are stored contiguously, as opposed to sparse formats that store only nonzero entries.

Column-Major Storage

In column-major (Fortran) order, the element aij is stored at linear offset:

offset(i,j)=jld+i

where ld (leading dimension) is greater than or equal to m. The leading dimension may be padded beyond m for cache alignment purposes. Smile computes an optimal leading dimension using the formula:

ld=ns+511512512+64s

where s is the element size in bytes (4 for float32, 8 for float64). This avoids cache line conflicts on modern processors with set-associative caches.

Special Matrices

Matrix Type Definition Properties
Zero Matrix Om×n where all aij=0 Additive identity: A+O=A
Identity Matrix In where aij=δij (Kronecker delta) Multiplicative identity: AI=IA=A
Diagonal Matrix D=diag(d1,d2,,dn) Dx scales each component of x
Random Matrix Elements drawn from a probability distribution Used for initialization, testing, and randomized algorithms
Toeplitz Matrix aij=a|ij| (constant diagonals) Arises in convolution, time series, signal processing

Scalar Type and Precision

Smile supports two floating-point scalar types for dense matrices:

  • Float64 (double precision, 8 bytes): 52-bit mantissa providing approximately 15--16 decimal digits of precision. Represented internally by DenseMatrix64.
  • Float32 (single precision, 4 bytes): 23-bit mantissa providing approximately 7 decimal digits of precision. Represented internally by DenseMatrix32. Offers 2x memory savings and potentially faster BLAS operations on hardware with SIMD float32 support.

The choice of scalar type is specified through the ScalarType enum, which is passed to all static factory methods.

Off-Heap Memory

Unlike traditional Java arrays stored on the JVM heap, Smile matrices use off-heap memory via java.lang.foreign.MemorySegment. This design provides:

  • Direct pointer access for native BLAS/LAPACK calls via the Foreign Function & Memory (FFM) API, avoiding JNI overhead
  • Deterministic memory management not subject to garbage collection pauses
  • Compatibility with memory-mapped I/O and GPU transfer operations

Relationship to the Matrix Decomposition Pipeline

Matrix construction is the first stage of the Matrix_Decomposition_Pipeline workflow. Every subsequent operation -- arithmetic, decomposition, solving, and result extraction -- depends on a properly constructed matrix with the correct scalar type, dimensions, and memory layout. The pipeline flows as:

Construction --> Arithmetic --> Decomposition --> Solving --> Result Extraction

Key Constraints

  • Matrix dimensions must satisfy m>0 and n>0.
  • The leading dimension must satisfy ldm for column-major order.
  • The of() factory methods infer scalar type from the input array type (double[][] yields Float64, float[][] yields Float32).
  • Symmetric or triangular structure is indicated by setting the UPLO flag (UPPER or LOWER), which enables optimized BLAS routines (e.g., dsymm instead of dgemm).

Knowledge Sources

Domains

Linear_Algebra, Numerical_Computing, Scientific_Computing

Related

Categories

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment