{"record":{"id":"ea8f5ddaf570c0cd","repo":"jax-ml/jax","slug":"expected-e-to-be-a-batched-square-matrix-got-e","errorCode":null,"errorMessage":"expected E to be a (batched) square matrix, got E.shape={E_arr.shape}","messagePattern":"expected E to be a \\(batched\\) square matrix, got E\\.shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":1616,"sourceCode":"    >>> expmA, expm_frechet_AE = jax.scipy.linalg.expm_frechet(A, E)\n\n    This can be equivalently computed using JAX's automatic differentiation methods;\n    here we'll compute the derivative of :func:`~jax.scipy.linalg.expm` in the\n    direction of ``E`` using :func:`jax.jvp`, and find the same results:\n\n    >>> expmA2, expm_frechet_AE2 = jax.jvp(jax.scipy.linalg.expm, (A,), (E,))\n    >>> jnp.allclose(expmA, expmA2)\n    Array(True, dtype=bool)\n    >>> jnp.allclose(expm_frechet_AE, expm_frechet_AE2)\n    Array(True, dtype=bool)\n  \"\"\"\n  del method  # unused\n  A_arr = jnp.asarray(A)\n  E_arr = jnp.asarray(E)\n  if A_arr.ndim < 2 or A_arr.shape[-2] != A_arr.shape[1]:\n    raise ValueError(f'expected A to be a (batched) square matrix, got A.shape={A_arr.shape}')\n  if E_arr.ndim < 2 or E_arr.shape[-2] != E_arr.shape[-1]:\n    raise ValueError(f'expected E to be a (batched) square matrix, got E.shape={E_arr.shape}')\n  if A_arr.shape != E_arr.shape:\n    raise ValueError('expected A and E to be the same shape, got '\n                     f'A.shape={A_arr.shape} E.shape={E_arr.shape}')\n  bound_fun = partial(expm, upper_triangular=False, max_squarings=16)\n  expm_A, expm_frechet_AE = jvp(bound_fun, (A_arr,), (E_arr,))\n  if compute_expm:\n    return expm_A, expm_frechet_AE\n  else:\n    return expm_frechet_AE\n\n\n@jit\ndef block_diag(*arrs: ArrayLike) -> Array:\n  \"\"\"Create a block diagonal matrix from input arrays.\n\n  JAX implementation of :func:`scipy.linalg.block_diag`.\n\n  Args:","sourceCodeStart":1598,"sourceCodeEnd":1634,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L1598-L1634","documentation":"expm_frechet's direction matrix E must itself be a (batched) square matrix (ndim >= 2, last two dims equal), matching the structure of A. A non-square or vector E raises ValueError before the jvp is computed.","triggerScenarios":"Calling expm_frechet(A, E) with E of shape (n,) or (m, k) with m != k.","commonSituations":"Passing a perturbation direction as a flattened vector instead of a matrix; broadcasting bugs making E rectangular.","solutions":["Reshape E to (n, n): E = E.reshape(n, n)","Ensure E has the same batch shape structure as A"],"exampleFix":"// before\nexpm_frechet(A, e_vec)  # e_vec.shape == (n,)\n// after\nexpm_frechet(A, e_vec.reshape(n, n))","handlingStrategy":"validation","validationCode":"assert E.ndim >= 2 and E.shape[-2] == E.shape[-1], 'E must be square'","typeGuard":"def is_square_batched(E): return E.ndim >= 2 and E.shape[-2] == E.shape[-1]","tryCatchPattern":null,"preventionTips":["Reshape flattened direction vectors to (n, n) before calling","Keep A and E construction in one helper so shapes stay consistent"],"tags":["jax","linalg","frechet-derivative","shape-mismatch"],"backgroundTag":"matrix-dimension-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}