From 2fe8d18ede84515fdfc928d27610cd63d743242e Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Wed, 12 Jun 2024 22:04:33 +0100 Subject: [PATCH] added polar_str method --- pyzx/graph/scalar.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pyzx/graph/scalar.py b/pyzx/graph/scalar.py index 28a8f4eb..7d9e2a9c 100644 --- a/pyzx/graph/scalar.py +++ b/pyzx/graph/scalar.py @@ -79,6 +79,14 @@ def __str__(self) -> str: def __complex__(self) -> complex: return self.to_number() + def polar_str(self) -> str: + """Returns a human-readable string of the scalar in polar format""" + r,th = cmath.polar(self.to_number()) + s = "{:.3}".format(r) + if th != 0: + s += " * exp(i pi * {:.3})".format(th/math.pi) + return s + def copy(self, conjugate: bool = False) -> 'Scalar': """Create a copy of the Scalar. If ``conjugate`` is set, the copy will be complex conjugated. @@ -211,6 +219,8 @@ def add_node(self, node: FractionLike) -> None: self.phasenodes.append(node) if node == 1: self.is_zero = True def add_float(self,f: complex) -> None: + if f == 0.0: + self.is_zero = True self.floatfactor *= f def mult_with_scalar(self, other: 'Scalar') -> None: @@ -260,4 +270,4 @@ def add_spider_pair(self, p1: FractionLike,p2: FractionLike) -> None: # Generic case self.add_power(-1) self.add_float(1+cexp(p1)+cexp(p2) - cexp(p1+p2)) - return \ No newline at end of file + return