Search code examples
pythonsympypiecewise

Interpreting SymPy.solveset() piecewise results


Really impressed with Sympy's handling the solution of a quartic equation with particularly ugly coefficients.

The quartic was solved on a variable I called Tb and the solution had the general form Tb = f(Tc)

I did not find much detail in the Sympy docs about the piecewise results solveset() returned (I will try to contribute to the docs on this where needed once I trudge through resolving my own answers here).

There were 4 piecewise sections headed by "{Piecewise(( ..." (reasonable for a quartic solution).

However, each Piecewise section was apparently divided into "chunks" with a seperating comma apparently indicating a special subcase.

For example, one piecewise had one chunk of three (truncated here for brevity),

(-sqrt(1.68483078787199*Tc**2 - 3.36390287324716*Tc - 2*(-(-15738.9526511382*Tc >+ .... + 5.04585430987074*Tc + 6222.41209283579)**3/108)**(1/3) - >8296.54945711438)/2 + 0.998291014581918,

followed by another "chunk" (again seperated by a comma),

Eq(-1.81898940354586e-12*Tc - (-2.52724618180798*Tc**2 + 5.04585430987074*Tc + 6222.41209283579)**2/12 + 14816961.9123814, 0)),

with the last "chunk" followed up by

... + 5.04585430987074*Tc + 6222.41209283579)**3/216)**(1/3) - >8296.54945711438)/2 + 0.998291014581918, True)),

There were two questions concerning the above:

  1. Do I correctly interpret that the ", True))," at the tail end of the last chunk implies that I simply have the general solution Tb = f(Tc) for two of the 3 special case chunks and the Eq simply means Tb = f(Tc) = 0?

  2. Are there Sympy methods to programatically isolate and extract these (assumed) special cases for further processing? I could have missed it in the Sympy docs.


Solution

  • The general format of Piecewise arguments is Piecewise((expr1, cond1), (expr2, cond2), ... [, (exprn, True)]). If a condition is True it applies if no condition preceding it applies. I believe the quartic solution is returned with the conditions under which various expressions apply. There is no "general solution" of a general quartic because the form of the solution depends on the values of the coefficients. So one way to dissect your result would be to just look at the conditions:

    for arg in piecewise.args:
        print(arg.cond)  # or arg[1]