{"record":{"id":"55601fd6e6facf3d","repo":"TheAlgorithms/Python","slug":"expected-the-same-number-of-rows-for-a-and-b-inst","errorCode":null,"errorMessage":"Expected the same number of rows for A and B. Instead found A of size {shape_a} and B of size {shape_b}","messagePattern":"Expected the same number of rows for A and B\\. Instead found A of size (.+?) and B of size (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/schur_complement.py","lineNumber":40,"sourceCode":"\n    >>> import numpy as np\n    >>> a = np.array([[1, 2], [2, 1]])\n    >>> b = np.array([[0, 3], [3, 0]])\n    >>> c = np.array([[2, 1], [6, 3]])\n    >>> schur_complement(a, b, c)\n    array([[ 5., -5.],\n           [ 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","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/schur_complement.py#L22-L58","documentation":"Raised by schur_complement when the number of rows of block matrix A differs from the number of rows of block matrix B. The function computes C - B.T @ inv(A) @ B, which requires A (p x p) and B (p x q) to share the row dimension p; a mismatch means the blocks cannot form a valid 2x2 block matrix.","triggerScenarios":"Calling schur_complement(a, b, c) with np.shape(mat_a)[0] != np.shape(mat_b)[0], e.g. A is 2x2 and B is 3x2 (error message reports the concrete shapes).","commonSituations":"Assembling blocks from separately computed matrices (e.g. covariance blocks estimated from data subsets with different sample counts), off-by-one slicing errors, or transposing B by mistake when constructing the partitioned matrix.","solutions":["Check the shapes before calling: assert mat_a.shape[0] == mat_b.shape[0].","Re-derive or re-slice B so it has exactly as many rows as A; if you have B.T stored, pass its transpose.","Verify your block partition of the full matrix M = [[A, B], [B.T, C]] is consistent."],"exampleFix":"# before\na = np.ones((2, 2)); b = np.ones((3, 2)); c = np.eye(2)\nschur_complement(a, b, c)\n\n# after\na = np.ones((2, 2)); b = np.ones((2, 2)); c = np.eye(2)\nschur_complement(a, b, c)","handlingStrategy":"validation","validationCode":"if mat_a.shape[0] != mat_b.shape[0]:\n    raise ValueError(f\"A rows {mat_a.shape[0]} != B rows {mat_b.shape[0]}\")\nresult = schur_complement(mat_a, mat_b, mat_c)","typeGuard":"def blocks_row_aligned(a: np.ndarray, b: np.ndarray) -> bool:\n    return a.ndim == 2 and b.ndim == 2 and a.shape[0] == b.shape[0]","tryCatchPattern":"try:\n    schur_complement(a, b, c)\nexcept ValueError as e:\n    if \"number of rows\" in str(e):\n        b = b[: a.shape[0]]  # only if truncation is semantically correct\n        schur_complement(a, b, c)","preventionTips":["Assemble A, B, C by slicing one full matrix M so dimensions stay consistent.","Assert block shapes before calling.","Remember the expected partition: A (p,p), B (p,q), C (q,q)."],"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"}