MCPcopy Index your code
hub / github.com/numpy/numpy / test_keepdims_argument

Method test_keepdims_argument

numpy/_core/tests/test_ufunc.py:1279–1355  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1277 assert_raises(TypeError, np.add, 1., 1., axis=0)
1278
1279 def test_keepdims_argument(self):
1280 # vecdot signature: '(n),(n)->()'
1281 a = np.arange(27.).reshape((3, 3, 3))
1282 b = np.arange(10., 19.).reshape((3, 1, 3))
1283 c = np.vecdot(a, b)
1284 assert_array_equal(c, (a * b).sum(-1))
1285 c = np.vecdot(a, b, keepdims=False)
1286 assert_array_equal(c, (a * b).sum(-1))
1287 c = np.vecdot(a, b, keepdims=True)
1288 assert_array_equal(c, (a * b).sum(-1, keepdims=True))
1289 out = np.zeros_like(c)
1290 d = np.vecdot(a, b, keepdims=True, out=out)
1291 assert_(d is out)
1292 assert_array_equal(d, c)
1293 # Now combined with axis and axes.
1294 c = np.vecdot(a, b, axis=-1, keepdims=False)
1295 assert_array_equal(c, (a * b).sum(-1, keepdims=False))
1296 c = np.vecdot(a, b, axis=-1, keepdims=True)
1297 assert_array_equal(c, (a * b).sum(-1, keepdims=True))
1298 c = np.vecdot(a, b, axis=0, keepdims=False)
1299 assert_array_equal(c, (a * b).sum(0, keepdims=False))
1300 c = np.vecdot(a, b, axis=0, keepdims=True)
1301 assert_array_equal(c, (a * b).sum(0, keepdims=True))
1302 c = np.vecdot(a, b, axes=[(-1,), (-1,), ()], keepdims=False)
1303 assert_array_equal(c, (a * b).sum(-1))
1304 c = np.vecdot(a, b, axes=[(-1,), (-1,), (-1,)], keepdims=True)
1305 assert_array_equal(c, (a * b).sum(-1, keepdims=True))
1306 c = np.vecdot(a, b, axes=[0, 0], keepdims=False)
1307 assert_array_equal(c, (a * b).sum(0))
1308 c = np.vecdot(a, b, axes=[0, 0, 0], keepdims=True)
1309 assert_array_equal(c, (a * b).sum(0, keepdims=True))
1310 c = np.vecdot(a, b, axes=[0, 2], keepdims=False)
1311 assert_array_equal(c, (a.transpose(1, 2, 0) * b).sum(-1))
1312 c = np.vecdot(a, b, axes=[0, 2], keepdims=True)
1313 assert_array_equal(c, (a.transpose(1, 2, 0) * b).sum(-1,
1314 keepdims=True))
1315 c = np.vecdot(a, b, axes=[0, 2, 2], keepdims=True)
1316 assert_array_equal(c, (a.transpose(1, 2, 0) * b).sum(-1,
1317 keepdims=True))
1318 c = np.vecdot(a, b, axes=[0, 2, 0], keepdims=True)
1319 assert_array_equal(c, (a * b.transpose(2, 0, 1)).sum(0, keepdims=True))
1320 # Hardly useful, but should work.
1321 c = np.vecdot(a, b, axes=[0, 2, 1], keepdims=True)
1322 assert_array_equal(c, (a.transpose(1, 0, 2) * b.transpose(0, 2, 1))
1323 .sum(1, keepdims=True))
1324 # Check with two core dimensions.
1325 a = np.eye(3) * np.arange(4.)[:, np.newaxis, np.newaxis]
1326 expected = uml.det(a)
1327 c = uml.det(a, keepdims=False)
1328 assert_array_equal(c, expected)
1329 c = uml.det(a, keepdims=True)
1330 assert_array_equal(c, expected[:, np.newaxis, np.newaxis])
1331 a = np.eye(3) * np.arange(4.)[:, np.newaxis, np.newaxis]
1332 expected_s, expected_l = uml.slogdet(a)
1333 cs, cl = uml.slogdet(a, keepdims=False)
1334 assert_array_equal(cs, expected_s)
1335 assert_array_equal(cl, expected_l)
1336 cs, cl = uml.slogdet(a, keepdims=True)

Callers

nothing calls this directly

Calls 6

assert_array_equalFunction · 0.90
assert_Function · 0.90
assert_raisesFunction · 0.90
reshapeMethod · 0.80
zeros_likeMethod · 0.80
sumMethod · 0.45

Tested by

no test coverage detected