# Least squares¶

To solve a linear least-squares problem, simply build the matrices that define it and call the solve_ls() function:

from numpy import array, dot
from qpsolvers import solve_ls

R = array([[1., 2., 0.], [-8., 3., 2.], [0., 1., 1.]])
s = array([3., 2., 3.])
G = array([[1., 2., 1.], [2., 0., 1.], [-1., 2., -1.]])
h = array([3., 2., -2.]).reshape((3,))

x_sol = solve_ls(R, s, G, h, solver=solver, verbose=True)
print(f"LS solution: x = {x}")


This example outputs the solution [-0.0530504, 0.0265252, 2.1061008]. The solve_ls() function accepts a solver keyword argument to select the backend solver:

qpsolvers.solve_ls(R, s, G=None, h=None, A=None, b=None, lb=None, ub=None, W=None, solver='quadprog', initvals=None, sym_proj=False, verbose=False, **kwargs)

Solve a constrained weighted linear Least Squares problem defined as:

$\begin{split}\begin{split}\begin{array}{ll} \mbox{minimize} & \frac12 \| R x - s \|^2_W = \frac12 (R x - s)^T W (R x - s) \\ \mbox{subject to} & G x \leq h \\ & A x = b \\ & lb \leq x \leq ub \end{array}\end{split}\end{split}$

using one of the available QP solvers.

Parameters
• R (Union[ndarray, csc_matrix, spmatrix]) – Symmetric matrix of the cost function (most solvers require it to be definite).

• s (Union[ndarray, csc_matrix, spmatrix]) – Vector term of the cost function.

• G (Union[ndarray, csc_matrix, spmatrix, None]) – Linear inequality matrix.

• h (Union[ndarray, csc_matrix, spmatrix, None]) – Linear inequality vector.

• A (Union[ndarray, csc_matrix, spmatrix, None]) – Linear equality matrix.

• b (Union[ndarray, csc_matrix, spmatrix, None]) – Linear equality vector.

• lb (Union[ndarray, csc_matrix, spmatrix, None]) – Lower bound constraint vector.

• ub (Union[ndarray, csc_matrix, spmatrix, None]) – Upper bound constraint vector.

• W (Union[ndarray, csc_matrix, spmatrix, None]) – Definite symmetric weight matrix used to define the norm of the cost function. The standard L2 norm (W = Identity) is used by default.

• solver (str) – Name of the QP solver, to choose in qpsolvers.available_solvers.

• initvals (Union[ndarray, csc_matrix, spmatrix, None]) – Vector of initial x values used to warm-start the solver.

• sym_proj (bool) – Set to True when the R matrix provided is not symmetric.

• verbose (bool) – Set to True to print out extra information.

Return type

Optional[ndarray]

Returns

Optimal solution if found, otherwise None.

Notes

Extra keyword arguments given to this function are forwarded to the underlying solvers. For example, OSQP has a setting eps_abs which we can provide by solve_ls(R, s, G, h, solver='osqp', eps_abs=1e-4).

See the examples/ folder in the repository for more advanced use cases. For a more general introduction you can also check out this post on least squares in Python.