You can use Matrix.applyfunc to apply a function to all the elements of a 
matrix:

n [18]: M = Matrix([[(x + x)**2, (x + y)**2],
            [(y + x)**2, (y + y)**2]])

In [20]: M
Out[20]: 
⎡     2           2⎤
⎢  4⋅x     (x + y) ⎥
⎢                  ⎥
⎢       2       2  ⎥
⎣(x + y)     4⋅y   ⎦

In [21]: M.applyfunc(expand)
Out[21]: 
⎡        2                 2    2⎤
⎢     4⋅x         2⋅x⋅y + x  + y ⎥
⎢                                ⎥
⎢         2    2          2      ⎥
⎣2⋅x⋅y + x  + y        4⋅y       ⎦

In [22]: M
Out[22]: 
⎡     2           2⎤
⎢  4⋅x     (x + y) ⎥
⎢                  ⎥
⎢       2       2  ⎥
⎣(x + y)     4⋅y   ⎦

Notice that even though this is a function, it does not modify the Matrix in 
place (it would be nice to have an option to do this, though).  

If the trig substitution you are doing is based on the identity sin(x)**2 + 
cos(x)**2 == 1, try using factor() on the expression before passing it to 
trigsimp().  This will only work in the git master, with the new polys.  We 
need to update trigsimp() to do this automatically.  

In [24]: expand((sin(x)**2 + cos(x)**2)**2)
Out[24]: 
     2       2         4         4   
2⋅cos (x)⋅sin (x) + cos (x) + sin (x)

In [25]: trigsimp(2*cos(x)**2*sin(x)**2 + cos(x)**4 + sin(x)**4)
Out[25]: 
     2         4         4   
2⋅cos (x) + sin (x) - cos (x)

In [26]: trigsimp(factor(2*cos(x)**2*sin(x)**2 + cos(x)**4 + sin(x)**4))
Out[26]: 1

If it is based on other identities, such as half-angle identities, you will 
probably have to do the simplification manually using .subs() because other 
types of trig simplification are not yet implemented (expand(expr, trig=True) 
is an exception).  You can use something like M.applyfunc(lambda i: i.subs(old, 
new)).

Finally, Jezreel (int3 on IRC) has been working recently on the same paper (Fu, 
et. al.), but I don't know if he has made any progress.  I have CC'd him too.  

Aaron Meurer
On Jun 25, 2010, at 11:40 AM, Ondrej Certik wrote:

> Hi Viswanath,
> 
> On Fri, Jun 25, 2010 at 3:07 AM, morovia morovia
> <[email protected]> wrote:
>> Hi Bastian,
>> 
>>    Thanks for your reply.
>> 
>>    I would like to have the final function
>> in a symbolic way and later do evalf.  But, the
>> trigsimp with deep and recursive options set
>> to true, did not yield the simplified form.
>> 
>> I am not sure how the trigonometry relations
>> are handled.
>> 
>> Earlier, I have tried with inverse_ADJ() which
>> keeps it slow, but with only inv it is faster.
> 
> Can you post here the script that generates the trig expressions that
> you  need to simplify? We started to write a better trig solver, but
> it's not finished yet. I CCed Luke, who has been working on it.
> 
> Ondrej
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to