forked from clbustos/integration
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to #6.
- Loading branch information
Showing
2 changed files
with
33 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
# Comparing the accuracy of different integraiton methods | ||
# text table is required to display the results in a nice table | ||
require 'integration' | ||
require 'text-table' | ||
|
||
# put the funtion you want to benchmark here | ||
func = lambda{|x| x} | ||
#put the actual result of the integration here | ||
actual_result = 5/2.0 + 2* Math::sin(1) | ||
METHODS = [:rectangle, :trapezoid, :simpson, :romberg, :adaptive_quadrature, | ||
:gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, | ||
:milne, :qng, :qag] | ||
TOL = 1e-9 | ||
|
||
# Function used in the benchmark. | ||
f = -> x { x } | ||
|
||
# Correct result of the integral of `f` in [0, 1]. | ||
actual_result = 5 / 2.0 + 2 * Math.sin(1) | ||
|
||
table = Text::Table.new | ||
table.head = ['Method','Result','Actual Result','Error','Accuracy'] | ||
table.head = ['Method', 'Error', 'Accuracy'] | ||
|
||
METHODS.each do |method| | ||
result = Integration.integrate(0, 1, { method: method }, &f) | ||
|
||
for method in [:rectangle,:trapezoid,:simpson,:romberg,:adaptive_quadrature, :gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, :milne,:qng, :qag] | ||
result = Integration.integrate(0,1,{:method=>method},&func) | ||
if result == nil | ||
if result.nil? | ||
puts method | ||
else | ||
error = (actual_result-result).abs | ||
table.rows << [method,result,actual_result,error,100*(1-error/actual_result.to_f)] | ||
error = (actual_result - result).abs | ||
table.rows << [method, error, 100 * (1 - error / actual_result.to_f)] | ||
end | ||
end | ||
puts table.to_s | ||
|
||
puts table.to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
# Checking the speed of different integraiton methods | ||
|
||
require 'benchmark' | ||
require 'integration' | ||
|
||
# set the number of iterations | ||
iterations = 100 | ||
puts "Benchmarking with #{iterations} iterations" | ||
METHODS = [:rectangle, :trapezoid, :simpson, :romberg, :adaptive_quadrature, | ||
:gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, | ||
:milne, :qng, :qag] | ||
TOL = 1e-9 | ||
ITERATIONS = 100 | ||
|
||
# Function used in the benchmark. | ||
f = -> x { x } | ||
|
||
puts "Benchmarking with #{ITERATIONS} iterations" | ||
|
||
# put the function to be benchmarked here | ||
func = lambda{|x| x} | ||
for method in [:rectangle,:trapezoid,:simpson,:romberg, :adaptive_quadrature, :gauss, :gauss_kronrod, :simpson3by8, :boole, :open_trapezoid, :milne, :qng, :qag] | ||
Benchmark.bm(25) do |bm| | ||
bm.report(method.to_s) do | ||
iterations.times do | ||
Integration.integrate(0,1,{:method=>method},&func) | ||
end | ||
end | ||
end | ||
Benchmark.bmbm(25) do |bm| | ||
METHODS.each do |method| | ||
params = { method: method, tolerance: TOL } | ||
bm.report(method.to_s) { ITERATIONS.times { Integration.integrate(0, 1, params, &f) } } | ||
end | ||
end |