Skip to content

Commit

Permalink
Did some stuff with quads and watched Simpson
Browse files Browse the repository at this point in the history
  • Loading branch information
Ultiminer committed Apr 10, 2024
1 parent d577dfa commit 8841dba
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
52 changes: 50 additions & 2 deletions qm_analysis.h → qm_derivative.h
Original file line number Diff line number Diff line change
@@ -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 <array>
#include <functional>
Expand Down Expand Up @@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -122,6 +130,46 @@ constexpr float finite_difference(const std::function<float(float)>& fun, float
return N*fun(param+eps)-N*fun(param-eps);
}

template <size_t N>
constexpr float newton_iteration(const std::function<FRetVal(FRetVal)>& fun, float guess)
{
for(size_t i=0; i<N; ++i)
{
const FRetVal y=fun(guess);
guess-=y.val/y.slope;
}

return guess;
}
template <size_t N>
constexpr FRetVal fixpoint_iteration(const std::function<FRetVal(FRetVal)>& fun, FRetVal guess)
{
for(size_t i=0; i<N; ++i)guess=fun(guess);

return guess;
}
template <size_t N>
constexpr float fixpoint_iteration(const std::function<float(float)>& fun, float guess)
{
for(size_t i=0; i<N; ++i)guess=fun(guess);

return guess;
}
template <size_t N>
constexpr float newton_iteration(const std::function<float(float)>& fun, float guess)
{
for(size_t i=0; i<N; ++i)guess-=fun(guess)/finite_difference<1000>(fun,guess);


return guess;
}








}

Expand Down
22 changes: 22 additions & 0 deletions qm_quadrature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef QM_QUADRATURE_H_
#define QM_QUADRATURE_H_
#include <functional>

namespace QM{

template <size_t N>
constexpr float quadrature(const std::function<float(float)>& 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
2 changes: 1 addition & 1 deletion quick_math.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef QUICK_MATH_H_
#define QUICK_MATH_H_

#include <cstdint>


namespace QM{
Expand Down

0 comments on commit 8841dba

Please sign in to comment.