Function to chain multiplications / `dot` for many arrays
See original GitHub issueI wrote myself a little helper function tat I constantly use. The function chains calls of dot
which makes long matrix multiplication much more readable. I call the function mdot
for “multiple dot”.
M = np.eye(5)
# instead of this
print M.dot(M).dot(M).dot(M).dot(M)
# or this
print np.dot(np.dot(np.dot(np.dot(M, M), M), M), M)
# I can write this
print mdot(M, M, M, M, M)
This is the implementation:
def mdot(*args):
return reduce(np.dot, args)
This is a really simple function but it helps me tremendously. Are you interested in integrating something like this?
Issue Analytics
- State:
- Created 10 years ago
- Comments:16 (16 by maintainers)
Top Results From Across the Web
numpy.linalg.multi_dot — NumPy v1.24 Manual
Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order. multi_dot chains ......
Read more >Matrix Chain Multiplication - Dynamic Programming - YouTube
We look at finding a more optimal way of multiplying a number of matrices together using dynamic programming!
Read more >Matrix Chain Multiplication | DP-8 - GeeksforGeeks
Create a recursive function that takes i and j as parameters that determines the range of a group. Iterate from k = i...
Read more >Matrix chain multiplication - Wikipedia
Matrix chain multiplication is an optimization problem concerning the most efficient way to multiply a given sequence of matrices. The problem is not ......
Read more >Are numpy matrix chain multiplication optimized?
It's probably possible to write an array type that does optimal parenthesize of matrix multiplication. You could write an array that defers ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
numpy.matrix
was designed for this.See #4977.