diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-06-15 08:34:56 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-15 08:34:56 -0600 |
commit | e26738a628de0fe1897f2960708ec16e4c3177f7 (patch) | |
tree | 1eaef0e87eea8712dda48c7f28831edf99da96cd | |
parent | 0b3a7d90c4d362c88d25150ada4cc95f6b8bc621 (diff) | |
parent | 050f390199b098b8f1d7bf89a003573f17a690ba (diff) | |
download | numpy-e26738a628de0fe1897f2960708ec16e4c3177f7.tar.gz |
Merge pull request #4073 from endolith/patch-2
BUG: change real output checking to test if all imaginary parts are zero
-rw-r--r-- | numpy/lib/polynomial.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 26 |
2 files changed, 27 insertions, 7 deletions
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index c0ab80ee8..c0a1cdaed 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -15,7 +15,7 @@ import numpy.core.numeric as NX from numpy.core import (isscalar, abs, finfo, atleast_1d, hstack, dot, array, ones) from numpy.lib.twodim_base import diag, vander -from numpy.lib.function_base import trim_zeros, sort_complex +from numpy.lib.function_base import trim_zeros from numpy.lib.type_check import iscomplex, real, imag, mintypecode from numpy.linalg import eigvals, lstsq, inv @@ -145,11 +145,7 @@ def poly(seq_of_zeros): if issubclass(a.dtype.type, NX.complexfloating): # if complex roots are all complex conjugates, the roots are real. roots = NX.asarray(seq_of_zeros, complex) - pos_roots = sort_complex(NX.compress(roots.imag > 0, roots)) - neg_roots = NX.conjugate(sort_complex( - NX.compress(roots.imag < 0, roots))) - if (len(pos_roots) == len(neg_roots) and - NX.alltrue(neg_roots == pos_roots)): + if NX.all(NX.sort(roots) == NX.sort(roots.conjugate())): a = a.real.copy() return a diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 5c15941e6..6d2e330ec 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -81,7 +81,7 @@ poly1d([ 2.]) import numpy as np from numpy.testing import ( run_module_suite, TestCase, assert_, assert_equal, assert_array_equal, - assert_almost_equal, rundocs + assert_almost_equal, assert_array_almost_equal, rundocs ) @@ -89,6 +89,30 @@ class TestDocs(TestCase): def test_doctests(self): return rundocs() + def test_poly(self): + assert_array_almost_equal(np.poly([3, -np.sqrt(2), np.sqrt(2)]), + [1, -3, -2, 6]) + + # From matlab docs + A = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] + assert_array_almost_equal(np.poly(A), [1, -6, -72, -27]) + + # Should produce real output for perfect conjugates + assert_(np.isrealobj(np.poly([+1.082j, +2.613j, -2.613j, -1.082j]))) + assert_(np.isrealobj(np.poly([0+1j, -0+-1j, 1+2j, + 1-2j, 1.+3.5j, 1-3.5j]))) + assert_(np.isrealobj(np.poly([1j, -1j, 1+2j, 1-2j, 1+3j, 1-3.j]))) + assert_(np.isrealobj(np.poly([1j, -1j, 1+2j, 1-2j]))) + assert_(np.isrealobj(np.poly([1j, -1j, 2j, -2j]))) + assert_(np.isrealobj(np.poly([1j, -1j]))) + assert_(np.isrealobj(np.poly([1, -1]))) + + assert_(np.iscomplexobj(np.poly([1j, -1.0000001j]))) + + np.random.seed(42) + a = np.random.randn(100) + 1j*np.random.randn(100) + assert_(np.isrealobj(np.poly(np.concatenate((a, np.conjugate(a)))))) + def test_roots(self): assert_array_equal(np.roots([1, 0, 0]), [0, 0]) |