Skip to content

Instantly share code, notes, and snippets.

@asukaminato0721
Created September 25, 2022 14:18
Show Gist options
  • Save asukaminato0721/1198c3025fa4e341ef8b6c1c0f2edc7a to your computer and use it in GitHub Desktop.
Save asukaminato0721/1198c3025fa4e341ef8b6c1c0f2edc7a to your computer and use it in GitHub Desktop.
a simple mat class which mimic the behavior of Eigen.
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