# ericlengyel/terathon-math-library architecture

> Architecture diagram of the GitHub repository ericlengyel/terathon-math-library, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ericlengyel/terathon-math-library
- Repository: https://github.com/ericlengyel/terathon-math-library

## Overview

1. This repository is a C++ numerical/geometry library, not an application. Its architecture is centered on reusable value types for linear algebra and projective geometric algebra, exposed primarily through headers and supported by a small set of `.cpp` translation units.

2. The lowest layer is the platform and SIMD foundation: `TSPlatform.h` and `TSSimd.h` likely encapsulate compiler/OS/CPU feature differences and vectorized math support. `TSMath.h/.cpp` sits above that as the common scalar math utility layer used by all higher-level types.

3. The core linear algebra subsystem consists of vectors and matrices: `TSVector2D/3D/4D`, `TSMatrix2D/3D/4D`, and `TSQuaternion`. These are the basic data carriers and are the substrate for almost every other type in the library.

4. There is a separate scalar extension type, `TSDualNum.h`, which supports dual-number arithmetic. It is part of the math foundation and likely feeds specialized geometric or derivative-style computations.

5. The `TSBivector3D` type provides a bridge between conventional vector math and geometric algebra. It sits close to the algebraic layer rather than the pure matrix/vector layer.

6. The library is organized into several geometric-algebra domains:
   - rigid 2D: `TSRigid2D`, `TSMotor2D`, `TSFlector2D`
   - rigid 3D: `TSRigid3D`, `TSMotor3D`, `TSFlector3D`
   - conformal 2D: `TSConformal2D`
   - conformal 3D: `TSConformal3D`
   These types represent specialized multivectors and transformations for motion, reflection, and conformal primitives.

7. The README describes the main data flow: primitives such as points, lines, planes, circles, spheres, motors, and flectors are manipulated algebraically and transformed through dedicated `Transform()` functions rather than through generic geometric-product operators. That suggests the API favors efficient, type-specific operations over constructing intermediate multivectors.

8. The matrix/vector types also expose “swizzling” and subobject extraction as expression-level views. This is an architectural boundary worth drawing: component access, row/column access, transpose, and submatrix selection are intended to be free or near-free operations when used in expressions.

9. The codebase is split into headers and implementation files in a way that suggests most functionality is inline/template-friendly, with `.cpp` files used for out-of-line definitions, shared routines, or to reduce compile cost for larger operator sets.

10. There are two distinct geometry families in the public API:
   - conventional Euclidean math: vectors, matrices, quaternions
   - projective/conformal geometric algebra: motors, flectors, flat points, lines, planes, circles, spheres
   This is the most important conceptual boundary for an architecture diagram.

11. The library appears self-contained: there are no application entry points, services, storage systems, or external runtime dependencies. The only notable build-time variability called out in the README is the `TERATHON_SWIZZLE_REPEAT` define, which affects compile-time surface area and API behavior.

12. For diagramming, the best high-level structure is: platform/SIMD and math utilities at the base; linear algebra primitives in the middle; then rigid and conformal geometric-algebra modules layered on top, all sharing the same underlying vector/matrix/scalar infrastructure.

