Skip to content

Commit

Permalink
Improving efficiency of algorithms for rect, simps and trapez
Browse files Browse the repository at this point in the history
  • Loading branch information
fusion809 committed Oct 7, 2020
1 parent e1540f7 commit f19eeed
Show file tree
Hide file tree
Showing 18 changed files with 26 additions and 42 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ julia:
- nightly
- 1.4
- 1.5
- 1.6
os:
- linux
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FunctionIntegrator"
uuid = "7685536e-2581-4f83-bef1-2ba363c9cb91"
authors = ["Brenton Horne <[email protected]>"]
version = "0.5.1"
version = "0.6.0"

[deps]
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"
Expand Down
6 changes: 3 additions & 3 deletions src/Rectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function rectangle_rule_left(f::Function, N::Number, a::Number, b::Number)
y = 0;
x = a;
for i=1:N
y += h*f(x)
y += h*f(x);
x += h;
end
return y
Expand All @@ -34,7 +34,7 @@ function rectangle_rule_midpoint(f::Function, N::Number, a::Number, b::Number)
y = 0;
x = a+h/2;
for i=1:N
y += h*f(x)
y += h*f(x);
x += h;
end
return y
Expand All @@ -55,7 +55,7 @@ function rectangle_rule_right(f::Function, N::Number, a::Number, b::Number)
y = 0;
x = a+h;
for i=1:N
y += h*f(x)
y += h*f(x);
x += h;
end
return y
Expand Down
19 changes: 5 additions & 14 deletions src/Simpson.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
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
function stepwise_simpsons(f::Function, h::Number, x::Number)
return h/6 * (f(x) + 4*f(x+h/2) + f(x+h));
end

