i'm not used with python. just know the basic things.
i'm using math operation in python for calculation of inverse jacobian which has super long character in one lines.
my question is 'do the python has limit character in one line?'
or how to make a right module in python?
or which operation in math for python that not allowed?
my python program is designed for ROS platform,
and this is part of my program:
class Skelplot():
def __init__(self):
....
self.q_raw =[0,0,0]
self.dq_ref =[0,0,0]
....
while not rospy.is_shutdown():
...
dx = x_now - right_hand.x()
dy = y_now - right_hand.y()
dz = z_now - right_hand.z()
self.inverse_jacobian(dx,dy,dz)
self.q_raw[0] = self.dq_ref[0] + self.q_raw[0]
self.q_raw[1] = self.dq_ref[1] + self.q_raw[1]
self.q_raw[2] = self.dq_ref[2] + self.q_raw[2]
...
def inverse_jacobian(self, dx, dy, dz):
self.dq_ref[0] = (-(5*sin(self.q_raw[0] + 2*self.q_raw[1]) - 5*sin(self.q_raw[0] - self.q_raw[2]) + 5*sin(q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) + 10*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 5*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) - 15*sin(self.q_raw[0]) + 10*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((5*cos(self.q_raw[0] + 2*self.q_raw[1]) - 5*cos(self.q_raw[0] - self.q_raw[2]) + 5*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) + 10*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 5*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) - 15*cos(self.q_raw[0]) + 10*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy
self.dq_ref[1] = (-(5*sin(self.q_raw[0] - self.q_raw[1]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1]) + 5*sin(self.q_raw[0] - self.q_raw[2]) - 5*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 5*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 10*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1]) + 5*sin(self.q_raw[0] + self.q_raw[2]) + 10*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((5*cos(self.q_raw[0] - self.q_raw[1]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1]) + 5*cos(self.q_raw[0] - self.q_raw[2]) - 5*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 5*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 10*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1]) + 5*cos(self.q_raw[0] + self.q_raw[2]) + 10*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy
self.dq_ref[2] = (-(10*sin(self.q_raw[0] - self.q_raw[2]) - 15*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) + 5*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((10*cos(self.q_raw[0] - self.q_raw[2]) - 15*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) + 5*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy
this is the respond:
Traceback (most recent call last):
File "/home/adelleodel/ros/src/mekatron/nodes/skelplot.py", line 40, in <module>
class Skelplot():
File "/home/adelleodel/ros/src/mekatron/nodes/skelplot.py", line 203, in Skelplot
self.dq_ref[2] = (-(10*sin(self.q_raw[0] - self.q_raw[2]) - 15*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) + 5*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((10*cos(self.q_raw[0] - self.q_raw[2]) - 15*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) + 5*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy
NameError: name 'self' is not defined
i also tried with exception handling like this:
....
def inverse_jacobian(self, dx, dy, dz):
try:
self.dq_ref[0] : ..
self.dq_ref[1] : ..
self.dq_ref[2] : ..
except KeyError:
pass
...
and the response is :
File "/home/adelleodel/ros/src/mekatron/nodes/skelplot.py", line 204
self.dq_ref[2] = (-(10*sin(self.q_raw[0] - self.q_raw[2]) - 15*sin(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*sin(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*sin(self.q_raw[0] + self.q_raw[1]) - 5*sin(self.q_raw[0] + self.q_raw[2]) + 5*sin(self.q_raw[0]) + 5*sin(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*sin(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dx + ((10*cos(self.q_raw[0] - self.q_raw[2]) - 15*cos(self.q_raw[0] + self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1] - self.q_raw[2]) - 10*cos(self.q_raw[0] + self.q_raw[1] + 2*self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + self.q_raw[2]) + 10*cos(self.q_raw[0] + self.q_raw[1]) - 5*cos(self.q_raw[0] + self.q_raw[2]) + 5*cos(self.q_raw[0]) + 5*cos(self.q_raw[0] - self.q_raw[1] - self.q_raw[2]) - 5*cos(self.q_raw[0] + 2*self.q_raw[1] + 2*self.q_raw[2]))/(cos(self.q_raw[1] + 2*self.q_raw[2]) + cos(2*self.q_raw[1] + self.q_raw[2]) + cos(2*self.q_raw[1])/2 + cos(2*self.q_raw[2]) + cos(2*self.q_raw[1] + 2*self.q_raw[2]) - cos(self.q_raw[1]) - cos(self.q_raw[2]) - 5/2))*dy
^
SyntaxError: invalid syntax
so, could you which one did i go wrong?
thankyou.
I copied your code into my Python2.7 and it doesn't give me any error. I suppose it is due to that you didn't use a constant indentation, make sure you use tab or the same of space all the time. You may consider choosing a proper IDE such as Spyder or Pycharm, they will do all this for you.
Related
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)
sympy conversion function?
2*a*b + 2*a*c + 2*b*c --> 2*(a*b + b*c + c*a) :
I want string ok
from sympy import *
var('a b c')
f=2*a*b+2*a*c+2*b*c
print("#f ",f)
print("#f/2",f/2)
#f 2*a*b + 2*a*c + 2*b*c
#f/2 a*b + a*c + b*c
(2022-02-02)
i try factor
i try function
thank you
from sympy import factor, factor_terms
from sympy.abc import a,b,c
def myFactor(h):
print("# factor :",h,"--->",factor(h))
print("# factor_terms :",h,"--->",factor_terms(h))
return
myFactor(2*a*b + 2*a*c + 2*b*c)
myFactor(a**2 - b**2)
print("# factor:thank you")
# factor : 2*a*b + 2*a*c + 2*b*c ---> 2*(a*b + a*c + b*c)
# factor_terms : 2*a*b + 2*a*c + 2*b*c ---> 2*(a*b + a*c + b*c)
# factor : a**2 - b**2 ---> (a - b)*(a + b)
# factor_terms : a**2 - b**2 ---> a**2 - b**2
# factor:thank you
>>> from sympy import factor, factor_terms
>>> from sympy.abc import a,b,c
>>> f = 2*a*b + 2*a*c + 2*b*c
>>> factor_terms(f)
2*(a*b + a*c + b*c)
>>> factor_terms(a**2 - b**2)
a**2 - b**2
>>> factor(_)
(a - b)*(a + b)
I have an expression which is the sum of some trig functions:
import sympy as sy
from sympy import cos,sin,pi
theta = sy.symbols('theta')
expr = 5*cos(theta) + sin(theta*2)+3*cos(3*theta)
I'd like to add a "phase shift" by pi/4 to each trig function:
sin(2*theta + pi/4) + 5*cos(theta + pi/4) + 3*cos(3*theta + pi/4)
How can this be achieved? Is there a way to walk the expression tree and do an insertion of pi/4?
There are many ways to do this. Here is one that uses pattern matching:
In [5]: expr
Out[5]: sin(2⋅θ) + 5⋅cos(θ) + 3⋅cos(3⋅θ)
In [6]: w = Wild('w')
In [7]: expr.replace(sin(w), sin(w+pi/4))
Out[7]:
⎛ π⎞
sin⎜2⋅θ + ─⎟ + 5⋅cos(θ) + 3⋅cos(3⋅θ)
⎝ 4⎠
In [8]: expr.replace(sin(w), sin(w+pi/4)).replace(cos(w), cos(w + pi/4))
Out[8]:
⎛ π⎞ ⎛ π⎞ ⎛ π⎞
sin⎜2⋅θ + ─⎟ + 5⋅cos⎜θ + ─⎟ + 3⋅cos⎜3⋅θ + ─⎟
⎝ 4⎠ ⎝ 4⎠ ⎝ 4⎠
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.
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.