Dev math lesson

From Nebulavenus/forge-gpu · View interactive version

Dev math lesson is an AI agent skill from the GitHub repository Nebulavenus/forge-gpu. Add a math concept - create lesson, update library, document usage

How to use this skill

Skill file

Create a new math lesson and update the math library (common/math/forge_math.h).

When to use this skill:

Smart behavior:

Arguments

The user (or you) can provide:

If any are missing, infer from context or ask.

Steps

1. Analyze what's needed

2. Create the lesson directory

lessons/math/NN-concept-name/

3. Create a demo program (`main.c`)

A small, focused C program that demonstrates the math concept visually or numerically.

Requirements:

Examples:

Template structure:

/*
 * Math Lesson NN — Concept Name
 *
 * Demonstrates: [what this shows]
 *
 * SPDX-License-Identifier: Zlib
 */

#include <SDL3/SDL.h>
#include "math/forge_math.h"

int main(int argc, char *argv[])
{
    (void)argc;
    (void)argv;

    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        return 1;
    }

    /* Demonstrate the math concept here */

    SDL_Quit();
    return 0;
}

Console output formatting:

4. Create `CMakeLists.txt`

add_executable(NN-concept-name main.c)
target_include_directories(NN-concept-name PRIVATE ${FORGE_COMMON_DIR})
target_link_libraries(NN-concept-name PRIVATE SDL3::SDL3)

add_custom_command(TARGET NN-concept-name POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:SDL3::SDL3-shared>
        $<TARGET_FILE_DIR:NN-concept-name>
)

5. Create `README.md`

Structure:

# Math Lesson NN — Concept Name

[Brief subtitle explaining what this teaches]

## What you'll learn

[Bullet list of math concepts covered]

## Key concepts

[Bullet list of core takeaways:]
- **Concept 1** — Brief explanation
- **Concept 2** — Brief explanation
- **Formula/operation** — What it does and when to use it

## The Math

[Explain the concept clearly, with intuition before formulas]

### [Subconcept 1]

[Explanation, formula, geometric meaning]

### [Subconcept 2]

[Explanation, formula, geometric meaning]

## Where it's used

Graphics and game programming uses this for:
- [Use case 1]
- [Use case 2]

**In forge-gpu lessons:**
- [Link to GPU lesson] uses `function_name()` for [purpose]
- [Link to GPU lesson] uses `function_name()` for [purpose]

## Building

cmake -B build

cmake --build build --config Debug

Windows

build\lessons\math\NN-concept-name\Debug\NN-concept-name.exe

Linux / macOS

./build/lessons/math/NN-concept-name/NN-concept-name


[Brief note on what the demo shows]

## Result

[Brief description of expected demo output or what the program demonstrates]

**Example output:**

[Copy actual program output here]


**Important:** Copy output directly from running the program—don't manually type it.

## Exercises

1. [Exercise extending the concept]
2. [Exercise applying it differently]
3. [Exercise combining with other concepts]

## Further reading

- [Relevant math lesson that builds on this]
- [External resource if helpful]

6. Update `common/math/forge_math.h`

Add the new math functions following the library's conventions:

Example:

/* Compute the dot product of two 3D vectors.
 *
 * The dot product measures how much two vectors point in the same direction.
 * Result = |a| * |b| * cos(θ), where θ is the angle between them.
 *
 * If the result is:
 *   - Positive: vectors point somewhat in the same direction
 *   - Zero:     vectors are perpendicular
 *   - Negative: vectors point in opposite directions
 *
 * Usage:
 *   vec3 a = vec3_create(1.0f, 0.0f, 0.0f);
 *   vec3 b = vec3_create(0.0f, 1.0f, 0.0f);
 *   float d = vec3_dot(a, b);  // 0.0 — perpendicular
 *
 * See: lessons/math/01-vectors
 */
static inline float vec3_dot(vec3 a, vec3 b)
{
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

If the function already exists, improve documentation or skip this step.

7. Update project files

8. Cross-reference GPU lessons

9. Build and test

cmake -B build
cmake --build build --config Debug
./build/lessons/math/NN-concept-name/NN-concept-name

Verify the demo runs and produces expected output.

10. Verify key topics are fully explained

Before finalizing, launch a verification agent (Task agent) that audits every

key topic in the README for completeness. This catches cases where a key topic

name-drops a term (like "similar triangles") but the lesson never actually

explains what that term means — leaving readers confused.

For each key topic / "What you'll learn" bullet, the agent must check:

  1. Explained in the README — Is the concept described clearly enough that a

reader encountering it for the first time could understand it? Look in "The

Math" section, key concepts, and any subsections.

  1. Demonstrated in the C program — Does main.c actually exercise this

concept with code and output? A key topic that's only mentioned in the README

but never shown in the demo is incomplete.

  1. All referenced terms are defined — Read the exact wording of each key

topic and identify every math term, technical term, or concept it references

(e.g., "similar triangles", "homogeneous coordinates", "affine transformation",

"orthonormal basis"). For each referenced term, confirm it is explained

somewhere in the lesson — either inline where first used, in its own

subsection, or with a cross-reference to another lesson that covers it.

What to flag:

the README

in "The Math" or the demo program

in the curriculum covers it either

to interpret those results (or vice versa)

How to fix flagged issues:

sentences is usually enough — not a full derivation, just enough for the reader

to follow along)