function stepwise_simpsons38(f::Function, h::Number, x::Number, i::Integer, N::Number)
Expand All @@ -28,15 +22,12 @@ uses [Simpson's rule](https://en.wikipedia.org/wiki/Simpson%27s_rule) to approxi
"""
function simpsons_rule(f::Function, N::Number, a::Number, b::Number)
N = convert(Int64, N);
iseven(N) || error("N must be even in order for Simspon's rule to work properly.")
h = (b-a)/N;
y = 0;
x = a;
for i=1:N+1
y = y + stepwise_simpsons(f, h, x, i, N);
if i < N+1
x += h;
end
for i=1:N
y += stepwise_simpsons(f, h, x);
x += h;
end
return y
end
Expand Down
14 changes: 4 additions & 10 deletions src/Trapezoidal.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
function stepwise_trapezoidal(f, h, x, i, N)
if i == 1 || i == N + 1
return h / 2 * f(x)
else
return h * f(x)
end
return h/2 * (f(x) + f(x+h));
end

"""
Expand All @@ -20,11 +16,9 @@ function trapezoidal_rule(f::Function, N::Number, a::Number, b::Number)
h = (b-a)/N;
y = 0;
x = a;
for i=1:N+1
y = y + stepwise_trapezoidal(f, h, x, i, N);
if i < N+1
x += h;
end
for i=1:N
y += stepwise_trapezoidal(f, h, x, i, N);
x += h;
end
return y
end
2 changes: 1 addition & 1 deletion test/airyai.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ printstyled("Performing the Airy Ai(x) test, where Ai(x) is integrated on the se
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(x -> airyai(x), 9, 0, 100) 1.0/3.0
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(x -> airyai(x), 2512, 0, 100) 1.0/3.0
@time @test simpsons_rule(x -> airyai(x), 1256, 0, 100) 1.0/3.0
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(x -> airyai(x), 3075, 0, 100) 1.0/3.0
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/besselj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ printstyled("Performing the besselj test, where BesselJ_1(2) is approximated and
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(besselj_integrand, 5, 0, pi/2) besselj(1,2)
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(besselj_integrand, 6, 0, pi/2) besselj(1,2)
@time @test simpsons_rule(besselj_integrand, 3, 0, pi/2) besselj(1,2)
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(besselj_integrand, 9, 0, pi/2) besselj(1,2)
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/cos_cot_integral.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ printstyled("Integrating cos^2(x)/(1+cot(x)) from 0 to pi/2 and comparing the re
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(cos_cot_fn, 4, 0, pi/2) 0.25
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(cos_cot_fn, 78, 0, pi/2) 0.25
@time @test simpsons_rule(cos_cot_fn, 39, 0, pi/2) 0.25
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(cos_cot_fn, 96, 0, pi/2) 0.25
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/cosine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ printstyled("Integrating cosine from 0 to pi/2 and comparing the result to the a
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(x -> cos(x), 3, 0, pi/2) 1
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(x -> cos(x), 40, 0, pi/2) 1
@time @test simpsons_rule(x -> cos(x), 20, 0, pi/2) 1
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(x -> cos(x), 48, 0, pi/2) 1
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/expnx2datan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ printstyled("Integrating e^(-x^2)/(1+x^2) on the infinite domain [-inf, inf], or
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(x -> exp(-x^2)/(x^2+1), 12, -100, 100) sol_9
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(x -> exp(-x^2)/(x^2+1), 1240, -100, 100) sol_9
@time @test simpsons_rule(x -> exp(-x^2)/(x^2+1), 620, -100, 100) sol_9
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(x -> exp(-x^2)/(x^2+1), 1767, -100, 100) sol_9
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/gaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ printstyled("Integrate exp(-x^2) from minus infinity to positive infinity and co
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(x -> exp(-x^2), 12, -100, 100) sqrt(pi)
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(x -> exp(-x^2), 536, -100, 100) sqrt(pi)
@time @test simpsons_rule(x -> exp(-x^2), 268, -100, 100) sqrt(pi)
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(x -> exp(-x^2), 780, -100, 100) sqrt(pi)
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/logarithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ printstyled("Integrating 1/x from 1 to e and comparing the result to the analyti
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(x -> 1/x, 5, 1, exp(1)) 1
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(x -> x^(-1), 70, 1, exp(1)) 1
@time @test simpsons_rule(x -> x^(-1), 34, 1, exp(1)) 1
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(x -> x^(-1), 84, 1, exp(1)) 1
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/logoverx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ printstyled("Integrating log(x)/x from 1 to e and comparing the result to the an
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(x -> log(x)/x, 5, 1, exp(1)) 0.5
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(x -> log(x)/x, 100, 1, exp(1)) 0.5
@time @test simpsons_rule(x -> log(x)/x, 50, 1, exp(1)) 0.5
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(x -> log(x)/x, 114, 1, exp(1)) 0.5
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/modbessel0.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ printstyled("Approximating the modified bessel function I_1(1) and comparing it
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(dmodified_bessel_1, 5, 0, pi/2) besseli(x,1)
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(dmodified_bessel_1, 6, 0, pi/2) besseli(x,1)
@time @test simpsons_rule(dmodified_bessel_1, 3, 0, pi/2) besseli(x,1)
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(dmodified_bessel_1, 9, 0, pi/2) besseli(x,1)
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/simppen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ printstyled("Integrating 1/sqrt(-19.6 sin(x)) from -pi to 0 and comparing the re
printstyled("Running: rombergs_method on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
@time @test abs(rombergs_method(x -> (-19.6*sin(x))^(-0.5), 24, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
printstyled("Running: simpsons_rule on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
@time @test abs(simpsons_rule(x -> (-19.6*sin(x))^(-0.5), 1e8, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
@time @test abs(simpsons_rule(x -> (-19.6*sin(x))^(-0.5), 5e7, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
printstyled("Running: simpsons38_rule on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
@time @test abs(simpsons38_rule(x -> (-19.6*sin(x))^(-0.5), 1e8+2, -pi+1e-8, -1e-8) - ellipk(1/2)/sqrt(2.45)) < 1e-4
printstyled("Running: trapezoidal_rule on [-pi+1e-8, -1e-8]. Only a rough approximation can be realistically achieved with this function, due to the singularities.\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/sinexpox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ printstyled("Integrating sin(x^2)e^(-x)/x from 0 to infinity, with the approxima
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(sinexpox, 12, 0, 100) sol_11
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(sinexpox, 4202, 0, 100) sol_11
@time @test simpsons_rule(sinexpox, 2101, 0, 100) sol_11
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(sinexpox, 5148, 0, 100) sol_11
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/sinxx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ printstyled("Integrating sin(x)/x from 0 to 100 and comparing it to the exact re
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(sinxx, 9, 0, 100) sol_8
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(sinxx, 678, 0, 100) sol_8
@time @test simpsons_rule(sinxx, 339, 0, 100) sol_8
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(sinxx, 831, 0, 100) sol_8
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down
2 changes: 1 addition & 1 deletion test/test_7.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ printstyled("Integrating (x^3+1)/(x^4 (x+1)(x^2+1)) from 1 to e and comparing th
printstyled("Running: rombergs_method\n"; color = :magenta)
@time @test rombergs_method(partfrac, 6, 1, exp(1)) sol_7
printstyled("Running: simpsons_rule\n"; color = :magenta)
@time @test simpsons_rule(partfrac, 200, 1, exp(1)) sol_7
@time @test simpsons_rule(partfrac, 100, 1, exp(1)) sol_7
printstyled("Running: simpsons38_rule\n"; color = :magenta)
@time @test simpsons38_rule(partfrac, 234, 1, exp(1)) sol_7
printstyled("Running: trapezoidal_rule\n"; color = :magenta)
Expand Down

2 comments on commit f19eeed

@fusion809
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22515

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.0 -m "<description of version>" f19eeed7f57d4136c7262ca62599f49aa235fc2d
git push origin v0.6.0

Please sign in to comment.