Main entry points: [`TSMath.h`](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMath.h) (Math), [`TSAlgebra.h`](https://github.com/ericlengyel/terathon-math-library/blob/main/TSAlgebra.h) (Algebra), [`TSConformal2D.h`](https://github.com/ericlengyel/terathon-math-library/blob/main/TSConformal2D.h) (Conformal 2D), [`TSConformal3D.h`](https://github.com/ericlengyel/terathon-math-library/blob/main/TSConformal3D.h) (Conformal 3D)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_foundation["Foundation"]
  node_platform["Platform<br/>[TSPlatform.h]"]
  node_simd["SIMD<br/>vector intrinsics<br/>[TSSimd.h]"]
  node_math["Math<br/>[TSMath.h]"]
  node_dualnum["Dual Num<br/>dual scalars<br/>[TSDualNum.h]"]
  node_algebra["Algebra<br/>[TSAlgebra.h]"]
end

subgraph group_euclidean["Euclidean"]
  node_vec2["Vector2D<br/>2D vector<br/>[TSVector2D.h]"]
  node_vec3["Vector3D<br/>3D vector<br/>[TSVector3D.h]"]
  node_vec4["Vector4D<br/>4D vector<br/>[TSVector4D.h]"]
  node_mat2["Matrix2D<br/>2x2 matrix<br/>[TSMatrix2D.h]"]
  node_mat3["Matrix3D<br/>3x3 matrix<br/>[TSMatrix3D.h]"]
  node_mat4["Matrix4D<br/>4x4 matrix<br/>[TSMatrix4D.h]"]
  node_quat["Quaternion<br/>rotation algebra<br/>[TSQuaternion.h]"]
  node_bivector3["Bivector3D<br/>bivector bridge<br/>[TSBivector3D.h]"]
end

subgraph group_rigid2d["Rigid 2D"]
  node_rigid2["Rigid 2D<br/>2D motion<br/>[TSRigid2D.h]"]
  node_motor2["Motor2D<br/>2D motor<br/>[TSMotor2D.h]"]
  node_flector2["Flector2D<br/>2D flector<br/>[TSFlector2D.h]"]
end

subgraph group_rigid3d["Rigid 3D"]
  node_rigid3["Rigid 3D<br/>3D motion<br/>[TSRigid3D.h]"]
  node_motor3["Motor3D<br/>3D motor<br/>[TSMotor3D.h]"]
  node_flector3["Flector3D<br/>3D flector<br/>[TSFlector3D.h]"]
end

subgraph group_conformal["Conformal"]
  node_conf2["Conformal 2D<br/>2D conformal<br/>[TSConformal2D.h]"]
  node_conf3["Conformal 3D<br/>3D conformal<br/>[TSConformal3D.h]"]
end

node_platform -->|"basis"| node_simd
node_simd -->|"accelerates"| node_math
node_math -->|"extends"| node_dualnum
node_math -->|"supports"| node_vec2
node_math -->|"supports"| node_vec3
node_math -->|"supports"| node_vec4
node_math -->|"supports"| node_mat2
node_math -->|"supports"| node_mat3
node_math -->|"supports"| node_mat4
node_vec2 -->|"feeds"| node_mat2
node_vec3 -->|"feeds"| node_mat3
node_vec4 -->|"feeds"| node_mat4
node_mat3 -->|"supports"| node_quat
node_vec3 -->|"bridges"| node_bivector3
node_algebra -->|"includes"| node_bivector3
node_algebra -->|"includes"| node_rigid2
node_algebra -->|"includes"| node_rigid3
node_algebra -->|"includes"| node_conf2
node_algebra -->|"includes"| node_conf3
node_rigid2 -->|"specializes"| node_motor2
node_rigid2 -->|"specializes"| node_flector2
node_rigid3 -->|"specializes"| node_motor3
node_rigid3 -->|"specializes"| node_flector3
node_conf2 -->|"reuses"| node_motor2
node_conf3 -->|"reuses"| node_motor3
node_conf2 -->|"leverages"| node_dualnum
node_conf3 -->|"leverages"| node_dualnum
node_conf2 -->|"represents"| node_vec2
node_conf3 -->|"represents"| node_vec3

click node_platform "https://github.com/ericlengyel/terathon-math-library/blob/main/TSPlatform.h"
click node_simd "https://github.com/ericlengyel/terathon-math-library/blob/main/TSSimd.h"
click node_math "https://github.com/ericlengyel/terathon-math-library/blob/main/TSMath.h"
click node_dualnum "https://github.com/ericlengyel/terathon-math-library/blob/main/TSDualNum.h"
click node_algebra "https://github.com/ericlengyel/terathon-math-library/blob/main/TSAlgebra.h"
click node_vec2 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSVector2D.h"
click node_vec3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSVector3D.h"
click node_vec4 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSVector4D.h"
click node_mat2 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSMatrix2D.h"
click node_mat3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSMatrix3D.h"
click node_mat4 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSMatrix4D.h"
click node_quat "https://github.com/ericlengyel/terathon-math-library/blob/main/TSQuaternion.h"
click node_bivector3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSBivector3D.h"
click node_rigid2 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSRigid2D.h"
click node_motor2 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSMotor2D.h"
click node_flector2 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSFlector2D.h"
click node_rigid3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSRigid3D.h"
click node_motor3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSMotor3D.h"
click node_flector3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSFlector3D.h"
click node_conf2 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSConformal2D.h"
click node_conf3 "https://github.com/ericlengyel/terathon-math-library/blob/main/TSConformal3D.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_platform,node_simd,node_math,node_dualnum,node_algebra toneBlue
class node_vec2,node_vec3,node_vec4,node_mat2,node_mat3,node_mat4,node_quat,node_bivector3 toneAmber
class node_rigid2,node_motor2,node_flector2 toneMint
class node_rigid3,node_motor3,node_flector3 toneRose
class node_conf2,node_conf3 toneIndigo
```

## Components

### Foundation

Platform, SIMD, and scalar math support.

- [Platform](https://github.com/ericlengyel/terathon-math-library/blob/main/TSPlatform.h): platform · Compiler and CPU portability layer. · `TSPlatform.h`
- [SIMD](https://github.com/ericlengyel/terathon-math-library/blob/main/TSSimd.h): vector intrinsics · SIMD abstraction and vectorized math support. · `TSSimd.h`
- [Math](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMath.h): scalar math · Common scalar math utilities shared by the library. · `TSMath.h`
- [Dual Num](https://github.com/ericlengyel/terathon-math-library/blob/main/TSDualNum.h): dual scalars · Dual-number arithmetic extension for specialized computations. · `TSDualNum.h`
- [Algebra](https://github.com/ericlengyel/terathon-math-library/blob/main/TSAlgebra.h): core algebra · Umbrella algebra layer that ties the library together. · `TSAlgebra.h`

### Euclidean

Core vector, matrix, and quaternion primitives.

- [Vector2D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSVector2D.h): 2D vector · 2D vector value type and operations. · `TSVector2D.h`
- [Vector3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSVector3D.h): 3D vector · 3D vector value type and operations. · `TSVector3D.h`
- [Vector4D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSVector4D.h): 4D vector · 4D vector value type and operations. · `TSVector4D.h`
- [Matrix2D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMatrix2D.h): 2x2 matrix · 2D matrix type with row, column, and subobject views. · `TSMatrix2D.h`
- [Matrix3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMatrix3D.h): 3x3 matrix · 3D matrix type with row, column, and subobject views. · `TSMatrix3D.h`
- [Matrix4D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMatrix4D.h): 4x4 matrix · 4D matrix type with row, column, and subobject views. · `TSMatrix4D.h`
- [Quaternion](https://github.com/ericlengyel/terathon-math-library/blob/main/TSQuaternion.h): rotation algebra · Quaternion rotation and orientation type. · `TSQuaternion.h`
- [Bivector3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSBivector3D.h): bivector bridge · Bridge between vector math and geometric algebra. · `TSBivector3D.h`

### Rigid 2D

2D rigid-motion geometric algebra.

- [Rigid 2D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSRigid2D.h): 2D motion · 2D rigid geometric-algebra domain. · `TSRigid2D.h`
- [Motor2D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMotor2D.h): 2D motor · 2D translation-rotation motion primitive. · `TSMotor2D.h`
- [Flector2D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSFlector2D.h): 2D flector · 2D reflection and transformation primitive. · `TSFlector2D.h`

### Rigid 3D

3D rigid-motion geometric algebra.

- [Rigid 3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSRigid3D.h): 3D motion · 3D rigid geometric-algebra domain. · `TSRigid3D.h`
- [Motor3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSMotor3D.h): 3D motor · 3D translation-rotation motion primitive. · `TSMotor3D.h`
- [Flector3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSFlector3D.h): 3D flector · 3D reflection and transformation primitive. · `TSFlector3D.h`

### Conformal

2D and 3D conformal geometric algebra.

- [Conformal 2D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSConformal2D.h): 2D conformal · 2D conformal geometric algebra and primitives. · `TSConformal2D.h`
- [Conformal 3D](https://github.com/ericlengyel/terathon-math-library/blob/main/TSConformal3D.h): 3D conformal · 3D conformal geometric algebra and primitives. · `TSConformal3D.h`

## Connections

- Platform → SIMD: basis · SIMD builds on platform-specific feature handling.
- SIMD → Math: accelerates · Vectorized support underpins common math routines.
- Math → Dual Num: extends · Dual numbers reuse the scalar math foundation.
- Math → Vector2D: supports · Scalar utilities feed vector arithmetic.
- Math → Vector3D: supports · Scalar utilities feed vector arithmetic.
- Math → Vector4D: supports · Scalar utilities feed vector arithmetic.
- Math → Matrix2D: supports · Scalar utilities feed matrix arithmetic.
- Math → Matrix3D: supports · Scalar utilities feed matrix arithmetic.
- Math → Matrix4D: supports · Scalar utilities feed matrix arithmetic.
- Vector2D → Matrix2D: feeds · 2D vectors and 2x2 matrices form the base Euclidean layer.
- Vector3D → Matrix3D: feeds · 3D vectors and 3x3 matrices form the base Euclidean layer.
- Vector4D → Matrix4D: feeds · 4D vectors and 4x4 matrices form the base Euclidean layer.
- Matrix3D → Quaternion: supports · Matrix and quaternion rotation types are closely related.
- Vector3D → Bivector3D: bridges · Bivector3D connects conventional vector math to GA.
- Algebra → Bivector3D: includes · The algebra layer exposes the bivector bridge.
- Algebra → Rigid 2D: includes · Core algebra namespace exposes the 2D rigid domain.
- Algebra → Rigid 3D: includes · Core algebra namespace exposes the 3D rigid domain.
- Algebra → Conformal 2D: includes · Core algebra namespace exposes the 2D conformal domain.
- Algebra → Conformal 3D: includes · Core algebra namespace exposes the 3D conformal domain.
- Rigid 2D → Motor2D: specializes · 2D motors implement rigid motion.
- Rigid 2D → Flector2D: specializes · 2D flectors implement reflection-based transforms.
- Rigid 3D → Motor3D: specializes · 3D motors implement rigid motion.
- Rigid 3D → Flector3D: specializes · 3D flectors implement reflection-based transforms.
- Conformal 2D → Motor2D: reuses · Conformal 2D builds on the rigid-motion primitives.
- Conformal 3D → Motor3D: reuses · Conformal 3D builds on the rigid-motion primitives.
- Conformal 2D → Dual Num: leverages · Conformal computations can use dual-number support.
- Conformal 3D → Dual Num: leverages · Conformal computations can use dual-number support.
- Conformal 2D → Vector2D: represents · 2D conformal primitives rest on Euclidean vector types.
- Conformal 3D → Vector3D: represents · 3D conformal primitives rest on Euclidean vector types.
