NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
gaussNewtonNlls.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include <array>
33#include <functional>
34#include <type_traits>
35#include <utility>
36
40#include "NumCpp/Core/Types.hpp"
42#include "NumCpp/Linalg/inv.hpp"
43#include "NumCpp/NdArray.hpp"
44
45namespace nc::linalg
46{
47 //============================================================================
48 // Method Description:
70 template<typename dtype,
71 typename... Params,
72 std::enable_if_t<std::is_arithmetic_v<dtype>, int> = 0,
73 std::enable_if_t<all_arithmetic_v<Params...>, int> = 0,
74 std::enable_if_t<all_same_v<dtype, Params...>, int> = 0>
75 std::pair<NdArray<double>, double>
76 gaussNewtonNlls(const uint32 numIterations,
78 const NdArray<dtype>& measurements,
79 const std::function<dtype(const NdArray<dtype>&, const NdArray<dtype>&)>& function,
80 const std::array<std::function<dtype(const NdArray<dtype>&, const NdArray<dtype>&)>,
81 sizeof...(Params)>& derivatives,
82 Params... initialGuess)
83 {
85
86 const auto coordinatesShape = coordinates.shape();
87
88 if (coordinatesShape.rows != measurements.size())
89 {
90 THROW_INVALID_ARGUMENT_ERROR("coordinates number of rows, and measurements size must be the same.");
91 }
92
93 NdArray<double> beta = NdArray<dtype>({ initialGuess... }).template astype<double>().transpose();
94 NdArray<double> residuals(coordinatesShape.rows, 1);
95 NdArray<double> jacobian(coordinatesShape.rows, sizeof...(Params));
96
97 const auto colSlice = coordinates.cSlice();
98 for (uint32 iteration = 1; iteration <= numIterations; ++iteration)
99 {
100 for (uint32 measIdx = 0; measIdx < coordinatesShape.rows; ++measIdx)
101 {
102 const auto coordinate = coordinates(measIdx, colSlice);
103
104 residuals[measIdx] =
105 static_cast<double>(measurements[measIdx]) - static_cast<double>(function(coordinate, beta));
106
107 for (uint32 paramIdx = 0; paramIdx < sizeof...(Params); ++paramIdx)
108 {
109 const auto& derivative = derivatives[paramIdx];
110 jacobian(measIdx, paramIdx) = static_cast<double>(derivative(coordinate, beta));
111 }
112 }
113
114 // perform the gauss-newton linear algebra
115 const auto jacobianT = jacobian.transpose();
116 const auto jacobianPsuedoInverse = linalg::inv(jacobianT.dot(jacobian));
117 const auto intermediate = jacobianPsuedoInverse.dot(jacobianT);
118 const auto deltaBeta = intermediate.dot(residuals);
119 beta += deltaBeta;
120 }
121
122 // calculate the final rms of the residuals
123 for (uint32 measIdx = 0; measIdx < coordinatesShape.rows; ++measIdx)
124 {
125 const auto coordinate = coordinates(measIdx, colSlice);
126
127 residuals[measIdx] =
128 static_cast<double>(measurements[measIdx]) - static_cast<double>(function(coordinate, beta));
129 }
130
131 return std::make_pair(beta.flatten(), rms(residuals).item());
132 }
133} // namespace nc::linalg
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
self_type transpose() const
Definition NdArrayCore.hpp:4959
Definition Cartesian.hpp:39
Definition cholesky.hpp:41
NdArray< double > inv(const NdArray< dtype > &inArray)
Definition inv.hpp:50
std::pair< NdArray< double >, double > gaussNewtonNlls(const uint32 numIterations, const NdArray< dtype > &coordinates, const NdArray< dtype > &measurements, const std::function< dtype(const NdArray< dtype > &, const NdArray< dtype > &)> &function, const std::array< std::function< dtype(const NdArray< dtype > &, const NdArray< dtype > &)>, sizeof...(Params)> &derivatives, Params... initialGuess)
Definition gaussNewtonNlls.hpp:76
constexpr bool all_arithmetic_v
Definition TypeTraits.hpp:67
constexpr bool all_same_v
Definition TypeTraits.hpp:101
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition rms.hpp:51
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40