solve doesn't return when solving a simultaneous equation

90 views
Skip to first unread message

Kevin Pauba

unread,
Aug 4, 2022, 11:18:00 PM8/4/22
to sympy
I've attached a portion of a jupyter notebook.  I'm attempting to solve a simultaneous equation using sympy.  The sym.solve() in the green input box doesn't return (well, I waited over night on my macbook pro).  Might the solution be intractable?  Is there another way to get a solution?  Any help is greatly appreciated.
Screen Shot 2022-08-04 at 8.10.33 PM.png

Jeremy Monat

unread,
Aug 4, 2022, 11:50:53 PM8/4/22
to sy...@googlegroups.com
Hi Kevin,

Could you post your code as text so people can copy and paste it?

Not sure if it will help here, but one tip is to set simplify=false to speed up solve().

Best,
Jeremy


On Thu, Aug 4, 2022 at 11:18 PM Kevin Pauba <klp...@gmail.com> wrote:
I've attached a portion of a jupyter notebook.  I'm attempting to solve a simultaneous equation using sympy.  The sym.solve() in the green input box doesn't return (well, I waited over night on my macbook pro).  Might the solution be intractable?  Is there another way to get a solution?  Any help is greatly appreciated.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/e01647ec-ccd4-48df-a9ef-a0af455a2804n%40googlegroups.com.

Kevin Pauba

unread,
Aug 5, 2022, 12:39:53 AM8/5/22
to sympy
Sorry, Jeremy.  Good suggestion!

s1, s2, q_dp1, q_dp2 = sym.symbols('s_1, s_2, Q_dp_1, Q_dp_2')
eq1 = equ1.subs({ s: s1 }) - q_dp1
md( "$" + sym.latex(eq1) + " = 0$\n" )

eq2 = equ1.subs({ s: s2 }) - q_dp2
md( "$" + sym.latex(eq2) + " = 0$\n" )

soln = sym.solve([eq1, eq2], q1, q2)
print(f"soln = {soln}")

I'll set simplify to False and see how it goes ...

Kevin Pauba

unread,
Aug 5, 2022, 12:54:24 AM8/5/22
to sympy
And eq1=Q_1*s_1 - Q_2*s_1 + 2*Q_2 - Q_dp_1 - 2*sqrt(Q_2*(Q_1*s_1 - Q_2*s_1 + Q_2 + 2*sqrt(Q_1*Q_2*s_1*(1 - s_1)))) + 2*sqrt(Q_1*Q_2*s_1*(1 - s_1)) ... sorry, long day!

Chris Smith

unread,
Aug 5, 2022, 9:01:42 AM8/5/22
to sympy
If you remove the radicals (`sympy.solvers.solvers.unrad(eq1)`) and replace `Q1` and `Q2` with `x` and `y` and `Q_dp_1` with a and `s1` with `b` you will get an expression that is of degree 4 in every variable and can be split into a term with `a` and `b` and a term with only `b` -- both with `x` and `y`.

u1 = a*(a**3 - 4*a**2*y*(2 - b) - 2*a*y**2*(-3*b**2 + 8*b - 8) - 4*b**3*x**3 + 2*b**2*(-x**2*(-3*a + 2*y*(b - 2)) + 2*y**3*(b - 2)) - 4*b*x*(a**2 + a*y*(b - 2) + y**2*(-b**2 - 8*b + 8))) + \
         b**2*(b*(x**2 + y**2) + 2*x*y*(b - 2))**2

Replace a,b with c,d (for `q_dp_2` and `s2`) to get `u2`. I can't imagine that solving a pair of quartics is going to give a nice solution. But solving this system with known values for `a` and `b` would be straightforward with `nsolve`.

/c

Oscar Benjamin

unread,
Aug 5, 2022, 10:47:07 AM8/5/22
to sympy
You should be able to obtain a parametric Groebner basis to represent
the solutions of this system. Whether that leads to an explicit
solution in radicals is hard to say without trying.

I would demonstrate how to do this but the code for putting together
the equations is incomplete.
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/72f1b075-dbe2-41c0-bba6-9305c5960443n%40googlegroups.com.
Message has been deleted

