Last active
October 4, 2022 12:12
-
-
Save drscotthawley/fa7a7c37c25158f53249bcfa5cd5174b to your computer and use it in GitHub Desktop.
Tries to multiply two arrays/matrices in a variety of ways; returns what "works"
This file contains 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 magic_mult(a, b): | |
""" | |
Tries to multiply two arrays/matrices in a variety of ways | |
Returns all possible working combos as a dict, with the shapes of their respective outputs | |
Author: Scott H. Hawley, @drscotthawley | |
""" | |
combos = ['a*b', 'a*b.T', 'a.T*b', 'a.T*b.T','b*a', 'b*a.T', 'b.T*a', 'b.T*a.T'] # elementwise multiplications | |
combos += [s.replace('*',' @ ') for s in combos] # matrix multiplications (I like the space here) | |
working_combos = {} | |
for s in combos: | |
try: | |
out = eval(s) # eval executes a string as if it were Python code. Usually regarded as a security nightmare. | |
working_combos[s] = out.shape | |
except: pass | |
return working_combos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.
.
.
.
.