Overall Grade: A (Excellent)
Derive is a well-engineered symbolic mathematics library that demonstrates professional code quality, thoughtful architecture, and comprehensive test coverage. After a thorough exploration of the codebase, here are my findings.
The codebase is organized into 11 distinct modules totaling ~11,600 lines of source code:
core/- Foundational types, symbols, constants, smart number handlingcalculus/- Differentiation, integration, limits, series, transforms, variational calculusalgebra/- Solving, simplification, linear algebradiffgeo/- Tensor calculus, metrics, Christoffel symbols, abstract indicesode/- Symbolic and numerical ODE solversfunctions/- 30+ special functions (Bessel, Legendre, Gamma, elliptic, etc.)patterns/- Pattern matching and term rewritingutils/- Composable API, caching, display, functional programmingtypes/- Custom mathematical types (Quaternion, Vector3D)plotting/- Matplotlib-based visualizationregression/- Symbolic regression via PySR
The separation of concerns is clean with no circular imports. The centralized math_api.py abstraction layer is a smart design choice that would allow swapping underlying libraries with minimal code changes.
Strengths:
- PEP 8 compliant with all imports at module top-level
- Consistent type hints across ~500 functions
- ~835 docstring definitions with Args, Returns, and Examples sections
- Factory pattern usage reduces boilerplate (e.g.,
matrix_method()for linear algebra ops) - Memoization via
@lru_cachefor expensive tensor computations - Context managers for exact/numerical mode control
- Composable Pipe API for fluent method chaining
Design patterns I appreciated:
# Self-referencing: uses own APIs instead of calling SymPy directly
from derive.calculus import D
from derive.algebra import Simplify
# Factory pattern for DRY code
Det = matrix_method('det')
Tr = matrix_method('trace')
# Tensor symmetry optimization (~4x speedup)
symmetric_christoffel_indices(n) # Only computes unique components
# Composable API
result = Pipe((x + 1)**3).then(Expand).then(Simplify).value- 542 tests passing in 6.36 seconds
- 5,195 lines of test code (~45% test-to-code ratio)
- Tests mirror module structure
- Clear test naming that documents expected behavior
- Proper use of pytest fixtures and markers
The test suite is thorough and fast. The emphasis on "fix the code, not the tests" (per CLAUDE.md) is the right philosophy.
- Clear README with quick start examples
- 8 interactive notebooks covering real physics (quantum mechanics, general relativity, classical mechanics, electromagnetism)
- Comprehensive docstrings throughout
- Dedicated docs/ directory with topic guides
The codebase demonstrates awareness of computational costs:
- Christoffel symbol computation exploits lower-index symmetry
- Ricci and Einstein tensor calculations only compute unique components
- Strategic use of caching for pure functions
- Lazy evaluation where appropriate
- API Documentation: Auto-generated docs via Sphinx would help discoverability
- Inline Comments: The
diffgeo/modules are mathematically dense and could benefit from more algorithmic explanations Dependency Weight: Full install includes marimo, cvxpy, and pysr - consider optional extras for lightweight usageADDRESSED: marimo and cvxpy are now optional extras- Edge Cases: Some numerical code could use more robust NaN/Inf handling
This is production-quality code suitable for academic research, physics simulations, and symbolic computation education. The architecture is sound, the tests are comprehensive, and the API is intuitive. The real-world physics examples (linearized gravity, renormalization group, etc.) demonstrate both the library's capabilities and the authors' domain expertise.
Minor improvements in auto-generated documentation and optional dependency grouping would elevate this to A+.
Review conducted via comprehensive codebase exploration including static analysis, test execution, and documentation review.