{"record":{"id":"74b0c37d632a8599","repo":"TheAlgorithms/Python","slug":"coefficient-matrix-is-not-strictly-diagonally-domi","errorCode":null,"errorMessage":"Coefficient matrix is not strictly diagonally dominant","messagePattern":"Coefficient matrix is not strictly diagonally dominant","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/jacobi_iteration_method.py","lineNumber":195,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: Coefficient matrix is not strictly diagonally dominant\n    \"\"\"\n\n    rows, cols = table.shape\n\n    is_diagonally_dominant = True\n\n    for i in range(rows):\n        total = 0\n        for j in range(cols - 1):\n            if i == j:\n                continue\n            else:\n                total += table[i][j]\n\n        if table[i][i] <= total:\n            raise ValueError(\"Coefficient matrix is not strictly diagonally dominant\")\n\n    return is_diagonally_dominant\n\n\n# Test Cases\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":177,"sourceCodeEnd":205,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/jacobi_iteration_method.py#L177-L205","documentation":"Thrown by strictly_diagonally_dominant(), called from jacobi_iteration_method(), when for any row the diagonal entry is <= the sum of the other coefficients in that row. Jacobi iteration is only guaranteed to converge for strictly diagonally dominant matrices, so a non-dominant matrix is rejected rather than iterated into divergence.","triggerScenarios":"Calling jacobi_iteration_method with A = [[1, 2], [3, 4]] (|1| <= 2 in row 0). Note the check sums off-diagonal entries without abs(), so matrices with large negative off-diagonals can slip through or trip unexpectedly. Systems assembled in arbitrary equation order are the usual culprit.","commonSituations":"Equations ordered so the large coefficient is off-diagonal (reorder rows to put each row's largest coefficient on the diagonal); physically ill-conditioned systems (weak diagonal coupling) that Jacobi cannot solve — use Gauss-Seidel or direct solvers instead.","solutions":["Reorder equations (pivot rows) so each row's largest-magnitude coefficient sits on the diagonal.","Verify dominance before calling: all(|A[i,i]| > sum(|A[i,j]| for j != i) for i in range(n)).","If reordering cannot achieve dominance, switch methods: gauss_seidel in this repo, or np.linalg.solve."],"exampleFix":"# before\nA = np.array([[1.0, 2.0], [3.0, 4.0]])  # row 0 not dominant\n\n# after\nA = np.array([[4.0, 3.0], [2.0, 1.0]])  # swap rows: |4|>3, |1|... use truly dominant rows\n# or verify first:\nassert all(abs(A[i, i]) > sum(abs(A[i, j]) for j in range(len(A)) if j != i) for i in range(len(A)))","handlingStrategy":"validation","validationCode":"def is_strictly_diagonally_dominant(A: np.ndarray) -> bool:\n    return all(\n        abs(A[i, i]) > sum(abs(A[i, j]) for j in range(len(A)) if j != i)\n        for i in range(len(A))\n    )\n\nassert is_strictly_diagonally_dominant(A)","typeGuard":"def is_jacobi_solvable(A: np.ndarray) -> bool:\n        return (\n        A.ndim == 2\n        and A.shape[0] == A.shape[1]\n        and is_strictly_diagonally_dominant(A)\n    )","tryCatchPattern":"try:\n    x = jacobi_iteration_method(A, b, x0, iters)\nexcept ValueError as e:\n    if \"diagonally dominant\" in str(e):\n        A2 = reorder_rows_for_dominance(A)  # put max |coef| on each diagonal\n        x = jacobi_iteration_method(A2, b, x0, iters)\n    else:\n        raise","preventionTips":["Pivot rows so each row's largest coefficient is on the diagonal.","Check dominance (with abs) before calling; the internal check sums raw values.","Fall back to Gauss-Seidel or np.linalg.solve when dominance is unattainable."],"tags":["linear-algebra","jacobi","convergence","diagonal-dominance","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}