{"record":{"id":"2988518b85b32b9b","repo":"TheAlgorithms/Python","slug":"coefficient-and-constant-matrices-dimensions-must","errorCode":null,"errorMessage":"Coefficient and constant matrices dimensions must be nxn and nx1 but received {rows1}x{cols1} and {rows2}x{cols2}","messagePattern":"Coefficient and constant matrices dimensions must be nxn and nx1 but received (.+?)x(.+?) and (.+?)x(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/jacobi_iteration_method.py","lineNumber":99,"sourceCode":"    \"\"\"\n\n    rows1, cols1 = coefficient_matrix.shape\n    rows2, cols2 = constant_matrix.shape\n\n    if rows1 != cols1:\n        msg = f\"Coefficient matrix dimensions must be nxn but received {rows1}x{cols1}\"\n        raise ValueError(msg)\n\n    if cols2 != 1:\n        msg = f\"Constant matrix must be nx1 but received {rows2}x{cols2}\"\n        raise ValueError(msg)\n\n    if rows1 != rows2:\n        msg = (\n            \"Coefficient and constant matrices dimensions must be nxn and nx1 but \"\n            f\"received {rows1}x{cols1} and {rows2}x{cols2}\"\n        )\n        raise ValueError(msg)\n\n    if len(init_val) != rows1:\n        msg = (\n            \"Number of initial values must be equal to number of rows in coefficient \"\n            f\"matrix but received {len(init_val)} and {rows1}\"\n        )\n        raise ValueError(msg)\n\n    if iterations <= 0:\n        raise ValueError(\"Iterations must be at least 1\")\n\n    table: NDArray[float64] = np.concatenate(\n        (coefficient_matrix, constant_matrix), axis=1\n    )\n\n    rows, _cols = table.shape\n\n    strictly_diagonally_dominant(table)","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/jacobi_iteration_method.py#L81-L117","documentation":"Thrown by jacobi_iteration_method() when the coefficient matrix's row count differs from the constant matrix's row count (rows1 != rows2). Each unknown needs exactly one equation with its own b entry; mismatched row counts mean the system A·x = b is not even well-formed for the iteration.","triggerScenarios":"Calling with a 3x3 coefficient matrix and a 2x1 constant matrix (e.g. one equation lost when assembling from data). Typically follows fixing earlier shape errors — A is made square but b is not resized to match.","commonSituations":"Assembling A and b in separate code paths so one drops a row; appending an equation to A without extending b; refactors that change n in one place only.","solutions":["Derive both from one item list so lengths stay in lockstep.","Assert A.shape[0] == b.shape[0] at system-assembly time.","Log both shapes on failure to spot which side lost the row."],"exampleFix":"# before\nA = np.array([[4, 1], [1, 3], [2, 2]])   # 3 rows\nb = np.array([[1], [2]])                  # 2 rows\n\n# after\nrows = [(np.array([4, 1]), 1), (np.array([1, 3]), 2)]\nA = np.array([r for r, _ in rows], dtype=float)\nb = np.array([[v] for _, v in rows], dtype=float)","handlingStrategy":"validation","validationCode":"assert A.shape[0] == b.shape[0], (\n    f\"A has {A.shape[0]} rows but b has {b.shape[0]}\"\n)","typeGuard":"def is_matching_system(A: np.ndarray, b: np.ndarray) -> bool:\n        return (\n        A.ndim == 2\n        and b.ndim == 2\n        and A.shape[0] == b.shape[0]\n        and A.shape[1] == A.shape[0]\n        and b.shape[1] == 1\n    )","tryCatchPattern":"try:\n    x = jacobi_iteration_method(A, b, x0, iters)\nexcept ValueError as e:\n    if \"dimensions\" in str(e):\n        raise ValueError(f\"A/b assembled out of sync: {A.shape} vs {b.shape}\") from e\n    raise","preventionTips":["Build A and b in the same loop over equations.","Assert row-count equality at assembly time.","Log both shapes when validation fails upstream."],"tags":["linear-algebra","jacobi","numpy","validation","matrix-shape"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}