2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// NumericalAnalysis.cpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 11/13/20.
|
|
|
|
//
|
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "numerical_analysis.h"
|
|
|
|
#include "../lin_alg/lin_alg.h"
|
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
#include <climits>
|
|
|
|
#include <cmath>
|
2023-01-23 21:13:26 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2023-04-22 17:17:58 +02:00
|
|
|
/*
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::numDiff(real_t (*function)(real_t), real_t x) {
|
|
|
|
real_t eps = 1e-10;
|
2023-01-24 19:00:54 +01:00
|
|
|
return (function(x + eps) - function(x)) / eps; // This is just the formal def. of the derivative.
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::numDiff_2(real_t (*function)(real_t), real_t x) {
|
|
|
|
real_t eps = 1e-5;
|
2023-01-24 19:00:54 +01:00
|
|
|
return (function(x + 2 * eps) - 2 * function(x + eps) + function(x)) / (eps * eps);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::numDiff_3(real_t (*function)(real_t), real_t x) {
|
|
|
|
real_t eps = 1e-5;
|
|
|
|
real_t t1 = function(x + 3 * eps) - 2 * function(x + 2 * eps) + function(x + eps);
|
|
|
|
real_t t2 = function(x + 2 * eps) - 2 * function(x + eps) + function(x);
|
2023-01-24 19:00:54 +01:00
|
|
|
return (t1 - t2) / (eps * eps * eps);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::constantApproximation(real_t (*function)(real_t), real_t c) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return function(c);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::linearApproximation(real_t (*function)(real_t), real_t c, real_t x) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return constantApproximation(function, c) + numDiff(function, c) * (x - c);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::quadraticApproximation(real_t (*function)(real_t), real_t c, real_t x) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return linearApproximation(function, c, x) + 0.5 * numDiff_2(function, c) * (x - c) * (x - c);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::cubicApproximation(real_t (*function)(real_t), real_t c, real_t x) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return quadraticApproximation(function, c, x) + (1 / 6) * numDiff_3(function, c) * (x - c) * (x - c) * (x - c);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::numDiff(real_t (*function)(std::vector<real_t>), std::vector<real_t> x, int axis) {
|
2023-01-24 19:00:54 +01:00
|
|
|
// For multivariable function analysis.
|
|
|
|
// This will be used for calculating Jacobian vectors.
|
|
|
|
// Diffrentiate with respect to indicated axis. (0, 1, 2 ...)
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t eps = 1e-10;
|
|
|
|
std::vector<real_t> x_eps = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_eps[axis] += eps;
|
|
|
|
|
|
|
|
return (function(x_eps) - function(x)) / eps;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::numDiff_2(real_t (*function)(std::vector<real_t>), std::vector<real_t> x, int axis1, int axis2) {
|
2023-01-24 19:00:54 +01:00
|
|
|
//For Hessians.
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t eps = 1e-5;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_pp = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_pp[axis1] += eps;
|
|
|
|
x_pp[axis2] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_np = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_np[axis2] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_pn = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_pn[axis1] += eps;
|
|
|
|
|
|
|
|
return (function(x_pp) - function(x_np) - function(x_pn) + function(x)) / (eps * eps);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::numDiff_3(real_t (*function)(std::vector<real_t>), std::vector<real_t> x, int axis1, int axis2, int axis3) {
|
2023-01-24 19:00:54 +01:00
|
|
|
// For third order derivative tensors.
|
|
|
|
// NOTE: Approximations do not appear to be accurate for sinusodial functions...
|
|
|
|
// Should revisit this later.
|
2023-04-16 16:05:50 +02:00
|
|
|
real_t eps = 1e-5;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_ppp = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_ppp[axis1] += eps;
|
|
|
|
x_ppp[axis2] += eps;
|
|
|
|
x_ppp[axis3] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_npp = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_npp[axis2] += eps;
|
|
|
|
x_npp[axis3] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_pnp = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_pnp[axis1] += eps;
|
|
|
|
x_pnp[axis3] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_nnp = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_nnp[axis3] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_ppn = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_ppn[axis1] += eps;
|
|
|
|
x_ppn[axis2] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_npn = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_npn[axis2] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> x_pnn = x;
|
2023-01-24 19:00:54 +01:00
|
|
|
x_pnn[axis1] += eps;
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t thirdAxis = function(x_ppp) - function(x_npp) - function(x_pnp) + function(x_nnp);
|
|
|
|
real_t noThirdAxis = function(x_ppn) - function(x_npn) - function(x_pnn) + function(x);
|
2023-01-24 19:00:54 +01:00
|
|
|
return (thirdAxis - noThirdAxis) / (eps * eps * eps);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::newtonRaphsonMethod(real_t (*function)(real_t), real_t x_0, real_t epoch_num) {
|
|
|
|
real_t x = x_0;
|
2023-01-24 19:00:54 +01:00
|
|
|
for (int i = 0; i < epoch_num; i++) {
|
|
|
|
x -= function(x) / numDiff(function, x);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::halleyMethod(real_t (*function)(real_t), real_t x_0, real_t epoch_num) {
|
|
|
|
real_t x = x_0;
|
2023-01-24 19:00:54 +01:00
|
|
|
for (int i = 0; i < epoch_num; i++) {
|
|
|
|
x -= ((2 * function(x) * numDiff(function, x)) / (2 * numDiff(function, x) * numDiff(function, x) - function(x) * numDiff_2(function, x)));
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:10:00 +01:00
|
|
|
real_t MLPPNumericalAnalysis::invQuadraticInterpolation(real_t (*function)(real_t), std::vector<real_t> x_0, int epoch_num) {
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t x = 0;
|
|
|
|
std::vector<real_t> currentThree = x_0;
|
2023-01-24 19:00:54 +01:00
|
|
|
for (int i = 0; i < epoch_num; i++) {
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t t1 = ((function(currentThree[1]) * function(currentThree[2])) / ((function(currentThree[0]) - function(currentThree[1])) * (function(currentThree[0]) - function(currentThree[2])))) * currentThree[0];
|
|
|
|
real_t t2 = ((function(currentThree[0]) * function(currentThree[2])) / ((function(currentThree[1]) - function(currentThree[0])) * (function(currentThree[1]) - function(currentThree[2])))) * currentThree[1];
|
|
|
|
real_t t3 = ((function(currentThree[0]) * function(currentThree[1])) / ((function(currentThree[2]) - function(currentThree[0])) * (function(currentThree[2]) - function(currentThree[1])))) * currentThree[2];
|
2023-01-24 19:00:54 +01:00
|
|
|
x = t1 + t2 + t3;
|
|
|
|
|
|
|
|
currentThree.erase(currentThree.begin());
|
|
|
|
currentThree.push_back(x);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::eulerianMethod(real_t (*derivative)(real_t), std::vector<real_t> q_0, real_t p, real_t h) {
|
2023-02-12 16:10:00 +01:00
|
|
|
int max_epoch = static_cast<int>((p - q_0[0]) / h);
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t x = q_0[0];
|
|
|
|
real_t y = q_0[1];
|
2023-01-24 19:00:54 +01:00
|
|
|
for (int i = 0; i < max_epoch; i++) {
|
|
|
|
y = y + h * derivative(x);
|
|
|
|
x += h;
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::eulerianMethod(real_t (*derivative)(std::vector<real_t>), std::vector<real_t> q_0, real_t p, real_t h) {
|
2023-02-12 16:10:00 +01:00
|
|
|
int max_epoch = static_cast<int>((p - q_0[0]) / h);
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t x = q_0[0];
|
|
|
|
real_t y = q_0[1];
|
2023-01-24 19:00:54 +01:00
|
|
|
for (int i = 0; i < max_epoch; i++) {
|
|
|
|
y = y + h * derivative({ x, y });
|
|
|
|
x += h;
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::growthMethod(real_t C, real_t k, real_t t) {
|
2023-04-22 17:17:58 +02:00
|
|
|
//dP/dt = kP
|
|
|
|
//dP/P = kdt
|
|
|
|
//integral(1/P)dP = integral(k) dt
|
|
|
|
//ln|P| = kt + C_initial
|
|
|
|
//|P| = e^(kt + C_initial)
|
|
|
|
//|P| = e^(C_initial) * e^(kt)
|
|
|
|
//P = +/- e^(C_initial) * e^(kt)
|
|
|
|
//P = C * e^(kt)
|
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
// auto growthFunction = [&C, &k](real_t t) { return C * exp(k * t); };
|
2023-01-24 19:00:54 +01:00
|
|
|
return C * std::exp(k * t);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> MLPPNumericalAnalysis::jacobian(real_t (*function)(std::vector<real_t>), std::vector<real_t> x) {
|
|
|
|
std::vector<real_t> jacobian;
|
2023-01-24 19:00:54 +01:00
|
|
|
jacobian.resize(x.size());
|
2023-02-12 16:10:00 +01:00
|
|
|
for (uint32_t i = 0; i < jacobian.size(); i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
jacobian[i] = numDiff(function, x, i); // Derivative w.r.t axis i evaluated at x. For all x_i.
|
|
|
|
}
|
|
|
|
return jacobian;
|
|
|
|
}
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> MLPPNumericalAnalysis::hessian(real_t (*function)(std::vector<real_t>), std::vector<real_t> x) {
|
|
|
|
std::vector<std::vector<real_t>> hessian;
|
2023-01-24 19:00:54 +01:00
|
|
|
hessian.resize(x.size());
|
2023-02-12 16:10:00 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < hessian.size(); i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
hessian[i].resize(x.size());
|
|
|
|
}
|
2023-02-12 16:10:00 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < hessian.size(); i++) {
|
|
|
|
for (uint32_t j = 0; j < hessian[i].size(); j++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
hessian[i][j] = numDiff_2(function, x, i, j);
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 16:10:00 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
return hessian;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<std::vector<real_t>>> MLPPNumericalAnalysis::thirdOrderTensor(real_t (*function)(std::vector<real_t>), std::vector<real_t> x) {
|
|
|
|
std::vector<std::vector<std::vector<real_t>>> tensor;
|
2023-01-24 19:00:54 +01:00
|
|
|
tensor.resize(x.size());
|
2023-02-12 16:10:00 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < tensor.size(); i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
tensor[i].resize(x.size());
|
2023-02-12 16:10:00 +01:00
|
|
|
for (uint32_t j = 0; j < tensor[i].size(); j++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
tensor[i][j].resize(x.size());
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 16:10:00 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < tensor.size(); i++) { // O(n^3) time complexity :(
|
|
|
|
for (uint32_t j = 0; j < tensor[i].size(); j++) {
|
|
|
|
for (uint32_t k = 0; k < tensor[i][j].size(); k++)
|
2023-01-24 19:00:54 +01:00
|
|
|
tensor[i][j][k] = numDiff_3(function, x, i, j, k);
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 16:10:00 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
return tensor;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::constantApproximation(real_t (*function)(std::vector<real_t>), std::vector<real_t> c) {
|
2023-01-24 19:00:54 +01:00
|
|
|
return function(c);
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::linearApproximation(real_t (*function)(std::vector<real_t>), std::vector<real_t> c, std::vector<real_t> x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:00:54 +01:00
|
|
|
return constantApproximation(function, c) + alg.matmult(alg.transpose({ jacobian(function, c) }), { alg.subtraction(x, c) })[0][0];
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::quadraticApproximation(real_t (*function)(std::vector<real_t>), std::vector<real_t> c, std::vector<real_t> x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-24 19:00:54 +01:00
|
|
|
return linearApproximation(function, c, x) + 0.5 * alg.matmult({ (alg.subtraction(x, c)) }, alg.matmult(hessian(function, c), alg.transpose({ alg.subtraction(x, c) })))[0][0];
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::cubicApproximation(real_t (*function)(std::vector<real_t>), std::vector<real_t> c, std::vector<real_t> x) {
|
2023-04-22 17:17:58 +02:00
|
|
|
//Not completely sure as the literature seldom discusses the third order taylor approximation,
|
|
|
|
//in particular for multivariate cases, but ostensibly, the matrix/tensor/vector multiplies
|
|
|
|
//should look something like this:
|
|
|
|
|
|
|
|
//(N x N x N) (N x 1) [tensor vector mult] => (N x N x 1) => (N x N)
|
|
|
|
//Perform remaining multiplies as done for the 2nd order approximation.
|
|
|
|
//Result is a scalar.
|
|
|
|
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> resultMat = alg.tensor_vec_mult(thirdOrderTensor(function, c), alg.subtraction(x, c));
|
|
|
|
real_t resultScalar = alg.matmult({ (alg.subtraction(x, c)) }, alg.matmult(resultMat, alg.transpose({ alg.subtraction(x, c) })))[0][0];
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
return quadraticApproximation(function, c, x) + (1 / 6) * resultScalar;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPNumericalAnalysis::laplacian(real_t (*function)(std::vector<real_t>), std::vector<real_t> x) {
|
|
|
|
std::vector<std::vector<real_t>> hessian_matrix = hessian(function, x);
|
|
|
|
real_t laplacian = 0;
|
2023-02-12 16:10:00 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < hessian_matrix.size(); i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
laplacian += hessian_matrix[i][i]; // homogenous 2nd derivs w.r.t i, then i
|
|
|
|
}
|
2023-02-12 16:10:00 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
return laplacian;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::string MLPPNumericalAnalysis::secondPartialDerivativeTest(real_t (*function)(std::vector<real_t>), std::vector<real_t> x) {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> hessianMatrix = hessian(function, x);
|
2023-04-22 17:17:58 +02:00
|
|
|
|
|
|
|
// The reason we do this is because the 2nd partial derivative test is less conclusive for functions of variables greater than
|
|
|
|
// 2, and the calculations specific to the bivariate case are less computationally intensive.
|
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
if (x.size() == 2) {
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t det = alg.det(hessianMatrix, hessianMatrix.size());
|
|
|
|
real_t secondDerivative = numDiff_2(function, x, 0, 0);
|
2023-01-24 19:00:54 +01:00
|
|
|
if (secondDerivative > 0 && det > 0) {
|
|
|
|
return "min";
|
|
|
|
} else if (secondDerivative < 0 && det > 0) {
|
|
|
|
return "max";
|
|
|
|
} else if (det < 0) {
|
|
|
|
return "saddle";
|
|
|
|
} else {
|
|
|
|
return "test was inconclusive";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (alg.positiveDefiniteChecker(hessianMatrix)) {
|
|
|
|
return "min";
|
|
|
|
} else if (alg.negativeDefiniteChecker(hessianMatrix)) {
|
|
|
|
return "max";
|
|
|
|
} else if (!alg.zeroEigenvalue(hessianMatrix)) {
|
|
|
|
return "saddle";
|
|
|
|
} else {
|
|
|
|
return "test was inconclusive";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 17:17:58 +02:00
|
|
|
*/
|
2023-02-12 16:06:59 +01:00
|
|
|
|
|
|
|
void MLPPNumericalAnalysis::_bind_methods() {
|
|
|
|
}
|