prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  lstsq
  
  
  </issue>
  <code>
  [start of ivy/functional/frontends/jax/numpy/linalg.py]
  1 # local
  2 import ivy
  3 from ivy.functional.frontends.jax import Array
  4 from ivy.functional.frontends.jax.func_wrapper import to_ivy_arrays_and_back
  5 from ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes
  6 from ivy.functional.frontends.jax.numpy import promote_types_of_jax_inputs
  7 
  8 
  9 @to_ivy_arrays_and_back
  10 def cholesky(a):
  11     return ivy.cholesky(a)
  12 
  13 
  14 @to_ivy_arrays_and_back
  15 def cond(x, p=None):
  16     return ivy.cond(x, p=p)
  17 
  18 
  19 @to_ivy_arrays_and_back
  20 def det(a):
  21     return ivy.det(a)
  22 
  23 
  24 @to_ivy_arrays_and_back
  25 def eig(a):
  26     return ivy.eig(a)
  27 
  28 
  29 @to_ivy_arrays_and_back
  30 def eigh(a, UPLO="L", symmetrize_input=True):
  31     def symmetrize(x):
  32         # TODO : Take Hermitian transpose after complex numbers added
  33         return (x + ivy.swapaxes(x, -1, -2)) / 2
  34 
  35     if symmetrize_input:
  36         a = symmetrize(a)
  37 
  38     return ivy.eigh(a, UPLO=UPLO)
  39 
  40 
  41 @to_ivy_arrays_and_back
  42 def eigvals(a):
  43     return ivy.eigvals(a)
  44 
  45 
  46 @to_ivy_arrays_and_back
  47 def eigvalsh(a, UPLO="L"):
  48     return ivy.eigvalsh(a, UPLO=UPLO)
  49 
  50 
  51 @to_ivy_arrays_and_back
  52 def inv(a):
  53     return ivy.inv(a)
  54 
  55 
  56 @to_ivy_arrays_and_back
  57 def matrix_power(a, n):
  58     return ivy.matrix_power(a, n)
  59 
  60 
  61 @to_ivy_arrays_and_back
  62 def matrix_rank(M, tol=None):
  63     return ivy.matrix_rank(M, atol=tol)
  64 
  65 
  66 @to_ivy_arrays_and_back
  67 def multi_dot(arrays, *, precision=None):
  68     return ivy.multi_dot(arrays)
  69 
  70 
  71 @to_ivy_arrays_and_back
  72 @with_supported_dtypes(
  73     {"0.4.14 and below": ("float32", "float64")},
  74     "jax",
  75 )
  76 def norm(x, ord=None, axis=None, keepdims=False):
  77     if ord is None:
  78         ord = 2
  79     if type(axis) in [list, tuple] and len(axis) == 2:
  80         return Array(ivy.matrix_norm(x, ord=ord, axis=axis, keepdims=keepdims))
  81     return Array(ivy.vector_norm(x, ord=ord, axis=axis, keepdims=keepdims))
  82 
  83 
  84 @to_ivy_arrays_and_back
  85 def pinv(a, rcond=None):
  86     return ivy.pinv(a, rtol=rcond)
  87 
  88 
  89 @to_ivy_arrays_and_back
  90 def qr(a, mode="reduced"):
  91     return ivy.qr(a, mode=mode)
  92 
  93 
  94 @to_ivy_arrays_and_back
  95 def slogdet(a, method=None):
  96     return ivy.slogdet(a)
  97 
  98 
  99 @to_ivy_arrays_and_back
  100 def solve(a, b):
  101     return ivy.solve(a, b)
  102 
  103 
  104 @to_ivy_arrays_and_back
  105 def svd(a, /, *, full_matrices=True, compute_uv=True, hermitian=None):
  106     if not compute_uv:
  107         return ivy.svdvals(a)
  108     return ivy.svd(a, full_matrices=full_matrices)
  109 
  110 
  111 @to_ivy_arrays_and_back
  112 @with_unsupported_dtypes({"0.4.14 and below": ("float16", "bfloat16")}, "jax")
  113 def tensorinv(a, ind=2):
  114     old_shape = ivy.shape(a)
  115     prod = 1
  116     if ind > 0:
  117         invshape = old_shape[ind:] + old_shape[:ind]
  118         for k in old_shape[ind:]:
  119             prod *= k
  120     else:
  121         raise ValueError("Invalid ind argument.")
  122     a = ivy.reshape(a, shape=(prod, -1))
  123     ia = ivy.inv(a)
  124     new_shape = tuple([*invshape])
  125     return Array(ivy.reshape(ia, shape=new_shape))
  126 
  127 
  128 @to_ivy_arrays_and_back
  129 def tensorsolve(a, b, axes=None):
  130     a, b = promote_types_of_jax_inputs(a, b)
  131     return ivy.tensorsolve(a, b, axes=axes)
  132
  [end of ivy/functional/frontends/jax/numpy/linalg.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/ivy/functional/frontends/jax/numpy/linalg.py b/ivy/functional/frontends/jax/numpy/linalg.py
  --- a/ivy/functional/frontends/jax/numpy/linalg.py
  +++ b/ivy/functional/frontends/jax/numpy/linalg.py
  @@ -4,6 +4,7 @@
   from ivy.functional.frontends.jax.func_wrapper import to_ivy_arrays_and_back
   from ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes
   from ivy.functional.frontends.jax.numpy import promote_types_of_jax_inputs
  +from ivy.functional.frontends.numpy.linalg import lstsq as numpy_lstsq
   
   
   @to_ivy_arrays_and_back
  @@ -53,6 +54,23 @@
       return ivy.inv(a)
   
   
  +# TODO: replace this with function from API
  +# As the composition provides numerically unstable results
  +@to_ivy_arrays_and_back
  +def lstsq(a, b, rcond=None, *, numpy_resid=False):
  +    if numpy_resid:
  +        return numpy_lstsq(a, b, rcond=rcond)
  +    least_squares_solution = ivy.matmul(
  +        ivy.pinv(a, rtol=1e-15).astype(ivy.float64), b.astype(ivy.float64)
  +    )
  +    residuals = ivy.sum((b - ivy.matmul(a, least_squares_solution)) ** 2).astype(
  +        ivy.float64
  +    )
  +    svd_values = ivy.svd(a, compute_uv=False)
  +    rank = ivy.matrix_rank(a).astype(ivy.int32)
  +    return (least_squares_solution, residuals, rank, svd_values[0])
  +
  +
   @to_ivy_arrays_and_back
   def matrix_power(a, n):
       return ivy.matrix_power(a, n)
  
verification_info: {"golden_diff": "diff --git a/ivy/functional/frontends/jax/numpy/linalg.py b/ivy/functional/frontends/jax/numpy/linalg.py\n--- a/ivy/functional/frontends/jax/numpy/linalg.py\n+++ b/ivy/functional/frontends/jax/numpy/linalg.py\n@@ -4,6 +4,7 @@\n from ivy.functional.frontends.jax.func_wrapper import to_ivy_arrays_and_back\n from ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes\n from ivy.functional.frontends.jax.numpy import promote_types_of_jax_inputs\n+from ivy.functional.frontends.numpy.linalg import lstsq as numpy_lstsq\n \n \n @to_ivy_arrays_and_back\n@@ -53,6 +54,23 @@\n     return ivy.inv(a)\n \n \n+# TODO: replace this with function from API\n+# As the composition provides numerically unstable results\n+@to_ivy_arrays_and_back\n+def lstsq(a, b, rcond=None, *, numpy_resid=False):\n+    if numpy_resid:\n+        return numpy_lstsq(a, b, rcond=rcond)\n+    least_squares_solution = ivy.matmul(\n+        ivy.pinv(a, rtol=1e-15).astype(ivy.float64), b.astype(ivy.float64)\n+    )\n+    residuals = ivy.sum((b - ivy.matmul(a, least_squares_solution)) ** 2).astype(\n+        ivy.float64\n+    )\n+    svd_values = ivy.svd(a, compute_uv=False)\n+    rank = ivy.matrix_rank(a).astype(ivy.int32)\n+    return (least_squares_solution, residuals, rank, svd_values[0])\n+\n+\n @to_ivy_arrays_and_back\n def matrix_power(a, n):\n     return ivy.matrix_power(a, n)\n", "issue": "lstsq\n\n", "before_files": [{"content": "# local\nimport ivy\nfrom ivy.functional.frontends.jax import Array\nfrom ivy.functional.frontends.jax.func_wrapper import to_ivy_arrays_and_back\nfrom ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes\nfrom ivy.functional.frontends.jax.numpy import promote_types_of_jax_inputs\n\n\n@to_ivy_arrays_and_back\ndef cholesky(a):\n    return ivy.cholesky(a)\n\n\n@to_ivy_arrays_and_back\ndef cond(x, p=None):\n    return ivy.cond(x, p=p)\n\n\n@to_ivy_arrays_and_back\ndef det(a):\n    return ivy.det(a)\n\n\n@to_ivy_arrays_and_back\ndef eig(a):\n    return ivy.eig(a)\n\n\n@to_ivy_arrays_and_back\ndef eigh(a, UPLO=\"L\", symmetrize_input=True):\n    def symmetrize(x):\n        # TODO : Take Hermitian transpose after complex numbers added\n        return (x + ivy.swapaxes(x, -1, -2)) / 2\n\n    if symmetrize_input:\n        a = symmetrize(a)\n\n    return ivy.eigh(a, UPLO=UPLO)\n\n\n@to_ivy_arrays_and_back\ndef eigvals(a):\n    return ivy.eigvals(a)\n\n\n@to_ivy_arrays_and_back\ndef eigvalsh(a, UPLO=\"L\"):\n    return ivy.eigvalsh(a, UPLO=UPLO)\n\n\n@to_ivy_arrays_and_back\ndef inv(a):\n    return ivy.inv(a)\n\n\n@to_ivy_arrays_and_back\ndef matrix_power(a, n):\n    return ivy.matrix_power(a, n)\n\n\n@to_ivy_arrays_and_back\ndef matrix_rank(M, tol=None):\n    return ivy.matrix_rank(M, atol=tol)\n\n\n@to_ivy_arrays_and_back\ndef multi_dot(arrays, *, precision=None):\n    return ivy.multi_dot(arrays)\n\n\n@to_ivy_arrays_and_back\n@with_supported_dtypes(\n    {\"0.4.14 and below\": (\"float32\", \"float64\")},\n    \"jax\",\n)\ndef norm(x, ord=None, axis=None, keepdims=False):\n    if ord is None:\n        ord = 2\n    if type(axis) in [list, tuple] and len(axis) == 2:\n        return Array(ivy.matrix_norm(x, ord=ord, axis=axis, keepdims=keepdims))\n    return Array(ivy.vector_norm(x, ord=ord, axis=axis, keepdims=keepdims))\n\n\n@to_ivy_arrays_and_back\ndef pinv(a, rcond=None):\n    return ivy.pinv(a, rtol=rcond)\n\n\n@to_ivy_arrays_and_back\ndef qr(a, mode=\"reduced\"):\n    return ivy.qr(a, mode=mode)\n\n\n@to_ivy_arrays_and_back\ndef slogdet(a, method=None):\n    return ivy.slogdet(a)\n\n\n@to_ivy_arrays_and_back\ndef solve(a, b):\n    return ivy.solve(a, b)\n\n\n@to_ivy_arrays_and_back\ndef svd(a, /, *, full_matrices=True, compute_uv=True, hermitian=None):\n    if not compute_uv:\n        return ivy.svdvals(a)\n    return ivy.svd(a, full_matrices=full_matrices)\n\n\n@to_ivy_arrays_and_back\n@with_unsupported_dtypes({\"0.4.14 and below\": (\"float16\", \"bfloat16\")}, \"jax\")\ndef tensorinv(a, ind=2):\n    old_shape = ivy.shape(a)\n    prod = 1\n    if ind > 0:\n        invshape = old_shape[ind:] + old_shape[:ind]\n        for k in old_shape[ind:]:\n            prod *= k\n    else:\n        raise ValueError(\"Invalid ind argument.\")\n    a = ivy.reshape(a, shape=(prod, -1))\n    ia = ivy.inv(a)\n    new_shape = tuple([*invshape])\n    return Array(ivy.reshape(ia, shape=new_shape))\n\n\n@to_ivy_arrays_and_back\ndef tensorsolve(a, b, axes=None):\n    a, b = promote_types_of_jax_inputs(a, b)\n    return ivy.tensorsolve(a, b, axes=axes)\n", "path": "ivy/functional/frontends/jax/numpy/linalg.py"}]}