Class: Malge::SimultaneousEquations

Inherits:
Object
  • Object
show all
Defined in:
lib/malge/simultaneousequations.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix, values) ⇒ SimultaneousEquations

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



39
40
41
42
# File 'lib/malge/simultaneousequations.rb', line 39

def initialize( matrix, values )
  @matrix = matrix
  @values = values
end

Class Method Details

.cramer(matrix, values) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/malge/simultaneousequations.rb', line 19

def self.cramer( matrix, values )
  matrix = Malge::Matrix.rows( matrix )
  raise "Matrix is not squre: #{matrix.row_size} x #{matrix.column_size}." if ( ! matrix.square? )
  raise "Matrix size (#{matrix.row_size}) and values size (#{values.size}) mismatched." if ( matrix.row_size != values.size )
  raise "Matrix is not regular. Degree of freedom is #{matrix.column_size - matrix.rank}.\n" if ( ! matrix.regular? )

  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

Instance Method Details

#cramerObject

Cramer’s formula で解く。



45
46
47
# File 'lib/malge/simultaneousequations.rb', line 45

def cramer
  Malge::SimultaneousEquations.cramer( @matrix, @values )
end