Skip to content

Commit

Permalink
Performance improvements for simpsons_rule and simpsons38_rule
Browse files Browse the repository at this point in the history
Improving performance of simpsons_rule and simpsons38_rule
  • Loading branch information
fusion809 committed Jul 9, 2020
1 parent 1271feb commit cdd820f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
4 changes: 4 additions & 0 deletions desktop.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[ViewState]
Mode=
Vid=
FolderType=Documents
37 changes: 18 additions & 19 deletions src/Simpson.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
function stepwise_simpsons(f::Function, h::Number, x::Number, i::Integer, N::Number, k::Integer)
if k==1
if i == 1 || i == N + 1
return h / 3 * f(x)
elseif (i % 2) == 1
return 2 * h / 3 * f(x)
else
return 4 * h / 3 * f(x)
end
elseif k==2
if i==1 || i == N + 1
return (3*h/8)*f(x)
elseif ((i-1) % 3) == 0
return (3*h/4)*f(x)
else
return (9*h/8)*f(x)
end
function stepwise_simpsons(f::Function, h::Number, x::Number, i::Integer, N::Number)
if i == 1 || i == N + 1
return h / 3 * f(x)
elseif (i % 2) == 1
return 2 * h / 3 * f(x)
else
return 4 * h / 3 * f(x)
end
end

