MCPcopy Create free account
hub / github.com/numpy/numpy / test_keepdims

Method test_keepdims

numpy/linalg/tests/test_linalg.py:1362–1402  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1360 assert_almost_equal(n, expected)
1361
1362 def test_keepdims(self):
1363 A = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4)
1364
1365 allclose_err = 'order {0}, axis = {1}'
1366 shape_err = 'Shape mismatch found {0}, expected {1}, order={2}, axis={3}'
1367
1368 # check the order=None, axis=None case
1369 expected = norm(A, ord=None, axis=None)
1370 found = norm(A, ord=None, axis=None, keepdims=True)
1371 assert_allclose(np.squeeze(found), expected,
1372 err_msg=allclose_err.format(None, None))
1373 expected_shape = (1, 1, 1)
1374 assert_(found.shape == expected_shape,
1375 shape_err.format(found.shape, expected_shape, None, None))
1376
1377 # Vector norms.
1378 for order in [None, -1, 0, 1, 2, 3, np.Inf, -np.Inf]:
1379 for k in range(A.ndim):
1380 expected = norm(A, ord=order, axis=k)
1381 found = norm(A, ord=order, axis=k, keepdims=True)
1382 assert_allclose(np.squeeze(found), expected,
1383 err_msg=allclose_err.format(order, k))
1384 expected_shape = list(A.shape)
1385 expected_shape[k] = 1
1386 expected_shape = tuple(expected_shape)
1387 assert_(found.shape == expected_shape,
1388 shape_err.format(found.shape, expected_shape, order, k))
1389
1390 # Matrix norms.
1391 for order in [None, -2, 2, -1, 1, np.Inf, -np.Inf, 'fro', 'nuc']:
1392 for k in itertools.permutations(range(A.ndim), 2):
1393 expected = norm(A, ord=order, axis=k)
1394 found = norm(A, ord=order, axis=k, keepdims=True)
1395 assert_allclose(np.squeeze(found), expected,
1396 err_msg=allclose_err.format(order, k))
1397 expected_shape = list(A.shape)
1398 expected_shape[k[0]] = 1
1399 expected_shape[k[1]] = 1
1400 expected_shape = tuple(expected_shape)
1401 assert_(found.shape == expected_shape,
1402 shape_err.format(found.shape, expected_shape, order, k))
1403
1404
1405class _TestNorm2D(_TestNormBase):

Callers

nothing calls this directly

Calls 5

normFunction · 0.90
assert_allcloseFunction · 0.90
assert_Function · 0.90
reshapeMethod · 0.80
squeezeMethod · 0.45

Tested by

no test coverage detected