(self)
| 1454 | assert_almost_equal(n, expected) |
| 1455 | |
| 1456 | def test_keepdims(self): |
| 1457 | A = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4) |
| 1458 | |
| 1459 | allclose_err = 'order {0}, axis = {1}' |
| 1460 | shape_err = 'Shape mismatch found {0}, expected {1}, order={2}, axis={3}' |
| 1461 | |
| 1462 | # check the order=None, axis=None case |
| 1463 | expected = norm(A, ord=None, axis=None) |
| 1464 | found = norm(A, ord=None, axis=None, keepdims=True) |
| 1465 | assert_allclose(np.squeeze(found), expected, |
| 1466 | err_msg=allclose_err.format(None, None)) |
| 1467 | expected_shape = (1, 1, 1) |
| 1468 | assert_(found.shape == expected_shape, |
| 1469 | shape_err.format(found.shape, expected_shape, None, None)) |
| 1470 | |
| 1471 | # Vector norms. |
| 1472 | for order in [None, -1, 0, 1, 2, 3, np.inf, -np.inf]: |
| 1473 | for k in range(A.ndim): |
| 1474 | expected = norm(A, ord=order, axis=k) |
| 1475 | found = norm(A, ord=order, axis=k, keepdims=True) |
| 1476 | assert_allclose(np.squeeze(found), expected, |
| 1477 | err_msg=allclose_err.format(order, k)) |
| 1478 | expected_shape = list(A.shape) |
| 1479 | expected_shape[k] = 1 |
| 1480 | expected_shape = tuple(expected_shape) |
| 1481 | assert_(found.shape == expected_shape, |
| 1482 | shape_err.format(found.shape, expected_shape, order, k)) |
| 1483 | |
| 1484 | # Matrix norms. |
| 1485 | for order in [None, -2, 2, -1, 1, np.inf, -np.inf, 'fro', 'nuc']: |
| 1486 | for k in itertools.permutations(range(A.ndim), 2): |
| 1487 | expected = norm(A, ord=order, axis=k) |
| 1488 | found = norm(A, ord=order, axis=k, keepdims=True) |
| 1489 | assert_allclose(np.squeeze(found), expected, |
| 1490 | err_msg=allclose_err.format(order, k)) |
| 1491 | expected_shape = list(A.shape) |
| 1492 | expected_shape[k[0]] = 1 |
| 1493 | expected_shape[k[1]] = 1 |
| 1494 | expected_shape = tuple(expected_shape) |
| 1495 | assert_(found.shape == expected_shape, |
| 1496 | shape_err.format(found.shape, expected_shape, order, k)) |
| 1497 | |
| 1498 | |
| 1499 | class _TestNorm2D(_TestNormBase): |
nothing calls this directly
no test coverage detected