Ruby 三元一次線性方程組

2022-03-30 19:02:47 字數 3414 閱讀 4056

專案裡需要求出兩個表示式之間的關係,涉及到3個未知數,就用ruby寫了乙個演算法,利用了ruby的bigdecimal,演算法本身應該不算複雜,也可以方便的擴充套件到n元線性方程。之前用js也寫了乙個,但js的浮點精度不夠,換成了ruby,都放在了github上(如果有用就直接拿過去了,free to copy and commercial use:-)

formula-solver.rb**如下:

require "bigdecimal"

#solver for a three variables linear formula (three dimensional matrix) 

# matrix m=

# a11 a12 a13

# a21 a22 a23

# a31 a32 a33

# variables = (x, y, z)

# res = (a, b, c)

# res = m * variables;

class formulasolver

def findanswer(matrixinput, resinput)

#combine matrix with resinput

matrix = array.new(3) }

for ii in 0..2

for jj in 0..2

matrix[ii][jj] =  bigdecimal.new(matrixinput[ii][jj].to_s)/bigdecimal.new("1.0")

endmatrix[ii][3] =  bigdecimal.new(resinput[ii].to_s)/bigdecimal.new("1.0")

end#step 1: row2 or row3+row1*something to get a21 and a31 0

#matrix[0][0] should not be zero, otherwise to reset it by row+row

#reset matrix to guarantee matrix[0][0]!=0

if matrix[0][0] == 0

line = 1;

if matrix[1][0] == 0 

if matrix[2][0] == 0

puts "this formula cannot be solved!"

return

endline = 2;

endfor ii in 0..3

matrix[0][ii] = matrix[0][ii]+matrix[line][ii]

endend

for ii in 1..2

if matrix[ii][0] != 0

factor = matrix[ii][0]/matrix[0][0]

for jj in 0..3

matrix[ii][jj] = matrix[ii][jj] - matrix[0][jj]*factor

endend

end#step 2: set matrix[2][1] = 0

if matrix[1][1] == 0

if matrix[2][1] == 0

puts "this formula cannot be solved!"

return

endfor ii in 1..3

matrix[1][ii] = matrix[1][ii]+matrix[2][ii]

endend

for ii in 2..2

factor = matrix[ii][1]/matrix[1][1]

if matrix[ii][1] != 0 

for jj in 1..3

matrix[ii][jj] = matrix[ii][jj] - matrix[1][jj]*factor

endend

end#step 3: set matrix[0][2], matrix[1][2] = 0

if matrix[2][2] == 0

puts "this formula cannot be solved!"

return

endfor ii in 0..1

factor = matrix[ii][2]/matrix[2][2]

if matrix[ii][2] != 0 

for jj in 2..3

matrix[ii][jj] = matrix[ii][jj] - matrix[2][jj]*factor

endend

end#step 4: set matrix[0][1] = 0

for ii in 0..0

factor = matrix[ii][1]/matrix[1][1]

if matrix[ii][1] != 0 

for jj in [1, 3]

matrix[ii][jj] = matrix[ii][jj] - matrix[1][jj]*factor

endend

end#step 5: set [0][0]=1, [1][1]=1, [2][2]=1

for ii in 0..2

matrix[ii][3] = matrix[ii][3]/matrix[ii][ii]

matrix[ii][ii] = 1.0

endmatrix

endend

測試**:test.rb

require_relative "formula-solver"

matrix = [[1, 113.9231311, 22.5034716], [1, 113.95313110000001, 22.5034716], [1, 113.95313110000001, 22.533471600000002]]

#res = [113.93454840179, 113.96456293154, 113.96454575759]

res = [113.93454840179, 113.96454575759, 113.96454575759]

puts formulasolver.new.findanswer(matrix, res)

matrix = [[1, 113.9231311, 22.5034716], [1, 113.95313110000001, 22.5034716], [1, 113.95313110000001, 22.533471600000002]]

#res = [22.506156406299, 22.506279984718, 22.536277316988]

res = [22.506279984718, 22.506279984718, 22.536277316988]

puts formulasolver.new.findanswer(matrix, res)

執行ruby test.rb得到輸出:(略)

線性方程組

給出乙個線性方程組的標準形式 a11x1 a12x 2 a1nx na21x 1 a22 x2 a2n xnan 1x1 an2x 2 annx n b1 b2 bn 1x 2y 34x 5y 6 1 2 這裡由克萊姆法則進行計算得出xy 3625 14 25 3 5 2 61 5 2 4 3 3 ...

線性方程組

若線性方程組相容,則此方程組有1個或無窮多個解 若線性方程組不相容,則該方程組無解。線性方程組所有解的集合被稱為線性方程組的解集 若線性方程組不相容,則解集為空集。若兩個含有相同變數的方程組具有相同的解集,則稱它們是等價的。有三種運算可以得到等價的方程組 交換任意兩個方程的順序 任一方程兩邊同乘乙個...

線性方程組

給出乙個線性方程組,有 n 個未知數和 m 個方程 a x 1 a x 2 a x n b 1 a x 1 a x 2 a x n b 2 a x 1 a x 2 a x n b m 對於解該線性方程組,首先構造增廣矩陣,按列分塊 a left begin a a a b a a a b a a a...