0

Let a be a numpy array of length n. Does the statement a == max(a) calculate the expression max(a) n-times or just one?

1
  • You probably want numpy.amax rather than plain Python max. Commented Aug 28, 2014 at 18:59

2 Answers 2

1

It computes max(a) once, then it compares the (scalar) result against each (scalar) element in a, and creates a bool-array for the result.

Sign up to request clarification or add additional context in comments.

Comments

1

It only evaluates max once. You could test this yourself by writing your own function:

def mymax(x):
    print("Calling mymax.")
    return max(x)

Then try

a == mymax(a)

1 Comment

Great! Thanks also for the insight about how to test it!

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.