How can I fix this limit-related error in sympy? - sympy

I was evaluating a limit in sympy with a line like so:
print(i, sympy.limit(ys[i],t,sympy.oo))
And I got an error like so while it was working on the first equation, i=0:
Traceback (most recent call last):
File "schurSolver.py", line 101, in <module>
print(i, sympy.limit(ys[i],t,sympy.oo))
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/limits.py", line 67, in limit
return Limit(e, z, z0, dir).doit(deep=False)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/limits.py", line 356, in doit
r = gruntz(e, z, z0, dir)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 709, in gruntz
r = limitinf(e0, z)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/core/cache.py", line 70, in wrapper
retval = cfunc(*args, **kwargs)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 452, in limitinf
c0, e0 = mrv_leadterm(e, x)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/core/cache.py", line 70, in wrapper
retval = cfunc(*args, **kwargs)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 545, in mrv_leadterm
f, logw = rewrite(exps, Omega, x, w)
File "/opt/apps/software/lang/Anaconda3/2022.05/lib/python3.9/site-packages/sympy/series/gruntz.py", line 630, in rewrite
raise NotImplementedError('Result depends on the sign of %s' % sig)
NotImplementedError: Result depends on the sign of sign(233668289711193/25000000000000000 - 111705529447*I/4882812500000000)
I read in another question that restricting the independent variable t to be "positive=True" helps, but after trying that, I still get this error. The limit works with something simple like t->0 as well.
Does anyone know how to go about fixing this error? It looks like there is some issues with how a logarithm is evaluated for an imaginary number? But the original expression for ys[0] was automatically generated so its difficult to get into the weeds of what in it is causing this. Just in case this helps, when I print ys[0], it looks like so:
1.67916841526936e-25*exp(8.35132990598358e-25*t) + 3.0440740315262e-24*exp(1.06540862878089e-21*t) + 1.22059099713886e-26*(-76032.6108848118 + 59544.5018334113*I)*exp(6.70981298653122e-19*t) + 1.12181373255683e-7*exp(8.2018946160967e-7*t) + (-0.95449878958437 - 0.442530252270205*I)*(-1.84466407525497e-8 - 3.08484093798915e-19*I)*exp(t*(-203112508392540.0 - 2.52331886301774e-5*I)) + (5.61795388979922e-16 + 1.49904195929834e-19*I)*(0.00167414740678268 + 0.000547114383265314*I)*exp(t*(-114151373602122.0 - 1.44325505129479e-6*I)) + (-0.36913636076371 - 0.168200589234581*I)*(1.14987183065264e-9 - 1.14995840520071e-19*I)*exp(t*(-86299833488193.7 + 3.78746585889043e-6*I)) + (-3.98480722981375e-10 + 3.67085318739672e-20*I)*(0.194565978525368 + 0.0531020230452043*I)*exp(t*(-33018951594173.2 - 9.14621382779521e-6*I)) + (-3.7738632642937e-6 - 2.84357156584772e-19*I)*(0.00392424219163312 - 9.28185623096522e-5*I)*exp(t*(-24277348192864.8 - 2.72941776322116e-6*I)) + (1.78858827490596e-9 - 4.62685872832677e-20*I)*(0.102466662684398 + 0.030057437651088*I)*exp(t*(-19673632056099.5 - 1.74631349146584e-6*I)) + (-0.169400731132417 - 0.0463863266825838*I)*(-2.25915622691549e-9 + 1.50588535699424e-18*I)*exp(t*(-17910930373255.5 + 6.46900742695516e-6*I)) + (-1.69426864229786e-11 + 9.82878246622891e-21*I)*(0.00285877926702012 + 0.000933750646141991*I)*exp(t*(-17681708085870.8 + 1.70613111855344e-7*I)) + (-8.30971456066843e-9 - 4.22255208842594e-19*I)*(0.481476468508761 + 0.231055579027011*I)*exp(t*(-14522372847753.0 - 1.85817290336922e-6*I)) + (-0.461383182582166 - 0.214048879996138*I)*(1.31142240200075e-8 + 5.55533771042068e-19*I)*exp(t*(-10147742280220.5 - 1.62187999351383e-6*I)) + (-0.486661737657793 - 0.216114311889499*I)*(1.85862388324154e-8 + 3.3997137693952e-19*I)*exp(t*(-9531943441405.24 - 7.01631376653731e-6*I)) + (-1.76398625737487e-8 + 5.57602445821458e-19*I)*(0.195890299389504 + 0.0928169451470297*I)*exp(t*(-6608103143888.75 - 3.12137731289971e-6*I)) + (-0.300435936362988 + 0.0247885982038744*I)*(0.00738168551602585 + 2.42530069240965e-18*I)*exp(t*(-5916103451972.79 - 5.93855525314882e-7*I)) + (-0.00675418345802343 - 7.23316575610358e-5*I)*(4.32851594777732e-5 + 4.79289382606912e-18*I)*exp(t*(-4836034968430.14 + 7.75353476640173e-6*I)) + (-3.31764505444197e-8 - 2.09598249896058e-19*I)*(0.344479780628037 + 0.118030498879817*I)*exp(t*(-3496538884334.02 + 5.29295674301267e-7*I)) + (-5.25617796619618e-13 - 7.45250916749707e-21*I)*(0.00275707897077351 + 0.000141288946087623*I)*exp(t*(-3323772234744.91 - 3.6474988304411e-6*I)) + (-0.00542897659975518 - 0.000278206489217352*I)*(1.62397206394641e-12 + 5.56535845516503e-21*I)*exp(t*(-3206838936689.46 + 2.45613790946467e-6*I)) + (-0.0230498965839992 - 0.00753056844522449*I)*(4.54615850953473e-10 - 6.47080380894056e-20*I)*exp(t*(-2492604622850.55 - 7.51649523472664e-8*I)) + (-0.00619804860313521 - 0.000726559611443317*I)*(5.77080251380177e-5 - 6.02185415742892e-19*I)*exp(t*(-2217655702083.05 + 2.89708056827509e-7*I)) + (-0.000142522800335491 + 1.6450739955635e-20*I)*(0.124639068367501 + 0.0352274573126279*I)*exp(t*(-1263964845792.25 - 7.34981620628e-8*I)) + (-0.0455190179060991 - 0.00925775971342165*I)*(0.00086928483841261 + 9.31504280092746e-19*I)*exp(t*(-773908440127.084 - 1.86748361027625e-6*I)) + (-0.111403121551235 - 0.0345813110663439*I)*(-0.00233084874287111 - 1.15315828126713e-18*I)*exp(t*(-529268989287.646 - 4.12203336149994e-6*I)) + (-1.01585724021082e-15 - 3.09000238538058e-22*I)*(0.00176746139529834 + 0.000729361212377768*I)*exp(t*(-478200000155.486 - 9.16921973000672e-8*I)) + (-0.811883096583459 - 4.2550099822616e-15*I)*(-0.518993293127577 - 0.00972110044929831*I)*exp(t*(-388991609584.927 - 6.7250608293468e-7*I)) + (-0.0387540720033096 - 0.00944863416708929*I)*(-0.00187060769547946 - 1.44794168105828e-17*I)*exp(t*(-352508469777.172 + 2.865559999185e-8*I)) + (0.29631140399024 + 7.16988455778452e-15*I)*(0.612797163963353 - 0.0148620615751015*I)*exp(t*(-84463778017.8318 - 4.82408096227738e-7*I)) + (0.228198845971918 + 1.68004186976606e-14*I)*(0.356205437578597 - 0.0277336294045075*I)*exp(t*(-27107026132.9127 - 1.99992438106899e-6*I)) + (-0.0487385829569165 - 0.00249774689748956*I)*(1.86468385973065e-6 + 1.24279803043807e-18*I)*exp(t*(-25213033282.4521 + 4.66630438163317e-9*I)) + (-2.04278153200167e-6 - 1.08454612298207e-18*I)*(0.546005043078077 + 0.211890631348977*I)*exp(t*(-15836041264.2504 + 3.82335923932025e-7*I)) + (-0.16001447920409 - 0.0566482734214268*I)*(-0.0311186495944563 - 6.03688946605135e-15*I)*exp(t*(-10181103587.4674 - 8.40281999554162e-7*I)) + (-0.274707832808365 - 3.32877456730487e-13*I)*(-0.274665221392941 + 0.00389439184741831*I)*exp(t*(-1686410364.60416 - 3.7288640561267e-7*I)) + (0.287768671750691 + 8.16549479265847e-13*I)*(0.31506604587839 + 0.0411560159407836*I)*exp(t*(-717879484.692659 + 1.84416110560464e-7*I)) + (-0.091266349697144 - 1.52390329033371e-11*I)*(-0.0775768881495083 + 0.0206396810586052*I)*exp(t*(-12216564.1775522 + 3.3056220731526e-8*I)) + (-2.48572373065549e-7 - 1.18575541574023e-12*I)*(0.0723915049547977 - 0.118332634253011*I)*exp(t*(-400.634044372763 + 7.33509752734055e-11*I)) + (0.0397048947140555 + 1.31903562882084e-6*I)*(0.124733085774612 + 0.0562645148688352*I)*exp(t*(-57.5413328754333 - 7.22187055217595e-8*I)) + (-0.0934748414187167 - 0.000209131734565415*I)*(0.270392227999757 + 0.251283191343448*I)*exp(t*(-1.01145147301153 - 4.20542377050406e-5*I)) + (-0.202161461920234 - 0.0384145274277075*I)*(-0.0824721391984489 - 0.00271286338632531*I)*exp(t*(-0.00630349107976167 + 0.000307267943474572*I)) + (-0.125465988501229 + 0.0674620600301526*I)*(-0.0298308611815717 - 0.0251828521116607*I)*exp(t*(-0.0019205456655073 + 0.00180509000286347*I)) + (-0.121159947977535 - 0.0737856751721838*I)*(0.00212643434209978 + 0.0254754055239891*I)*exp(t*(-0.0018734497073853 - 0.00178036658087088*I)) + (-0.00196516569683774 + 0.0215263485842828*I)*(0.0410800376485034 + 0.160689514241982*I)*exp(t*(-0.00108161664395421 + 1.19430123636756e-6*I)) + (-0.0202920869563751 + 0.00106100069684705*I)*(-0.00779169899616668 + 0.00154197328770158*I)*exp(t*(-0.000593010359987407 - 3.86238217908242e-5*I)) + (-0.0646951328450709 - 0.0397891367685005*I)*(-0.0164606651475395 - 0.0174961689207555*I)*exp(t*(-0.000469971561489008 + 0.000767572303526128*I)) + (0.0178379629823442 - 0.014839268413719*I)*(0.128918118224738 + 0.0979644433573652*I)*exp(t*(-0.000449612109163136 - 0.000795598483467528*I)) + (0.00226837757770358 - 0.000994463857550389*I)*(0.00469456609139216 - 0.000935663565947804*I)*exp(t*(-0.000253173466113437 - 1.33316728772136e-9*I)) + (-0.00210634241308871 + 0.0109200919539407*I)*(-0.002103999532501 + 0.0109176696626757*I)*exp(t*(0.000183788647827431 + 0.000132411817609813*I)) + (-0.00975797212108995 + 0.00462796874285418*I)*(-0.00971161913606152 + 0.00466170117633005*I)*exp(t*(0.000183792089896646 - 0.000132430365770814*I)) + (-0.0238780404492042 - 0.0145154904924849*I)*(-0.00720496445224956 - 0.026515983959851*I)*exp(t*(0.000516688789621054 + 0.000387050742857334*I)) + (-0.0420944049105853 - 0.0641195080657347*I)*(-0.0335551849338057 - 0.0295925730945855*I)*exp(t*(0.000522024704024684 - 0.000383032189929175*I)) + (-0.0623495819266974 - 0.0967910744442665*I)*(-0.0385208326751541 - 0.001201727526135*I)*exp(t*(0.000731712217325226 + 0.00234124893486142*I)) + (-0.116642132297297 - 0.0573466554899272*I)*(-0.0381566622068165 + 0.000983531052733214*I)*exp(t*(0.000827457254100448 + 3.71786946011765e-6*I)) + (-0.0106542274642987 + 0.0382367626572694*I)*(0.175850479214185 + 0.0049312841730841*I)*exp(t*(0.000985016465311529 - 0.00255688765145607*I)) + (-0.138824440584369 + 0.261687056926164*I)*(-0.00944226247868757 + 0.0689966002025778*I)*exp(t*(0.00934673158844772 - 2.28772924307456e-5*I)) + (-8.02691775013775e-5 + 2.3285071861864e-7*I)*(0.0717823437666412 + 0.0476844488649595*I)*exp(t*(0.683054643685093 - 3.63677586488862e-9*I)) + (-0.000139235936271859 + 3.66048036224373e-7*I)*(0.0858393118234517 + 0.02638281491092*I)*exp(t*(0.753476907056178 - 6.49899851030096e-9*I)) + 5.04810441792277e-8*exp(-6.14767048612559e-9*t)

Related

Collecting sub-expressions of a multivariate polynomial function in Sympy

I have a degree 6 multivariate equation in x and y written in Sympy, e.g.
eqn = a*x**6 + b*x**5*y + c*x**4*y + d*x**3*y + e*x**3*y**2 + ...
Is there a way to collect (x**2+y**2) and rearrange them into the following format?
eqn2 = A*(x**2+y**2)**3 + B*(x**2+y**2)**2 + C*(x**2+y**2) + D
A, B, C, D can be in x, y.
So far I have only tried collect(eqn, x**2 + y**2) and it returned the original equation.
Thank you!
Consider using a temporary symbol z = x**2 + y**2 and replace x**2 with z - y**2, then expand and restore:
>>> ex
A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6 + B*x**4 + 2*B*x**2*y**2 +
B*y**4 + C*x**2 + C*y**2 + D
>>> ex.subs(x**2, z - y**2).expand().subs(z, x**2 + y**2)
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D
Although that works, perhaps a more direct thing to do is separate the expression by coefficients A-D and then factor those collections of terms:
def separatevars_additively(expr, symbols=[]):
free = set(symbols) or expr.free_symbols
d = {}
while free:
f = free.pop()
expr, dep = expr.as_independent(f, as_Add=True)
if dep.has(*free):
return None
d[f] = dep
if expr:
d[0] = expr
return d
>>> coeff = var("A:D")
>>> separatevars_additively(ex, coeff)
{B: B*x**4 + 2*B*x**2*y**2 + B*y**4, A: A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6, D: D, C: C*x**2 + C*y**2}
>>> Add(*[factor(i) for i in _.values()])
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D

How can I make sympy.diff print expressions use pow instead of ** for power?

I use SymPy’s diff to get analytical expressions for my derivatives and then I need to use them in C++. Since C++ seems to only support the pow function, I was wondering if there is a way to get the final expression from sympy.diff using the C++ pow notation since the latter is also supported in Python. Since the expressions for my derivatives are quite lenghty (example below), it isn’t very convenient to change the notation manually, even more considering multiple expressions.
2.71828182845905**(-1.0*ba**4*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) - 1.0*c1*da*r12/(da*r12 + 1.0) - 1.0*c2*da**2*r12**2/(da*r12 + 1.0)**2 - 1.0*c4*da**4*r12**4/(da*r12 + 1.0)**4 - 1.0*c4*da**3*r12**3/(da*r12 + 1.0)**3 - 1.0*c5*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2) - 1.0*c6*(ba**3*r1**3/(ba*r1 + 1.0)**3 + ba**3*r2**3/(ba*r2 + 1.0)**3) - 1.0*c7*(ba**4*r1**4/(ba*r1 + 1.0)**4 + ba**4*r2**4/(ba*r2 + 1.0)**4) - 1.0*c9*da**2*r12**2*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*2.71828182845905**(1.0*ba**4*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) + 1.0*c1*da*r12/(da*r12 + 1.0) + 1.0*c2*da**2*r12**2/(da*r12 + 1.0)**2 + 1.0*c4*da**4*r12**4/(da*r12 + 1.0)**4 + 1.0*c4*da**3*r12**3/(da*r12 + 1.0)**3 + 1.0*c5*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2) + 1.0*c6*(ba**3*r1**3/(ba*r1 + 1.0)**3 + ba**3*r2**3/(ba*r2 + 1.0)**3) + 1.0*c7*(ba**4*r1**4/(ba*r1 + 1.0)**4 + ba**4*r2**4/(ba*r2 + 1.0)**4) + 1.0*c9*da**2*r12**2*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*(-2.0*ba**4*c8*r1**3*r2**2/((ba*r1 + 1.0)**3*(ba*r2 + 1.0)**2) - 2.0*ba**4*c8*r1**2*r2**3/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**3) + 4.0*ba**3*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) + 1.0*c5*(-2*ba**2*r1**3/(ba*r1 + 1.0)**3 - 2*ba**2*r2**3/(ba*r2 + 1.0)**3 + 2*ba*r1**2/(ba*r1 + 1.0)**2 + 2*ba*r2**2/(ba*r2 + 1.0)**2) + 1.0*c6*(-3*ba**3*r1**4/(ba*r1 + 1.0)**4 - 3*ba**3*r2**4/(ba*r2 + 1.0)**4 + 3*ba**2*r1**3/(ba*r1 + 1.0)**3 + 3*ba**2*r2**3/(ba*r2 + 1.0)**3) + 1.0*c7*(-4*ba**4*r1**5/(ba*r1 + 1.0)**5 - 4*ba**4*r2**5/(ba*r2 + 1.0)**5 + 4*ba**3*r1**4/(ba*r1 + 1.0)**4 + 4*ba**3*r2**4/(ba*r2 + 1.0)**4) + 1.0*c9*da**2*r12**2*(-2*ba**2*r1**3/(ba*r1 + 1.0)**3 - 2*ba**2*r2**3/(ba*r2 + 1.0)**3 + 2*ba*r1**2/(ba*r1 + 1.0)**2 + 2*ba*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*(2.0*ba**4*c8*r1**3*r2**2/((ba*r1 + 1.0)**3*(ba*r2 + 1.0)**2) + 2.0*ba**4*c8*r1**2*r2**3/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**3) - 4.0*ba**3*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) - 1.0*c5*(-2*ba**2*r1**3/(ba*r1 + 1.0)**3 - 2*ba**2*r2**3/(ba*r2 + 1.0)**3 + 2*ba*r1**2/(ba*r1 + 1.0)**2 + 2*ba*r2**2/(ba*r2 + 1.0)**2) - 1.0*c6*(-3*ba**3*r1**4/(ba*r1 + 1.0)**4 - 3*ba**3*r2**4/(ba*r2 + 1.0)**4 + 3*ba**2*r1**3/(ba*r1 + 1.0)**3 + 3*ba**2*r2**3/(ba*r2 + 1.0)**3) - 1.0*c7*(-4*ba**4*r1**5/(ba*r1 + 1.0)**5 - 4*ba**4*r2**5/(ba*r2 + 1.0)**5 + 4*ba**3*r1**4/(ba*r1 + 1.0)**4 + 4*ba**3*r2**4/(ba*r2 + 1.0)**4) - 1.0*c9*da**2*r12**2*(-2*ba**2*r1**3/(ba*r1 + 1.0)**3 - 2*ba**2*r2**3/(ba*r2 + 1.0)**3 + 2*ba*r1**2/(ba*r1 + 1.0)**2 + 2*ba*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2) + 2.71828182845905**(-1.0*ba**4*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) - 1.0*c1*da*r12/(da*r12 + 1.0) - 1.0*c2*da**2*r12**2/(da*r12 + 1.0)**2 - 1.0*c4*da**4*r12**4/(da*r12 + 1.0)**4 - 1.0*c4*da**3*r12**3/(da*r12 + 1.0)**3 - 1.0*c5*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2) - 1.0*c6*(ba**3*r1**3/(ba*r1 + 1.0)**3 + ba**3*r2**3/(ba*r2 + 1.0)**3) - 1.0*c7*(ba**4*r1**4/(ba*r1 + 1.0)**4 + ba**4*r2**4/(ba*r2 + 1.0)**4) - 1.0*c9*da**2*r12**2*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*2.71828182845905**(1.0*ba**4*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) + 1.0*c1*da*r12/(da*r12 + 1.0) + 1.0*c2*da**2*r12**2/(da*r12 + 1.0)**2 + 1.0*c4*da**4*r12**4/(da*r12 + 1.0)**4 + 1.0*c4*da**3*r12**3/(da*r12 + 1.0)**3 + 1.0*c5*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2) + 1.0*c6*(ba**3*r1**3/(ba*r1 + 1.0)**3 + ba**3*r2**3/(ba*r2 + 1.0)**3) + 1.0*c7*(ba**4*r1**4/(ba*r1 + 1.0)**4 + ba**4*r2**4/(ba*r2 + 1.0)**4) + 1.0*c9*da**2*r12**2*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*(16.0*(-0.5*ba**4*c8*r1**3*r2**2/((ba*r1 + 1.0)**3*(ba*r2 + 1.0)**2) - 0.5*ba**4*c8*r1**2*r2**3/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**3) + ba**3*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) + 0.25*c5*(-2*ba**2*r1**3/(ba*r1 + 1.0)**3 - 2*ba**2*r2**3/(ba*r2 + 1.0)**3 + 2*ba*r1**2/(ba*r1 + 1.0)**2 + 2*ba*r2**2/(ba*r2 + 1.0)**2) + 0.25*c6*(-3*ba**3*r1**4/(ba*r1 + 1.0)**4 - 3*ba**3*r2**4/(ba*r2 + 1.0)**4 + 3*ba**2*r1**3/(ba*r1 + 1.0)**3 + 3*ba**2*r2**3/(ba*r2 + 1.0)**3) + 0.25*c7*(-4*ba**4*r1**5/(ba*r1 + 1.0)**5 - 4*ba**4*r2**5/(ba*r2 + 1.0)**5 + 4*ba**3*r1**4/(ba*r1 + 1.0)**4 + 4*ba**3*r2**4/(ba*r2 + 1.0)**4) + 0.25*c9*da**2*r12**2*(-2*ba**2*r1**3/(ba*r1 + 1.0)**3 - 2*ba**2*r2**3/(ba*r2 + 1.0)**3 + 2*ba*r1**2/(ba*r1 + 1.0)**2 + 2*ba*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)**2) + 2.71828182845905**(-1.0*ba**4*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) - 1.0*c1*da*r12/(da*r12 + 1.0) - 1.0*c2*da**2*r12**2/(da*r12 + 1.0)**2 - 1.0*c4*da**4*r12**4/(da*r12 + 1.0)**4 - 1.0*c4*da**3*r12**3/(da*r12 + 1.0)**3 - 1.0*c5*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2) - 1.0*c6*(ba**3*r1**3/(ba*r1 + 1.0)**3 + ba**3*r2**3/(ba*r2 + 1.0)**3) - 1.0*c7*(ba**4*r1**4/(ba*r1 + 1.0)**4 + ba**4*r2**4/(ba*r2 + 1.0)**4) - 1.0*c9*da**2*r12**2*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*2.71828182845905**(1.0*ba**4*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) + 1.0*c1*da*r12/(da*r12 + 1.0) + 1.0*c2*da**2*r12**2/(da*r12 + 1.0)**2 + 1.0*c4*da**4*r12**4/(da*r12 + 1.0)**4 + 1.0*c4*da**3*r12**3/(da*r12 + 1.0)**3 + 1.0*c5*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2) + 1.0*c6*(ba**3*r1**3/(ba*r1 + 1.0)**3 + ba**3*r2**3/(ba*r2 + 1.0)**3) + 1.0*c7*(ba**4*r1**4/(ba*r1 + 1.0)**4 + ba**4*r2**4/(ba*r2 + 1.0)**4) + 1.0*c9*da**2*r12**2*(ba**2*r1**2/(ba*r1 + 1.0)**2 + ba**2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)*(6.0*ba**4*c8*r1**4*r2**2/((ba*r1 + 1.0)**4*(ba*r2 + 1.0)**2) + 8.0*ba**4*c8*r1**3*r2**3/((ba*r1 + 1.0)**3*(ba*r2 + 1.0)**3) + 6.0*ba**4*c8*r1**2*r2**4/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**4) - 16.0*ba**3*c8*r1**3*r2**2/((ba*r1 + 1.0)**3*(ba*r2 + 1.0)**2) - 16.0*ba**3*c8*r1**2*r2**3/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**3) + 12.0*ba**2*c8*r1**2*r2**2/((ba*r1 + 1.0)**2*(ba*r2 + 1.0)**2) + 1.0*c5*(6*ba**2*r1**4/(ba*r1 + 1.0)**4 + 6*ba**2*r2**4/(ba*r2 + 1.0)**4 - 8*ba*r1**3/(ba*r1 + 1.0)**3 - 8*ba*r2**3/(ba*r2 + 1.0)**3 + 2*r1**2/(ba*r1 + 1.0)**2 + 2*r2**2/(ba*r2 + 1.0)**2) + 1.0*c6*(12*ba**3*r1**5/(ba*r1 + 1.0)**5 + 12*ba**3*r2**5/(ba*r2 + 1.0)**5 - 18*ba**2*r1**4/(ba*r1 + 1.0)**4 - 18*ba**2*r2**4/(ba*r2 + 1.0)**4 + 6*ba*r1**3/(ba*r1 + 1.0)**3 + 6*ba*r2**3/(ba*r2 + 1.0)**3) + 1.0*c7*(20*ba**4*r1**6/(ba*r1 + 1.0)**6 + 20*ba**4*r2**6/(ba*r2 + 1.0)**6 - 32*ba**3*r1**5/(ba*r1 + 1.0)**5 - 32*ba**3*r2**5/(ba*r2 + 1.0)**5 + 12*ba**2*r1**4/(ba*r1 + 1.0)**4 + 12*ba**2*r2**4/(ba*r2 + 1.0)**4) + 1.0*c9*da**2*r12**2*(6*ba**2*r1**4/(ba*r1 + 1.0)**4 + 6*ba**2*r2**4/(ba*r2 + 1.0)**4 - 8*ba*r1**3/(ba*r1 + 1.0)**3 - 8*ba*r2**3/(ba*r2 + 1.0)**3 + 2*r1**2/(ba*r1 + 1.0)**2 + 2*r2**2/(ba*r2 + 1.0)**2)/(da*r12 + 1.0)**2)
I guess it would be equally useful if I could use the ** operator for power in C++, but I don’t know if this is even possible.
If your target is C++ code, the designated method is sympy.printing.ccode, which should take care of anything specific to C (and thus C++) and has further features that might be useful to you:
from sympy.printing import ccode
from sympy import a,b,c
expression = a**b+c**a-b**c**a
print(ccode(expression)) # 'pow(a, b) - pow(b, pow(c, a)) + pow(c, a)'
Printing sympy.Expr objects is delegated to sympy.printing.sstr, which in turn is a small wrapper that uses sympy.printing.StrPrinter.
You can thus monkeypatch sympy.printing.StrPrinter._print_Pow to generate a pow(x, y) string instead of x**y.

Trying to implement IDL broyden function in Python using broyden1, but the answers I am getting aren't correct

I have converted following code from IDL and I replaced Broyden function of IDL by broyden1 function of python, are both similar ?
import numpy as np
import spiceypy as spice
import pandas as pd
import scipy as sp
import math as mt
from scipy.optimize.nonlin import NoConvergence
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
from scipy import signal as sg
print('Start of Program.\n')
def nonrelbroyfunc(nonrelguessx):
global passdf,passc,passf,passbetaa,passvra,passvza,passdeltab,passvrb,passvzb,passzb,passra,passza,passgamma
return [passdf * passc / passf - np.cos(passbetaa) * passvra - np.sin(passbetaa) * passvza + np.sin(passdeltab) * passvrb + np.cos(passdeltab) * passvzb + np.cos(passbetaa - nonrelguessx[1]) * passvra + np.sin(passbetaa - nonrelguessx[1]) * passvza - np.sin(passdeltab - nonrelguessx[0]) * passvrb - np.cos(passdeltab - nonrelguessx[0]) * passvzb, (-1) * passzb * np.sin(passdeltab - nonrelguessx[0]) - np.sqrt(passra**2 + passza**2) * np.sin(passbetaa - nonrelguessx[1] - passgamma)]
def relbroyfunc(relguessx):
global passdf,passc,passf,passbetaa,passvra,passvza,passdeltab,passvrb,passvzb,passzb,passra,passza,passgamma
return [passdf / passf - (1 + passvrb * np.sin(passdeltab-relguessx[0]) / passc + passvzb * np.cos(passdeltab-relguessx[0]) / passc + 0.5 * passvbsqd / passc**2 - passub/passc**2) / (1 + passvra * np.cos(passbetaa-relguessx[1]) / passc + passvza * np.sin(passbetaa-relguessx[1]) / passc + 0.5 * passvasqd / passc**2 - passua/passc**2) + (1 + passvrb * np.sin(passdeltab) / passc + passvzb * np.cos(passdeltab) / passc + 0.5 * passvbsqd / passc**2 - passub/passc**2) / (1 + passvra * np.cos(passbetaa) / passc + passvza * np.sin(passbetaa) / passc + 0.5 * passvasqd / passc**2 - passua/passc**2),(-1) * passzb * np.sin(passdeltab - relguessx[0]) - np.sqrt(passra**2 + passza**2) * np.sin(passbetaa - relguessx[1] - passgamma)]
nconvergence = 1000
freq = 8423*pow(10,6)
for iobs in range(0,nobs):
print(iobs)
passdf = deltaf[iobs]
passf = freq
passc = c_mks
passbetaa = betaa
passdeltab = deltab
passvra = vra
passvza = vza
passvrb = vrb
passvzb = vzb
passzb = zb
passra = ra
passza = za
passgamma = gamma
nonrelguessx = [0,0]
try :
nonrelresult = sp.optimize.broyden1( nonrelbroyfunc, nonrelguessx, maxiter = 5000, x_tol = 10**-20)
converged = True
except NoConvergence as e:
nonrelresult = e.args[0]
converged = False
nonreldeltararr[iobs] = nonrelresult[0]
nonrelbetararr[iobs] = nonrelresult[1]
relguessx = [0,0]
try :
relresult = sp.optimize.broyden1( relbroyfunc, relguessx, maxiter = 5000, x_tol = 10**-20)
converged = True
except NoConvergence as e:
relresult = e.args[0]
converged = False
reldeltararr[iobs] = relresult[0]
relbetararr[iobs] = relresult[1]
Values assigned to all the pass variables are derived from computation and those values are correct. I am trying to find nonreldeltararr, nonrelbetararr, reldeltararr and relbetararr. When applying broyden1 function , I am getting answer but not of the order I want. I think I am not properly applying broyden1 function or is there any another equivalent function in python to that of Broyden function in IDL. As there is no mention about Please, any help will be appreciated.
The documentation of broyden function in IDL
The documentation of broyden1 function in python
IDL code which I am implementing in python2.7 is as follows :
; Improve IDL's chances of using these functions correctly
; Make these two functions separate programs if problems are encountered
forward_function nonrelbroyfunc, relbroyfunc
; Common block used for solving a pair of equations for two critical angles
common passstuff, passdf, passf, passc, passbetaa, passdeltab, passvra, passvza, passvrb, passvzb, passzb, passra, passza, passgamma, passua, passub, passvasqd, passvbsqd
freq = 8423.d6
nconvergence = 1000L
for iobs = 0, nobs-1 do begin
; Variables to pass to the Broyden root-finding functions
passdf = deltaf(iobs)
passf = freq
passc = c_mks
passbetaa = betaa
passdeltab = deltab
passvra = vra
passvza = vza
passvrb = vrb
passvzb = vzb
passzb = zb
passra = ra
passza = za
passgamma = gamma
nonrelguessx = [delta_r, beta_r] ; Either make use of prior non-relativistic result from Fjeldbo et al. (1971) numerical method
nonrelguessx = [0d,0d] ; Or ensure independent result
nonrelresult = broyden(nonrelguessx, 'nonrelbroyfunc', itmax=5000, /double, tolx=1d-20)
nonreldeltararr(iobs) = nonrelresult[0]
nonrelbetararr(iobs) = nonrelresult[1]
relguessx = [delta_r, beta_r] ; Either make use of prior non-relativistic result from Fjeldbo et al. (1971) numerical method
relguessx = [0d,0d] ; Or ensure independent result
relresult = broyden(relguessx, 'relbroyfunc', itmax=5000, /double, tolx=1d-20)
reldeltararr(iobs) = relresult[0]
relbetararr(iobs) = relresult[1]
endfor ; for iobs = 0, nobs-1 do begin
stop
end
FUNCTION nonrelbroyfunc, nonrelguessx
common passstuff
RETURN, $
[passdf * passc / passf $
- cos(passbetaa) * passvra $
- sin(passbetaa) * passvza $
+ sin(passdeltab) * passvrb $
+ cos(passdeltab) * passvzb $
+ cos(passbetaa - nonrelguessx[1]) * passvra $
+ sin(passbetaa - nonrelguessx[1]) * passvza $
- sin(passdeltab - nonrelguessx[0]) * passvrb $
- cos(passdeltab - nonrelguessx[0]) * passvzb, $
(-1d) * passzb * sin(passdeltab - nonrelguessx[0]) - sqrt(passra^2 + passza^2) * sin(passbetaa - nonrelguessx[1] - passgamma)]
END
FUNCTION relbroyfunc, relguessx
common passstuff
RETURN, $
[passdf / passf - $
(1d + passvrb * sin(passdeltab-relguessx[0]) / passc + passvzb * cos(passdeltab-relguessx[0]) / passc + $
0.5d * passvbsqd / passc^2 - passub/passc^2) / $
(1d + passvra * cos(passbetaa-relguessx[1]) / passc + passvza * sin(passbetaa-relguessx[1]) / passc + $
0.5d * passvasqd / passc^2 - passua/passc^2) + $
(1d + passvrb * sin(passdeltab) / passc + passvzb * cos(passdeltab) / passc + $
0.5d * passvbsqd / passc^2 - passub/passc^2) / $
(1d + passvra * cos(passbetaa) / passc + passvza * sin(passbetaa) / passc + $
0.5d * passvasqd / passc^2 - passua/passc^2), $
(-1d) * passzb * sin(passdeltab - relguessx[0]) - sqrt(passra^2 + passza^2) * sin(passbetaa - relguessx[1] - passgamma)]
END
There is an example in the IDL documentation page you linked to, have you tried to implement it in python?
The bit of code below returns exactly the same values as in the example. This means that they are at least similar.
import numpy as np
import scipy.optimize
def fcn(x):
return np.array([3.0 * x[0] - np.cos(x[1]*x[2]) - 0.5,
x[0]**2 - 81.0*(x[1] + 0.1)**2 + np.sin(x[2]) + 1.06,
np.exp(-x[0]*x[1]) + 20.0 * x[2] + (10.0*np.pi - 3.0)/3.0])
scipy.optimize.broyden1(fcn, np.array([1.0, 1.0, 1.0]))
# returns: array([ 5.00000011e-01, -2.34224116e-10, -5.23598775e-01])
Edit: And since your bits of code are non-functional because incomplete we can't test them. But I see that relguessx is not defined in the python code.

Trouble iterating lines from a text file in Python for reuse

I am having trouble iterating a process in Python 2.7.
I have simplified it as much as possible to debug the first steps, and want to build on this.
If I specify the data
data = ("-4.916409,36.629535,3.721236,255,232,242")
And then get it to split
X,Y,Z,R,G,B = data.split(",")
I can recombine some of the elements to create new files and/or folders with the name of those elements:
RGB = (R + "+" + G + "+" + B)
os.makedirs(inputFolder + os.sep + RGB)
fo = open("Z:\\temp\\output" + os.sep + RGB + os.sep + RGB + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
But when I try to do that from a longer text file, I can no longer combine the elements into this "RGB" as a file and/or folder name (only "R" or "G" or "B"), and only get a return for the first line.
inputFolder = ("Z:\\temp\\output")
coordinates = open("Z:\\temp\\accident2.txt", "r")
for line in coordinates:
X,Y,Z,R,G,B = line.split(",")
RGB = (R + "+" + G + "+" + B)
os.makedirs(inputFolder + os.sep + R)
fo = open("Z:\\temp\\output" + os.sep + R + os.sep + R + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
But it works if I switch from the whole numbers to the decimals:
RGB = (X + "," + Y + "," + Z)
Then I can write:
fo = open("Z:\\temp\\output" + os.sep + RGB + os.sep + RGB + ".txt", "w")
Which is not quite right, but closer to what I want.
Why are numbers with decimals easier to "read" than those without?
How do I fix it so the whole numbers are treated like those with decimals?
well your coordinates is a file object, you need to read from this object. You can actually read line-by-line using readlines, function of file object. Check documentation
inputFolder = ("Z:\\temp\\output")
with open("Z:\\temp\\accident2.txt", "r") as coordinates:
for linenumber,line in enumerate( coordinates.readlines() ):
X,Y,Z,R,G,B = line[:-1].split(",")
RGB = (R + "+" + G + "+" + B)
try:
os.makedirs(inputFolder + os.sep + R)
fo = open("Z:\\temp\\output" + os.sep + R + os.sep + R + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
except:
print "problem in line %d"%linenumber

How to print an expression ordered by a symbol in sympy?

I tried to use init_printing(order=..) but none of those orders match what i need.
For example:
exp=m*x**3 + 3*m**2*x + x**2 + 1
print(exp,x) will give:
m*x**3 + x**2 + 3*m**2*x + 1
print(exp,m) will give:
3*x*m**2 + 3*x**3*m + x**2 + 1