Math" and link to it from the key topic

"See Lesson NN — Name for an explanation of [term]"

the key topic to match what the lesson actually teaches

The lesson is not complete until every key topic passes all three checks.

11. Run markdown linting

Use the /dev-markdown-lint skill to check all markdown files:

npx markdownlint-cli2 "**/*.md"

If errors are found:

  1. Try auto-fix: npx markdownlint-cli2 --fix "**/*.md"
  2. Manually fix remaining errors (especially MD040 - missing language tags)
  3. Verify: npx markdownlint-cli2 "**/*.md"

Common fixes needed:

MANDATORY: Chunked writes for large files

Task agents have a 32K output token limit per Write call. **Any file over ~800

lines** MUST be written in chunks — split into parts, write each to /tmp/,

then concatenate.

Recovery rule — if a writing agent fails with a token limit error:

See CODE0

for the full strategy.

Math Library Conventions

Coordinate System

Matrix Layout

Naming

Documentation Standard

Every function needs:

  1. Summary sentence
  2. Parameter descriptions (if not obvious)
  3. Return value explanation
  4. Geometric intuition
  5. Usage example
  6. Reference to math lesson

Example: Adding Vector Operations

Scenario: Lesson 02 needs vec2 and vec3 for vertex positions and colors.

  1. Check library: vec2 and vec3 types exist, basic ops exist
  2. Determine scope: Need create, add, scale, normalize, dot, lerp
  3. Create lesson: lessons/math/01-vectors/
  4. Demo program: Print dot products of various vector pairs, show perpendicular/parallel cases
  5. README: Explain vectors, dot product, normalization with geometric intuition
  6. Update library: Add any missing vector operations with full documentation
  7. Cross-reference: Note that Lesson 02 uses vec2_create and vec3_create
  8. Build and test: Verify demo runs

Example: Extracting Math from a GPU Lesson

Scenario: You're reviewing Lesson 03 and notice it has bespoke rotation math.

  1. Identify the math: 2D rotation using sin/cos
  2. Check library: mat4_rotate_z exists!
  3. Decision: Don't create a new lesson for rotation yet (matrices lesson will cover it)
  4. Refactor Lesson 03: Replace bespoke math with mat4_rotate_z
  5. Plan: Add matrices lesson later (lesson 05-matrices)

Tips

Diagrams and Formulas

**Find opportunities to create compelling diagrams and visualizations via the

matplotlib scripts** — they increase reader engagement and help learners

understand the topics being taught. Use the /dev-create-diagram skill to add

diagrams following the project's visual identity and quality standards.

Matplotlib diagrams

For geometric or mathematical visuals (vector diagrams, interpolation grids,

frustum views), add a diagram function to

scripts/forge_diagrams/math/lesson_NN.py (create the file if it doesn't

exist):

  1. Write a function following the existing pattern (shared setup_axes,

draw_vector, save helpers from _common.py)

  1. Re-export from scripts/forge_diagrams/math/__init__.py
  2. Import and register in the DIAGRAMS dict in __main__.py with the lesson key
  3. Run python scripts/forge_diagrams --lesson math/NN to generate the PNG
  4. Reference in the README: ![Description](assets/diagram_name.png)

Output goes to each lesson's assets/ directory at 200 DPI (PNG only).

Mermaid diagrams

For flow/pipeline diagrams (transformation pipelines, data flow), use inline

mermaid blocks. GitHub renders these natively:

flowchart LR

A[Step 1] --> B[Step 2] --> C[Step 3]

Use mermaid for sequential flows.

KaTeX math

For formulas, use inline $...$ and display $$...$$ math notation.

GitHub renders these via KaTeX:

$$
\text{lerp}(a, b, t) = a + t \cdot (b - a)
$$

Keep worked-examples-with-numbers in `text blocks — only convert

symbolic/algebraic formulas to KaTeX.

When NOT to Create a Lesson

In these cases, just update documentation or plan for later.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.