Spaces:
Sleeping
Sleeping
File size: 22,526 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
import contextlib
import itertools
import re
import typing
from enum import Enum
from typing import Callable
import sympy
from sympy import Add, Implies, sqrt
from sympy.core import Mul, Pow
from sympy.core import (S, pi, symbols, Function, Rational, Integer,
Symbol, Eq, Ne, Le, Lt, Gt, Ge)
from sympy.functions import Piecewise, exp, sin, cos
from sympy.assumptions.ask import Q
from sympy.printing.smtlib import smtlib_code
from sympy.testing.pytest import raises, Failed
x, y, z = symbols('x,y,z')
class _W(Enum):
DEFAULTING_TO_FLOAT = re.compile("Could not infer type of `.+`. Defaulting to float.", re.I)
WILL_NOT_DECLARE = re.compile("Non-Symbol/Function `.+` will not be declared.", re.I)
WILL_NOT_ASSERT = re.compile("Non-Boolean expression `.+` will not be asserted. Converting to SMTLib verbatim.", re.I)
@contextlib.contextmanager
def _check_warns(expected: typing.Iterable[_W]):
warns: typing.List[str] = []
log_warn = warns.append
yield log_warn
errors = []
for i, (w, e) in enumerate(itertools.zip_longest(warns, expected)):
if not e:
errors += [f"[{i}] Received unexpected warning `{w}`."]
elif not w:
errors += [f"[{i}] Did not receive expected warning `{e.name}`."]
elif not e.value.match(w):
errors += [f"[{i}] Warning `{w}` does not match expected {e.name}."]
if errors: raise Failed('\n'.join(errors))
def test_Integer():
with _check_warns([_W.WILL_NOT_ASSERT] * 2) as w:
assert smtlib_code(Integer(67), log_warn=w) == "67"
assert smtlib_code(Integer(-1), log_warn=w) == "-1"
with _check_warns([]) as w:
assert smtlib_code(Integer(67)) == "67"
assert smtlib_code(Integer(-1)) == "-1"
def test_Rational():
with _check_warns([_W.WILL_NOT_ASSERT] * 4) as w:
assert smtlib_code(Rational(3, 7), log_warn=w) == "(/ 3 7)"
assert smtlib_code(Rational(18, 9), log_warn=w) == "2"
assert smtlib_code(Rational(3, -7), log_warn=w) == "(/ -3 7)"
assert smtlib_code(Rational(-3, -7), log_warn=w) == "(/ 3 7)"
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT] * 2) as w:
assert smtlib_code(x + Rational(3, 7), auto_declare=False, log_warn=w) == "(+ (/ 3 7) x)"
assert smtlib_code(Rational(3, 7) * x, log_warn=w) == "(declare-const x Real)\n" \
"(* (/ 3 7) x)"
def test_Relational():
with _check_warns([_W.DEFAULTING_TO_FLOAT] * 12) as w:
assert smtlib_code(Eq(x, y), auto_declare=False, log_warn=w) == "(assert (= x y))"
assert smtlib_code(Ne(x, y), auto_declare=False, log_warn=w) == "(assert (not (= x y)))"
assert smtlib_code(Le(x, y), auto_declare=False, log_warn=w) == "(assert (<= x y))"
assert smtlib_code(Lt(x, y), auto_declare=False, log_warn=w) == "(assert (< x y))"
assert smtlib_code(Gt(x, y), auto_declare=False, log_warn=w) == "(assert (> x y))"
assert smtlib_code(Ge(x, y), auto_declare=False, log_warn=w) == "(assert (>= x y))"
def test_AppliedBinaryRelation():
with _check_warns([_W.DEFAULTING_TO_FLOAT] * 12) as w:
assert smtlib_code(Q.eq(x, y), auto_declare=False, log_warn=w) == "(assert (= x y))"
assert smtlib_code(Q.ne(x, y), auto_declare=False, log_warn=w) == "(assert (not (= x y)))"
assert smtlib_code(Q.lt(x, y), auto_declare=False, log_warn=w) == "(assert (< x y))"
assert smtlib_code(Q.le(x, y), auto_declare=False, log_warn=w) == "(assert (<= x y))"
assert smtlib_code(Q.gt(x, y), auto_declare=False, log_warn=w) == "(assert (> x y))"
assert smtlib_code(Q.ge(x, y), auto_declare=False, log_warn=w) == "(assert (>= x y))"
raises(ValueError, lambda: smtlib_code(Q.complex(x), log_warn=w))
def test_AppliedPredicate():
with _check_warns([_W.DEFAULTING_TO_FLOAT] * 6) as w:
assert smtlib_code(Q.positive(x), auto_declare=False, log_warn=w) == "(assert (> x 0))"
assert smtlib_code(Q.negative(x), auto_declare=False, log_warn=w) == "(assert (< x 0))"
assert smtlib_code(Q.zero(x), auto_declare=False, log_warn=w) == "(assert (= x 0))"
assert smtlib_code(Q.nonpositive(x), auto_declare=False, log_warn=w) == "(assert (<= x 0))"
assert smtlib_code(Q.nonnegative(x), auto_declare=False, log_warn=w) == "(assert (>= x 0))"
assert smtlib_code(Q.nonzero(x), auto_declare=False, log_warn=w) == "(assert (not (= x 0)))"
def test_Function():
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(sin(x) ** cos(x), auto_declare=False, log_warn=w) == "(pow (sin x) (cos x))"
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
abs(x),
symbol_table={x: int, y: bool},
known_types={int: "INTEGER_TYPE"},
known_functions={sympy.Abs: "ABSOLUTE_VALUE_OF"},
log_warn=w
) == "(declare-const x INTEGER_TYPE)\n" \
"(ABSOLUTE_VALUE_OF x)"
my_fun1 = Function('f1')
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
my_fun1(x),
symbol_table={my_fun1: Callable[[bool], float]},
log_warn=w
) == "(declare-const x Bool)\n" \
"(declare-fun f1 (Bool) Real)\n" \
"(f1 x)"
with _check_warns([]) as w:
assert smtlib_code(
my_fun1(x),
symbol_table={my_fun1: Callable[[bool], bool]},
log_warn=w
) == "(declare-const x Bool)\n" \
"(declare-fun f1 (Bool) Bool)\n" \
"(assert (f1 x))"
assert smtlib_code(
Eq(my_fun1(x, z), y),
symbol_table={my_fun1: Callable[[int, bool], bool]},
log_warn=w
) == "(declare-const x Int)\n" \
"(declare-const y Bool)\n" \
"(declare-const z Bool)\n" \
"(declare-fun f1 (Int Bool) Bool)\n" \
"(assert (= (f1 x z) y))"
assert smtlib_code(
Eq(my_fun1(x, z), y),
symbol_table={my_fun1: Callable[[int, bool], bool]},
known_functions={my_fun1: "MY_KNOWN_FUN", Eq: '=='},
log_warn=w
) == "(declare-const x Int)\n" \
"(declare-const y Bool)\n" \
"(declare-const z Bool)\n" \
"(assert (== (MY_KNOWN_FUN x z) y))"
with _check_warns([_W.DEFAULTING_TO_FLOAT] * 3) as w:
assert smtlib_code(
Eq(my_fun1(x, z), y),
known_functions={my_fun1: "MY_KNOWN_FUN", Eq: '=='},
log_warn=w
) == "(declare-const x Real)\n" \
"(declare-const y Real)\n" \
"(declare-const z Real)\n" \
"(assert (== (MY_KNOWN_FUN x z) y))"
def test_Pow():
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(x ** 3, auto_declare=False, log_warn=w) == "(pow x 3)"
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(x ** (y ** 3), auto_declare=False, log_warn=w) == "(pow x (pow y 3))"
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(x ** Rational(2, 3), auto_declare=False, log_warn=w) == '(pow x (/ 2 3))'
a = Symbol('a', integer=True)
b = Symbol('b', real=True)
c = Symbol('c')
def g(x): return 2 * x
# if x=1, y=2, then expr=2.333...
expr = 1 / (g(a) * 3.5) ** (a - b ** a) / (a ** 2 + b)
with _check_warns([]) as w:
assert smtlib_code(
[
Eq(a < 2, c),
Eq(b > a, c),
c & True,
Eq(expr, 2 + Rational(1, 3))
],
log_warn=w
) == '(declare-const a Int)\n' \
'(declare-const b Real)\n' \
'(declare-const c Bool)\n' \
'(assert (= (< a 2) c))\n' \
'(assert (= (> b a) c))\n' \
'(assert c)\n' \
'(assert (= ' \
'(* (pow (* 7.0 a) (+ (pow b a) (* -1 a))) (pow (+ b (pow a 2)) -1)) ' \
'(/ 7 3)' \
'))'
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Mul(-2, c, Pow(Mul(b, b, evaluate=False), -1, evaluate=False), evaluate=False),
log_warn=w
) == '(declare-const b Real)\n' \
'(declare-const c Real)\n' \
'(* -2 c (pow (* b b) -1))'
def test_basic_ops():
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(x * y, auto_declare=False, log_warn=w) == "(* x y)"
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(x + y, auto_declare=False, log_warn=w) == "(+ x y)"
# with _check_warns([_SmtlibWarnings.DEFAULTING_TO_FLOAT, _SmtlibWarnings.DEFAULTING_TO_FLOAT, _SmtlibWarnings.WILL_NOT_ASSERT]) as w:
# todo: implement re-write, currently does '(+ x (* -1 y))' instead
# assert smtlib_code(x - y, auto_declare=False, log_warn=w) == "(- x y)"
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(-x, auto_declare=False, log_warn=w) == "(* -1 x)"
def test_quantifier_extensions():
from sympy.logic.boolalg import Boolean
from sympy import Interval, Tuple, sympify
# start For-all quantifier class example
class ForAll(Boolean):
def _smtlib(self, printer):
bound_symbol_declarations = [
printer._s_expr(sym.name, [
printer._known_types[printer.symbol_table[sym]],
Interval(start, end)
]) for sym, start, end in self.limits
]
return printer._s_expr('forall', [
printer._s_expr('', bound_symbol_declarations),
self.function
])
@property
def bound_symbols(self):
return {s for s, _, _ in self.limits}
@property
def free_symbols(self):
bound_symbol_names = {s.name for s in self.bound_symbols}
return {
s for s in self.function.free_symbols
if s.name not in bound_symbol_names
}
def __new__(cls, *args):
limits = [sympify(a) for a in args if isinstance(a, (tuple, Tuple))]
function = [sympify(a) for a in args if isinstance(a, Boolean)]
assert len(limits) + len(function) == len(args)
assert len(function) == 1
function = function[0]
if isinstance(function, ForAll): return ForAll.__new__(
ForAll, *(limits + function.limits), function.function
)
inst = Boolean.__new__(cls)
inst._args = tuple(limits + [function])
inst.limits = limits
inst.function = function
return inst
# end For-All Quantifier class example
f = Function('f')
with _check_warns([_W.DEFAULTING_TO_FLOAT]) as w:
assert smtlib_code(
ForAll((x, -42, +21), Eq(f(x), f(x))),
symbol_table={f: Callable[[float], float]},
log_warn=w
) == '(assert (forall ( (x Real [-42, 21])) true))'
with _check_warns([_W.DEFAULTING_TO_FLOAT] * 2) as w:
assert smtlib_code(
ForAll(
(x, -42, +21), (y, -100, 3),
Implies(Eq(x, y), Eq(f(x), f(y)))
),
symbol_table={f: Callable[[float], float]},
log_warn=w
) == '(declare-fun f (Real) Real)\n' \
'(assert (' \
'forall ( (x Real [-42, 21]) (y Real [-100, 3])) ' \
'(=> (= x y) (= (f x) (f y)))' \
'))'
a = Symbol('a', integer=True)
b = Symbol('b', real=True)
c = Symbol('c')
with _check_warns([]) as w:
assert smtlib_code(
ForAll(
(a, 2, 100), ForAll(
(b, 2, 100),
Implies(a < b, sqrt(a) < b) | c
)),
log_warn=w
) == '(declare-const c Bool)\n' \
'(assert (forall ( (a Int [2, 100]) (b Real [2, 100])) ' \
'(or c (=> (< a b) (< (pow a (/ 1 2)) b)))' \
'))'
def test_mix_number_mult_symbols():
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
1 / pi,
known_constants={pi: "MY_PI"},
log_warn=w
) == '(pow MY_PI -1)'
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
[
Eq(pi, 3.14, evaluate=False),
1 / pi,
],
known_constants={pi: "MY_PI"},
log_warn=w
) == '(assert (= MY_PI 3.14))\n' \
'(pow MY_PI -1)'
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Add(S.Zero, S.One, S.NegativeOne, S.Half,
S.Exp1, S.Pi, S.GoldenRatio, evaluate=False),
known_constants={
S.Pi: 'p', S.GoldenRatio: 'g',
S.Exp1: 'e'
},
known_functions={
Add: 'plus',
exp: 'exp'
},
precision=3,
log_warn=w
) == '(plus 0 1 -1 (/ 1 2) (exp 1) p g)'
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Add(S.Zero, S.One, S.NegativeOne, S.Half,
S.Exp1, S.Pi, S.GoldenRatio, evaluate=False),
known_constants={
S.Pi: 'p'
},
known_functions={
Add: 'plus',
exp: 'exp'
},
precision=3,
log_warn=w
) == '(plus 0 1 -1 (/ 1 2) (exp 1) p 1.62)'
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Add(S.Zero, S.One, S.NegativeOne, S.Half,
S.Exp1, S.Pi, S.GoldenRatio, evaluate=False),
known_functions={Add: 'plus'},
precision=3,
log_warn=w
) == '(plus 0 1 -1 (/ 1 2) 2.72 3.14 1.62)'
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Add(S.Zero, S.One, S.NegativeOne, S.Half,
S.Exp1, S.Pi, S.GoldenRatio, evaluate=False),
known_constants={S.Exp1: 'e'},
known_functions={Add: 'plus'},
precision=3,
log_warn=w
) == '(plus 0 1 -1 (/ 1 2) e 3.14 1.62)'
def test_boolean():
with _check_warns([]) as w:
assert smtlib_code(x & y, log_warn=w) == '(declare-const x Bool)\n' \
'(declare-const y Bool)\n' \
'(assert (and x y))'
assert smtlib_code(x | y, log_warn=w) == '(declare-const x Bool)\n' \
'(declare-const y Bool)\n' \
'(assert (or x y))'
assert smtlib_code(~x, log_warn=w) == '(declare-const x Bool)\n' \
'(assert (not x))'
assert smtlib_code(x & y & z, log_warn=w) == '(declare-const x Bool)\n' \
'(declare-const y Bool)\n' \
'(declare-const z Bool)\n' \
'(assert (and x y z))'
with _check_warns([_W.DEFAULTING_TO_FLOAT]) as w:
assert smtlib_code((x & ~y) | (z > 3), log_warn=w) == '(declare-const x Bool)\n' \
'(declare-const y Bool)\n' \
'(declare-const z Real)\n' \
'(assert (or (> z 3) (and x (not y))))'
f = Function('f')
g = Function('g')
h = Function('h')
with _check_warns([_W.DEFAULTING_TO_FLOAT]) as w:
assert smtlib_code(
[Gt(f(x), y),
Lt(y, g(z))],
symbol_table={
f: Callable[[bool], int], g: Callable[[bool], int],
}, log_warn=w
) == '(declare-const x Bool)\n' \
'(declare-const y Real)\n' \
'(declare-const z Bool)\n' \
'(declare-fun f (Bool) Int)\n' \
'(declare-fun g (Bool) Int)\n' \
'(assert (> (f x) y))\n' \
'(assert (< y (g z)))'
with _check_warns([]) as w:
assert smtlib_code(
[Eq(f(x), y),
Lt(y, g(z))],
symbol_table={
f: Callable[[bool], int], g: Callable[[bool], int],
}, log_warn=w
) == '(declare-const x Bool)\n' \
'(declare-const y Int)\n' \
'(declare-const z Bool)\n' \
'(declare-fun f (Bool) Int)\n' \
'(declare-fun g (Bool) Int)\n' \
'(assert (= (f x) y))\n' \
'(assert (< y (g z)))'
with _check_warns([]) as w:
assert smtlib_code(
[Eq(f(x), y),
Eq(g(f(x)), z),
Eq(h(g(f(x))), x)],
symbol_table={
f: Callable[[float], int],
g: Callable[[int], bool],
h: Callable[[bool], float]
},
log_warn=w
) == '(declare-const x Real)\n' \
'(declare-const y Int)\n' \
'(declare-const z Bool)\n' \
'(declare-fun f (Real) Int)\n' \
'(declare-fun g (Int) Bool)\n' \
'(declare-fun h (Bool) Real)\n' \
'(assert (= (f x) y))\n' \
'(assert (= (g (f x)) z))\n' \
'(assert (= (h (g (f x))) x))'
# todo: make smtlib_code support arrays
# def test_containers():
# assert julia_code([1, 2, 3, [4, 5, [6, 7]], 8, [9, 10], 11]) == \
# "Any[1, 2, 3, Any[4, 5, Any[6, 7]], 8, Any[9, 10], 11]"
# assert julia_code((1, 2, (3, 4))) == "(1, 2, (3, 4))"
# assert julia_code([1]) == "Any[1]"
# assert julia_code((1,)) == "(1,)"
# assert julia_code(Tuple(*[1, 2, 3])) == "(1, 2, 3)"
# assert julia_code((1, x * y, (3, x ** 2))) == "(1, x .* y, (3, x .^ 2))"
# # scalar, matrix, empty matrix and empty list
# assert julia_code((1, eye(3), Matrix(0, 0, []), [])) == "(1, [1 0 0;\n0 1 0;\n0 0 1], zeros(0, 0), Any[])"
def test_smtlib_piecewise():
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Piecewise((x, x < 1),
(x ** 2, True)),
auto_declare=False,
log_warn=w
) == '(ite (< x 1) x (pow x 2))'
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(
Piecewise((x ** 2, x < 1),
(x ** 3, x < 2),
(x ** 4, x < 3),
(x ** 5, True)),
auto_declare=False,
log_warn=w
) == '(ite (< x 1) (pow x 2) ' \
'(ite (< x 2) (pow x 3) ' \
'(ite (< x 3) (pow x 4) ' \
'(pow x 5))))'
# Check that Piecewise without a True (default) condition error
expr = Piecewise((x, x < 1), (x ** 2, x > 1), (sin(x), x > 0))
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
raises(AssertionError, lambda: smtlib_code(expr, log_warn=w))
def test_smtlib_piecewise_times_const():
pw = Piecewise((x, x < 1), (x ** 2, True))
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(2 * pw, log_warn=w) == '(declare-const x Real)\n(* 2 (ite (< x 1) x (pow x 2)))'
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(pw / x, log_warn=w) == '(declare-const x Real)\n(* (pow x -1) (ite (< x 1) x (pow x 2)))'
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(pw / (x * y), log_warn=w) == '(declare-const x Real)\n(declare-const y Real)\n(* (pow x -1) (pow y -1) (ite (< x 1) x (pow x 2)))'
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
assert smtlib_code(pw / 3, log_warn=w) == '(declare-const x Real)\n(* (/ 1 3) (ite (< x 1) x (pow x 2)))'
# todo: make smtlib_code support arrays / matrices ?
# def test_smtlib_matrix_assign_to():
# A = Matrix([[1, 2, 3]])
# assert smtlib_code(A, assign_to='a') == "a = [1 2 3]"
# A = Matrix([[1, 2], [3, 4]])
# assert smtlib_code(A, assign_to='A') == "A = [1 2;\n3 4]"
# def test_julia_matrix_1x1():
# A = Matrix([[3]])
# B = MatrixSymbol('B', 1, 1)
# C = MatrixSymbol('C', 1, 2)
# assert julia_code(A, assign_to=B) == "B = [3]"
# raises(ValueError, lambda: julia_code(A, assign_to=C))
# def test_julia_matrix_elements():
# A = Matrix([[x, 2, x * y]])
# assert julia_code(A[0, 0] ** 2 + A[0, 1] + A[0, 2]) == "x .^ 2 + x .* y + 2"
# A = MatrixSymbol('AA', 1, 3)
# assert julia_code(A) == "AA"
# assert julia_code(A[0, 0] ** 2 + sin(A[0, 1]) + A[0, 2]) == \
# "sin(AA[1,2]) + AA[1,1] .^ 2 + AA[1,3]"
# assert julia_code(sum(A)) == "AA[1,1] + AA[1,2] + AA[1,3]"
def test_smtlib_boolean():
with _check_warns([]) as w:
assert smtlib_code(True, auto_assert=False, log_warn=w) == 'true'
assert smtlib_code(True, log_warn=w) == '(assert true)'
assert smtlib_code(S.true, log_warn=w) == '(assert true)'
assert smtlib_code(S.false, log_warn=w) == '(assert false)'
assert smtlib_code(False, log_warn=w) == '(assert false)'
assert smtlib_code(False, auto_assert=False, log_warn=w) == 'false'
def test_not_supported():
f = Function('f')
with _check_warns([_W.DEFAULTING_TO_FLOAT, _W.WILL_NOT_ASSERT]) as w:
raises(KeyError, lambda: smtlib_code(f(x).diff(x), symbol_table={f: Callable[[float], float]}, log_warn=w))
with _check_warns([_W.WILL_NOT_ASSERT]) as w:
raises(KeyError, lambda: smtlib_code(S.ComplexInfinity, log_warn=w))
def test_Float():
assert smtlib_code(0.0) == "0.0"
assert smtlib_code(0.000000000000000003) == '(* 3.0 (pow 10 -18))'
assert smtlib_code(5.3) == "5.3"
|