First time here? Check out the FAQ!

Ask Your Question
6

Is there a way to simplify_full and trig_reduce a matrix?

asked 14 years ago

Shashank gravatar image

updated 14 years ago

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

Preview: (hide)

Comments

3 Answers

Sort by » oldest newest most voted
6

answered 14 years ago

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]
Preview: (hide)
link

Comments

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

cswiercz gravatar imagecswiercz ( 14 years ago )

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 ( 14 years ago )
1

answered 14 years ago

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.

Preview: (hide)
link
0

answered 13 years ago

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]
Preview: (hide)
link

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: 14 years ago

Seen: 3,180 times

Last updated: May 16 '11