Repository: scipy/scipy

Issue:
scipy.optimize.least_squares fails if the return array of the `fun` callback is reused
<!-- 
Thank you for taking the time to file a bug report. 
Please fill in the fields below, deleting the sections that 
don't apply to your issue. You can view the final output
by clicking the preview button above.
Note: This is a comment, and won't appear in the output.
-->

`scipy.optimize.least_squares` thinks that it has converged if the return array of the `fun` callback is reused. Due to this error I need to do one additional copy of a very big array every time I calculate my residuals. This is of course waste of memory.

#### Reproducing code example:
<!-- 
If you place your code between the triple backticks below, 
it will be rendered as a code block. 
-->
This is a modified version of the example found in [scipy.optimize.least_squares](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.least_squares.html) documentation. The `enable_bug` flag enables and disables the bug.
```
import numpy as np
from scipy.optimize import least_squares

enable_bug = True
buffer = np.empty(2)

def fun_rosenbrock(x):
    buffer[:] = np.array([10 * (x[1] - x[0] ** 2), (1 - x[0])])
    return buffer if enable_bug else np.array(buffer)

x0_rosenbrock = np.array([2, 2])
res_1 = least_squares(fun_rosenbrock, x0_rosenbrock)

print('x:', res_1.x)
print('nfev: ', res_1.nfev)
print('message: ', res_1.message)
```
#### Output (bug disabled):
<!-- If any, paste the *full* error message inside a code block
as above (starting from line Traceback)
-->
This is how the output should look like (`enable_bug = False`):
```
x: [1. 1.]
nfev:  3
message:  `gtol` termination condition is satisfied.
```

#### Output (bug enabled):
<!-- If any, paste the *full* error message inside a code block
as above (starting from line Traceback)
-->
This is the wrong output  (`enable_bug = True`):
```
x: [2. 2.]
nfev:  1
message:  `gtol` termination condition is satisfied.
```

#### Scipy/Numpy/Python version information:
<!-- You can simply run the following and paste the result in a code block
```
import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
```
-->
 By running the following:
```
import sys, scipy, numpy
print(scipy.__version__, numpy.__version__, sys.version_info)
```
I get the following:
```
1.4.1 1.18.1 sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0)
```


Discussion:

Resolving patch:
diff --git a/scipy/optimize/_lsq/least_squares.py b/scipy/optimize/_lsq/least_squares.py
--- a/scipy/optimize/_lsq/least_squares.py
+++ b/scipy/optimize/_lsq/least_squares.py
@@ -258,10 +258,10 @@ def least_squares(
         ``fun(x, *args, **kwargs)``, i.e., the minimization proceeds with
         respect to its first argument. The argument ``x`` passed to this
         function is an ndarray of shape (n,) (never a scalar, even for n=1).
-        It must return a 1-D array_like of shape (m,) or a scalar. If the
-        argument ``x`` is complex or the function ``fun`` returns complex
-        residuals, it must be wrapped in a real function of real arguments,
-        as shown at the end of the Examples section.
+        It must allocate and return a 1-D array_like of shape (m,) or a scalar.
+        If the argument ``x`` is complex or the function ``fun`` returns
+        complex residuals, it must be wrapped in a real function of real
+        arguments, as shown at the end of the Examples section.
     x0 : array_like with shape (n,) or float
         Initial guess on independent variables. If float, it will be treated
         as a 1-D array with one element.


Test patch: