Skip to content

Mat (C) — Implementation

File Structure

mat/
├── tiny_mat.h        (39 lines — declarations + submodule includes)
└── tiny_mat.c        (47 lines — implementation, only print helpers)

Dependencies: tiny_linalg.h, tiny_cfloat.h, tiny_decomp.h, tiny_eigen.h, tiny_modal.h, tiny_iterative.h.


Implementation

After the refactoring, tiny_mat.c contains only two functions:

void print_matrix(const char *name, const float *mat, int rows, int cols)
{
    printf("%s =\n\r", name);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            printf("%10.6f ", mat[i * cols + j]);
        printf("\n\r");
    }
    printf("\n\r");
}
  • Assumes contiguous storage (no padding)
  • Row order: element(i, j) = mat[i * cols + j]
  • Precision: 6 decimal places, 10 characters wide
void print_matrix_padded(const char *name, const float *mat,
                          int rows, int cols, int step)
{
    printf("%s =\n\r", name);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            printf("%10.6f ", mat[i * step + j]);
        printf("\n\r");
    }
    printf("\n\r");
}
  • Accounts for row padding: element(i, j) = mat[i * step + j]
  • step = logical columns + padding per row
  • Padding elements are skipped in display but exist in memory

Include Graph

tiny_mat.h
  ├── tiny_math_config.h
  ├── tiny_linalg.h         (linalg API)
  ├── tiny_cfloat.h         (complex API)
  ├── tiny_decomp.h         (decomposition API)
  ├── tiny_eigen.h          (eigenvalue API)
  ├── tiny_modal.h          (modal analysis API)
  └── tiny_iterative.h      (iterative methods API)

All submodule headers are guarded by #pragma once, so double inclusion is harmless. The unified tiny_math.h also includes all of these — there is no conflict.


Legacy Code

Before the refactoring (see PLAN.md in the tiny_math source directory), tiny_mat.c contained:

  • Element-wise arithmetic → linalg/
  • Complex number type → cfloat/
  • MGS QR / HQR → decomp/
  • Hessenberg / Francis QR / modal → eigen/
  • Arnoldi / Lanczos / RSVD → iterative/
  • Test code → test/

The old monolithic approach made maintenance difficult (single 1920-line C file). The new modular structure keeps each concern separate and testable.