Overview
Giac is a sophisticated computer algebra system built with a modular architecture that separates concerns between different computational domains. The system is organized under thegiac namespace and consists of several interconnected layers.
Namespace Organization
All Giac components are contained within thegiac namespace (unless compiled with NO_NAMESPACE_GIAC). This provides clean separation from other libraries and prevents naming conflicts.
Core Type System
At the heart of Giac is thegen class (defined in gen.h), which serves as a universal container for all mathematical objects. The type system uses a tagged union approach for efficiency:
Type Hierarchy
Memory Management
Giac uses reference counting for efficient memory management:- Immediate types (
_INT_,_DOUBLE_): Stored directly in thegenobject without heap allocation - Pointer types: Use reference-counted smart pointers with structures like
ref_mpz_t,ref_vecteur, etc.
Key Modules
1. Symbolic Layer (symbolic.h)
Handles symbolic expressions and provides:
- Expression tree representation
- Pattern matching and rewriting
- Simplification algorithms
2. Polynomial Layer (poly.h, monomial.h)
Implements:
- Multivariate polynomials as sparse tensors
- Efficient polynomial arithmetic
- GCD and factorization algorithms
3. Linear Algebra (lin.h, vecteur.h)
Provides:
- Matrix and vector operations
- Gaussian elimination
- Eigenvalue/eigenvector computation
4. Calculus (derive.h, intg.h)
Implements:
- Symbolic differentiation
- Integration algorithms (Risch, numerical methods)
- Series expansion
5. Equation Solving (solve.h)
Handles:
- Polynomial equation solving
- Systems of equations
- Differential equations
6. Number Theory (ifactor.h, modpoly.h)
Supports:
- Integer factorization
- Modular arithmetic
- Primality testing
Component Interaction Flow
Evaluation Architecture
Expression Evaluation
Giac uses a multi-level evaluation system:- Level 0: No evaluation
- Level 1: Single pass evaluation
- Higher levels: More aggressive simplification
Context-Based Evaluation
All evaluations occur within a context that stores:- Symbol table (variable bindings)
- Global settings (angle mode, precision, etc.)
- Evaluation history
- Parent/child context relationships
Platform Adaptations
Giac supports multiple architectures through compile-time configuration:Smart Pointers (64-bit optimization)
Double Precision Handling
Integration Points
External Libraries
Giac integrates with:- GMP/MPFR: Arbitrary precision arithmetic
- NTL: Advanced number theory algorithms
- PARI: Additional number theory support
- GSL: Numerical methods
Language Bindings
The architecture supports:- C++ native API
- Python bindings
- JavaScript (via Emscripten)
- Java (Android)
Performance Considerations
Type Dispatch Optimization
Type Dispatch Optimization
Binary operations use a computed switch value:
Memory Allocation
Memory Allocation
- Small integers are stored directly (no allocation)
- Reference counting prevents unnecessary copies
- Copy-on-write for large structures
Algorithm Selection
Algorithm Selection
Giac automatically selects algorithms based on problem size:
- Karatsuba multiplication for large polynomials
- FFT multiplication for very large integers
- Modular methods for polynomial GCD
Thread Safety
Giac provides thread support through:- Per-thread contexts
- Mutex protection for shared resources
- Configurable thread evaluation with
thread_param
Next Steps
Data Types
Learn about the gen class and type system
Contexts
Understand evaluation contexts
