Skip to content

Commit

Permalink
added polar_str method
Browse files Browse the repository at this point in the history
  • Loading branch information
akissinger committed Jun 12, 2024
1 parent e70245c commit 2fe8d18
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pyzx/graph/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
return

0 comments on commit 2fe8d18

Please sign in to comment.