uqfoundation / mystic

constrained nonlinear optimization for scientific machine learning, UQ, and AI
http://mystic.rtfd.io
Other
465 stars 50 forks source link

TypeError due to simplify1 producing a complex number #215

Closed mmckerns closed 10 months ago

mmckerns commented 11 months ago

A TypeError is thrown irregularly from examples2/g09.py

$ python g09.py 
Traceback (most recent call last):
  File "/Users/mmckerns/dev/git/uqfoundation/mystic/examples2/g09.py", line 39, in <module>
    cf = generate_constraint(generate_solvers(simplify(equations)))
                                              ^^^^^^^^^^^^^^^^^^^
  File "/Users/mmckerns/lib/python3.12/site-packages/mystic/symbolic.py", line 807, in simplify
    cons = [simple(ci, **kwds) for ci in cons] if type(cons) is tuple else simple(cons, **kwds)
                                                                           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/mmckerns/lib/python3.12/site-packages/mystic/symbolic.py", line 763, in _simplify
    res = _simplify1(eqn, variables=variables, target=vars, **kwds)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mmckerns/lib/python3.12/site-packages/mystic/symbolic.py", line 736, in _simplify1
    if equals(before,after,next(testvals),**kwds):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mmckerns/lib/python3.12/site-packages/mystic/symbolic.py", line 474, in equals
    after, before = eval(after,{},locals_), eval(before,{},locals_)
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
TypeError: '<=' not supported between instances of 'float' and 'complex'
mmckerns commented 10 months ago

issue seems to not be a parsing issue, but is instead when comparing a complex using an inequality. This can be mitigated a bit by better sorting (to avoid powers), and possibly by punting when checking whether to flip the sign or not. (Not sure the latter is valid...)