Solving Matrix with a simple ruby Script

in #ruby7 years ago

This script can be used to solve matrix problems where you have unknowns. The matrices solved are such as the ones derived from equations with common unknowns.


require ‘matrix’
puts “Enter the dimension of the matrix\neg. 22”
a=gets.chomp
b=a.split(“
”)
c=b.map { |d| d.to_i}
puts “Enter the matrix you want to solve separated by commas\nUse (Enter) for new row”
c[1].times do
col=gets.chomp
split=col.split(“,”)
arr=split.map{ |e| e.to_i}
$mat=Matrix.rows($mat.to_a << arr)
end
puts $mat
puts “Now enter the answers inorder to get the unknowns\n…Expecting only one element in column”
c[0].times do
col1=gets.chomp
el=col1.to_i
arry=Array[el]
$ans1=Matrix.rows($ans1.to_a << arry)
end
puts $ans1
begin
inv=$mat.inv
res=inv*$ans1
puts “The value of your unknowns is #{res} in matrix form”
rescue => error
puts error
end

Coin Marketplace

STEEM 0.27
TRX 0.21
JST 0.038
BTC 96819.40
ETH 3702.03
USDT 1.00
SBD 3.87