Reputation: 33
I feel stupid asking this, but I'm having a really hard time understanding the synatx of scipy.optimize
I have an mxm matrix M, and I simply like to find an m-dimensional vector x which
minimizes M.dot(x)
such that ||x||_2^2 = 1
Essentially I want to minimize Mx with x being l2-normalized.
I can't seem to find a way to fit my idea of what is going on within the syntax of numpy. I'd greatly appreciate any help in remedying my stupid with understanding.
The problem also has a special case where it can be solved by finding x such that
M.dot(x) = zeros
such that ||x||_2^2 = 1
Would the same optimization algorithm be appropriate for both problems? Or are there different ways to solve the minimization and finding zeros problem?
Upvotes: 3
Views: 143
Reputation: 46578
Your function to minimize is:
def f(x, M):
return M.dot(x)
Your starting point can be anything, say,
m = M.shape[0]
x0 = np.ones(m)/np.sqrt(m)
You can apply the constraint as:
def con(x):
return np.linalg.norm(x) - 1
cons = {'type':'eq', 'fun': con}
Finally, the minimization should run as:
scipy.optimize.minimize(f, x0, args=(M,), constraints=cons)
Lots of options can be changed, and very well might be necessary for your problem, but hopefully this helps you apply the syntax of the module to your problem. See these examples for more information.
In the case where the right-hand-side is zero, your problem is linear and can be solved more directly, for example, with scipy.linalg.solve
:
from scipy import linalg
x = linalg.solve(M, np.zeros(M.shape[0]))
I don't know how to implement the constraint here, but I believe it can simply be added after the fact, since in this case it would hold with any constant multiplier:
x /= linalg.norm(x)
Upvotes: 4