Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
christoffel_dual.cpp
Go to the documentation of this file.
1/// @file src/tensor/christoffel_dual.cpp
2/// @brief Christoffel symbols via dual-number automatic differentiation.
3///
4/// Replaces the O(h²) central finite-difference approximation in
5/// christoffel.cpp with exact forward-mode autodiff using DualNumber.
6///
7/// ## Method
8/// For each coordinate direction σ ∈ {0,1,2,3}:
9/// 1. Seed the input point: xd[k] = {x(k), k==σ ? 1.0 : 0.0}
10/// 2. Evaluate the dual-number metric function at xd.
11/// 3. The .deriv component of each matrix entry is ∂g_μν/∂x^σ — exactly,
12/// with no finite-difference truncation error.
13///
14/// The assembled Christoffel symbols then use these exact derivatives:
15/// Γ^λ_μν = ½ g^λσ (∂_μ g_{νσ} + ∂_ν g_{μσ} − ∂_σ g_{μν})
16///
17/// ## Comparison with ChristoffelSymbols (finite differences)
18/// - Central FD: 8 metric evaluations, error O(h²), step-size sensitive
19/// - Dual-number: 4 metric evaluations, error 0 (machine epsilon only)
20
21#include "srfm/tensor.hpp"
22#include "srfm/constants.hpp"
23
24namespace srfm::tensor {
25
26// ─── Construction ─────────────────────────────────────────────────────────────
27
29 DualMetricFunction dual_fn)
30 : metric_(metric)
31 , dual_fn_(std::move(dual_fn)) {}
32
33// ─── Private: exact derivative via dual evaluation ────────────────────────────
34
35MetricMatrix ChristoffelSymbolsDual::dual_metric_derivative(
36 const SpacetimePoint& x, int sigma) const
37{
38 // Build the dual-number point:
39 // xd[k] = x(k) + (k==sigma ? 1 : 0)·ε
40 // So the ε-component of dual_fn(xd)(μ,ν) equals ∂g_μν/∂x^sigma exactly.
42 for (int k = 0; k < SPACETIME_DIM; ++k) {
43 xd(k) = DualNumber{x(k), k == sigma ? 1.0 : 0.0};
44 }
45
46 // Evaluate the dual metric.
47 DualMetricMatrix gd = dual_fn_(xd);
48
49 // Extract the derivative component into a plain MetricMatrix.
50 MetricMatrix dg;
51 for (int mu = 0; mu < SPACETIME_DIM; ++mu) {
52 for (int nu = 0; nu < SPACETIME_DIM; ++nu) {
53 dg(mu, nu) = gd(mu, nu).deriv;
54 }
55 }
56 return dg;
57}
58
59// ─── compute ──────────────────────────────────────────────────────────────────
60
62 // Initialise all symbols to zero.
63 ChristoffelArray result;
64 for (int l = 0; l < SPACETIME_DIM; ++l) {
65 result[l] = MetricMatrix::Zero();
66 }
67
68 // Obtain the inverse metric g^λσ via the base MetricTensor (which now
69 // applies Tikhonov regularization on near-singular metrics — Task 4).
70 std::optional<MetricMatrix> g_inv_opt = metric_.inverse(x);
71 if (!g_inv_opt) {
72 // Still singular even after regularization — return zero Christoffels.
73 return result;
74 }
75 const MetricMatrix& g_inv = *g_inv_opt;
76
77 // Compute exact metric partial derivatives via dual-number autodiff.
78 // dg[sigma](mu, nu) = ∂g_μν/∂x^σ (no truncation error)
79 std::array<MetricMatrix, SPACETIME_DIM> dg;
80 for (int s = 0; s < SPACETIME_DIM; ++s) {
81 dg[s] = dual_metric_derivative(x, s);
82 }
83
84 // Γ^λ_μν = ½ Σ_σ g^{λσ} (∂_μ g_{νσ} + ∂_ν g_{μσ} − ∂_σ g_{μν})
85 for (int lambda = 0; lambda < SPACETIME_DIM; ++lambda) {
86 for (int mu = 0; mu < SPACETIME_DIM; ++mu) {
87 for (int nu = 0; nu < SPACETIME_DIM; ++nu) {
88 double sum = 0.0;
89 for (int sigma = 0; sigma < SPACETIME_DIM; ++sigma) {
90 const double bracket =
91 dg[mu](nu, sigma) // ∂_μ g_{νσ}
92 + dg[nu](mu, sigma) // ∂_ν g_{μσ}
93 - dg[sigma](mu, nu); // ∂_σ g_{μν}
94 sum += g_inv(lambda, sigma) * bracket;
95 }
96 result[lambda](mu, nu) = 0.5 * sum;
97 }
98 }
99 }
100
101 return result;
102}
103
104// ─── contract ─────────────────────────────────────────────────────────────────
105
107 const FourVelocity& u) const {
108 // Identical to ChristoffelSymbols::contract:
109 // result^λ = Σ_{μ,ν} Γ^λ_{μν} u^μ u^ν
110 FourVelocity result = FourVelocity::Zero();
111
112 for (int lambda = 0; lambda < SPACETIME_DIM; ++lambda) {
113 double sum = 0.0;
114 for (int mu = 0; mu < SPACETIME_DIM; ++mu) {
115 for (int nu = 0; nu < SPACETIME_DIM; ++nu) {
116 sum += gamma[lambda](mu, nu) * u(mu) * u(nu);
117 }
118 }
119 result(lambda) = sum;
120 }
121
122 return result;
123}
124
125} // namespace srfm::tensor
FourVelocity contract(const ChristoffelArray &gamma, const FourVelocity &u) const
ChristoffelArray compute(const SpacetimePoint &x) const
ChristoffelSymbolsDual(const MetricTensor &metric, DualMetricFunction dual_fn)
std::optional< MetricMatrix > inverse(const SpacetimePoint &x) const
Physical and financial constants for the SRFM system.
Eigen::Matrix< DualNumber, SPACETIME_DIM, 1 > DualSpacetimePoint
Definition tensor.hpp:131
std::function< DualMetricMatrix(const DualSpacetimePoint &)> DualMetricFunction
Definition tensor.hpp:138
Eigen::Matrix< DualNumber, SPACETIME_DIM, SPACETIME_DIM > DualMetricMatrix
A 4×4 matrix of dual numbers — the metric evaluated at a dual-number point.
Definition tensor.hpp:134
std::array< MetricMatrix, SPACETIME_DIM > ChristoffelArray
Definition tensor.hpp:148
Eigen::Vector< double, SPACETIME_DIM > FourVelocity
A tangent vector at a spacetime point (four-velocity: dx^μ/dτ).
Definition types.hpp:44
Eigen::Matrix< double, SPACETIME_DIM, SPACETIME_DIM > MetricMatrix
The covariant metric tensor g_μν: a 4×4 symmetric matrix.
Definition types.hpp:47
Eigen::Vector< double, SPACETIME_DIM > SpacetimePoint
Definition types.hpp:41
static constexpr int SPACETIME_DIM
Dimensionality of the financial spacetime manifold (1 time + 3 assets).
Definition types.hpp:21
Tensor Calculus & Covariance Engine — AGT-04 public API.