# prac0204.rb class Matrix def initialize(a=[[1,0,0],[0,1,0],[0,0,1]]) @m = a @size = 3 end attr_accessor :m def add(x) ans = Matrix.new for i in 0...@size for j in 0...@size ans.m[i][j] = x.m[i][j] end end return ans end def mult(x) ans = Matrix.new for i in 0...@size for j in 0...@size sum = 0 for k in 0...@size sum += @m[i][k] * x.m[k][j] end ans.m[i][j] = sum end end return ans end def show for i in 0...@size for j in 0...@size printf(" %5.2f",@m[i][j]) end print "\n" end end end # x = Matrix.new([[1,2,3],[1,-1, 2],[-3,-4,1]]) y = Matrix.new([[-3,-2,-1],[2,-1,2],[3,4,4]]) x.mult(y).show