nsimplify tries to convert floating point numbers into exact numbers, like sqrt(2) or pi + 1. The tolerance flag specifies how close the exact number needs to be to the floating point number. For example:
In [8]: nsimplify(pi.evalf(), tolerance=0.1) Out[8]: 22/7 In [9]: nsimplify(pi.evalf(), tolerance=0.01) Out[9]: 22/7 In [10]: nsimplify(pi.evalf(), tolerance=0.001) Out[10]: 355 ─── 113 In [11]: nsimplify(pi.evalf(), tolerance=0.0001) Out[11]: 355 ─── 113 Aaron Meurer On Mon, Nov 10, 2014 at 3:52 PM, Andrew Spielberg <[email protected]> wrote: > Thanks a ton - yes, using N and then specifying a tolerance with N worked. > > Out of curiosity then, what is the point of the nsimplify tolerance? What > does that do? I appear to be misunderstanding it. > > -Andy S. > > On Sunday, November 9, 2014 8:38:00 PM UTC-5, Aaron Meurer wrote: >> >> I think you want the chop option, like expr.eval(chop=True) or N(expr, >> chop=True). >> >> Aaron Meurer >> >> On Thu, Nov 6, 2014 at 9:52 PM, Andrew Spielberg <[email protected]> >> wrote: >> >>> Hi all, >>> >>> Recent adopter, first time caller, bear with me if I have some newbish >>> misunderstandings about sympy. >>> >>> I am working on a project where we are trying to use sympy to do a lot >>> of symbolic geometric computation. The project predates sympy, so I cannot >>> really change too much code to accomodate it. >>> >>> Our calculations create matrices, and sometimes, the expressions can >>> seem large, like the one seen below. I know that this is actually supposed >>> to be a linear expression, and I need to get it into a linear form. >>> >>> *Matrix([* >>> *[ >>> 1.0*leg1.beamwidth - (2.05432527401305e-33*leg1.beamwidth**3 + >>> 0.166666666666667*leg1.beamwidth**2*total_length + >>> 0.166666666666667*leg1.beamwidth**2*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + total_length/2)), >>> >>> 1.0*leg1.beamwidth - (2.05432527401305e-33*leg1.beamwidth**3 + >>> 0.166666666666667*leg1.beamwidth**2*total_length + >>> 0.166666666666667*leg1.beamwidth**2*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + total_length/2)), >>> 1.0*leg1.beamwidth - >>> (2.05432527401305e-33*leg1.beamwidth**3 + >>> 0.166666666666667*leg1.beamwidth**2*total_length + >>> 0.166666666666667*leg1.beamwidth**2*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + total_length/2)), >>> 1.0*leg1.beamwidth - >>> (2.05432527401305e-33*leg1.beamwidth**3 + >>> 0.166666666666667*leg1.beamwidth**2*total_length + >>> 0.166666666666667*leg1.beamwidth**2*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2))],* >>> *[-6.12323399573677e-17*leg1.beamwidth - >>> (-2.53215139886928e-65*leg1.beamwidth**3 + >>> 0.0416666666666667*leg1.beamwidth*total_length**2 + >>> 0.166666666666667*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2)*(1.23259516440783e-32*leg1.beamwidth + >>> total_length))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + total_length/2)), >>> 6.12323399573677e-17*leg1.beamwidth + total_length/2 - >>> (-2.53215139886928e-65*leg1.beamwidth**3 + >>> 0.0416666666666667*leg1.beamwidth*total_length**2 + >>> 0.166666666666667*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2)*(1.23259516440783e-32*leg1.beamwidth + >>> total_length))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + total_length/2)), >>> total_length/2 - (-2.53215139886928e-65*leg1.beamwidth**3 + >>> 0.0416666666666667*leg1.beamwidth*total_length**2 + >>> 0.166666666666667*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2)*(1.23259516440783e-32*leg1.beamwidth + >>> total_length))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + total_length/2)), >>> -(-2.53215139886928e-65*leg1.beamwidth**3 + >>> 0.0416666666666667*leg1.beamwidth*total_length**2 + >>> 0.166666666666667*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2)*(1.23259516440783e-32*leg1.beamwidth + >>> total_length))/(6.16297582203915e-33*leg1.beamwidth**2 + >>> 0.25*leg1.beamwidth*total_length + >>> 0.5*leg1.beamwidth*(1.23259516440783e-32*leg1.beamwidth + >>> total_length/2))],* >>> *[ >>> >>> >>> >>> >>> -1.0*leg1.beamwidth, >>> >>> >>> >>> >>> >>> -1.0*leg1.beamwidth, >>> >>> >>> >>> >>> 0, >>> >>> >>> >>> >>> >>> 0]])* >>> >>> If I perform nsimplify on this with a tolerance = 1e-5, I get something >>> reasonable: >>> >>> *Matrix([* >>> *[ >>> leg1.beamwidth/2, >>> leg1.beamwidth/2, leg1.beamwidth/2, leg1.beamwidth/2],* >>> *[-612323399573677*leg1.beamwidth/10000000000000000000000000000000 - >>> total_length/4, >>> 612323399573677*leg1.beamwidth/10000000000000000000000000000000 + >>> total_length/4, total_length/4, -total_length/4],* >>> *[ >>> -leg1.beamwidth, >>> -leg1.beamwidth, 0, 0]])* >>> >>> But I can't simplify it further for some reason. Even though >>> *-612323399573677*leg1.beamwidth/10000000000000000000000000000000 >>> = -6e-17, *I can't seem to make that term disappear. >>> >>> This was a simple case, but in more complex cases, sympy fails to cancel >>> out terms and the solution becomes highly nonlinear. As an example, this >>> won't simplify with nsimplify for some reason, even though certain terms >>> clearly dominate others: >>> >>> *(leg1.beamwidth*total_length*(24492935982947*leg2.beamwidth/200000000000000000000000000000 >>> + total_length/2)**2/2 - >>> 306161699786839*leg2.beamwidth*total_length**2*(24492935982947*leg2.beamwidth/200000000000000000000000000000 >>> + total_length/2)/10000000000000000000000000000000 + >>> 153080849893419*total_length**3*(24492935982947*leg2.beamwidth/200000000000000000000000000000 >>> + >>> total_length/2)/10000000000000000000000000000000)/(total_length*(24492935982947*leg2.beamwidth/200000000000000000000000000000 >>> + total_length/2)**2)* >>> >>> Lastly, things sometimes get worse. If I try to play around with >>> simplify, N, and nsimplify to try to "force" it to simplify things, I >>> sometimes get the error attached. >>> >>> Is there a proper way to get do the simplifications I need? And, why >>> would I get that value error I displayed attached. >>> >>> Thanks for any and all help, >>> Andy S. >>> >>> -- >>> 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 [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/sympy. >>> To view this discussion on the web visit https://groups.google.com/d/ >>> msgid/sympy/c94c3d7f-623e-4a3c-9ff4-f19515598a2b%40googlegroups.com >>> <https://groups.google.com/d/msgid/sympy/c94c3d7f-623e-4a3c-9ff4-f19515598a2b%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/d8685eaf-c936-4c6e-8a3d-ea8c101e8245%40googlegroups.com > <https://groups.google.com/d/msgid/sympy/d8685eaf-c936-4c6e-8a3d-ea8c101e8245%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KHSA7h%3DC_cT_oC93h9JhqcnCiD3WhXweZ7Ntq7fvNFiw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
