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

Method test_axis

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

Source from the content-addressed store, hash-verified

1323 _test(v)
1324
1325 def test_axis(self):
1326 # Vector norms.
1327 # Compare the use of `axis` with computing the norm of each row
1328 # or column separately.
1329 A = array([[1, 2, 3], [4, 5, 6]], dtype=self.dt)
1330 for order in [None, -1, 0, 1, 2, 3, np.Inf, -np.Inf]:
1331 expected0 = [norm(A[:, k], ord=order) for k in range(A.shape[1])]
1332 assert_almost_equal(norm(A, ord=order, axis=0), expected0)
1333 expected1 = [norm(A[k, :], ord=order) for k in range(A.shape[0])]
1334 assert_almost_equal(norm(A, ord=order, axis=1), expected1)
1335
1336 # Matrix norms.
1337 B = np.arange(1, 25, dtype=self.dt).reshape(2, 3, 4)
1338 nd = B.ndim
1339 for order in [None, -2, 2, -1, 1, np.Inf, -np.Inf, 'fro']:
1340 for axis in itertools.combinations(range(-nd, nd), 2):
1341 row_axis, col_axis = axis
1342 if row_axis < 0:
1343 row_axis += nd
1344 if col_axis < 0:
1345 col_axis += nd
1346 if row_axis == col_axis:
1347 assert_raises(ValueError, norm, B, ord=order, axis=axis)
1348 else:
1349 n = norm(B, ord=order, axis=axis)
1350
1351 # The logic using k_index only works for nd = 3.
1352 # This has to be changed if nd is increased.
1353 k_index = nd - (row_axis + col_axis)
1354 if row_axis < col_axis:
1355 expected = [norm(B[:].take(k, axis=k_index), ord=order)
1356 for k in range(B.shape[k_index])]
1357 else:
1358 expected = [norm(B[:].take(k, axis=k_index).T, ord=order)
1359 for k in range(B.shape[k_index])]
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)

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