# 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

edit retag close merge delete

Sort by » oldest newest most voted You 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]

more

The apply_map method is a great trick! Thanks for sharing.

That 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.

I 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.

more

The 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]

more