ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 16 May 2011 09:21:54 -0500Is there a way to simplify_full and trig_reduce a matrix?https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/I know I can do it component by component and then construct a matrix out of the output. But it would be nice if I could just say matrix.trig_reduce() and get a matrix with all the components trig_reduced.
Thanks in advance
Thu, 25 Nov 2010 10:54:20 -0600https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/Comment by kcrisman for <p>I know I can do it component by component and then construct a matrix out of the output. But it would be nice if I could just say matrix.trig_reduce() and get a matrix with all the components trig_reduced.
Thanks in advance</p>
https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?comment=22344#post-id-22344See also http://trac.sagemath.org/sage_trac/ticket/10552Mon, 03 Jan 2011 15:01:50 -0600https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?comment=22344#post-id-22344Answer by Mike Hansen for <p>I know I can do it component by component and then construct a matrix out of the output. But it would be nice if I could just say matrix.trig_reduce() and get a matrix with all the components trig_reduced.
Thanks in advance</p>
https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?answer=11790#post-id-11790You can use the `apply_map` method which applies a function to each of the elements of the matrix. For example,
sage: m = matrix([[sin(x), cos(x)], [sin(x), cos(x)]]); m
[sin(x) cos(x)]
[sin(x) cos(x)]
sage: o = m*m.transpose(); o
[sin(x)^2 + cos(x)^2 sin(x)^2 + cos(x)^2]
[sin(x)^2 + cos(x)^2 sin(x)^2 + cos(x)^2]
sage: o.apply_map(lambda x: x.trig_reduce())
[1 1]
[1 1]
You could equivalently use `attrcall`
sage: o.apply_map(attrcall('trig_reduce'))
[1 1]
[1 1]
Thu, 25 Nov 2010 14:25:22 -0600https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?answer=11790#post-id-11790Comment by cswiercz for <p>You can use the <code>apply_map</code> method which applies a function to each of the elements of the matrix. For example,</p>
<pre><code>sage: m = matrix([[sin(x), cos(x)], [sin(x), cos(x)]]); m
[sin(x) cos(x)]
[sin(x) cos(x)]
sage: o = m*m.transpose(); o
[sin(x)^2 + cos(x)^2 sin(x)^2 + cos(x)^2]
[sin(x)^2 + cos(x)^2 sin(x)^2 + cos(x)^2]
sage: o.apply_map(lambda x: x.trig_reduce())
[1 1]
[1 1]
</code></pre>
<p>You could equivalently use <code>attrcall</code></p>
<pre><code>sage: o.apply_map(attrcall('trig_reduce'))
[1 1]
[1 1]
</code></pre>
https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?comment=22468#post-id-22468The apply_map method is a great trick! Thanks for sharing.Tue, 30 Nov 2010 09:33:47 -0600https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?comment=22468#post-id-22468Comment by Shashank for <p>You can use the <code>apply_map</code> method which applies a function to each of the elements of the matrix. For example,</p>
<pre><code>sage: m = matrix([[sin(x), cos(x)], [sin(x), cos(x)]]); m
[sin(x) cos(x)]
[sin(x) cos(x)]
sage: o = m*m.transpose(); o
[sin(x)^2 + cos(x)^2 sin(x)^2 + cos(x)^2]
[sin(x)^2 + cos(x)^2 sin(x)^2 + cos(x)^2]
sage: o.apply_map(lambda x: x.trig_reduce())
[1 1]
[1 1]
</code></pre>
<p>You could equivalently use <code>attrcall</code></p>
<pre><code>sage: o.apply_map(attrcall('trig_reduce'))
[1 1]
[1 1]
</code></pre>
https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?comment=22458#post-id-22458That solves my problem I have a small function written which does, but would it be very difficult to define trig_reduce(), full_simplify() etc for matrices in sage. I don't know of any sage webpage for feature requests. But this is a feature that would make things a lot easier and user friendly.Fri, 03 Dec 2010 08:26:56 -0600https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?comment=22458#post-id-22458Answer by kcrisman for <p>I know I can do it component by component and then construct a matrix out of the output. But it would be nice if I could just say matrix.trig_reduce() and get a matrix with all the components trig_reduced.
Thanks in advance</p>
https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?answer=12354#post-id-12354The trig one is possible, the other one is in the ticket. For posterity:
As it turns out, we already have it, just not with all possible aliases! I didn't even know this.
sage: m = matrix([[sin(x), cos(x)], [sin(x), cos(x)]]); m
[sin(x) cos(x)]
[sin(x) cos(x)]
sage: o = m*m.transpose()
sage: o.simplify_trig()
[1 1]
[1 1]Mon, 16 May 2011 09:21:54 -0500https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?answer=12354#post-id-12354Answer by tcfisher for <p>I know I can do it component by component and then construct a matrix out of the output. But it would be nice if I could just say matrix.trig_reduce() and get a matrix with all the components trig_reduced.
Thanks in advance</p>
https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?answer=11807#post-id-11807I have to do this often, so I wrote a function:
def matrix_full_simplify(mat,m,n):
matsimp=mat;
for i in range(m):
for j in range(n):
matsimp[i,j]=mat[i,j].full_simplify();
return matsimp;
Use:
amat = matrix_full_simplify(amat)
You could easily adapt this to be better or require fewer arguments and can create a similar function for vectors. Wed, 01 Dec 2010 04:15:57 -0600https://ask.sagemath.org/question/7773/is-there-a-way-to-simplify_full-and-trig_reduce-a-matrix/?answer=11807#post-id-11807