|
1 | | -# Utilities |
| 1 | +# Internal Utilities |
2 | 2 |
|
3 | | -Internal utility functions and types that may be useful for advanced users. |
| 3 | +Internal utility functions and algorithms used throughout FiniteDiff.jl. These functions are primarily for advanced users who need to understand or extend the library's functionality. |
4 | 4 |
|
5 | | -## Step Size Computation |
| 5 | +## Array Utilities |
6 | 6 |
|
7 | | -While these functions are primarily internal, they can be useful for understanding and customizing finite difference computations: |
| 7 | +Internal utilities for handling different array types and ensuring compatibility across the Julia ecosystem: |
8 | 8 |
|
9 | | -### Default Step Sizes |
| 9 | +### Type Conversion Functions |
10 | 10 |
|
11 | | -Different finite difference methods use different optimal step sizes to balance truncation error and round-off error: |
| 11 | +These functions help FiniteDiff.jl work with various array types including `StaticArrays`, structured matrices, and GPU arrays: |
12 | 12 |
|
13 | | -- **Forward differences**: `sqrt(eps(T))` - balances O(h) truncation error with round-off |
14 | | -- **Central differences**: `cbrt(eps(T))` - balances O(h²) truncation error with round-off |
15 | | -- **Hessian central**: `eps(T)^(1/4)` - balances O(h²) truncation error for second derivatives |
| 13 | +- **`_vec(x)`**: Vectorizes arrays while preserving scalars unchanged |
| 14 | +- **`_mat(x)`**: Ensures matrix format, converting vectors to column matrices |
| 15 | +- **`setindex(x, v, i...)`**: Non-mutating setindex operations for immutable arrays |
16 | 16 |
|
17 | | -### Complex Step Differentiation |
| 17 | +### Hessian Utilities |
18 | 18 |
|
19 | | -Complex step differentiation uses machine epsilon since it avoids subtractive cancellation: |
| 19 | +Helper functions specifically for Hessian computation: |
20 | 20 |
|
21 | | -⚠️ **Important**: `f` must be a function of a real variable that is also complex analytic when the input is complex! |
| 21 | +- **`_hessian_inplace(x)`**: Determines whether to use in-place operations based on array mutability |
| 22 | +- **`__Symmetric(x)`**: Wraps matrices in `Symmetric` views for mathematical correctness |
| 23 | +- **`mutable_zeromatrix(x)`**: Creates mutable zero matrices compatible with input arrays |
22 | 24 |
|
23 | | -## Array Utilities |
| 25 | +## Sparse Jacobian Internals |
| 26 | + |
| 27 | +Graph coloring and sparse matrix algorithms for efficient Jacobian computation: |
| 28 | + |
| 29 | +### Matrix Construction |
| 30 | + |
| 31 | +- **`_make_Ji(...)`**: Constructs Jacobian contribution matrices for both sparse and dense cases |
| 32 | +- **`_colorediteration!(...)`**: Core loop for sparse Jacobian assembly using graph coloring |
| 33 | +- **`_findstructralnz(A)`**: Finds structural non-zero patterns in dense matrices |
| 34 | + |
| 35 | +### Sparsity Detection |
| 36 | + |
| 37 | +- **`_use_findstructralnz(sparsity)`**: Determines when to use structural sparsity information |
| 38 | +- **`_use_sparseCSC_common_sparsity(J, sparsity)`**: Tests for common sparsity patterns between matrices |
| 39 | + |
| 40 | +### Performance Optimizations |
| 41 | + |
| 42 | +- **`fast_jacobian_setindex!(...)`**: Optimized index operations for sparse Jacobian assembly |
| 43 | +- **`void_setindex!(...)`**: Wrapper for setindex operations that discards return values |
| 44 | + |
| 45 | +## JVP Utilities |
| 46 | + |
| 47 | +- **`resize!(cache::JVPCache, i)`**: Resizes JVP cache arrays for dynamic problems |
| 48 | +- **`resize!(cache::JacobianCache, i)`**: Resizes Jacobian cache arrays for dynamic problems |
| 49 | + |
| 50 | +## Error Handling |
| 51 | + |
| 52 | +- **`fdtype_error(::Type{T})`**: Provides informative error messages for unsupported finite difference type combinations |
| 53 | + |
| 54 | +## Design Philosophy |
| 55 | + |
| 56 | +These internal utilities follow several design principles: |
| 57 | + |
| 58 | +1. **Type Stability**: All functions are designed for type-stable operations |
| 59 | +2. **Zero Allocation**: Internal utilities avoid allocations when possible |
| 60 | +3. **Genericity**: Support for multiple array types (dense, sparse, static, GPU) |
| 61 | +4. **Performance**: Optimized for the specific needs of finite difference computation |
| 62 | +5. **Safety**: Proper error handling and bounds checking where needed |
| 63 | + |
| 64 | +## Advanced Usage |
24 | 65 |
|
25 | | -Internal utilities for handling different array types and ensuring compatibility: |
| 66 | +These functions are primarily internal, but advanced users may find them useful for: |
26 | 67 |
|
27 | | -- `_vec(x)`: Vectorizes arrays while preserving scalars |
28 | | -- `_mat(x)`: Ensures matrix format, converting vectors to column matrices |
29 | | -- `setindex(x, v, i...)`: Non-mutating setindex for immutable arrays |
| 68 | +- **Custom finite difference implementations** |
| 69 | +- **Integration with other differentiation libraries** |
| 70 | +- **Performance optimization in specialized applications** |
| 71 | +- **Understanding the implementation details of FiniteDiff.jl** |
30 | 72 |
|
31 | | -These functions help FiniteDiff.jl work with various array types including `StaticArrays`, structured matrices, and GPU arrays. |
| 73 | +Most users should rely on the main API functions rather than calling these utilities directly. |
0 commit comments