| 1 |
############################################ |
| 2 |
# Copyright (c) 2012 Microsoft Corporation |
| 3 |
# |
| 4 |
# Complex numbers in Z3 |
| 5 |
# See http://research.microsoft.com/en-us/um/people/leonardo/blog/2013/01/26/complex.html |
| 6 |
# |
| 7 |
# Author: Leonardo de Moura (leonardo) |
| 8 |
############################################ |
| 9 |
from __future__ import print_function |
| 10 |
import sys |
| 11 |
if sys.version_info.major >= 3: |
| 12 |
from functools import reduce |
| 13 |
from z3 import * |
| 14 |
|
| 15 |
def _to_complex(a): |
| 16 |
if isinstance(a, ComplexExpr): |
| 17 |
return a |
| 18 |
else: |
| 19 |
return ComplexExpr(a, RealVal(0)) |
| 20 |
|
| 21 |
def _is_zero(a): |
| 22 |
return (isinstance(a, int) and a == 0) or (is_rational_value(a) and a.numerator_as_long() == 0) |
| 23 |
|
| 24 |
class ComplexExpr: |
| 25 |
def __init__(self, r, i): |
| 26 |
self.r = r |
| 27 |
self.i = i |
| 28 |
|
| 29 |
def __add__(self, other): |
| 30 |
other = _to_complex(other) |
| 31 |
return ComplexExpr(self.r + other.r, self.i + other.i) |
| 32 |
|
| 33 |
def __radd__(self, other): |
| 34 |
other = _to_complex(other) |
| 35 |
return ComplexExpr(other.r + self.r, other.i + self.i) |
| 36 |
|
| 37 |
def __sub__(self, other): |
| 38 |
other = _to_complex(other) |
| 39 |
return ComplexExpr(self.r - other.r, self.i - other.i) |
| 40 |
|
| 41 |
def __rsub__(self, other): |
| 42 |
other = _to_complex(other) |
| 43 |
return ComplexExpr(other.r - self.r, other.i - self.i) |
| 44 |
|
| 45 |
def __mul__(self, other): |
| 46 |
other = _to_complex(other) |
| 47 |
return ComplexExpr(self.r*other.r - self.i*other.i, self.r*other.i + self.i*other.r) |
| 48 |
|
| 49 |
def __rmul__(self, other): |
| 50 |
other = _to_complex(other) |
| 51 |
return ComplexExpr(other.r*self.r - other.i*self.i, other.i*self.r + other.r*self.i) |
| 52 |
|
| 53 |
def __pow__(self, k): |
| 54 |
if k == 0: |
| 55 |
return ComplexExpr(1, 0) |
| 56 |
if k == 1: |
| 57 |
return self |
| 58 |
if k < 0: |
| 59 |
return (self ** (-k)).inv() |
| 60 |
return reduce(lambda x, y: x * y, [self for _ in range(k)], ComplexExpr(1, 0)) |
| 61 |
|
| 62 |
def inv(self): |
| 63 |
den = self.r*self.r + self.i*self.i |
| 64 |
return ComplexExpr(self.r/den, -self.i/den) |
| 65 |
|
| 66 |
def __div__(self, other): |
| 67 |
inv_other = _to_complex(other).inv() |
| 68 |
return self.__mul__(inv_other) |
| 69 |
|
| 70 |
if sys.version_info.major >= 3: |
| 71 |
# In python 3 the meaning of the '/' operator |
| 72 |
# was changed. |
| 73 |
def __truediv__(self, other): |
| 74 |
return self.__div__(other) |
| 75 |
|
| 76 |
def __rdiv__(self, other): |
| 77 |
other = _to_complex(other) |
| 78 |
return self.inv().__mul__(other) |
| 79 |
|
| 80 |
def __eq__(self, other): |
| 81 |
other = _to_complex(other) |
| 82 |
return And(self.r == other.r, self.i == other.i) |
| 83 |
|
| 84 |
def __neq__(self, other): |
| 85 |
return Not(self.__eq__(other)) |
| 86 |
|
| 87 |
def simplify(self): |
| 88 |
return ComplexExpr(simplify(self.r), simplify(self.i)) |
| 89 |
|
| 90 |
def repr_i(self): |
| 91 |
if is_rational_value(self.i): |
| 92 |
return "%s*I" % self.i |
| 93 |
else: |
| 94 |
return "(%s)*I" % str(self.i) |
| 95 |
|
| 96 |
def __repr__(self): |
| 97 |
if _is_zero(self.i): |
| 98 |
return str(self.r) |
| 99 |
elif _is_zero(self.r): |
| 100 |
return self.repr_i() |
| 101 |
else: |
| 102 |
return "%s + %s" % (self.r, self.repr_i()) |
| 103 |
|
| 104 |
def Complex(a): |
| 105 |
return ComplexExpr(Real('%s.r' % a), Real('%s.i' % a)) |
| 106 |
I = ComplexExpr(RealVal(0), RealVal(1)) |
| 107 |
|
| 108 |
def evaluate_cexpr(m, e): |
| 109 |
return ComplexExpr(m[e.r], m[e.i]) |
| 110 |
|
| 111 |
x = Complex("x") |
| 112 |
s = Tactic('qfnra-nlsat').solver() |
| 113 |
s.add(x*x == -2) |
| 114 |
print(s) |
| 115 |
print(s.check()) |
| 116 |
m = s.model() |
| 117 |
print('x = %s' % evaluate_cexpr(m, x)) |
| 118 |
print((evaluate_cexpr(m,x)*evaluate_cexpr(m,x)).simplify()) |
| 119 |
s.add(x.i != -1) |
| 120 |
print(s) |
| 121 |
print(s.check()) |
| 122 |
print(s.model()) |
| 123 |
s.add(x.i != 1) |
| 124 |
print(s.check()) |
| 125 |
# print(s.model()) |
| 126 |
print(((3 + I) ** 2)/(5 - I)) |
| 127 |
print(((3 + I) ** -3)/(5 - I)) |