Class: Malge::SimultaneousEquations

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

Overview

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

Defined Under Namespace

Classes: NotRegularError, NotSquareError, SizeMismatchError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix, values) ⇒ SimultaneousEquations

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



54
55
56
57
# File 'lib/malge/simultaneousequations.rb', line 54

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

Class Method Details

.cramer(matrix, values) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#cramerObject

Cramer’s formula で解く。



60
61
62
# File 'lib/malge/simultaneousequations.rb', line 60

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