From 8841dbae32cffe436de20eff9d6b709da2b59c67 Mon Sep 17 00:00:00 2001 From: FloDev <43553244+Ultiminer@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:10:17 +0200 Subject: [PATCH] Did some stuff with quads and watched Simpson --- qm_analysis.h => qm_derivative.h | 52 ++++++++++++++++++++++++++++++-- qm_quadrature.h | 22 ++++++++++++++ quick_math.h | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) rename qm_analysis.h => qm_derivative.h (73%) create mode 100644 qm_quadrature.h diff --git a/qm_analysis.h b/qm_derivative.h similarity index 73% rename from qm_analysis.h rename to qm_derivative.h index 3e1deb9..06472c3 100644 --- a/qm_analysis.h +++ b/qm_derivative.h @@ -1,5 +1,5 @@ -#ifndef QM_ANALYSIS_H_ -#define QM_ANALYSIS_H_ +#ifndef QM_DERIVATIVE_H_ +#define QM_DERIVATIVE_H_ #include "quick_math.h" #include #include @@ -45,6 +45,10 @@ constexpr FRetVal operator+(const FRetVal& A, FCONST c) { return {A.val+c,A.slope}; } +constexpr FRetVal operator+(FCONST c, const FRetVal& A) +{ + return {A.val+c,A.slope}; +} constexpr FRetVal operator-(const FRetVal& A, FCONST c) { return {A.val-c,A.slope}; @@ -53,6 +57,10 @@ constexpr FRetVal operator*(const FRetVal& A, FCONST c) { return {A.val*c,A.slope*c}; } +constexpr FRetVal operator*(FCONST c,const FRetVal& A) +{ + return {A.val*c,A.slope*c}; +} constexpr FRetVal operator/(const FRetVal& A, FCONST c) { return {A.val/c,A.slope/c}; @@ -122,6 +130,46 @@ constexpr float finite_difference(const std::function& fun, float return N*fun(param+eps)-N*fun(param-eps); } +template +constexpr float newton_iteration(const std::function& fun, float guess) +{ + for(size_t i=0; i +constexpr FRetVal fixpoint_iteration(const std::function& fun, FRetVal guess) +{ + for(size_t i=0; i +constexpr float fixpoint_iteration(const std::function& fun, float guess) +{ + for(size_t i=0; i +constexpr float newton_iteration(const std::function& fun, float guess) +{ + for(size_t i=0; i(fun,guess); + + + return guess; +} + + + + + + + } diff --git a/qm_quadrature.h b/qm_quadrature.h new file mode 100644 index 0000000..3ac0b8c --- /dev/null +++ b/qm_quadrature.h @@ -0,0 +1,22 @@ +#ifndef QM_QUADRATURE_H_ +#define QM_QUADRATURE_H_ +#include + +namespace QM{ + +template +constexpr float quadrature(const std::function& f, float a, float b) +{ + const float alpha{(b-a)/N}; + float sum=0.5f*(f(a)+f(b)); + for(size_t i=1; i< N-1;++i)sum+=f(a+i*alpha); + + return alpha*sum; +} + + + +} + + +#endif \ No newline at end of file diff --git a/quick_math.h b/quick_math.h index ed9a9d3..b99f879 100644 --- a/quick_math.h +++ b/quick_math.h @@ -1,6 +1,6 @@ #ifndef QUICK_MATH_H_ #define QUICK_MATH_H_ - +#include namespace QM{