Created
September 25, 2022 14:18
-
-
Save asukaminato0721/1198c3025fa4e341ef8b6c1c0f2edc7a to your computer and use it in GitHub Desktop.
a simple mat class which mimic the behavior of Eigen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from typing import Any, Optional, Sequence | |
@dataclass | |
class Mat: | |
row: int | |
col: int | |
data: Optional[Sequence[Sequence[Any]]] = None | |
def __lshift__(self, other: Sequence): | |
if not isinstance(other, Sequence): | |
raise TypeError("other must be a sequence") | |
if len(other) != self.col * self.row: | |
raise ValueError("Number of elements does not match") | |
self.data = [ | |
other[i : i + self.col] | |
for i in range(0, self.col * self.row, self.col) | |
] | |
def __str__(self) -> str: | |
return str(self.data) | |
m = Mat(4, 5) | |
# fmt: off | |
m << ( | |
1, 2, 3, 4, 5, # | |
6, 7, 8, 9, 10, # | |
11, 12, 13, 14, 15,# | |
16, 17, 18, 19, 20) | |
# fmt: on | |
n = Mat(4, 5) | |
# fmt: off | |
n << [ | |
1, 2, 3, 4, 5, # | |
6, 7, 8, 9, 10, # | |
11, 12, 13, 14, 15,# | |
16, 17, 18, 19, 20] | |
# fmt: on | |
print(m, n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment