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/']
There is a library called [CVXOPT](https://scaron.info/blog/quadratic-programming-in-python.html) that has quadratic programming in it.

```
def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None):
    qp_G = .5 * (P + P.T)   # make sure P is symmetric
    qp_a = -q
    if A is not None:
        qp_C = -numpy.vstack([A, G]).T
        qp_b = -numpy.hstack([b, h])
        meq = A.shape[0]
    else:  # no equality constraint
        qp_C = -G.T
        qp_b = -h
        meq = 0
    return quadprog.solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0] 

```
[OSQP](https://osqp.org) is a specialized free QP solver based on ADMM. I have adapted the [OSQP documentation demo](http://osqp.org/docs/examples/demo.html) and the OSQP call in the [qpsolvers repository](https://github.com/stephane-caron/qpsolvers/blob/master/qpsolvers/osqp_.py) for your problem. 

Note that matrices `H` and `G` are supposed to be sparse in [CSC format](http://www.scipy-lectures.org/advanced/scipy_sparse/csc_matrix.html). Here is the script

```
import numpy as np
import scipy.sparse as spa
import osqp

def quadprog(P, q, G=None, h=None, A=None, b=None,
             initvals=None, verbose=True):
    l = -np.inf * np.ones(len(h))
    if A is not None:
        qp_A = spa.vstack([G, A]).tocsc()
        qp_l = np.hstack([l, b])
        qp_u = np.hstack([h, b])
    else:  # no equality constraint
        qp_A = G
        qp_l = l
        qp_u = h
    model = osqp.OSQP()
    model.setup(P=P, q=q,
                A=qp_A, l=qp_l, u=qp_u, verbose=verbose)
    if initvals is not None:
        model.warm_start(x=initvals)
    results = model.solve()
    return results.x, results.info.status

# Generate problem data
n = 2   # Variables
H = spa.csc_matrix([[4, 1], [1, 2]])
f = np.array([1, 1])
G = spa.csc_matrix([[1, 0], [0, 1]])
h = np.array([0.7, 0.7])
A = spa.csc_matrix([[1, 1]])
b = np.array([1.])

# Initial point
q0 = np.ones(n)

x, status = quadprog(H, f, G, h, A, b, initvals=q0, verbose=True)

```