{"id":158,"date":"2026-11-13T12:33:04","date_gmt":"2026-11-13T04:33:04","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-conjugate-gradient-solves-a-symmetric-positive-system\/"},"modified":"2026-11-13T21:51:30","modified_gmt":"2026-11-13T13:51:30","slug":"how-conjugate-gradient-solves-a-symmetric-positive-system","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-conjugate-gradient-solves-a-symmetric-positive-system\/","title":{"rendered":"How conjugate gradient solves a symmetric positive system"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Conjugate gradient<\/strong> solves Ax = b when A is symmetric positive definite. It builds A-orthogonal search directions so that each new direction does not undo the minimization along earlier ones. In exact arithmetic it reaches the solution in at most <strong>n<\/strong> steps for an n by n system, and it often reaches a useful residual much sooner when the eigenvalues are clustered. Each iteration is one matrix-vector product, so a sparse A stays sparse. The memory is a handful of vectors.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Gaussian elimination factors a sparse matrix and fills in new nonzeros. Gradient descent on the quadratic (1\/2)x\u00b7Ax \u2212 b\u00b7x uses only matrix-vector products and takes small steps in a narrow valley, because successive gradients are not conjugate. Conjugate gradient chooses the next direction as the residual plus a multiple of the previous direction, with the multiple chosen so the directions are A-orthogonal. The line search along that direction is then a closed-form step length.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Positive definiteness makes the quadratic bowl have a unique minimum at the solution of Ax = b. A symmetric indefinite matrix breaks the line-search guarantees. Those systems want MINRES or a factorization.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">In exact arithmetic, the iterate after at most n steps is the solution, and the residual is orthogonal to the Krylov subspace generated so far. The error measured in the A-norm decreases. Time is O(n) iterations times the cost of a matrix-vector product in the worst case, and far fewer iterations when the condition number is moderate or a preconditioner clusters the eigenvalues. Extra memory is a few n-vectors. Floating-point loss of conjugacy is real on ill-conditioned systems. Restarting or, better, preconditioning is the practical response.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm does not form A inverse. It does not need A stored as a dense matrix. A preconditioner M approximates A and is applied by solving M z = r each iteration. The preconditioner has to be symmetric positive definite too if the standard convergence story is kept.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set x to an initial guess, r = b \u2212 Ax, and the first direction d = r.<\/li>\n<li>The step length is (r\u00b7r) \/ (d\u00b7A\u00b7d). Update x by adding that length times d, and update r by subtracting that length times A\u00b7d.<\/li>\n<li>The next direction is the new residual plus \u03b2 times d, where \u03b2 = (new r \u00b7 new r) \/ (old r \u00b7 old r) in the Fletcher-Reeves form used by the classical algorithm.<\/li>\n<li>Stop when the residual norm is small relative to b, or after a budget. In exact arithmetic the residual becomes zero by step n.<\/li>\n<li>If A is ill-conditioned, apply a preconditioner rather than expecting the n-step bound to be a fast floating-point run.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The n-step exact termination needs exact arithmetic and a symmetric positive definite matrix. It is not a general optimizer of a nonquadratic loss. Nonlinear conjugate gradient borrows the direction update and loses the finite termination. A dense well-conditioned system may still be faster to factor. Conjugate gradient wins when the matrix-vector product is cheap and a modest number of iterations suffices.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the matrix is only symmetric<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run the algorithm on a symmetric indefinite system<\/td>\n<td>The step-length denominator d\u00b7A\u00b7d can be zero or negative. The quadratic is not a bowl, and the iteration is not the right Krylov method.<\/td>\n<\/tr>\n<tr>\n<td>Replace the matrix-vector product by an explicit inverse<\/td>\n<td>The method exists to avoid the inverse. Forming it is a dense factorization.<\/td>\n<\/tr>\n<tr>\n<td>Expect n floating-point iterations to produce a zero residual on a badly conditioned matrix<\/td>\n<td>Conjugacy erodes. The residual may stall above the exact-arithmetic zero. Precondition or iterate with a residual check, not a promise of step n.<\/td>\n<\/tr>\n<tr>\n<td>Use the linear update formulas on a general neural-net loss and expect termination in n steps<\/td>\n<td>Those formulas used the quadratic model. Nonlinear conjugate gradient is a heuristic cousin.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A dense factorization is Gaussian elimination. A general smooth minimization is gradient descent or a Newton method. Conjugate gradient is the Krylov solver for symmetric positive definite linear systems.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment takes Fletcher\u2013Reeves steps on a symmetric positive-definite matrix. For A = [[4, 1], [1, 3]] and b = [1, 2] the solution is x = 1\/11, y = 7\/11. It prints <code>0.0909 0.6364<\/code>.<\/p>\n\n<pre><code class=\"language-python\">A = [[4.0, 1.0], [1.0, 3.0]]\nb = [1.0, 2.0]\n\ndef matvec(v):\n    return [A[i][0] * v[0] + A[i][1] * v[1] for i in range(2)]\n\nx = [0.0, 0.0]\nr = b[:]\nd = r[:]\nrr = r[0] * r[0] + r[1] * r[1]\nfor _ in range(2):\n    Ad = matvec(d)\n    alpha = rr \/ (d[0] * Ad[0] + d[1] * Ad[1])\n    x = [x[i] + alpha * d[i] for i in range(2)]\n    r = [r[i] - alpha * Ad[i] for i in range(2)]\n    rr_new = r[0] * r[0] + r[1] * r[1]\n    if rr_new &lt; 1e-18:\n        break\n    beta = rr_new \/ rr\n    d = [r[i] + beta * d[i] for i in range(2)]\n    rr = rr_new\nAx = matvec(x)\nassert abs(Ax[0] - b[0]) &lt; 1e-8 and abs(Ax[1] - b[1]) &lt; 1e-8\nprint(f&quot;{x[0]:.4f}&quot;, f&quot;{x[1]:.4f}&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>PETSc<\/strong>, MATLAB <code>pcg<\/code>, and finite-element codes solve a large symmetric positive-definite system with conjugate gradient. Each step is a matrix-vector product, so a sparse stiffness matrix stays sparse. Gaussian elimination is the dense, direct alternative.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop conjugate gradient when A is not symmetric positive definite. Use a solver that matches the matrix. Keep it, with a preconditioner, when A is a large sparse SPD system and matrix-vector products are the natural operation.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for conjugate gradient returns the A-orthogonal directions and the requirement that A be symmetric positive definite. <strong>Conjugate gradient solves a symmetric positive definite linear system with one matrix-vector product per iteration, reaching the exact solution in at most n steps in exact arithmetic.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Conjugate gradient solves Ax = b when A is symmetric positive definite. It builds A-orthogonal search directions so that each new direction does not undo the minimization along earlier ones. In exact arithmetic it reaches the solution in at most n steps for an n by n&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,45],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-numerical"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/158\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}