|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "numerical/math/Matrix.hpp" |
| 4 | +#include <algorithm> |
| 5 | +#include <cmath> |
| 6 | +#include <gtest/gtest.h> |
| 7 | +#include <limits> |
| 8 | + |
| 9 | +namespace math::test |
| 10 | +{ |
| 11 | + template<typename T, std::size_t N> |
| 12 | + math::Vector<T, N> DefaultFiniteDifferenceSteps(const math::Vector<T, N>& x) |
| 13 | + { |
| 14 | + static_assert(std::is_floating_point_v<T>, "DefaultFiniteDifferenceSteps requires a floating-point type"); |
| 15 | + const T cbrtEps = std::cbrt(std::numeric_limits<T>::epsilon()); |
| 16 | + math::Vector<T, N> h; |
| 17 | + for (std::size_t i = 0; i < N; ++i) |
| 18 | + h.at(i, 0) = cbrtEps * std::max(T{ 1 }, std::abs(x.at(i, 0))); |
| 19 | + return h; |
| 20 | + } |
| 21 | + |
| 22 | + template<typename T, std::size_t N, typename Func> |
| 23 | + math::Vector<T, N> CentralDifferenceGradient(Func f, const math::Vector<T, N>& x, const math::Vector<T, N>& h) |
| 24 | + { |
| 25 | + static_assert(std::is_floating_point_v<T>, "CentralDifferenceGradient requires a floating-point type"); |
| 26 | + math::Vector<T, N> gradient; |
| 27 | + for (std::size_t i = 0; i < N; ++i) |
| 28 | + { |
| 29 | + math::Vector<T, N> xPlus = x; |
| 30 | + math::Vector<T, N> xMinus = x; |
| 31 | + xPlus.at(i, 0) += h.at(i, 0); |
| 32 | + xMinus.at(i, 0) -= h.at(i, 0); |
| 33 | + gradient.at(i, 0) = (f(xPlus) - f(xMinus)) / (xPlus.at(i, 0) - xMinus.at(i, 0)); |
| 34 | + } |
| 35 | + return gradient; |
| 36 | + } |
| 37 | + |
| 38 | + template<typename T, std::size_t N, typename Func> |
| 39 | + math::Vector<T, N> CentralDifferenceGradient(Func f, const math::Vector<T, N>& x, T h) |
| 40 | + { |
| 41 | + math::Vector<T, N> steps; |
| 42 | + for (std::size_t i = 0; i < N; ++i) |
| 43 | + steps.at(i, 0) = h; |
| 44 | + return CentralDifferenceGradient(f, x, steps); |
| 45 | + } |
| 46 | + |
| 47 | + template<typename T, std::size_t N, typename Func> |
| 48 | + math::Vector<T, N> CentralDifferenceGradient(Func f, const math::Vector<T, N>& x) |
| 49 | + { |
| 50 | + return CentralDifferenceGradient(f, x, DefaultFiniteDifferenceSteps(x)); |
| 51 | + } |
| 52 | + |
| 53 | + template<typename T, std::size_t N, typename Func> |
| 54 | + void ExpectGradientNear(const math::Vector<T, N>& analytic, Func f, const math::Vector<T, N>& x, const math::Vector<T, N>& h, T tol) |
| 55 | + { |
| 56 | + const auto numeric = CentralDifferenceGradient(f, x, h); |
| 57 | + for (std::size_t i = 0; i < N; ++i) |
| 58 | + { |
| 59 | + const T a = analytic.at(i, 0); |
| 60 | + const T n = numeric.at(i, 0); |
| 61 | + const T absErr = std::abs(a - n); |
| 62 | + const T relErr = absErr / (std::max(std::abs(a), std::abs(n)) + std::numeric_limits<T>::min()); |
| 63 | + if (absErr > tol && relErr > tol) |
| 64 | + ADD_FAILURE() << "component[" << i << "] analytic=" << a << " numeric=" << n |
| 65 | + << " absErr=" << absErr << " relErr=" << relErr << " tol=" << tol; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + template<typename T, std::size_t N, typename Func> |
| 70 | + void ExpectGradientNear(const math::Vector<T, N>& analytic, Func f, const math::Vector<T, N>& x, T h, T tol) |
| 71 | + { |
| 72 | + math::Vector<T, N> steps; |
| 73 | + for (std::size_t i = 0; i < N; ++i) |
| 74 | + steps.at(i, 0) = h; |
| 75 | + ExpectGradientNear(analytic, f, x, steps, tol); |
| 76 | + } |
| 77 | + |
| 78 | + template<typename T, std::size_t N, typename Func> |
| 79 | + void ExpectGradientNear(const math::Vector<T, N>& analytic, Func f, const math::Vector<T, N>& x, T tol) |
| 80 | + { |
| 81 | + ExpectGradientNear(analytic, f, x, DefaultFiniteDifferenceSteps(x), tol); |
| 82 | + } |
| 83 | +} |
0 commit comments