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.
For more options, visit https://groups.google.com/d/optout.
math.nsimplify(math.simplify(math.N(math.transpose(face.get2DCoords()))), 1e-5)

CAUSES:
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)
/home/aespielberg/ppr2/ppr/svggen/api/proto_conversion3.pyc in <module>()
----> 1 
math.nsimplify(math.simplify(math.N(math.transpose(face.get2DCoords()))), 1e-5)

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/simplify/simplify.pyc
 in nsimplify(expr, constants, tolerance, full, rational)
   3837     expr = sympify(expr)
   3838     if rational or expr.free_symbols:
-> 3839         return _real_to_rational(expr, tolerance)
   3840 
   3841     # SymPy's default tolerance for Rationals is 15; other numbers may 
have

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/simplify/simplify.pyc
 in _real_to_rational(expr, tolerance)
   3792                     r = Integer(0)
   3793         reps[key] = r
-> 3794     return p.subs(reps, simultaneous=True)
   3795 
   3796 

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/core/basic.pyc
 in subs(self, *args, **kwargs)
    966             for old, new in sequence:
    967                 d = C.Dummy(commutative=new.is_commutative)
--> 968                 rv = rv._subs(old, d, **kwargs)
    969                 reps[d] = new
    970                 if not isinstance(rv, Basic):

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/core/cache.pyc
 in wrapper(*args, **kw_args)
     91         except (KeyError, TypeError):
     92             pass
---> 93         r = func(*args, **kw_args)
     94         try:
     95             func_cache_it_cache[k] = r

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/core/basic.pyc
 in _subs(self, old, new, **hints)
   1088         rv = self._eval_subs(old, new)
   1089         if rv is None:
-> 1090             rv = fallback(self, old, new)
   1091         return rv
   1092 

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/core/basic.pyc
 in fallback(self, old, new)
   1065                     args[i] = arg
   1066             if hit:
-> 1067                 rv = self.func(*args)
   1068                 hack2 = hints.get('hack2', False)
   1069                 if hack2 and self.is_Mul and not rv.is_Mul:  # 2-arg 
hack

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/matrices/immutable.pyc
 in __new__(cls, *args, **kwargs)
     46 
     47     def __new__(cls, *args, **kwargs):
---> 48         return cls._new(*args, **kwargs)
     49 
     50     @property

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/matrices/immutable.pyc
 in _new(cls, *args, **kwargs)
     39         if len(args) == 1 and isinstance(args[0], ImmutableMatrix):
     40             return args[0]
---> 41         rows, cols, flat_list = cls._handle_creation_inputs(*args, 
**kwargs)
     42         rows = Integer(rows)
     43         cols = Integer(cols)

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/matrices/matrices.pyc
 in _handle_creation_inputs(cls, *args, **kwargs)
    144         if len(args) == 3:
    145             rows = as_int(args[0])
--> 146             cols = as_int(args[1])
    147 
    148         # Matrix(2, 2, lambda i, j: i+j)

/usr/local/lib/python2.7/dist-packages/sympy-0.7.5-py2.7.egg/sympy/core/compatibility.pyc
 in as_int(n)
    388             raise TypeError
    389     except TypeError:
--> 390         raise ValueError('%s is not an integer' % n)
    391     return result
    392 

ValueError: -_Dummy_518 is not an integer

Reply via email to