Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leastsq needlessly appends extra dimension for scalar problems #8532

Closed
nschloe opened this issue Mar 7, 2018 · 6 comments
Closed

leastsq needlessly appends extra dimension for scalar problems #8532

nschloe opened this issue Mar 7, 2018 · 6 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.optimize
Milestone

Comments

@nschloe
Copy link
Contributor

nschloe commented Mar 7, 2018

When treating a scalar problem, leastsq needlessly appends an extra dimension to the argument of the function to be minimized as well as the result. MCVE:

from scipy.optimize import leastsq

def f(x):
    print(x.shape)
    return x


out = leastsq(f, 0.0)
print(out)
print()
leastsq(f, [0.0])
print(out)

Output:

(1,)
(1,)
(1,)
(1,)
(array([0.]), 4)

(1,)
(1,)
(1,)
(1,)
(array([0.]), 4)

Expected output:

()
()
()
()
(array(0.), 4)

(1,)
(1,)
(1,)
(1,)
(array([0.]), 4)

As a consequence of this, many codes have to select the "first" element in a scalar problem

out, _ = leastsq(f, 0.0)
theta = out[0]  # this should be unnecessary

I personally ran into this when trying to clean up my f(x) which involves dots and such, and wondered why things like

array = something()

def f(theta):
    sc = numpy.array([numpy.sin(theta), numpy.cos(theta)])
    return numpy.dot(sc, array)

wouldn't work. (It was the superfluous dimension in theta that had the dot product fail.)

@ilayn ilayn added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.optimize labels Mar 10, 2018
@sudheerachary
Copy link

I think this is due to the conversion of x0 i.e (The starting estimate for the minimization) into a numpy.array in method leastsq, should this be handled if x0 has only one value or add appropriate documentation for inputs of func in method leastsq

@pv
Copy link
Member

pv commented Apr 2, 2018

At this point, I would favor simply documentation-only fix as no doubt there is code out there relying on the current behavior, so it cannot just be changed.

The issue is that in general these routines work only on 1D vectors and don't preserve the input shape, so the current behavior is not immediately considered a bug. If they generally preserved the input shape one could argue differently, but this is not the case.

@nschloe
Copy link
Contributor Author

nschloe commented Apr 2, 2018

Ah, you're right: It's the flatten() at https://github.com/scipy/scipy/blob/master/scipy/optimize/minpack.py#L377.

@sudheerachary
Copy link

I think some change required in line 279 also, may be similar to this

diff --git a/scipy/optimize/minpack.py b/scipy/optimize/minpack.py
index a39bc15..5812e9e 100644
--- a/scipy/optimize/minpack.py
+++ b/scipy/optimize/minpack.py
@@ -275,7 +275,7 @@ def leastsq(func, x0, args=(), Dfun=None, full_output=0,
     Parameters
     ----------
     func : callable
-        should take at least one (possibly length N vector) argument and
+        should take at least one (possibly length N vector) argument of type 1D numpy.array and
         returns M floating point numbers. It must not return NaNs or
         fitting might fail.
     x0 : ndarray

@andyfaff
Copy link
Contributor

andyfaff commented Apr 3, 2018

This is being addressed in #8666

@ev-br ev-br closed this as completed in ef0cf9f Apr 3, 2018
ev-br added a commit that referenced this issue Apr 3, 2018
@ev-br ev-br added this to the 1.1.0 milestone Apr 3, 2018
@nschloe
Copy link
Contributor Author

nschloe commented Apr 28, 2021

I've added a wrapper function for this in scipyx, so I guess everyone's happy now. 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.optimize
Projects
None yet
Development

No branches or pull requests

6 participants