Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <cstdlib>
#include <iostream>
void wikipediaExample()
{
{
const double s = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
return (beta1 * s) / (beta2 + s);
};
{
const double s = coordinates.
at(0);
const double beta2 = betas.at(1);
return s / (beta2 + s);
};
{
const double s = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
};
const double beta1Guess = 0.9;
const double beta2Guess = 0.2;
rateMeasured,
function,
{ delFdelBeta1, delFdelBeta2 },
beta1Guess,
beta2Guess);
std::cout << "==========Wikipedia Example==========\n";
std::cout << "beta values = " << betas;
std::cout <<
"RMS = " <<
rms <<
'\n';
}
void exponentialExample()
{
{
const double t = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
};
{
const double t = coordinates.
at(0);
const double beta2 = betas.at(1);
};
{
const double t = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
return beta1 * t *
nc::exp(beta2 * t);
};
const double beta1Guess = 6.0;
const double beta2Guess = 0.3;
population,
exponentialFunction,
{ delFdelBeta1, delFdelBeta2 },
beta1Guess,
beta2Guess);
std::cout << "==========Exponential Population Example==========\n";
std::cout << "beta values = " << betas;
std::cout <<
"RMS = " <<
rms <<
'\n';
}
void sinusoidalExample()
{
nc::NdArray<double> temperature = { 61.0, 65.0, 72.0, 78.0, 85.0, 90.0, 92.0, 92.0, 88.0, 81.0, 72.0, 63.0 };
{
const double t = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
const double beta3 = betas.at(2);
const double beta4 = betas.at(3);
return beta1 *
nc::sin(beta2 * t + beta3) + beta4;
};
{
const double t = coordinates.
at(0);
const double beta2 = betas.at(1);
const double beta3 = betas.at(2);
};
{
const double t = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
const double beta3 = betas.at(2);
return beta1 * t *
nc::cos(beta2 * t + beta3);
};
{
const double t = coordinates.
at(0);
const double beta1 = betas.at(0);
const double beta2 = betas.at(1);
const double beta3 = betas.at(2);
return beta1 *
nc::cos(beta2 * t + beta3);
};
const double beta1Guess = 17.0;
const double beta2Guess = 0.5;
const double beta3Guess = 10.5;
const double beta4Guess = 77.0;
temperature,
sinusodialFunction,
{ delFdelBeta1, delFdelBeta2, delFdelBeta3, delFdelBeta4 },
beta1Guess,
beta2Guess,
beta3Guess,
beta4Guess);
std::cout << "==========Sinusodial Temperature Example==========\n";
std::cout << "beta values = " << betas;
std::cout <<
"RMS = " <<
rms <<
'\n';
}
{
const auto x = coordinates.
at(0);
const auto y = coordinates.
at(1);
const auto a = betas.
at(0);
const auto x0 = betas.
at(2);
const auto y0 = betas.
at(3);
const auto sigmaX = betas.
at(4);
const auto sigmaY = betas.
at(5);
}
{
const auto dcOffset = betas.
at(1);
return baseGaussianFunction(coordinates, betas) + dcOffset;
}
void twoDimensionalGaussianExample()
{
auto coords = nc::arange<double>(-3.0, 3.1, 0.1);
auto [y, x] = nc::meshgrid<double>(coords, coords);
const auto aTruth = nc::random::randFloat<double>(900.0, 1100.0);
const auto dcOffsetTruth = nc::random::randFloat<double>(90.0, 100.0);
const auto x0Truth = nc::random::randFloat<double>(-0.5, 0.5);
const auto y0Truth = nc::random::randFloat<double>(-0.5, 0.5);
const auto sigmaXTruth = nc::random::randFloat<double>(0.9, 1.1);
const auto sigmaYTruth = nc::random::randFloat<double>(0.9, 1.1);
nc::NdArray<double> betasTruth = { aTruth, dcOffsetTruth, x0Truth, y0Truth, sigmaXTruth, sigmaYTruth };
const auto cSlice = coordinates.
cSlice();
for (
nc::uint32 i = 0; i < measurements.size(); ++i)
{
measurements[i] = gaussianFunction(coordinates(i, cSlice), betasTruth);
}
auto noise = nc::random::randN<double>(measurements.shape()) * 10.0;
measurements += noise;
{
const auto a = betas.
at(0);
return baseGaussianFunction(coordinates_, betas) / a;
};
{
const auto x_ = coordinates_.
at(0);
const auto x0 = betas.
at(2);
const auto sigmaX = betas.
at(4);
return (x_ - x0) /
nc::square(sigmaX) * baseGaussianFunction(coordinates_, betas);
};
{
const auto y_ = coordinates_.
at(1);
const auto y0 = betas.
at(3);
const auto sigmaY = betas.
at(5);
return (y_ - y0) /
nc::square(sigmaY) * baseGaussianFunction(coordinates_, betas);
};
{
const auto x_ = coordinates_.
at(0);
const auto x0 = betas.
at(2);
const auto sigmaX = betas.
at(4);
return (
nc::square(x_ - x0) /
nc::power(sigmaX, 3)) * baseGaussianFunction(coordinates_, betas);
};
{
const auto y_ = coordinates_.
at(1);
const auto y0 = betas.
at(2);
const auto sigmaY = betas.
at(4);
return (
nc::square(y_ - y0) /
nc::power(sigmaY, 3)) * baseGaussianFunction(coordinates_, betas);
};
const double aGuess = aTruth + nc::random::randN<double>() * 5.0;
const double dcOffsetGuess = dcOffsetTruth + nc::random::randN<double>() * 5.0;
const double x0Guess = x0Truth + nc::random::randN<double>() * 0.2;
const double y0Guess = y0Truth + nc::random::randN<double>() * 0.2;
const double sigmaXGuess = sigmaXTruth + nc::random::randN<double>() * 0.2;
const double sigmaYGuess = sigmaYTruth + nc::random::randN<double>() * 0.2;
coordinates,
measurements,
FunctionType(gaussianFunction),
{ delFdelA, delFdelDcOffset, delFdelX0, delFdelY0, delFdelSigmaX, delFdelSigmaY },
aGuess,
dcOffsetGuess,
x0Guess,
y0Guess,
sigmaXGuess,
sigmaYGuess);
nc::NdArray<double> initialGuess = { aGuess, dcOffsetGuess, x0Guess, y0Guess, sigmaXGuess, sigmaYGuess };
std::cout << "==========2D Gaussian Example==========\n";
std::cout << "truth values = " << betasTruth;
std::cout << "initial guess = " << initialGuess;
std::cout << "beta values = " << betas;
std::cout <<
"RMS = " <<
rms <<
'\n';
}
int main()
{
wikipediaExample();
exponentialExample();
sinusoidalExample();
twoDimensionalGaussianExample();
return EXIT_SUCCESS;
}
self_type transpose() const
Definition: NdArrayCore.hpp:4882
reference at(index_type inIndex)
Definition: NdArrayCore.hpp:1034
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition: NdArrayCore.hpp:1008
uint32 rows
Definition: Core/Shape.hpp:44
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:77
void seed(int inSeed)
Definition: generator.hpp:46
constexpr dtype power(dtype inValue, uint8 inExponent) noexcept
Definition: Functions/power.hpp:52
constexpr dtype square(dtype inValue) noexcept
Definition: square.hpp:47
auto sin(dtype inValue) noexcept
Definition: sin.hpp:49
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
auto exp(dtype inValue) noexcept
Definition: exp.hpp:49
NdArray< double > rms(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: rms.hpp:51
std::uint32_t uint32
Definition: Types.hpp:40
NdArray< dtype > transpose(const NdArray< dtype > &inArray)
Definition: transpose.hpp:45
NdArray< dtype > vstack(std::initializer_list< NdArray< dtype > > inArrayList)
Definition: vstack.hpp:49