optimize.least_squares memory allocation every function call
![](https://secure.gravatar.com/avatar/37b7b804b046d8ea0caabd5b80559fd2.jpg?s=120&d=mm&r=g)
I have a question about the scipy.optimize.least_squares function. I have found that the function to optimize needs to reallocate the return array every time the function is called. I've tried having the calling function return the same array every time (just re-filled with the correct return values), but least_squares doesn't seem to converge when I do this. I do not like having to allocate memory in the calling function, this seems slow and unnecessary. Is there a way around this? If not, could scipy.optimize.least_squares be adjusted so this is possible? The Gnu Scientific Library (which I sometimes fall back on when scipy.optimize.least_squares is too slow, I think this allocation issue is one major contributor), passes an array to the calling function to fill. Thank you, Brian Hare
![](https://secure.gravatar.com/avatar/df94900476a6290e98a26979ce635071.jpg?s=120&d=mm&r=g)
Hey Brian, I haven't tested this, but, off the top of my head: would it not be possible to pass a pre-existing allocated array as a `kwarg` to the function, and have your function write values into that array? Then you can add it to the `least_squares` call as `{"return_array": np.zeros(N)}). Cheers, Dominik On Tue, 14 Apr 2020 at 10:40, Brian Hare <bhare8972@hotmail.com> wrote:
I have a question about the scipy.optimize.least_squares function. I have found that the function to optimize needs to reallocate the return array every time the function is called. I've tried having the calling function return the same array every time (just re-filled with the correct return values), but least_squares doesn't seem to converge when I do this.
I do not like having to allocate memory in the calling function, this seems slow and unnecessary. Is there a way around this? If not, could scipy.optimize.least_squares be adjusted so this is possible?
The Gnu Scientific Library (which I sometimes fall back on when scipy.optimize.least_squares is too slow, I think this allocation issue is one major contributor), passes an array to the calling function to fill.
Thank you, Brian Hare _______________________________________________ SciPy-User mailing list SciPy-User@python.org https://mail.python.org/mailman/listinfo/scipy-user
participants (3)
-
Andrew Nelson
-
Brian Hare
-
Dominik Stańczak