MCPcopy
hub / github.com/numpy/numpy / test_axis

Method test_axis

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

Source from the content-addressed store, hash-verified

1417 _test(v)
1418
1419 def test_axis(self):
1420 # Vector norms.
1421 # Compare the use of `axis` with computing the norm of each row
1422 # or column separately.
1423 A = array([[1, 2, 3], [4, 5, 6]], dtype=self.dt)
1424 for order in [None, -1, 0, 1, 2, 3, np.inf, -np.inf]:
1425 expected0 = [norm(A[:, k], ord=order) for k in range(A.shape[1])]
1426 assert_almost_equal(norm(A, ord=order, axis=0), expected0)
1427 expected1 = [norm(A[k, :], ord=order) for k in range(A.shape[0])]
1428 assert_almost_equal(norm(A, ord=order, axis=1), expected1)
1429
1430 # Matrix norms.
1431 B = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4)
1432 nd = B.ndim
1433 for order in [None, -2, 2, -1, 1, np.inf, -np.inf, 'fro']:
1434 for axis in itertools.combinations(range(-nd, nd), 2):
1435 row_axis, col_axis = axis
1436 if row_axis < 0:
1437 row_axis += nd
1438 if col_axis < 0:
1439 col_axis += nd
1440 if row_axis == col_axis:
1441 assert_raises(ValueError, norm, B, ord=order, axis=axis)
1442 else:
1443 n = norm(B, ord=order, axis=axis)
1444
1445 # The logic using k_index only works for nd = 3.
1446 # This has to be changed if nd is increased.
1447 k_index = nd - (row_axis + col_axis)
1448 if row_axis < col_axis:
1449 expected = [norm(B[:].take(k, axis=k_index), ord=order)
1450 for k in range(B.shape[k_index])]
1451 else:
1452 expected = [norm(B[:].take(k, axis=k_index).T, ord=order)
1453 for k in range(B.shape[k_index])]
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)

Callers

nothing calls this directly

Calls 6

arrayFunction · 0.90
normFunction · 0.90
assert_almost_equalFunction · 0.90
assert_raisesFunction · 0.90
reshapeMethod · 0.80
takeMethod · 0.80

Tested by

no test coverage detected