def test_transpose(self): v = Variable(['time', 'x'], self.d) v2 = Variable(['x', 'time'], self.d.T) self.assertVariableIdentical(v, v2.transpose()) self.assertVariableIdentical(v.transpose(), v.T) x = np.random.randn(2, 3, 4, 5) w = Variable(['a', 'b', 'c', 'd'], x) w2 = Variable(['d', 'b', 'c', 'a'], np.einsum('abcd->dbca', x)) self.assertEqual(w2.shape, (5, 3, 4, 2)) self.assertVariableIdentical(w2, w.transpose('d', 'b', 'c', 'a')) self.assertVariableIdentical(w, w2.transpose('a', 'b', 'c', 'd')) w3 = Variable(['b', 'c', 'd', 'a'], np.einsum('abcd->bcda', x)) self.assertVariableIdentical(w, w3.transpose('a', 'b', 'c', 'd'))
def test_transpose(self): v = Variable(["time", "x"], self.d) v2 = Variable(["x", "time"], self.d.T) self.assertVariableIdentical(v, v2.transpose()) self.assertVariableIdentical(v.transpose(), v.T) x = np.random.randn(2, 3, 4, 5) w = Variable(["a", "b", "c", "d"], x) w2 = Variable(["d", "b", "c", "a"], np.einsum("abcd->dbca", x)) self.assertEqual(w2.shape, (5, 3, 4, 2)) self.assertVariableIdentical(w2, w.transpose("d", "b", "c", "a")) self.assertVariableIdentical(w, w2.transpose("a", "b", "c", "d")) w3 = Variable(["b", "c", "d", "a"], np.einsum("abcd->bcda", x)) self.assertVariableIdentical(w, w3.transpose("a", "b", "c", "d"))
def test_concat(self): x = np.arange(5) y = np.ones(5) v = self.cls(['a'], x) w = self.cls(['a'], y) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b', length=2)) with self.assertRaisesRegexp(ValueError, 'actual length'): Variable.concat([v, w], 'b', length=1) with self.assertRaisesRegexp(ValueError, 'actual length'): Variable.concat([v, w, w], 'b', length=4) with self.assertRaisesRegexp(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test concatenating along a dimension v = Variable(['time', 'x'], np.random.random((10, 8))) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5], v[6:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[0], v[1:]], 'time')) # test dimension order self.assertVariableIdentical(v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) self.assertVariableIdentical(v.transpose(), Variable.concat([v[:, 0], v[:, 1:]], 'x'))