diff options
author | Nao Pross <np@0hm.ch> | 2024-05-07 15:38:03 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-05-08 10:16:03 +0200 |
commit | 2d0ea0ab92f01999a59473827c371f7e8b747d99 (patch) | |
tree | 96018bb8b74097e8c66c2a1bc6ef397a9720a287 | |
parent | Lay out new structure for SumOfSquares package (diff) | |
download | sumofsquares-2d0ea0ab92f01999a59473827c371f7e8b747d99.tar.gz sumofsquares-2d0ea0ab92f01999a59473827c371f7e8b747d99.zip |
Add __str__ dunder methods to a few classes
-rw-r--r-- | sumofsquares/abc.py | 17 | ||||
-rw-r--r-- | sumofsquares/constraints.py | 18 |
2 files changed, 34 insertions, 1 deletions
diff --git a/sumofsquares/abc.py b/sumofsquares/abc.py index 482ba7f..95eacad 100644 --- a/sumofsquares/abc.py +++ b/sumofsquares/abc.py @@ -42,10 +42,27 @@ class Constraint(ABC): def expression(self) -> Expression: """ Expression under the constraint. """ + def __str__(self): + return f"{self.__class__.__qualname__}({str(self.expression)})" + class Problem(ABC): """ Optimization Problem. """ + # --- dunder methods --- + + def __str__(self): + vs = ", ".join(map(str, self.variables)) + n = len(self.constraints) + s = f"SumOfSquares Problem ({self.__class__.__qualname__}):\n\n " \ + f"find {vs}\n minimize {self.cost}\n " \ + f"subject to ({n} constraint{'s' if n > 1 else ''})\n" + + for c in self.constraints: + s += f"\t* {str(c)}\n" + + return s + # --- Properties --- @property diff --git a/sumofsquares/constraints.py b/sumofsquares/constraints.py index 3634e61..41f2b14 100644 --- a/sumofsquares/constraints.py +++ b/sumofsquares/constraints.py @@ -1,11 +1,15 @@ +from dataclasses import dataclass from dataclassabc import dataclassabc +from typing_extensions import override +from typing import NamedTuple from polymatrix.expression.expression import Expression from .abc import Set, Constraint -class ArchimedeanSet(Set, tuple[Expression]): +@dataclass(frozen=True) +class ArchimedeanSet(Set): r""" A set that is described by the intersection of the positive loci of a finite number of polynomials (under some conditions). Roughly speaking this @@ -26,6 +30,10 @@ class ArchimedeanSet(Set, tuple[Expression]): Archimedean! It is assumed that the given :math:`g_i` make :math:`\mathbf{K}` Archimedean. """ + polynomials: tuple[Expression] + + def __str__(self): + return f"{self.__class__.__qualname__}({', '.join(map(str, self.polynomials))})" @dataclassabc(frozen=True) @@ -48,3 +56,11 @@ class NonNegative(Constraint): """ expression: Expression domain: ArchimedeanSet | None = None + + @override + def __str__(self): + s = f"{self.__class__.__qualname__} {str(self.expression)}" + if self.domain: + s += f"\n\t\t over {self.domain}" + return s + |