stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Pure Fortran Toeplitz and Circulant Matrices Module

Open srinjoy933 opened this issue 8 months ago • 4 comments

Motivation: Why is this needed? stdlib currently lacks native structures for highly symmetric dense matrices. The official documentation specifically notes that Toeplitz and Circulant matrices are "not yet supported." Researchers and engineers frequently deal with:Signal processing and linear time-invariant (LTI) system modeling. Solving partial differential equations using finite difference methods.Time-series analysis and auto-correlation matrices. While libraries in Python (SciPy) and MATLAB have built-in functions to handle these memory-efficiently, Fortran developers areforced to instantiate massive dense arrays and use standard matmul for these structures. Providing a native, compressed matrix implementation fulfills a critical gap in the modern Fortran stdlib ecosystem.

Implementation Idea: Compressed Storage and Custom SpMVI propose implementing a dedicated special matrices module (e.g., stdlib_specialmatrices_toeplitz.fypp) entirely in Fortran .Why Pure Fortran? It seamlessly extends the existing stdlib_specialmatrices architecture (like the current tridiagonal.fypp) while maintaining zero external dependencies.Massive Memory Savings: A dense N X N matrix requires storing N^2 elements. A Toeplitz matrix only requires storing the first column and first row, reducing the memory footprint to exactly 2N - 1 elements.

Guaranteed Performance: Custom Sparse Matrix-Vector (SpMV) drivers will utilize the compressed $2N-1$ array. Even with a direct loop approach, the cache locality and memory bandwidth savings provide a massive speedup over unpacking into a standard dense matmul.

The implementation will follow a 3-stage structure: Derived Type: Define a toeplitz_matrix type that safely encapsulates the 2N-1 elements to prevent invalid state manipulation. Constructors: Provide user-friendly initialization interfaces accepting either a single 2N-1 array or separate column/row vectors. SpMV Driver: Overload the * operator (or provide a matmul equivalent) to compute y = T xefficiently on the compressed data.

Prior Art SciPy (scipy.linalg.toeplitz): The Python standard for initializing and interacting with these structures. MATLAB (toeplitz): The gold standard for user-friendly matrix generation in engineering workflows

.Additional Information Initial Scope: Support for standard real and complex kinds (sp, dp, qp) using fypp templating. The initial SpMV will use the direct O(N^2) memory-efficient loop.Future Extension: The architecture will allow for easy upgrading to Fast Fourier Transform (FFT) based O(N log N) multiplication in the future once the base types are merged.

srinjoy933 avatar Mar 31 '26 15:03 srinjoy933

(double precision) Circulant and Toeplitz matrices are already available from SpecialMatrices which is gradually being ported to stdlib. It already uses fftpack behind the scenes for fast $n\ \log(n)$ matrix-vector products. Porting these to stdlib is definitely on my agenda, but there are a few things to consider:

  • fftpack ain't currently linked with stdlib. We haven't even really started to discuss high-level interfaces for fft and the likes, see #1023 and here.
  • the tridiagonal and sym_tridiagonal types in stdlib only include the constructor and spmv so far. In contrast, we have solve, eig, det, trace, etc available in SpecialMatrices. The idea I had when starting to port SpecialMatrices to stdlib was to first begin with feature-complete tridiagonal and sym_tridiagonal before moving to another type (e.g. circulant).

Eventhough I use Toeplitz and Hankel matrices almost on a weekly basis in my research, I would much prefer to see progress on the Fourier transform (which will be needed for these fast spmv routines anyway) for now. FFT has by far more applications than Circulant, Toeplitz, or Hankel matrices.

loiseaujc avatar Apr 02 '26 06:04 loiseaujc

Hi @loiseaujc, thanks for the detailed feedback! I completely agree that relying on the FFT for the O(N log N) speedup is the right architectural move. An O(N^2) direct loop would just be a temporary band-aid, so it makes total sense to wait for fftpack integration.Since the generic interfaces for Fourier transforms (#1023) are the main bottleneck here, I would love to shift my focus to help unblock that. I can review the current discussions on #1023 and the existing SpecialMatrices implementation. Is there a specific area within the FFT interface design or the fftpack linking process where I could contribute right now to help move this forward?

srinjoy933 avatar Apr 21 '26 15:04 srinjoy933

Well, the whole design of the interfaces is on the table. Except for the couple of posts in the aforementioned issues, none of us really had time to make any progress there.

loiseaujc avatar Apr 28 '26 06:04 loiseaujc

Hey @loiseaujc , I have already planned to make some progress here within few days ,as my exams are going I am not able to do any work regarding this. Within few days as it will end I will get back to this issue

srinjoy933 avatar Apr 28 '26 07:04 srinjoy933