Oscar Benjamin

unread,
Aug 5, 2022, 10:56:00 AM8/5/22
to sympy
That code still doesn't work: it gives SyntaxError. Please test the
code yourself in a fresh Python process before sending it.

On Fri, 5 Aug 2022 at 15:51, Kevin Pauba <klp...@gmail.com> wrote:
>
> As you suspected, Jeremy, the simplify=False didn't have any visible affect.
>
> BTW, earlier, I should have posted this as the code:
>
> equ1 = =Q_1*s_1 - Q_2*s_1 + 2*Q_2 - Q_dp_1 - 2*sqrt(Q_2*(Q_1*s_1 - Q_2*s_1 + Q_2 + 2*sqrt(Q_1*Q_2*s_1*(1 - s_1)))) + 2*sqrt(Q_1*Q_2*s_1*(1 - s_1))
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/55366c61-c3ca-445e-b792-b2dcb2fdfe65n%40googlegroups.com.

Kevin Pauba

unread,
Aug 5, 2022, 11:08:33 AM8/5/22
to sympy
Here's the minimal working example (except for it hanging on solve()):

import sympy as sym
from sympy import sqrt

q1, q2, s, s1, s2, q_dp1, q_dp2 = sym.symbols('Q_1, Q_2, s, s_1, s_2, Q_dp_1, Q_dp_2')

equ1 = q1*s - q2*s + 2*q2 - 2*sqrt(q2*(q1*s - q2*s + q2 + 2*sqrt(q1*q2*s*(1 - s)))) + 2*sqrt(q1*q2*s*(1 - s))


eq1 = equ1.subs({ s: s1 }) - q_dp1
print(f"{eq1} = 0")


eq2 = equ1.subs({ s: s2 }) - q_dp2
print(f"{eq2} = 0")

soln = sym.solve([eq1, eq2], (q1, q2), simplify=False)

print(f"soln = {soln}")

Oscar Benjamin

unread,
Aug 5, 2022, 7:50:09 PM8/5/22
to sympy
I just had a quick look and I think that maybe this has a positive
dimensional solution set.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/54eddcdc-a42e-4ebc-87b8-b6aaea1e2d0an%40googlegroups.com.

Kevin Pauba

unread,
Aug 5, 2022, 10:11:39 PM8/5/22
to sympy
Hi Oscar,

