question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function to chain multiplications / `dot` for many arrays

See original GitHub issue

I 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:closed
  • Created 10 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
argriffingcommented, Feb 17, 2014

numpy.matrix was designed for this.

M**5
(X.T * X).I * X.T * y
0reactions
sottecommented, Aug 20, 2014

See #4977.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found