2

Say I have 3 dimensional numpy array a, for example as below:

import numpy as np
a = np.random.randn(3, 3, 3)

How can I apply (matrix->scalar)-type function to a? More specifically, I want to do an equivalent thing as below in a more computationally efficient way:

[np.linalg.det(e) for e in a]
1

1 Answer 1

2

np.linalg.det(a) seems to work just fine and has significantly better runtime:

a = np.random.rand(100,3,3)

%timeit -n 100 [np.linalg.det(e) for e in a]
626 µs ± 26.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit -n 100 np.linalg.det(a)
33.9 µs ± 7.08 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.