I want to find the maximum value for a that doesn't change the output from another function. (_find_mean simply finds the location of the maximum element in a matrix)
This is how I am currently doing it and was wondering if there was a better way.
def _find_max_a(matrix1, matrix2):
a = np.arange(0,10, 0.01)[::-1]
for val in a:
mat = matrix1 + val * matrix2
if _find_mean(matrix1) == _find_mean(mat):
return val
else:
pass
1 Answer 1
arange
can build a descending vector. Building an ascending vector and reversing it seems wasteful. Considera = np.arange(10, 0, -0.01)
matrix1
is never modified, but its_find_mean
is recomputed on each iteration. Yet another waste. Compute it once.Instead of linear search, you may want to consider a bisection of the search interval.
else: pass
achieves nothing, and can be safely omitted._find_mean
is a kinda strange name for a function whichfinds the location of the maximum element in a matrix
-
\$\begingroup\$ Now the question is how to do the
arange
thing more efficiently. For larger range with finer accuracy, the amount of required memory is quite high (800kB for range100, 0, -0.001
withdtype
ofnp.float64
, which is the default). Loop with manual decrement by0.001
would introduce errors, whereas a scaled loop would require division. \$\endgroup\$kyrill– kyrill2017年05月02日 11:18:06 +00:00Commented May 2, 2017 at 11:18
a
which doesn't change the output from the other function? \$\endgroup\$a
, it loops on values ofa
. So is your description correct? \$\endgroup\$