Created
May 18, 2016 15:03
-
-
Save gorborukov/abab59a34f71939a07396d9bf7511637 to your computer and use it in GitHub Desktop.
matrix multiplying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def multiply m_1, m_2 | |
m_2 = m_2.reduce(:zip).map(&:flatten) | |
m_1.collect do |i| | |
m_2.collect do |j| | |
i.zip(j).map{|k| k.reduce(:*)}.reduce(:+) | |
end | |
end | |
end | |
m_1 = Array.new(2){Array.new(2){rand}} | |
m_2 = Array.new(2){Array.new(2){rand}} | |
puts "Matrix 1:\n" | |
puts m_1.map(&:inspect) | |
puts "Matrix 2:\n" | |
puts m_2.map(&:inspect) | |
puts "Result:\n" | |
puts (multiply m_1, m_2).map(&:inspect) | |
require 'minitest/spec' | |
require 'minitest/autorun' | |
describe "multiply" do | |
it "passes" do | |
multiply([[0.0, -0.25],[0.25, 0.0]], | |
[[0.0, -0.25],[0.25, 0.0]]).must_equal [[-0.0625, -0.0], [0.0, -0.0625]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment