Class: Interpolation::OneDimensional

Inherits:
Base
  • Object
show all
Defined in:
lib/interpolation/one_dimensional.rb

Overview

Implements one dimensional interpolation routines.

Usage

x = (1..10).step(1).to_a y = x.exp

f = Interpolation::OneDimensional.new x, y, :linear, sorted: true i = f.interpolate 2.5

puts “Interpolated value for 2.5 is #i”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, opts = {}) ⇒ OneDimensional

Constructor for all One Dimensional interpolation operations.

The function values to be supplied to this class are of the form y = f(x).

Henceforth, y will be referred to as ordinate and x as absicca. If absicca and ordinate arrays are not of the same length, then the effective size used for interpolation will be MIN(x.size, y.size).

Arguments

  • x - The collection of absiccas. Must be a 1 D NMatrix or ruby Array.

  • y - The collection of ordinates corresponding to the absicca. ‘y’ can

    either be a 1D NMatrix or Array OR a 2D NMatrix. In case y contains 
    multiple columns, the interpolation is carried out on each column,
    unless specified.
    
  • opts - Various options for carrying out the interpolation.

Options

  • :type - The kind of interpolation that the user wants to perform. Should be

    specified as a symbol. Defaults to linear. Only linear and cubic
    interpolation supported as of now. Cubic interpolation done with splines.
    
  • :sorted - Set this option as true if the absicca collection is supplied in

    the arguments in a sorted manner. If not supplied, it will be assumed
    that absiccas are not sorted and they will sorted be sorted anyway.
    
  • :axis - In case of a multidimensional ordinate matrix, specify the column over

which interpolation must be performed. axis starts indexing from 0 and should be lower than the number of columns in the ordinate matrix.

  • :precision - Specifies the precision of the interpolated values returned. Defaults

to 3.

  • :yp1 - First derivative of the 0th point (cubic spline).

  • :ypn - First derivative of the last (n-1)th point (cubic spline).

Usage

x = (0..9).step(1).to_a
y = x.map { |n| Math.exp(n) }
f = Interpolation::OneDimensional.new x,y, type: :cubic
f.interpolate 2.5
  #=> 12.287


89
90
91
92
93
94
# File 'lib/interpolation/one_dimensional.rb', line 89

def initialize x, y, opts={}
  @return_type = :array
  super(x,y,opts)
  
  compute_second_derivatives if @opts[:type] == :cubic
end

Instance Attribute Details

#return_type=(value) ⇒ Object (writeonly)

!@attribute return_type

The data type of the returned data. Set to :array, :matrix, nmatrix.
:array by default (only supports Arrray as of now).
@return [Array]   Returns data as a ruby Array if set to :array
@return [Matrix]  Returns data as a ruby Matrix if set to :matrix
@return [NMatrix] Returns data as an NMatrix if set to :nmatrix. Needs 
the NMatrix gem installed.


40
41
42
# File 'lib/interpolation/one_dimensional.rb', line 40

def return_type=(value)
  @return_type = value
end

Instance Method Details

#interpolate(interpolant) ⇒ Object Also known as: []

Performs the actual interpolation on the value passed as an argument. Kind of interpolation performed is determined according to what is specified in the constructor.

Arguments

  • interpolant - The value for which the interpolation is to be performed. Can

    either be a Numeric, Array of Numerics or NMatrix. If multidimensional
    NMatrix is supplied then will flatten it and interpolate over
    all its values. Will return answer in the form of an NMatrix if
    *interpolant* is supplied as an NMatrix.
    


107
108
109
110
111
112
113
114
115
116
# File 'lib/interpolation/one_dimensional.rb', line 107

def interpolate interpolant
  case @opts[:type]
  when :linear
    for_each (interpolant) { |x| linear_interpolation(x)  }
  when :cubic
    cubic_spline_interpolation interpolant
  else
    raise ArgumentError, "1 D interpolation of type #{@opts[:type]} not supported"
  end
end