You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As raised in the issue in which a light weight solution is given, a good numerical solver for equations that one might encounter when dealing with 2D geometric entities would be nice.
And here is the start of a routine that I put (cobbled) together.
defn2roots(eq1, eq2, x, y, n=3):
"""Return numerical approximations of solution for coupled equations that are polynomial of degree 2 or less in variables x and y."""fromsympy.core.containersimportTuplefromsympy.solvers.solversimportunrad, solvefromsympyimportnfloateqs=Tuple(*[e.expand().n(n) forein (eq1,eq2)])
ifany(e.is_numberforeineqs) ornotset([x,y]) &eqs.free_symbols:
raiseValueError('one or more equations is missing symbols')
foreineqs:
ifany(e.diff(v,2).free_symbolsforvin (x,y)):
raiseValueError('only quadratics (or less) in %s and %s supported'% (x, y))
# check for independent case: one are both are independent of the otherfree= [e.free_symbolsforeineqs]
ifany(len(i)==1foriinfree):
iflen(free[0]|free[1]) ==1:
raiseValueError('uncoupled system')
iflen(free[0])==1:
i=0iffree[0].pop()==xelse1j=0ifielse1else:
i=1iffree[1].pop()==xelse0j=0ifielse1rv= []
forxiinroots(eqs[i]):
foryiinroots(eqs[j].subs(x, xi)):
rv.append({x:xi, y:yi})
print('case0')
returnnfloat(rv, n)
# check for linear case: one is linear in x or yfori, einenumerate(eqs):
forjin (x, y):
ifjnotinfree[i]:
continueifjnotine.diff(j).free_symbols:
ji=solve(e, j)[0]
s=xifj==yelseyo=eqs[0ifielse1]
print('case1')
rv= [{j: ji.subs(s, si).expand(), s: si} forsiinroots(o.subs(j, ji))]
returnnfloat(rv, n)
# biquadraticanx=solve(eqs[0], x)[0]
yeq=eqs[1].subs(x, anx)
z=unrad(yeq)
ifnotz:
z=yeqelse:
z=z[0]
yy=roots(z)
defnorm(x,y):
returnabs((x**2+y**2).n(2))
got=[]
foryiinyy:
ty=eqs.subs(y, yi)
forxiinroots(ty[0],x):
got.append((norm(*ty.subs(x,xi)),(xi,yi)))
print('case2')
rv=sorted(got, key=lambdax:x[0])
returnsorted(nfloat([tfori, tinrv[:len(rv)//2]], n))
But sorting out the use of roots, real_roots or nroots is not done. The above was tested with the following:
As raised in the issue in which a light weight solution is given, a good numerical solver for equations that one might encounter when dealing with 2D geometric entities would be nice.
Here is such an equation set:
And here is the start of a routine that I put (cobbled) together.
But sorting out the use of
roots
,real_roots
ornroots
is not done. The above was tested with the following:giving output
You can see that imaginary numbers slipped by in one of the
case1
cases. So there is still more work to be done.Anyway, this is here if anyone --even me, perhaps -- would like to take it up.
cf #3571
The text was updated successfully, but these errors were encountered: