0

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.

Larry
  • 3
  • 2
  • 1
    Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Stelios Jun 30 '17 at 13:47
  • Thanx for the interest Stelios. Not clear about some things. The complete 4 Piecewise parts are in no way minimal. You want to see the entire result that solveset returned? What is it that needs verfication? Do you want to see some of the code I used to set up solveset? – Larry Jun 30 '17 at 14:33
  • 1
    Can't you come up with a minimal/simple example in order to demonstrate your issue? In any case, I would recommend providing a complete reproducible code snippet and clearly indicating where the problem is. – Stelios Jun 30 '17 at 14:38
  • My issue concerned properly interpreting the piecewise results of sympy's solveset not that there is any problem with the code results - I don't think there is any problem. Am I correct assuming each subpart (seperated by commas - apparently a piecewise syntax indicating such) as a special case solution within the give piecewise solution. Having solved a quartic equation, there were 4 piecewise solutions BUT each piecewise solution had subparts. Everything said, my question is: do the comma's in a piecewise solution delineate special cases within {Piecewise}? – Larry Jun 30 '17 at 16:14
  • Stelios, I see what you are saying. It looks as though I will not get a direct answer here and will pursue other methods to analyze how the solveset() result should be interpreted. In terms of simplification, it only took 3 lines of sympy code to solve the general quartic aX\**4+bX\**3+cX\**2+dX+e = 0. I got the same general format as for my problem i.e. 4 piecewise with 3 "special cases" each. I will plinky dink with the general solution until I can decipher mine. Thanks for your input. – Larry Jul 01 '17 at 21:19
  • Regarding your first question, check the [`piecewise` documentation](http://docs.sympy.org/dev/modules/functions/elementary.html#piecewise) (and the examples there). For extracting parameters from expressions use `.args`, see, e.g., [here](http://docs.sympy.org/latest/guide.html#common-tasks). – Stelios Jul 02 '17 at 07:01
  • Thanks again, Stelios. Actually I did see the piecewise documentation that came with the Sympy download but was hoping for more detail ... kind of like Schaum's Outlines - skimpy. Enough there, just have to work the problem - experiment and think. I had tried .args() also but it simply returned what I already had - there again, experiment and think. Again, appreciate your help. – Larry Jul 03 '17 at 10:45

1 Answers1

1

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]
smichr
  • 16,948
  • 2
  • 27
  • 34
  • The args thing worked splitting the 22,936 character solveset result into 4 piecewise sets. It did not further split each piecewise into the (expr, cond) format, nevertheless; Given the (expr, cond) format chunks within each piecewise result, my particular problem splits neatly into two such (expr, cond) chunks for each piecewise string. The first of the two had the format (exprA, Eq(exprB, 0) and the second had (exprC, True). Understanding the first chunk correctly, exprA is one possible root provided exprB = 0. The exprC is a distinct and independent root from exprA provided exprB not= 0. – Larry Jul 06 '17 at 12:09
  • > exprA is one possible root provided exprB = 0. The exprC is a distinct and independent root from exprA provided exprB not= 0. Yes. – smichr Jul 06 '17 at 20:07