Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active March 8, 2016 03:40
Show Gist options
  • Save t-nissie/9f7cd7d4991530a3fac7 to your computer and use it in GitHub Desktop.
Save t-nissie/9f7cd7d4991530a3fac7 to your computer and use it in GitHub Desktop.
いくつかの補間法
#!/usr/bin/env ruby
# interpolations.rb interpolates a data set.
# Gist: https://gist.github.com/t-nissie/9f7cd7d4991530a3fac7
# Author: Takeshi Nishimatsu
# Copying:
# Copyright (c) 2016 by Takeshi Nishimatsu
# interpolations.rb is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY.
# You can copy, modify and redistribute interpolations.rb,
# but only under the conditions described in
# the GNU General Public License version 3 (the "GPLv3").
##
class DataSet
def initialize(fd, using_x=0, using_y=1)
@x = []
@y = []
while line=fd.gets
ary = line.strip.split(nil)
@x << ary[using_x].to_f
@y << ary[using_y].to_f
end
end
def linear_interpolation(x)
i=0
while @x[i]<x
i+=1
end
@y[i-1] + (@y[i] - @y[i-1]) * (x - @x[i-1]) / (@x[i] - @x[i-1])
end
end
if $0 == __FILE__
d = DataSet.new(open(ARGV[0], "r"))
p d.linear_interpolation(ARGV[1].to_f)
d = DataSet.new(open(ARGV[0], "r"),0,2)
p d.linear_interpolation(ARGV[1].to_f)
end
#Local variables:
# compile-command: "ruby dislocation.rb"
#End:
1 1 3
2 2 3
3 2 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment