I am in the process of translating some MATLAB code into Python. There is one line that is giving me a bit of trouble:

```
[q,f_dummy,exitflag, output] = quadprog(H,f,-A,zeros(p*N,1),E,qm,[],[],q0,options);

```

I looked up the documentation in MATLAB to find that the quadprog function is used for optimization (particularly minimization). 

I attempted to find a similar function in Python (using numpy) and there does not seem to be any. 

Is there a better way to translate this line of code into Python? Or are there other packages that can be used? Do I need to make a new function that accomplishes the same task? 

Thanks for your time and help!
['https://Stackoverflow.com/questions/51161348', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9645799/']
I will start by mentioning that *quadratic programming* problems are a subset of *convex optimization* problems which are a subset of *optimization* problems.

There are multiple python packages which solve quadratic programming problems, notably

1. [cvxopt](https://cvxopt.org/userguide/coneprog.html#quadratic-programming) -- which solves all kinds of *convex optimization* problems (including quadratic programming problems). This is a python version of the previous [cvx MATLAB package](http://cvxr.com/cvx/).
2. [quadprog](https://github.com/rmcgibbo/quadprog) -- this is exclusively for quadratic programming problems but doesn't seem to have much documentation.
3. [scipy.optimize.minimize](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) -- this is a very general minimizer which can solve quadratic programming problems, as well as other *optimization* problems (convex and non-convex).

You might also benefit from looking at the answers to [this stackoverflow post](https://stackoverflow.com/questions/17009774/quadratic-program-qp-solver-that-only-depends-on-numpy-scipy) which has more details and references.

Note: The code snippet in user1911226' answer appears to come from this blog post:
<https://scaron.info/blog/quadratic-programming-in-python.html>
which compares some of these quadratic programming packages. I can't comment on their answer, but they claim to be mentioning the cvxopt solution, but the code is actually for the quadprog solution.
You could use the [`solve_qp`](https://scaron.info/doc/qpsolvers/quadratic-programming.html#qpsolvers.solve_qp) function from [qpsolvers](https://github.com/stephane-caron/qpsolvers). It can be installed, along with a starter kit of open source solvers, by `pip install qpsolvers[open_source_solvers]`. Then you can replace your line with:

```py
from qpsolvers import solve_qp

solver = "proxqp"  # or "osqp", "quadprog", "cvxopt", ...
x = solve_qp(H, f, G, h, A, b, initvals=q_0, solver=solver, **options)

```

There are many solvers available in Python, each coming with their pros and cons. Make sure you try [different values](https://github.com/stephane-caron/qpsolvers#solvers) for the `solver` keyword argument to find the one that fits your problem best.

Here is a standalone example based on your question and the other comments:

```py
import numpy as np

from qpsolvers import solve_qp

H = np.array([[4.0, 1.0], [1.0, 2.0]])
f = np.array([1.0, 1])
G = np.array([[1.0, 0.0], [0.0, 1.0]])
h = np.array([0.7, 0.7])
A = np.array([[1.0, 1.0]])
b = np.array([1.0])
q_0 = np.array([1.0, 1.0])

solver = "cvxopt"  # or "osqp", "proxqp", "quadprog", ...
options = {"verbose": True}
x = solve_qp(H, f, G, h, A, b, initvals=q_0, solver=solver, **options)

```