class Malge::SimultaneousEquations

連立1次方程式を表現するクラス。

Public Class Methods

cramer( matrix, values ) click to toggle source

Solve equation by Cramer's. matrix は2重配列で与える。 返されるのは、解の値を格納した配列。

# File lib/malge/simultaneousequations.rb, line 24
def self.cramer( matrix, values )
  matrix = Malge::Matrix.rows( matrix )
  if ( ! matrix.square? )
    str = "Matrix is not squre: #{matrix.row_size} x #{matrix.column_size}."
    raise NotSquareError, str
  end
  if ( matrix.row_size != values.size )
    str = "Matrix size (#{matrix.row_size}) and values size (#{values.size}) mismatched."
    raise SizeMismatchError, str
  end
  if ( ! matrix.regular? )
    str = "Matrix is not regular. Degree of freedom is #{matrix.column_size - matrix.rank}.\n"
    str += matrix.to_s
    raise NotRegularError, str
  end

  results = Array.new
  n = matrix.column_size
  n.times do |i|
    tmp = Marshal.load( Marshal.dump( matrix ) )
    n.times do |j|
      tmp[ j, i ] = values[ j ]
    end
    results[i] = tmp.determinant / ( matrix.determinant )
  end
  results
end
new( matrix, values ) click to toggle source

matrix は左辺の行列、values は右辺の値の配列 ともに配列で与える。

# File lib/malge/simultaneousequations.rb, line 54
def initialize( matrix, values )
  @matrix = matrix
  @values = values
end

Public Instance Methods

cramer() click to toggle source

Cramer's formula で解く。

# File lib/malge/simultaneousequations.rb, line 60
def cramer
  Malge::SimultaneousEquations.cramer( @matrix, @values )
end