That very well may be (as you've noticed, I'm not well versed in math as I once was).  This is an applied geometry problem (see attached diagram) where the triangle rotates about point A.  I am solving for the relationships between q1 and q2, the perpendicular distance from P to the base of the triangle (segment DP) and the angular measure s using  rational trigonometry (see https://stijnoomes.com/laws-of-rational-trigonometry/).   I have a solution using sin, cos but I'm interested in this alternative method.

equ1 in my working example solves for the segment DP given q1, q2 and s (and angular measure).  The problem presented here is to determine q1 and q2 given two known segments and angular measures (q_dp1, s1) and (q_dp2, s2).

I hope this makes sense and that the information might help you help me.

Thanks for taking the time looking into it!

Screen Shot 2022-08-05 at 6.40.43 PM.png

Chris Smith

unread,
Aug 6, 2022, 7:45:13 AM8/6/22
to sympy

Kevin, I took a look at some of the rational geometry links -- it makes me wonder if you might approach the relationship between Q1 and Q2 and the area of the triangle extending past the line instead of simply the length DP.

/c

Kevin Pauba

unread,
Aug 6, 2022, 11:22:19 AM8/6/22
to sympy
That might be another approach but this represents an applied geometry problem (mechanics). The segment DP (along with s) is an input measure that is easy to determine/specify.

Oscar Benjamin

unread,
Aug 6, 2022, 12:14:19 PM8/6/22
to sympy
The equations you are attempting to solve lead to a very complicated
Groebner basis that is slow to compute (I'm not sure how long it
takes) and probably gives quite a complicated expression for the
solution.

It might be better if you can derive the equations without introducing
radicals i.e. to work with the quadrances rather than square rooting
them to get lengths. I'm not sure how you derived this but I imagine
the intention of rational geometry is to avoid things like square
roots.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/357f1ceb-d50f-484f-ae1e-0c82a1414a62n%40googlegroups.com.

Alan Bromborsky

unread,
Aug 6, 2022, 12:33:41 PM8/6/22
to sy...@googlegroups.com
Do you have a write up to go with the picture so I can understand
exactly what are the inputs to the problem and what you are trying to
solve for (I am less clear on the input parameters than what you are
solving for).  There may be a way of using 2D conformal geometric
algebra to solve the problem.

Kevin Pauba

unread,
Aug 6, 2022, 1:35:38 PM8/6/22
to sympy
Rational trigonometry totally eschews the use of transcendental functions like sine and cosine.  The "Triple Spread Formula" relating the speads to the quadrances, for example, is quadratic (as is the "Triple Quad Formula") -- the use of square roots is unavoidable.

After working the same problem using traditional circular functions (sine & cosine), I was pleased to see a much simpler solution (that took relatively little processing time).  I am intrigued with rational trigonometry but a bit disappointed that a solution is more elusive.

Kevin Pauba

unread,
Aug 6, 2022, 1:41:36 PM8/6/22
to sympy
I do have a jupyter notebook but I suppose that might not be easily digested by many.  Maybe a good compromise is the attached HTML (you might have to download it and point your browser to the static file).  That may allow you to pull out the sympy code I used to derive equ1 I presented earlier.  Those pure math type folks might find the latex-derived equation easier on the eyes.

Let me know how else I can make it easier for you (and others) to contribute.

RationalSoln-1.html

Kevin Pauba

unread,
Aug 6, 2022, 1:45:30 PM8/6/22
to sympy
Drat, when I downloaded the file Chrome treated it as text.  I did a "save as" to text to my drive and renamed the file to RationalSoln.html.  I then pointed my browser to the renamed file and it comes up as expected.  YMMV.

Chris Smith

unread,
Aug 6, 2022, 1:59:19 PM8/6/22
to sympy
I would state the problem as:

D is the intersection of Segment((0,0),(q1,0)), rotated ccw angle s about Point(0,q2), and the Line((q1,0),slope=m), where m < M and M is the slope of the line tangent to Circle((0,q2),q2) that passes through Point(q1,0). (If the line has a less negative slope then the segment will no longer intersect it.) What is the relationship between D and s in terms of q1 and q2?

/c

Kevin Pauba

unread,
Aug 6, 2022, 2:30:16 PM8/6/22
to sympy
brombo, using 2D conformal geometric algebra seems interesting ... just looked it up on The Google.  It does go beyond my current mathematical understanding but I'm willing to learn about it (although it might take more time than I have left!).

Kevin Pauba

unread,
Aug 6, 2022, 3:45:09 PM8/6/22
to sympy
smi...@gmail.com (or should I address you as /c?),

Your statement of the problem is similar to what I worked out for the "circular" solution using sine & cosine.  The equations I provided have q1 and q2 as quadrances (the squares of the distances) whereas your description treats them as distances (as did my circular solution).  The rational trigonometry solution is based strictly off of triangles with the quadrances of their sides and the spreads between them.

I understand that this is very convoluted when compared to the traditional trigonometry that most of us have used before.  Though it's probably more than most want to spend time with, https://web.maths.unsw.edu.au/~norman/papers/Chapter1.pdf describes a bit on why the author thinks the rational trigonometry has advantages.

Chris Smith

unread,
Aug 6, 2022, 4:44:07 PM8/6/22
to sympy
I read a few of the introductory papers and think this is a good problem to test its mettle! It might just be that some are better one way and some another. This one maybe facors the circular solution because the path of the segment doing the intersection follows a circular path. @sylee957 posted about the angle-graph method for working out relationships between lines here (https://github.com/sympy/sympy/issues/22644). That, too, shows an approach that, in the right situation, is very nice.

I recall the beauty of inversion when solving the "shoemaker's knife" problem -- I wonder if there is an inversion that would make this more tractable so the segment was not moving on a circle, but a line.

/c

Kevin Pauba

unread,
Aug 6, 2022, 5:26:36 PM8/6/22
to sympy
Oscar, I think I misstated something about rational trig that reinforces your statement that it avoids square roots.  Maybe there is some manipulation of the equations (similar in spirit to using sympy.unrad()) that would keep them in a form that doesn't require the square roots.  It would seem that sqrt would only be needed in the very last step when evaluating the quadrances when I want a final distance measure once solved ( d = sqrt(q1), for instance).

Kevin Pauba

unread,
Aug 6, 2022, 10:37:08 PM8/6/22
to sympy
Also, oscar, none of the radicals in the formulas are due to my getting the lengths.  All q* terms are quadrances ... no lengths are used in the formulas.  I should have said quadrance of DP (the square of the length of the segment DP) instead of just segment DP.  Once I have the values for q1, q2, q_dp and s, I'll convert them to length and angles for human consumption. 

Oscar Benjamin

unread,
Aug 7, 2022, 8:27:49 AM8/7/22
to sympy
The question is just if you can derive equations without radicals that
are simpler than those generated by unrad. The ones generated by unrad
are already too complicated because you have a pair of multivariate
quartics. Only if you are lucky could that lead to any explicit
analytic solution in radicals.

Note that if you choose numbers for some or all of the symbolic
parameters then the problem becomes much easier to solve. The main
thing that is difficult here is computing a solution in terms of
arbitrary parameters because the expression for that solution is
probably extremely complicated (and therefore unlikely to be of much
use anyway).

--
Oscar
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/da2d4dba-9b7a-478c-9454-755cf0b9f2cbn%40googlegroups.com.

Alan Bromborsky

unread,
Aug 8, 2022, 3:53:08 PM8/8/22
to sy...@googlegroups.com

It is not hard to write a closed form solution in terms of sine, cosine and the lengths of Q1, Q2, and the distance of the pivot point from the origin assuming the initial rotation angle is zero (makes it simpler).  Is there any reason to object to that solution letting sympy do the messy algebra.  I just takes a little vector algebra to set up the problem.

Kevin Pauba

unread,
Aug 8, 2022, 6:02:04 PM8/8/22
to sy...@googlegroups.com
Hello Alen, so nice of you to reply.

I have already worked out the solution using sin, cos and tangent.  I also used sympy to help solve the simultaneous equations and the solution was succinct.  I used rational trig as it seemed interesting and I had thought that sympy might help to solve the problem in an even simpler form.  As we have seen, that's not the case.

I'm now investigating the use of the parametric circle equation https://mathnow.wordpress.com/2009/11/06/a-rational-parameterization-of-the-unit-circle/.  It seems that this model fits the problem space more closely.

Again, thanks for the feedback and feel free to share anything else that comes to mind.

You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/vPE0dlZTqVM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/489ed715-994d-f2c7-3e87-bcf6307ab061%40gmail.com.

Kevin Pauba

unread,
Aug 8, 2022, 6:03:06 PM8/8/22
to sy...@googlegroups.com
Dang, didn't mean to misspell your name!

Kevin Pauba

unread,
Aug 8, 2022, 6:25:56 PM8/8/22
to sympy
Oscar,

I was hoping for a general solution (one that unfortunately seems out of reach).  I'm not at liberty to explain more of the nature of the application but I can probably say that determining q1 and q2 from two q_dp1 and q_dp2 is a calibration step for a mechanism.  The plan is commit a solution (probably not the rational trig solution discussed here) to code on a microcontroller.  Avoiding sin, cos and tan (and just use ratios and sqrt) would save code space and seemed (at first glance) to be elegant.

While I had hoped a simple change or rearrangement would make a simpler solution appear, I'm thankful for the math wizards of sympy to provide the valuable feedback. I do plan on asking the professor that has written a book on the subject to take a look and see if he agrees that the solution is not as straight-forward as I envisioned (or he may suggest an alternative approach).

Thanks, everyone, for the help!!

Reply all
Reply to author
Forward
0 new messages