{"record":{"id":"25a1613a0b92f00b","repo":"jax-ml/jax","slug":"dimensional-array-given-array-must-be-at-least","errorCode":null,"errorMessage":"{}-dimensional array given. Array must be at least two-dimensional","messagePattern":"(.+?)-dimensional array given\\. Array must be at least two-dimensional","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/linalg.py","lineNumber":381,"sourceCode":"    and also supports negative powers:\n\n    >>> with jnp.printoptions(precision=3):\n    ...   jnp.linalg.matrix_power(a, -2)\n    Array([[ 5.5 , -2.5 ],\n           [-3.75,  1.75]], dtype=float32)\n\n    Negative powers are equivalent to matmul of the inverse:\n\n    >>> inv_a = jnp.linalg.inv(a)\n    >>> with jnp.printoptions(precision=3):\n    ...   inv_a @ inv_a\n    Array([[ 5.5 , -2.5 ],\n           [-3.75,  1.75]], dtype=float32)\n  \"\"\"\n  arr = ensure_arraylike(\"jnp.linalg.matrix_power\", a)\n\n  if arr.ndim < 2:\n    raise TypeError(\"{}-dimensional array given. Array must be at least \"\n                    \"two-dimensional\".format(arr.ndim))\n  if arr.shape[-2] != arr.shape[-1]:\n    raise TypeError(\"Last 2 dimensions of the array must be square\")\n  try:\n    n = operator.index(n)\n  except TypeError as err:\n    raise TypeError(f\"exponent must be an integer, got {n}\") from err\n\n  if n == 0:\n    return jnp.broadcast_to(jnp.eye(arr.shape[-2], dtype=arr.dtype), arr.shape)\n  elif n < 0:\n    arr = inv(arr)\n    n = abs(n)\n\n  if n == 1:\n    return arr\n  elif n == 2:\n    return arr @ arr","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/linalg.py#L363-L399","documentation":"jnp.linalg.matrix_power raises a matrix to an integer power n and therefore requires at least a 2D square input. If the input array has fewer than 2 dimensions, TypeError('{ndim}-dimensional array given. Array must be at least two-dimensional') is raised, matching NumPy.","triggerScenarios":"jnp.linalg.matrix_power(1D_vector, n), or a scalar/0-dim input; e.g. matrix_power(jnp.arange(4), 3). Called in tests like testMatrixPowerBool with non-matrix input.","commonSituations":"Passing a vector when a (1, n) or square matrix was intended; iterating over rows of a batch and forgetting to index yields scalars; boolean arrays also flow through this same check.","solutions":["Reshape to 2D: a.reshape(1, -1) or the intended square shape","Use jnp.pow / ** for elementwise powers of vectors or scalars","Verify a.ndim >= 2 and a.shape[-2] == a.shape[-1] before calling"],"exampleFix":"// before\njnp.linalg.matrix_power(vec, 3)  # vec is 1D\n// after\njnp.linalg.matrix_power(vec.reshape(1, -1) @ vec.reshape(-1, 1) ... )\n// or if elementwise power was meant:\nvec ** 3","handlingStrategy":"validation","validationCode":"a = jnp.asarray(a)\nassert a.ndim >= 2, 'matrix_power needs at least 2D input'\njnp.linalg.matrix_power(a, n)","typeGuard":"def is_matrix(x) -> bool:\n    return jnp.asarray(x).ndim >= 2","tryCatchPattern":null,"preventionTips":["Reshape vectors to (1, n) if a matrix was meant","Use ** for elementwise powers of scalars/vectors","Check ndim before calling linalg ops"],"tags":["jax","linalg","matrix-power","ndim-validation"],"backgroundTag":"shape-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}