From ca928cb5df3416481ef5d7991a4391e3615279e7 Mon Sep 17 00:00:00 2001 From: kaihsin Date: Mon, 15 Apr 2024 15:26:41 -0400 Subject: [PATCH] add pretty print for Bond --- src/cytnx_torch/bond.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/cytnx_torch/bond.py b/src/cytnx_torch/bond.py index 630e9ab..bf733ab 100644 --- a/src/cytnx_torch/bond.py +++ b/src/cytnx_torch/bond.py @@ -2,6 +2,7 @@ from dataclasses import dataclass, field from beartype.typing import List from enum import Enum +from abc import abstractmethod from .symmetry import Symmetry @@ -30,6 +31,11 @@ class AbstractBond: dim: int bond_type: BondType = field(default=BondType.NONE) + @property + @abstractmethod + def nsym(self) -> int: + raise NotImplementedError("not implement for abstract type trait.") + @dataclass class Bond(AbstractBond): @@ -38,6 +44,15 @@ def __post_init__(self): if self.dim < 0: raise ValueError(f"dim should be non-negative. got {self.dim}") + def __str__(self): + out = f"Dim = {self.dim} |\n" + out += f"{self.bond_type} :\n" + return out + + @property + def nsym(self) -> int: + return 0 + @dataclass(init=False) class SymBond(AbstractBond): @@ -52,6 +67,10 @@ class SymBond(AbstractBond): ) _syms: List[Symmetry] = field(default_factory=list) + @property + def nsym(self) -> int: + return len(self._syms) + def _check_qnums(self) -> None: for i, s in enumerate(self._syms): if not s.check_qnums(self._qnums[:, i]): @@ -83,3 +102,16 @@ def __init__(self, bond_type: BondType, qnums: List[Qs], syms: List[Symmetry]): # checking qnums are consistent! self._check_qnums() + + def __str__(self): + + out = f"Dim = {self.dim} |\n" + out += f"{self.bond_type} :\n" + + for n in range(self.nsym): + out += f" {str(self._syms[n])}:: " + for idim in range(len(self._qnums)): + out += " %+d" % (self._qnums[idim, n]) + out += "\n " + + return out