def test_prod(self): tensor = Tensor([2, 2]) tensor.set([ [4, 3], [2, 1] ]) prod = td.prod(tensor) self.assertEqual(prod, 24.0)
def test_mul(self): t1 = Tensor([2, 2]) t1.set([ [4, 3], [2, 1] ]) tensor = Tensor.mul(t1, 2.0) self.assertEqual(tensor.data, [[8.0, 6.0], [4.0, 2.0]])
def test_maximum(self): t1 = Tensor([2, 2]) t1.set([ [4, -3], [-2, 1] ]) tensor = td.maximum(t1, 0) self.assertEqual(tensor.data, [[4.0, 0.0], [0.0, 1.0]])
def test_get_value_at_index(self): t1 = Tensor([2, 3]) t1.set([[1, 2, 3], [4, 5, 6]]) self.assertEqual(t1.get([1, 1]), 5.0) t2 = Tensor([3, 2, 1]) t2.set([ [[1], [2]], [[3], [4]], [[5], [6]] ]) self.assertEqual(t2.get([2, 0, 0]), 5.0)
def angle(cls, v1, v2, degrees=False): if Tensor.match_shapes(v1, v2): if degrees: return LIB().v_angle(v1.pointer, v2.pointer, 1) return LIB().v_angle(v1.pointer, v2.pointer, 0) else: raise ValueError("Vectors must have matching lengths")
def test_add(self): t1 = Tensor([2, 2]) t1.set([ [1, 2], [3, 4] ]) t2 = Tensor([2, 2]) t2.set([ [4, 3], [2, 1] ]) tensor = Tensor.add(t1, t2) self.assertEqual(tensor.data, [[5.0, 5.0], [5.0, 5.0]])
def test_reshape(self): tensor = Tensor([3, 2]) tensor.set([ [1, 2], [3, 4], [5, 6] ]) tensor.reshape(2, 3) self.assertEqual(tensor.data, [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) tensor.reshape(-1, 6) self.assertEqual(tensor.data, [[1., 2., 3., 4., 5., 6.]])
def test_from_shape(self): tensor = Tensor([3, 2, 5]) self.assertEqual(tensor.shape, [3, 2, 5])
def test_wrong_shape_type(self): with self.assertRaises(TypeError): Tensor(['string'])
def test_data(self): tensor = Tensor([2, 2]) tensor.set([[1, 2], [3, 4]]) self.assertEqual(tensor.data, [[1.0, 2.0], [3.0, 4.0]])
def __init__(self, *data, pointer=None): if pointer: Tensor.__init__(self, pointer=pointer) else: Tensor.__init__(self, [len(data)]) self.set(data)
def sub(cls, v1, v2): if Tensor.match_shapes(v1, v2): pointer = LIB().v_sub(v1.pointer, v2.pointer) return cls(pointer=pointer) else: raise ValueError("Vectors must have matching lengths")
def dot(cls, v1, v2): if Tensor.match_shapes(v1, v2): return LIB().v_dot(v1.pointer, v2.pointer) else: raise ValueError("Vectors must have matching lengths")