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.