NetFabric/NetFabric.Numerics / .agents/skills/vector-algebra
vector-algebra
VerifiedAbstract math for vector algebra in 2D and 3D Euclidean space. USE FOR: implementing vector operations (add, subtract, negate, scalar multiply/divide); dot product and geometric interpretation; 3D cross product; 2D pseudo-cross product (scalar z-component); vector magnitude and normalization; angle between two vectors; linear interpolation; checking zero/unit/finite vectors. 2D is a special case of 3D with z=0. DO NOT USE FOR: quaternion operations (use quaternion-algebra); rotation representations (use 3d-rotation-theory); coordinate system conversions (use coordinate-system-conversions).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 2.5 KB |
| references/formulas.md | markdown | 2.0 KB |
| references/numerical-stability.md | markdown | 2.3 KB |
references/numerical-stability.md
View on GitHub# Vector Algebra — Numerical Stability
## Near-Zero Normalization (Critical)
`v̂ = v / ‖v‖` is undefined when ‖v‖ = 0. Near zero:
- Division by a subnormal causes ‖v̂‖ >> 1
- Division by 0.0 produces ±Inf components
**Guard**: always check `‖v‖ > ε` before normalizing; return zero vector or signal error for degenerate input.
**Relative epsilon**: use `ε ≈ float.Epsilon * 4` or check `MagnitudeSquared > ε²`.
## Magnitude Comparison (Avoid Sqrt)
To compare two lengths: compare `MagnitudeSquared` values to avoid the cost and precision loss of `sqrt`.
```
‖a‖ < ‖b‖ ⟺ ‖a‖² < ‖b‖² (all values ≥ 0, so squaring preserves order)
```
## Dot Product Cancellation
When **a** and **b** nearly parallel (same direction), `a · b ≈ ‖a‖‖b‖`; the identity `sin θ = ‖a×b‖ / (‖a‖‖b‖)` is more accurate for small angles.
When **a** ⊥ **b**, `a · b ≈ 0`; subtraction of nearly-equal numbers occurs in the component products. This is unavoidable but bounded by 1 ulp per term.
## arccos Argument Clamping (Angle Between)
`a · b / (‖a‖‖b‖)` may exceed [−1, 1] by tiny amounts due to rounding in magnitude computation.
Always clamp to [−1, 1] before calling arccos:
```csharp
double cosTheta = Math.Clamp(dot / (magA * magB), -1.0, 1.0);
```
## Catastrophic Cancellation in Cross Product
`aᵧb_z − a_z bᵧ`: when both products are nearly equal, up to N bits of precision can be lost.
For nearly-parallel vectors, use `‖a‖‖b‖ sin θ` formulation or higher-precision arithmetic.
## MagnitudeSquared Overflow
For components near `float.MaxValue / √2`, `vₓ² + vᵧ²` overflows.
If overflow is possible, scale the vector first: `v' = v / max(|vₓ|, |vᵧ|[, |v_z|])`, compute ‖v'‖, then scale back.
## IsNormalized Tolerance
Exact bit-equality `‖v‖ == 1.0` will fail for nearly all normalized vectors.
Use: `Math.Abs(MagnitudeSquared - 1.0) < ε` where ε is chosen per use case (e.g., 1e-6 for float, 1e-12 for double).
## NaN/Inf Propagation
- Any NaN component propagates through all operations.
- Addition of +Inf and −Inf components gives NaN.
- Normalizing a vector with Inf component: Inf/Inf = NaN.
Guard with `IsFinite` checks at system boundaries (inputs from users/files).
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |