-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapgwrapper.py
54 lines (37 loc) · 1.24 KB
/
apgwrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import numpy as np
class IWrapper:
def dot(self, other):
raise NotImplementedError("Implement in subclass")
def __add__(self, other):
raise NotImplementedError("Implement in subclass")
def __sub__(self, other):
raise NotImplementedError("Implement in subclass")
def __mul__(self, scalar):
raise NotImplementedError("Implement in subclass")
def copy(self):
raise NotImplementedError("Implement in subclass")
def norm(self):
raise NotImplementedError("Implement in subclass")
@property
def data(self):
return self
__rmul__ = __mul__
class NumpyWrapper(IWrapper):
def __init__(self, nparray):
self._nparray = nparray
def dot(self, other):
return np.inner(self.data, other.data)
def __add__(self, other):
return NumpyWrapper(self.data + other.data)
def __sub__(self, other):
return NumpyWrapper(self.data - other.data)
def __mul__(self, scalar):
return NumpyWrapper(self.data * scalar)
def copy(self):
return NumpyWrapper(np.copy(self.data))
def norm(self):
return np.linalg.norm(self.data)
@property
def data(self):
return self._nparray
__rmul__ = __mul__