Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/4bc0a789919f8ce9470c to your computer and use it in GitHub Desktop.
Save JoshCheek/4bc0a789919f8ce9470c to your computer and use it in GitHub Desktop.
Matrix multiplication... an ill fitting metaphor

Matrix Multiplication...

An ill fitting metaphor?

Matrix multiplication is not commutative.

This is very unintuitive, especially once they replace it with variables. At which point you get equations that look the same, but behave very differently.

I tried to compare its procedure, structurally, to multiplication and exponentiation, to see which it is closer to.

Conclusion

Matrix multiplication is procedurally closer to exponentiation, though not sufficiently close to say they are the same. It does alleviate the commutativity issue, though, as exponentiation is not commutative either (2³ ≠ 3²), so at least you expect the behaviour.

The metaphor is probably just a poor fit, and the right name is "the one that helps us think about it best".

Also, worth noting that matrix addition is a series of additions (example), which is what multiplication is (3*5 = 3+3+3+3+3). So naming this process "addition" is pretty questionable, as well.

# 3*4 means sum 4 groups of 3 = 0+3+3+3+3
# 3^4 means multiply 4 groups of 3 = 1*3*3*3*3
# = (1+1+1)*3*3*3*3
# = ((1+1+1)+(1+1+1)+(1+1+1))*3*3*3
# = (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))*3*3
# = ((((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1))))*3
#
# = (((((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1))))
# +((((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1))))
# +((((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))
# (((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)) + ((1+1+1)+(1+1+1)+(1+1+1)))))
# = 81
#
# |1 2| * |5 6| = |(1*5 + 2*7) (1*6 + 2*8)| = |19 22|
# |3 4| |7 8| |(3*5 + 4*7) (3*6 + 4*8)| |43 50|
def matrix_multiply(left, right)
left.map do |row|
right.transpose.map do |col|
row.zip(col).map { |a, b| a * b }.inject(0, :+)
end
end
end
matrix_multiply [[1, 2],
[3, 4],
],
[[5, 6],
[7, 8],
] # => [[19, 22], [43, 50]]
def multiply(left, right)
left.times.inject(0) do |product, _|
product + right
end
end
multiply 3, 4 # => 12
def exponent1(left, right)
right.times.inject(1) do |result, _|
left.times.map { result }.inject(0, :+)
end
end
exponent1 3, 4 # => 81
def exponent2(left, right)
right.times.inject(1) do |result, _|
left.times.inject(0) do |product, _|
product + result
end
end
end
exponent2 3, 4 # => 81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment