class idouble
The idouble class is AADC’s primary active type, designed as a drop-in replacement for native double.
Overview
idouble is defined in aadc/idouble.h and enables extraction of valuation graphs (DAGs) which can be JIT compiled for forward and adjoint evaluations when required. The class maintains identical memory layout to native double by containing only a single double val member and no virtual functions.
assert(sizeof(double) == sizeof(idouble)); // Always trueInternal State Flags
Each idouble object maintains two internal flags managed by AADC Core:
- IsRandom: Indicates connection to graph input that can change between kernel executions
- IsDiff: Indicates connection to graph input used for derivative computation
These flags propagate automatically through recorded operations and can be queried using isRandom() and isDiff() methods.
Variable Types in Recorded Graph
AADC classifies idouble variables into four categories based on their flag combinations:
| Type | IsRandom | IsDiff | Description |
|---|---|---|---|
| Constants | false | false | Fixed values, no differentiation |
| Random non-diff inputs | true | false | Variable inputs, no derivatives needed |
| Random diff inputs | true | true | Variable inputs requiring derivatives |
| Non-random diff inputs | false | true | Fixed inputs requiring derivatives |
By default, idouble variables are treated as constants. Users must explicitly mark variables using the methods below.
Input/Output Marking Methods
markAsInput()
AADCArgument markAsInput()- Sets
IsRandom = trueandIsDiff = true - Returns
AADCArgumentfor setting input values and reading adjoints - Use for variables that change between executions AND require derivatives
markAsDiff()
AADCArgument markAsDiff()- Sets
IsRandom = falseandIsDiff = true
- Returns
AADCArgumentfor reading adjoints only - Use for fixed values that require derivative computation
markAsInputNoDiff()
AADCArgument markAsInputNoDiff()- Sets
IsRandom = trueandIsDiff = false - Returns
AADCArgumentfor setting input values only - Use for variables that change but don’t require derivatives
markAsOutput()
AADCResult markAsOutput()- Marks variable as output of the recorded function
- Returns
AADCResultfor reading function values and setting adjoint seeds - Use for all output variables of the function
Recording Control
Static methods for managing the recording state:
static bool isRecording() // Check if recording is active
static void pauseRecording() // Temporarily disable recording
static void resumeRecording() // Re-enable recordingRecording control affects whether operations are tracked. When recording is OFF, idouble behaves like native double with few exceptions.
Type Conversions
Implicit Conversion Prevention
operator double() const = delete; // Prevents accidental conversionExplicit Conversions
Explicit conversions to integer types are available when AADC_ALLOW_TO_PASSIVE_BOOL is defined:
explicit operator int() const
explicit operator bool() const
explicit operator long() const
// ... other integer typesThese conversions include runtime checks for active-to-passive conversion, helping debug potential issues in AAD code.
Mathematical Operations
idouble supports all standard mathematical operations through operator overloading:
Basic Arithmetic
- Binary operators:
+,-,*,/ - Assignment operators:
+=,-=,*=,/= - Unary operators:
+,- - Increment/decrement:
++,--
Mathematical Functions
Standard mathematical functions are available in the std namespace:
- Trigonometric:
sin(),cos(),tan(), etc. - Exponential/logarithmic:
exp(),log(),log10(), etc. - Power functions:
pow(),sqrt(), etc. - Other:
abs(),fabs(),min(),max(), etc.
Comparison Operations
All comparison operators are supported: ==, !=, <, <=, >, >=.
Note that comparisons yield active ibool results
Memory Layout and Performance
The idouble class design ensures:
- Zero memory overhead: Identical size to native
doublewhen not recording - Compatible: Direct memory compatibility with existing data structures(e.g.,
std::vector<double>,double*) - Minimal runtime overhead: Operations are recorded only when
isRecording()returns true - Nearly identical performance as native
doublewhen used with Matlogica AADC++ compiler
Integration Notes
- Drop-in replacement: Can replace
doublein most contexts with minimal code changes - Template compatibility: Works with template functions expecting floating-point types
- STL compatibility: Can be used with standard containers and algorithms
- Mixed-mode support: Can coexist with native
doublein the same codebase