TinyMath HEADER FILE¶
Overview¶
tiny_math.h is the unified entry point of the TinyMath library. Including this single header gives you access to all mathematical functionality — from basic vector operations to advanced iterative solvers and eigenvalue decomposition.
It acts as an include hub: rather than remembering each submodule's header path, you simply
and everything is wired up.
Architecture & Dependency Flow¶
The header is structured in layers of increasing complexity. Each layer builds upon the previous ones:
%%{init: {'themeVariables': {'fontSize': '32px'}}}%%
graph BT
subgraph Stdlib["Standard C Library"]
STD["stdio.h / stdlib.h / string.h<br/>math.h / stdbool.h / stdint.h"]
end
subgraph Independent["Independent Base Layers"]
TOOL["tiny_toolbox.h<br/><i>FreeRTOS, ESP-IDF,<br/>time, logging, heap</i>"]
CONST["tiny_constants.h<br/><i>π, √2, bit macros</i>"]
ERR["tiny_error_type.h<br/><i>error codes</i>"]
end
subgraph Config["Configuration Hub<br/><b>included by every submodule</b>"]
CONF["tiny_math_config.h"]
end
subgraph Submodules["Core Submodules (each self‑contained)"]
VEC["tiny_vec.h<br/><b>Vector</b>"]
LINALG["tiny_linalg.h<br/><b>Linear Algebra</b>"]
CFLOAT["tiny_cfloat.h<br/><b>Complex</b>"]
DECOMP["tiny_decomp.h<br/><b>Decomposition</b>"]
EIGEN["tiny_eigen.h<br/><b>Eigenvalue</b>"]
MODAL["tiny_modal.h<br/><b>Modal</b>"]
ITER["tiny_iterative.h<br/><b>Iterative</b>"]
MAT["tiny_mat.h<br/><b>C Matrix</b>"]
MATCPP["tiny_matrix.hpp<br/><b>C++ Mat</b>"]
end
subgraph Entry["Unified Entry Point"]
MATH["tiny_math.h<br/><b>aggregates all submodules</b>"]
end
subgraph Test["Test Suite (guarded by TINY_MATH_ENABLE_TESTS)"]
TEST_VEC["test/vec/tiny_vec_test.h"]
TEST_MAT["test/mat/tiny_mat_test.h"]
TEST_CPP["test/mat/tiny_matrix_test.hpp"]
end
STD --> TOOL
STD --> CONST
STD --> ERR
TOOL --> CONF
CONST --> CONF
ERR --> CONF
CONF --> VEC
CONF --> LINALG
CONF --> CFLOAT
CONF --> DECOMP
CONF --> EIGEN
CONF --> MODAL
CONF --> ITER
CONF --> MAT
CONF -.->|"#ifdef __cplusplus"| MATCPP
VEC --> MATH
LINALG --> MATH
CFLOAT --> MATH
DECOMP --> MATH
EIGEN --> MATH
MODAL --> MATH
ITER --> MATH
MAT --> MATH
MATCPP -.->|"#ifdef __cplusplus"| MATH
VEC -.-> TEST_VEC
MAT -.->|C| TEST_MAT
MATCPP -.->|"C++"| TEST_CPP
style CONF fill:#eef2ff,stroke:#4f6af5,stroke-width:2px
style MATH fill:#eef2ff,stroke:#4f6af5
style TOOL fill:#f8fafc,stroke:#94a3b8
style CONST fill:#f8fafc,stroke:#94a3b8
style ERR fill:#f8fafc,stroke:#94a3b8
style VEC fill:#f8fafc,stroke:#94a3b8
style LINALG fill:#f8fafc,stroke:#94a3b8
style CFLOAT fill:#f8fafc,stroke:#94a3b8
style DECOMP fill:#f8fafc,stroke:#94a3b8
style EIGEN fill:#f8fafc,stroke:#94a3b8
style MODAL fill:#f8fafc,stroke:#94a3b8
style ITER fill:#f8fafc,stroke:#94a3b8
style MAT fill:#f8fafc,stroke:#94a3b8
style MATCPP fill:#f8fafc,stroke:#94a3b8 Include Order
The submodules are listed in order of increasing computational complexity: vec → linalg → cfloat → decomp → eigen/modal → iterative → mat/matrix. This mirrors the intended learning and dependency chain.
Submodule Reference¶
| Submodule | Header File | Description | Documentation |
|---|---|---|---|
| Vector | tiny_vec.h | 1D vector arithmetic: add, sub, mul, div, sqrt, dot product (with stride support) | API → · Code → |
| Linear Algebra | tiny_linalg.h | 2D matrix element-wise operations: add, sub, mul, transpose, norm, eye, fill, diag | API → · Code → |
| Complex | tiny_cfloat.h | tiny_cfloat_t type and operations: add, sub, mul, div, abs, arg, sqrt, log | API → · Code → |
| Decomposition | tiny_decomp.h | Matrix decomposition: Modified Gram-Schmidt QR, Householder QR | API → · Code → |
| Eigenvalue | tiny_eigen.h | 2×2 closed-form, Hessenberg reduction, Francis double-shift QR, Schur extraction | API → · Code → |
| Modal Analysis | tiny_modal.h | MIMO modal analysis: poles → frequency/damping, MAC, mode filtering/sorting | API → · Code → |
| Iterative | tiny_iterative.h | Krylov subspace methods: Arnoldi, Lanczos, Randomized SVD, linear operator interface | API → · Code → |
| C Matrix | tiny_mat.h | C interface utilities: matrix printing, C wrappers for decomposition/eigenvector extraction | API → · Code → |
| C++ Mat | tiny_matrix.hpp | Full-featured C++ Mat class: constructors, operators, ROI, views, blocks (C++ only) | API → · Code → |
C/C++ Dual-Path Design¶
TinyMath supports both C and C++ compilation modes:
// C/C++ submodules (always available)
#include "tiny_vec.h" // pure C
#include "tiny_linalg.h" // pure C
#include "tiny_cfloat.h" // pure C
#include "tiny_decomp.h" // pure C
#include "tiny_eigen.h" // pure C
#include "tiny_modal.h" // pure C
#include "tiny_iterative.h" // pure C
#include "tiny_mat.h" // pure C
// C++ only (guarded)
#ifdef __cplusplus
#include "tiny_matrix.hpp" // requires C++ compiler
#endif
The extern "C" block at the end is kept for future expansion — it ensures any C functions added at this scope will have C linkage under C++ compilation.
Test Gating¶
Test files are not included by default. They are only activated when the preprocessor macro TINY_MATH_ENABLE_TESTS is defined, typically via:
#ifdef TINY_MATH_ENABLE_TESTS
#include "test/vec/tiny_vec_test.h" // vector test functions
#include "test/mat/tiny_mat_test.h" // matrix C test functions
#ifdef __cplusplus
#include "test/mat/tiny_matrix_test.hpp" // matrix C++ test functions
#endif
#endif /* TINY_MATH_ENABLE_TESTS */
Why guarded?
Test code adds significant binary size. On resource-constrained MCUs (ESP32, STM32), you only want test code in debug builds. Gate it with TINY_MATH_ENABLE_TESTS and disable in release.
Source Code¶
/**
* @file tiny_math.h
* @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg)
* @brief This file is the header file for the tiny_math middleware.
* @version 1.0
* @date 2025-03-26
* @copyright Copyright (c) 2025
*
*/
#pragma once
/* DEPENDENCIES */
// this layer
#include "tiny_math_config.h"
/* SUBMODULES */
// vector operations
#include "tiny_vec.h"
// linalg operations (element-wise, contiguous)
#include "tiny_linalg.h"
// complex float arithmetic
#include "tiny_cfloat.h"
// matrix decompositions (QR, MGS, HQR)
#include "tiny_decomp.h"
// eigenvalue computation (Francis QR, Hessenberg)
#include "tiny_eigen.h"
// modal analysis (eigs→freq/damp, MAC)
#include "tiny_modal.h"
// iterative methods (Arnoldi, Lanczos, RSVD)
#include "tiny_iterative.h"
// matrix operations
#include "tiny_mat.h"
// advanced matrix operations
#ifdef __cplusplus
#include "tiny_matrix.hpp"
#endif
/* TEST — guarded by TINY_MATH_ENABLE_TESTS (defined in sdkconfig or build flags) */
#ifdef TINY_MATH_ENABLE_TESTS
#include "test/vec/tiny_vec_test.h"
#include "test/mat/tiny_mat_test.h"
#ifdef __cplusplus
#include "test/mat/tiny_matrix_test.hpp"
#endif
#endif /* TINY_MATH_ENABLE_TESTS */
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
Source of Truth
The code above is the actual content of tiny_math.h. Always refer to the GitHub file as the authoritative version.