{"record":{"id":"9326733f3039f937","repo":"TheAlgorithms/Python","slug":"expected-the-same-number-of-columns-for-b-and-c-i","errorCode":null,"errorMessage":"Expected the same number of columns for B and C. Instead found B of size {shape_b} and C of size {shape_c}","messagePattern":"Expected the same number of columns for B and C\\. Instead found B of size (.+?) and C of size (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/schur_complement.py","lineNumber":47,"sourceCode":"           [ 0.,  6.]])\n    \"\"\"\n    shape_a = np.shape(mat_a)\n    shape_b = np.shape(mat_b)\n    shape_c = np.shape(mat_c)\n\n    if shape_a[0] != shape_b[0]:\n        msg = (\n            \"Expected the same number of rows for A and B. \"\n            f\"Instead found A of size {shape_a} and B of size {shape_b}\"\n        )\n        raise ValueError(msg)\n\n    if shape_b[1] != shape_c[1]:\n        msg = (\n            \"Expected the same number of columns for B and C. \"\n            f\"Instead found B of size {shape_b} and C of size {shape_c}\"\n        )\n        raise ValueError(msg)\n\n    a_inv = pseudo_inv\n    if a_inv is None:\n        try:\n            a_inv = np.linalg.inv(mat_a)\n        except np.linalg.LinAlgError:\n            raise ValueError(\n                \"Input matrix A is not invertible. Cannot compute Schur complement.\"\n            )\n\n    return mat_c - mat_b.T @ a_inv @ mat_b\n\n\nclass TestSchurComplement(unittest.TestCase):\n    def test_schur_complement(self) -> None:\n        a = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]])\n        b = np.array([[0, 3], [3, 0], [2, 3]])\n        c = np.array([[2, 1], [6, 3]])","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/schur_complement.py#L29-L65","documentation":"Raised by schur_complement when the column count of B differs from the column count of C. The final product B.T @ inv(A) @ B yields a q x q matrix that must subtract cleanly from C, so C must be q x q with q = mat_b.shape[1].","triggerScenarios":"Calling schur_complement(a, b, c) where np.shape(mat_b)[1] != np.shape(mat_c)[1], e.g. B is 3x2 while C is 2x3.","commonSituations":"Passing a non-square C, forgetting that C indexes the same coordinates as the columns of B, or building C from a differently-ordered subset of features than B.","solutions":["Check np.shape(mat_b)[1] == np.shape(mat_c)[1] == np.shape(mat_c)[0] before calling (C should be square with dimension equal to B's columns).","Fix the construction of C so it covers exactly the variables spanned by B's columns.","If C came from a larger matrix, slice it to the correct block instead of passing the whole matrix."],"exampleFix":"# before\nb = np.ones((3, 2)); c = np.eye(3)  # c is 3x3, b has 2 columns\nschur_complement(np.eye(3), b, c)\n\n# after\nb = np.ones((3, 2)); c = np.eye(2)\nschur_complement(np.eye(3), b, c)","handlingStrategy":"validation","validationCode":"if mat_b.shape[1] != mat_c.shape[1] or mat_c.shape[0] != mat_c.shape[1]:\n    raise ValueError(\"C must be square with dimension equal to B's columns\")\nresult = schur_complement(mat_a, mat_b, mat_c)","typeGuard":"def blocks_col_aligned(b: np.ndarray, c: np.ndarray) -> bool:\n    return b.ndim == 2 and c.ndim == 2 and b.shape[1] == c.shape[0] == c.shape[1]","tryCatchPattern":"try:\n    schur_complement(a, b, c)\nexcept ValueError as e:\n    if \"number of columns\" in str(e):\n        raise ValueError(f\"block partition inconsistent: {e}\") from None","preventionTips":["Derive C from the same feature ordering as B's columns.","Unit-test block construction with one canonical symmetric matrix.","Never pass a full unsliced matrix where a q x q block is expected."],"tags":["linear-algebra","schur-complement","shape-mismatch","numpy"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}