diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-01-23 20:04:05 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-01-23 20:18:12 -0700 |
commit | 855b66f3eeab166ee504d73952b61b4b713f3c6f (patch) | |
tree | 3e015dab6a457b37692c9493dda1b9538898d9eb /numpy/polynomial/polynomial.py | |
parent | dce10183bc8f3d243bd5fc70140f5ad71179d05c (diff) | |
download | numpy-855b66f3eeab166ee504d73952b61b4b713f3c6f.tar.gz |
BUG: gh-2790, fix column scaling in polynomial package least squares.
The columns should be scaled using their 2-norm, but in the complex case
that was being incorrectly computed as the square root of the sum of the
squared elements rather than as the square root of the sum of their squared
real and imaginary parts.
Diffstat (limited to 'numpy/polynomial/polynomial.py')
-rw-r--r-- | numpy/polynomial/polynomial.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py index 324bec9c0..4e24e9c1f 100644 --- a/numpy/polynomial/polynomial.py +++ b/numpy/polynomial/polynomial.py @@ -1365,8 +1365,14 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None): if rcond is None : rcond = len(x)*np.finfo(x.dtype).eps - # scale the design matrix and solve the least squares equation - scl = np.sqrt((lhs*lhs).sum(1)) + # Determine the norms of the design matrix columns. + if lhs.dtype.char in np.typecodes['Complex']: + scl = np.sqrt((np.square(lhs.real) + np.square(lhs.imag)).sum(1)) + else: + scl = np.sqrt(np.square(lhs).sum(1)) + scl[scl == 0] = 1 + + # Solve the least squares problem. c, resids, rank, s = la.lstsq(lhs.T/scl, rhs.T, rcond) c = (c.T/scl).T |