{"id":6218,"library":"quadprog","title":"quadprog: Quadratic Programming Solver","description":"quadprog is a Python wrapper for a C++ library that efficiently solves quadratic programming problems. It minimizes `0.5 * x^T G x + a^T x` subject to `C^T x >= b` and an optional number of equality constraints (`meq`). The library is currently at version 0.1.13 and receives active, though somewhat irregular, maintenance.","status":"active","version":"0.1.13","language":"python","source_language":"en","source_url":"https://github.com/quadprog/quadprog","tags":["optimization","quadratic programming","numerical","solver","convex"],"install":[{"cmd":"pip install quadprog","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for numerical array operations and input/output formats.","package":"numpy"}],"imports":[{"symbol":"solve_qp","correct":"from quadprog import solve_qp"}],"quickstart":{"code":"import numpy as np\nfrom quadprog import solve_qp\n\n# Define the quadratic program:\n# Minimize 0.5 * x^T G x + a^T x\n# Subject to C^T x >= b\n\n# Example: Minimize x^2 + y^2 - x - y\n# This means G = [[2, 0], [0, 2]] and a = [-1, -1]\nG = np.array([[2., 0.], [0., 2.]])\na = np.array([-1., -1.])\n\n# Constraints: x >= 0, y >= 0, x + y >= 1\n# In quadprog, C's columns are the normal vectors of the constraints.\n# C^T x >= b  =>  [[1, 0, 1], [0, 1, 1]]^T x >= [0, 0, 1]^T\n# Which means:\n# 1*x + 0*y >= 0\n# 0*x + 1*y >= 0\n# 1*x + 1*y >= 1\nC = np.array([[1., 0., 1.],\n              [0., 1., 1.]])\nb = np.array([0., 0., 1.])\n\n# Number of equality constraints (first 'meq' rows of C and b)\nmeq = 0\n\n# Solve the QP. It returns (x, fval, xu, l)\n# x: solution vector\n# fval: objective function value at x\n# xu: unconstrained solution (unused in this example)\n# l: Lagrange multipliers (unused in this example)\nsolution, _, _, _ = solve_qp(G, a, C, b, meq)\n\nprint(f\"Optimal solution x: {solution}\")\n# Expected output for this problem: Optimal solution x: [0.5 0.5]","lang":"python","description":"This example demonstrates how to set up and solve a simple quadratic programming problem with inequality constraints using `quadprog.solve_qp`. The problem minimizes `x^2 + y^2 - x - y` subject to `x >= 0`, `y >= 0`, and `x + y >= 1`."},"warnings":[{"fix":"Upgrade to version 0.1.11 or later to avoid this critical bug.","message":"Version 0.1.10 is explicitly marked as 'not recommended for use' due to a bug related to Lagrange multipliers for equality constraints.","severity":"breaking","affected_versions":"0.1.10"},{"fix":"Verify that your `G` matrix is symmetric and strictly positive definite. For indefinite problems, consider other QP solvers or reformulate the problem.","message":"The matrix `G` must be symmetric and positive definite. If `G` is not positive definite, the solver may fail or return incorrect results. Ensure numerical stability for your problem.","severity":"gotcha","affected_versions":"All"},{"fix":"Always transpose your constraint matrix `A` (where `A x >= b`) when passing it as `C` to `solve_qp`, i.e., `C = A.T`.","message":"The `C` matrix requires constraint vectors as its columns. If you define constraints as `A x >= b`, then `C` in `solve_qp` should be `A.T`.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure all equality constraints are placed at the beginning of your `C` and `b` arrays, and set `meq` accordingly.","message":"The `meq` parameter specifies the number of *equality* constraints, and these constraints *must* be the first `meq` rows in both `C` and `b`. Mixing equality and inequality constraints out of order will lead to incorrect solutions.","severity":"gotcha","affected_versions":"All"},{"fix":"Only set `factorized=True` if you are providing the Cholesky decomposition `R` of `G`. Otherwise, keep the default `factorized=False` and provide the original `G` matrix.","message":"The `factorized` parameter (default `False`) determines if `G` is expected as the Cholesky factor `R` (such that `R^T R = G`). Setting it to `True` when `G` is the original quadratic matrix will lead to incorrect results.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'0.1.13':50 '0.5':22 'activ':53 'b':35 'c':12,32 'constraint':42 'convex':63 'current':47 'effici':15 'equal':41 'g':25 'irregular':56 'librari':13,45 'mainten':57 'meq':43 'minim':21 'number':39 'numer':61 'optim':58 'option':38 'problem':19 'program':3,18,60 'python':8 'quadprog':1,5 'quadrat':2,17,59 'receiv':52 'solv':16 'solver':4,62 'somewhat':55 'subject':30 'though':54 'version':49 'wrapper':9 'x':23,26,29,34","created_at":"2026-04-14T18:47:06.157547+00:00","updated_at":"2026-04-16T20:42:08.540784+00:00","problems":[{"fix":"Install the library using pip: `pip install quadprog`","cause":"The 'quadprog' library has not been installed in the current Python environment or is not accessible.","error":"ModuleNotFoundError: No module named 'quadprog'"},{"fix":"Ensure the 'a' vector has a shape of `(n,)` by using methods like `a = a.flatten()` or `a = a.ravel()`.","cause":"The 'a' vector (linear term in the QP objective) must be a 1D NumPy array (shape `(n,)`), but a 2D array (e.g., shape `(n, 1)`) was provided.","error":"ValueError: array must be 1-dimensional"},{"fix":"Verify that 'G' is `(n, n)`, 'a' is `(n,)`, 'C' is `(n, m)`, and 'b' is `(m,)` where `n` is the number of variables and `m` is the number of constraints, reshaping them if necessary.","cause":"One or more of the input matrices ('G', 'C') or vectors ('a', 'b') do not have the precise dimensions required by the `solve_qp` function for the given number of variables (n) and constraints (m).","error":"ValueError: Expects G to be a (n, n) matrix"},{"fix":"Check if the 'G' matrix is strictly positive definite and well-conditioned; ensure the problem is feasible by reviewing your problem formulation, constraints, and objective function coefficients, and consider adding a small regularization term (e.g., `G = G + 1e-6 * np.eye(n)`) if 'G' is only positive semi-definite or nearly singular.","cause":"The underlying C++ solver failed to converge or find a solution, typically because the quadratic programming problem is ill-posed, infeasible, or the 'G' matrix (Hessian) is not strictly positive definite.","error":"ValueError: quadprog.solve_qp: failed to solve problem"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.1.13","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/quadprog/quadprog","docs":null,"changelog":null,"pypi":"https://pypi.org/project/quadprog/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["ai-ml","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}