function stepwise_simpsons38(f::Function, h::Number, x::Number, i::Integer, N::Number)
if i==1 || i == N + 1
return (3*h/8)*f(x)
elseif ((i-1) % 3) == 0
return (3*h/4)*f(x)
else
return (9*h/8)*f(x)
end
end
"""
simpsons_rule(f::Function, N::Number, a::Number, b::Number)
Expand All @@ -34,7 +33,7 @@ function simpsons_rule(f::Function, N::Number, a::Number, b::Number)
y = 0;
x = a;
for i=1:N+1
y = y + stepwise_simpsons(f, h, x, i, N, 1);
y = y + stepwise_simpsons(f, h, x, i, N);
if i < N+1
x = x + h;
end
Expand All @@ -58,7 +57,7 @@ function simpsons38_rule(f::Function, N::Number, a::Number, b::Number)
y = 0;
x = a;
for i=1:N+1
y = y + stepwise_simpsons(f, h, x, i, N, 2);
y = y + stepwise_simpsons38(f, h, x, i, N);
if i < N+1
x = x + h;
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
# Taken from https://travis-ci.com/github/fusion809/FunctionIntegrator.jl/jobs/355708079
# Taken from https://travis-ci.com/github/fusion809/FunctionIntegrator.jl/jobs/358878829
# 10th entry is simppen
L_chebyshev1=[1.435044; 0.307644; 0.045131; 0.104719; 0.116603; 0.090129; 0.088141; 0.114219; 0.162800; 0.441775; 0.102337; 0.050055; 0.048896];
L_chebyshev2=[0.197077; 0.068784; 0.043831; 0.099813; 0.111966; 0.125740; 0.103428; 0.098729; 0.064575; 8.991385; 0.096214; 0.052295; 0.049748];
L_chebyshev3 = [0.155732; 0.072039; 0.049798; 0.111535; 0.099392; 0.101607; 0.111045; 0.111373; 0.070177; 3.208550; 0.092763; 0.057960; 0.055562];
L_chebyshev4 = [0.146024; 0.070966; 0.049619; 0.092735; 0.098091; 0.100671; 0.099425; 0.099531; 0.070195; 4.146543; 0.094798; 0.056763; 0.053876];
L_jacobi = [21.200547; 0.741378; 0.079454; 0.468545; 0.220772; 2.786482; 0.531896; 0.446746; 0.479568; 22.058539; 0.242638; 1.933591; 0.694156];
L_legendre=[0.105352; 0.076702; 0.030384; 0.071562; 0.078758; 0.078509; 0.088236; 0.090079; 0.043331; 4.383104; 0.059413; 0.036632; 0.033564];
L_lobatto=[0.192820; 0.012460; 0.000103; 0.080504; 0.143451; 0.117507; 0.086438; 0.091315; 0.000142; 21.927058; 0.025977; 0.000409; 0.000141];
L_radau = [0.138636; 0.012143; 0.000040; 0.094062; 0.129707; 0.104701; 0.086289; 0.087083; 0.000057; 22.282400; 0.025089; 0.000345; 0.000109];
L_rectangle_midpoint = [0.073889; 0.000017; 0.000330; 0.019159; 0.022585; 0.030032; 0.018576; 0.021532; 0.000027; 1.863286; 0.035673; 0.000545; 0.000903];
L_simpsons=[0.060178; 0.040657; 0.000012; 0.023218; 0.041971; 0.028887; 0.025965; 0.031452; 0.018607; 13.831230; 0.047495; 0.000064; 0.000078];
L_trapezoidal=[0.161483; 0.034553; 0.000577; 0.017627; 0.032416; 0.022288; 0.019312; 0.023317; 0.013583; 13.361007; 0.077226; 0.001042; 0.001420];
L_adaptive_simpsons = [0.153903;
0.045990;
0.000090;

L_chebyshev1=[1.493455; # AiryAi(x)
0.291795; # besselj
0.041686; # cos_cot
0.104719; #
0.116603;
0.090129;
0.088141;
0.114219;
0.162800;
0.441775;
0.102337;
0.050055;
0.048896];
L_chebyshev2=[0.176496; 0.060008; 0.041355; 0.099813; 0.111966; 0.125740; 0.103428; 0.098729; 0.064575; 8.991385; 0.096214; 0.052295; 0.049748];
L_chebyshev3 = [0.150834; 0.072039; 0.049798; 0.111535; 0.099392; 0.101607; 0.111045; 0.111373; 0.070177; 3.208550; 0.092763; 0.057960; 0.055562];
L_chebyshev4 = [0.125087; 0.070966; 0.049619; 0.092735; 0.098091; 0.100671; 0.099425; 0.099531; 0.070195; 4.146543; 0.094798; 0.056763; 0.053876];
L_jacobi = [20.655073; 0.741378; 0.079454; 0.468545; 0.220772; 2.786482; 0.531896; 0.446746; 0.479568; 22.058539; 0.242638; 1.933591; 0.694156];
L_legendre=[0.122725; 0.076702; 0.030384; 0.071562; 0.078758; 0.078509; 0.088236; 0.090079; 0.043331; 4.383104; 0.059413; 0.036632; 0.033564];
L_lobatto=[0.169799; 0.012460; 0.000103; 0.080504; 0.143451; 0.117507; 0.086438; 0.091315; 0.000142; 21.927058; 0.025977; 0.000409; 0.000141];
L_radau = [0.151726; 0.012143; 0.000040; 0.094062; 0.129707; 0.104701; 0.086289; 0.087083; 0.000057; 22.282400; 0.025089; 0.000345; 0.000109];
L_rectangle_midpoint = [0.073208; 0.000017; 0.000330; 0.019159; 0.022585; 0.030032; 0.018576; 0.021532; 0.000027; 1.863286; 0.035673; 0.000545; 0.000903];
L_simpsons=[0.080275; 0.040657; 0.000012; 0.023218; 0.041971; 0.028887; 0.025965; 0.031452; 0.018607; 13.831230; 0.047495; 0.000064; 0.000078];
L_simpsons38=[0.068510; ]
L_trapezoidal=[0.149524; 0.034553; 0.000577; 0.017627; 0.032416; 0.022288; 0.019312; 0.023317; 0.013583; 13.361007; 0.077226; 0.001042; 0.001420];

N = length(L_simpsons);

Expand Down

0 comments on commit cdd820f

Please sign in to comment.