From e456af142e5e51ab80e5cf8f49a1f09a32368a5d Mon Sep 17 00:00:00 2001 From: Nikolaj Bjorner Date: Fri, 27 Mar 2015 02:42:08 -0700 Subject: [PATCH] fix complex.py example with power prompted by suggestion of smilliken Signed-off-by: Nikolaj Bjorner --- examples/python/complex/complex.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/python/complex/complex.py b/examples/python/complex/complex.py index a2de4a6cc..5d6283745 100644 --- a/examples/python/complex/complex.py +++ b/examples/python/complex/complex.py @@ -46,6 +46,15 @@ class ComplexExpr: other = _to_complex(other) return ComplexExpr(other.r*self.r - other.i*self.i, other.i*self.r + other.r*self.i) + def __pow__(self, k): + if k == 0: + return ComplexExpr(1, 0) + if k == 1: + return self + if k < 0: + return (self ** (-k)).inv() + return reduce(lambda x, y: x * y, [self for _ in xrange(k)], ComplexExpr(1, 0)) + def inv(self): den = self.r*self.r + self.i*self.i return ComplexExpr(self.r/den, -self.i/den) @@ -104,4 +113,5 @@ print(s.model()) s.add(x.i != 1) print(s.check()) # print(s.model()) -print (3 + I)^2/(5 - I) +print ((3 + I) ** 2)/(5 - I) +print ((3 + I) ** -3)/(5 - I)