Skip to content

Function to chain multiplications / dot for many arrays #4311

Closed
@sotte

Description

@sotte

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions