{"record":{"id":"db472c4baba824e1","repo":"TheAlgorithms/Python","slug":"number-of-initial-values-must-be-equal-to-number-o","errorCode":null,"errorMessage":"Number of initial values must be equal to number of rows in coefficient matrix but received {len(init_val)} and {rows1}","messagePattern":"Number of initial values must be equal to number of rows in coefficient matrix but received (.+?) and (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/jacobi_iteration_method.py","lineNumber":106,"sourceCode":"        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)\n\n    \"\"\"\n    # Iterates the whole matrix for given number of times\n    for _ in range(iterations):\n        new_val = []\n        for row in range(rows):\n            temp = 0","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/jacobi_iteration_method.py#L88-L124","documentation":"Thrown by jacobi_iteration_method() when len(init_val) != rows1 — the initial guess vector does not have one entry per unknown. The iteration indexes x_old[i] for every row i; a shorter guess raises IndexError and a longer one indicates a wrong system, so the function validates the count upfront.","triggerScenarios":"Calling with init_val = [0, 0] for a 3x3 system, or passing a 1-D numpy array of a different length (e.g. built with np.zeros(n) where n was hardcoded or from a previous problem size).","commonSituations":"Reusing an initial guess across problems of different dimensionality; hardcoded np.zeros(3) copied from an example; n changed in the system builder but not in the guess construction.","solutions":["Always derive the guess from the matrix: init_val = np.zeros(coefficient_matrix.shape[0]).","Build A, b, and init_val from the same n variable in one place.","Prefer letting the function's own default/zero start be used rather than hand-building guesses."],"exampleFix":"# before\ninit_val = [0.0, 0.0]  # but A is 3x3\n\n# after\ninit_val = np.zeros(coefficient_matrix.shape[0])","handlingStrategy":"validation","validationCode":"x0 = np.zeros(A.shape[0])  # derive guess length from the matrix","typeGuard":"def is_matching_guess(A: np.ndarray, x0: np.ndarray) -> bool:\n        return A.ndim == 2 and x0.shape[0] == A.shape[0]","tryCatchPattern":"try:\n    x = jacobi_iteration_method(A, b, x0, iters)\nexcept ValueError as e:\n    if \"initial values\" in str(e):\n        x = jacobi_iteration_method(A, b, np.zeros(A.shape[0]), iters)\n    else:\n        raise","preventionTips":["Never hardcode guess lengths; use np.zeros(A.shape[0]).","Derive A, b, and x0 from one n variable.","Rebuild guesses when problem size changes."],"tags":["linear-algebra","jacobi","numpy","validation","initial-guess"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}