8000 BUG: linalg.norm(): Don't convert object arrays to float by mgeier · Pull Request #7587 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: linalg.norm(): Don't convert object arrays to float #7587

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

Merged
merged 2 commits into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions numpy/linalg/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
csingle, cdouble, inexact, complexfloating, newaxis, ravel, all, Inf, dot,
add, multiply, sqrt, maximum, fastCopyAndTranspose, sum, isfinite, size,
finfo, errstate, geterrobj, longdouble, rollaxis, amin, amax, product, abs,
broadcast, atleast_2d, intp, asanyarray, isscalar
broadcast, atleast_2d, intp, asanyarray, isscalar, object_
)
from numpy.lib import triu, asfarray
from numpy.linalg import lapack_lite, _umath_linalg
Expand Down Expand Up @@ -2112,7 +2112,7 @@ def norm(x, ord=None, axis=None, keepdims=False):
"""
x = asarray(x)

if not issubclass(x.dtype.type, inexact):
if not issubclass(x.dtype.type, (inexact, object_)):
x = x.astype(float)

# Immediately handle some default, simple, fast, and common cases.
Expand Down
46 changes: 46 additions & 0 deletions numpy/linalg/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,52 @@ def test_svd_no_uv(self):
assert_equal(np.linalg.matrix_rank(a), 1)
assert_array_less(1, np.linalg.norm(a, ord=2))

def test_norm_object_array(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to test more of the norms, or at least all that work with this fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added more tests in bfa3ed9d29314d9d4a06eb5172ae855d19ae7c8d.

Everything that was broken before the regression is still broken.

# gh-7575
testvector = np.array([np.array([0, 1]), 0, 0], dtype=object)

norm = linalg.norm(testvector)
assert_array_equal(norm, [0, 1])
self.assertEqual(norm.dtype, np.dtype('float64'))

norm = linalg.norm(testvector, ord=1)
assert_array_equal(norm, [0, 1])
self.assertNotEqual(norm.dtype, np.dtype('float64'))

norm = linalg.norm(testvector, ord=2)
assert_array_equal(norm, [0, 1])
self.assertEqual(norm.dtype, np.dtype('float64'))

self.assertRaises(ValueError, linalg.norm, testvector, ord='fro')
self.assertRaises(ValueError, linalg.norm, testvector, ord='nuc')
self.assertRaises(ValueError, linalg.norm, testvector, ord=np.inf)
self.assertRaises(ValueError, linalg.norm, testvector, ord=-np.inf)
self.assertRaises((AttributeError, DeprecationWarning),
linalg.norm, testvector, ord=0)
self.assertRaises(ValueError, linalg.norm, testvector, ord=-1)
self.assertRaises(ValueError, linalg.norm, testvector, ord=-2)

testmatrix = np.array([[np.array([0, 1]), 0, 0],
[0, 0, 0]], dtype=object)

norm = linalg.norm(testmatrix)
assert_array_equal(norm, [0, 1])
self.assertEqual(norm.dtype, np.dtype('float64'))

norm = linalg.norm(testmatrix, ord='fro')
assert_array_equal(norm, [0, 1])
self.assertEqual(norm.dtype, np.dtype('float64'))

self.assertRaises(TypeError, linalg.norm, testmatrix, ord='nuc')
self.assertRaises(ValueError, linalg.norm, testmatrix, ord=np.inf)
self.assertRaises(ValueError, linalg.norm, testmatrix, ord=-np.inf)
self.assertRaises(ValueError, linalg.norm, testmatrix, ord=0)
self.assertRaises(ValueError, linalg.norm, testmatrix, ord=1)
self.assertRaises(ValueError, linalg.norm, testmatrix, ord=-1)
self.assertRaises(TypeError, linalg.norm, testmatrix, ord=2)
self.assertRaises(TypeError, linalg.norm, testmatrix, ord=-2)
self.assertRaises(ValueError, linalg.norm, testmatrix, ord=3)


if __name__ == '__main__':
run_module_suite()
0