# test_solve.rb uses ruby matrix require 'matrix' puts "test_solve.rb running" # consider these three linear equations in three variables: # 2x + 10y + 8z = 54 # 0x + 7y + 4z = 30 # 5x + 5y + 5z = 35 # To solve these equations, create the two matrices: coefficients = Matrix[[Complex(2.0,0.0), Complex(10,0), Complex(8,0)], [Complex(0.0,0.0), Complex(7,0), Complex(4,0)], [Complex(5.0,0.0), Complex(5.0), Complex(5,0)]] puts "coefficients = Matrix[[Complex(2.0,0.0), Complex(10,0), Complex(8,0)]," puts " [Complex(0.0,0.0), Complex(7,0), Complex(4,0)]," puts " [Complex(5.0,0.0), Complex(5.0), Complex(5,0)]]" puts " " puts coefficients puts " " constants = Matrix[[Complex(54.0)], [Complex(30,0)], [Complex(35,0)]] puts "constants = Matrix[[Complex(54.0)], [Complex(30,0)], [Complex(35,0)]]" puts constants puts " " # Take the inverse of the coefficient matrix, and multiply it by the results matrix. # The result will be a matrix containing the values for your variables. solutions = coefficients.inverse * constants # => Matrix[[Complex(1,0)], [Complex(2,0)], [Complex(4,0)]] puts "solutions = coefficients.inverse * constants" puts solutions puts " " puts "test_solve.rb finished"