Ruby Interpolation with gem interpolator (basic)

So, ruby already has lots of stuff done so you don’t have to reinvent the wheel and so is with Mathematical Interpolation!

Interpolator implements ways for you to do linear, cubic, lagrange and spline interpolation easyly.

Check this short example:

# a curve that rise and fall fast
t = Interpolator::Table.new 0.1 => 2, 0.4 => 3, 0.8 => 10, 1.0 => 12, 1.2 => 11, 1.4 => 8
# LINEAR
 t.style = 1
 [0, 0.5, 1.0, 1.5, 2.0].each {|x| puts( t.interpolate(x)) }	
1.66666666666667
4.75
12.0
6.5
-1.0

# LAGRANGE
 t.style = 2
 [0, 0.5, 1.0, 1.5, 2.0].each {|x| puts( t.interpolate(x)) }
2.47619047619048
5.125
12.0
5.75
-13.0

# LAGRANGE 3
 t.style = 3
 [0, 0.5, 1.0, 1.5, 2.0].each {|x| puts( t.interpolate(x)) }
3.64021164021164
4.57936507936508
12.0
6.0625
-3.00000000000003

# CUBIC
 t.style = 4
 [0, 0.5, 1.0, 1.5, 2.0].each {|x| puts( t.interpolate(x)) }
1.99712997129971
4.39777444649446
12.0
6.36704335793358
7.50922509225093

# SPLINE
 t.style = 5
 [0, 0.5, 1.0, 1.5, 2.0].each {|x| puts( t.interpolate(x)) }
1.30687830687831
4.45535714285714
12.0
6.875
35.0000000000001

There you go, a simple use of the Interpolator, very nice lib..
For tests about performance I don’t have any result or benchmark, but sure would be cool post 🙂

Get the gem

Interpolação linear com biblioteca em ruby, fácil!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s