Ruby错误的结果转向矢量计算

时间:2015-01-06 21:21:37

标签: ruby math vector

我们正在尝试实施“Calculating coefficients of interpolating polynomial using Neville's algorithm。”

这是我们的Vector Class:

class Wektor < Array
  attr_accessor :coordinatesAmount

  def initialize(length)
    super(length, 0)
    @coordinatesAmount = length
  end

  def SetVector(w)
    for i in (0...coordinatesAmount) do
      self[i] = w[i]
    end
    return self
  end

  def MultiplyBy(n)
    for i in (0...coordinatesAmount) do
      self[i] *= n      
    end
    return self
  end

  def DivideBy(n)
    for i in (0...coordinatesAmount) do
      self[i] /= n
    end
    return self
  end

  def Sub(w)
    for i in (0...coordinatesAmount) do
      self[i] -= w[i]
    end
    return self
  end

  def Add(w)
    for i in (0...coordinatesAmount) do
      self[i] += w[i]
    end
    return self
  end

  def ShiftRight
    coordinatesAmount.downto(1) { |i|
      self[i-1] = self[i-2]
    }
    self[0] = 0
    return self
  end

这是我们的计算部分:

require_relative 'wektor.rb'
#Coefficients

def Coefficients(i, j, tabX, tabY, pyramid)
  #mnozymy przez Xj
  length = pyramid[i][j-1].length

  temp = Wektor.new(length).SetVector(pyramid[i][j-1])
  temp.MultiplyBy(tabX[j])
  pyramid[i][j].SetVector(temp)

  #odjęcie Pi,j-1 przesuniętego o 1 w prawo
  temp = Wektor.new(length).SetVector(pyramid[i][j-1])
  temp.ShiftRight
  pyramid[i][j].Sub(temp)

  #dodanie Pi+1, j przesuniętego o 1 w prawo
  temp = Wektor.new(length).SetVector(pyramid[i+1][j])
  temp.ShiftRight
  pyramid[i][j].Add(temp)

  #odjęcie Pi+1, j pomnożonego przez Xi
  temp = Wektor.new(length).SetVector(pyramid[i+1][j])
  temp.MultiplyBy(tabX[i])
  pyramid[i][j].Sub(temp)

  #podzielenie przez (Xj - Xi)
  pyramid[i][j].DivideBy(tabX[j] - tabX[i])

  return pyramid[i][j]
end

#CalculateResult 
def CalculatePolynomialResult(x,y,n)
  pyramid = Hash.new {|h,k| h[k] = Wektor.new(n)}

  for i in 0...n do
      for j in 0...n-i do
          if (j != i+j) then
              next
          end

          pyramid[j][j] = Wektor.new(n)
          pyramid[j][j].push(y[j])
      end
  end

  for i in 0...n do
      for j in 0...n-i do
          if (j == i+j) then
              next
          end

          pyramid[j][i+j] = Wektor.new(n)
          Coefficients(j, i+j, x, y, pyramid)
      end
  end

  return pyramid[0][n-1]

end

当Coefficients方法计算我们仍然得到零的东西时,我们正面临着这个问题。没有异常错误,但我们的Vector是零。我在Math Overflow中添加了一个Source链接来解释发生了什么。

这是the same code but written in C#,用于.NET。

为什么我们的载体没用?喜欢什么东西会覆盖它们或者可能带有一些价值/参考?我们无法跟踪该问题。

0 个答案:

没有答案