{"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":"en","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,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}