Ask Your Question
6

Is there a way to simplify_full and trig_reduce a matrix?

asked 2010-11-25 17:54:20 +0200

Shashank gravatar image

updated 2010-11-28 14:27:26 +0200

niles gravatar image

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 flag offensive close merge delete

Comments

kcrisman gravatar imagekcrisman ( 2011-01-03 22:01:50 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted
6

answered 2010-11-25 21:25:22 +0200

Mike Hansen gravatar image

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]
edit flag offensive delete link more

Comments

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

cswiercz gravatar imagecswiercz ( 2010-11-30 16:33:47 +0200 )edit

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.

Shashank gravatar imageShashank ( 2010-12-03 15:26:56 +0200 )edit
1

answered 2010-12-01 11:15:57 +0200

tcfisher gravatar image

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.

edit flag offensive delete link more
0

answered 2011-05-16 16:21:54 +0200

kcrisman gravatar image

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]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2010-11-25 17:54:20 +0200

Seen: 2,866 times

Last updated: May 16 '11