Skip to content

Commit c904110

Browse files
generatedunixname975288348396129meta-codesync[bot]
authored andcommitted
fbcode/erc/fb_packages/stat_valid_infer/svinfer_open_source/svinfer/processor/matrix.py
Reviewed By: calmarj Differential Revision: D105807025 fbshipit-source-id: 4124bc8dd04551c818b2862c50c35107d47e34f6
1 parent 7674b6f commit c904110

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

svinfer/processor/matrix.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18+
from __future__ import annotations
19+
1820
import abc
1921

2022
import numpy as np
2123
import sqlalchemy
2224

2325

2426
class AbstractMatrix(abc.ABC):
25-
def __init__(self):
27+
def __init__(self) -> None:
2628
self.value = None
2729
self.ncol = None
2830
self.dim = None
@@ -104,7 +106,7 @@ def log(self):
104106

105107

106108
class NumpyMatrix(AbstractMatrix):
107-
def __init__(self, x, dim=None):
109+
def __init__(self, x, dim=None) -> None:
108110
assert isinstance(x, np.ndarray)
109111
super().__init__()
110112
if len(x.shape) == 1:
@@ -117,10 +119,10 @@ def __init__(self, x, dim=None):
117119
assert np.prod(dim) == self.ncol
118120
self.dim = dim
119121

120-
def __pos__(self):
122+
def __pos__(self) -> NumpyMatrix:
121123
return NumpyMatrix(self.value)
122124

123-
def __neg__(self):
125+
def __neg__(self) -> NumpyMatrix:
124126
# pyrefly: ignore [unsupported-operation]
125127
return NumpyMatrix(-self.value)
126128

@@ -172,19 +174,19 @@ def __rtruediv__(self, other):
172174
return NumpyMatrix(other.value / self.value)
173175
return NotImplemented
174176

175-
def dot(self, b):
177+
def dot(self, b: np.ndarray) -> NumpyMatrix:
176178
assert isinstance(b, np.ndarray)
177179
assert len(b.shape) == 1
178180
assert self.ncol == b.size
179181
return NumpyMatrix(np.dot(self.value, b))
180182

181-
def outer(self, b):
183+
def outer(self, b: np.ndarray) -> NumpyMatrix:
182184
assert isinstance(b, np.ndarray)
183185
assert len(b.shape) == 1
184186
assert self.ncol == 1
185187
return NumpyMatrix(np.outer(self.value, b))
186188

187-
def cross(self, other):
189+
def cross(self, other: NumpyMatrix) -> NumpyMatrix:
188190
assert isinstance(other, NumpyMatrix)
189191
result = []
190192
# pyrefly: ignore [no-matching-overload]
@@ -194,15 +196,15 @@ def cross(self, other):
194196
result = np.concatenate(result, axis=1)
195197
return NumpyMatrix(result, dim=(self.ncol, other.ncol))
196198

197-
def exp(self):
199+
def exp(self) -> NumpyMatrix:
198200
return NumpyMatrix(np.exp(self.value))
199201

200-
def log(self):
202+
def log(self) -> NumpyMatrix:
201203
return NumpyMatrix(np.log(self.value))
202204

203205

204206
class SqlMatrix(AbstractMatrix):
205-
def __init__(self, x, dim=None):
207+
def __init__(self, x, dim=None) -> None:
206208
assert isinstance(x, list)
207209
super().__init__()
208210
self.value = x.copy()
@@ -213,10 +215,10 @@ def __init__(self, x, dim=None):
213215
assert np.prod(dim) == self.ncol
214216
self.dim = dim
215217

216-
def __pos__(self):
218+
def __pos__(self) -> SqlMatrix:
217219
return SqlMatrix(self.value)
218220

219-
def __neg__(self):
221+
def __neg__(self) -> SqlMatrix:
220222
# pyrefly: ignore [no-matching-overload, unsupported-operation]
221223
return SqlMatrix([-self.value[j] for j in range(self.ncol)])
222224

@@ -280,7 +282,7 @@ def __rtruediv__(self, other):
280282
)
281283
return NotImplemented
282284

283-
def dot(self, b):
285+
def dot(self, b: np.ndarray) -> SqlMatrix:
284286
assert isinstance(b, np.ndarray)
285287
assert len(b.shape) == 1
286288
assert self.ncol == b.size
@@ -291,7 +293,7 @@ def dot(self, b):
291293
result += self.value[j] * b[j]
292294
return SqlMatrix([result])
293295

294-
def outer(self, b):
296+
def outer(self, b: np.ndarray) -> SqlMatrix:
295297
assert isinstance(b, np.ndarray)
296298
assert len(b.shape) == 1
297299
assert self.ncol == 1
@@ -301,7 +303,7 @@ def outer(self, b):
301303
result.append(self.value[0] * b[j])
302304
return SqlMatrix(result)
303305

304-
def cross(self, other):
306+
def cross(self, other: SqlMatrix) -> SqlMatrix:
305307
assert isinstance(other, SqlMatrix)
306308
result = []
307309
# pyrefly: ignore [no-matching-overload]
@@ -312,15 +314,15 @@ def cross(self, other):
312314
result.append(self.value[i] * other.value[j])
313315
return SqlMatrix(result, dim=(self.ncol, other.ncol))
314316

315-
def exp(self):
317+
def exp(self) -> SqlMatrix:
316318
result = []
317319
# pyrefly: ignore [no-matching-overload]
318320
for j in range(self.ncol):
319321
# pyrefly: ignore [unsupported-operation]
320322
result.append(sqlalchemy.func.exp(self.value[j]))
321323
return SqlMatrix(result)
322324

323-
def log(self):
325+
def log(self) -> SqlMatrix:
324326
result = []
325327
# pyrefly: ignore [no-matching-overload]
326328
for j in range(self.ncol):

0 commit comments

Comments
 (0)