Ask Your Question
1

sum of elements by row or column in a matrix in Sage

asked 2016-06-26 10:19:07 +0100

fagui gravatar image

in Numpy, we can use sum(M,axis=0) or sum(M,axis=1)

what is the equivalent instruction syntax with sage ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-06-26 11:32:05 +0100

tmonteil gravatar image

updated 2016-06-26 12:16:41 +0100

For summing rows:

sage: M = random_matrix(ZZ,3,5)
sage: M
[ -2   1 -10  -1  -1]
[ -2   1  -1   1  -1]
[ -3   5  -2   1   2]

sage: sum(M)
(-7, 7, -13, 1, 0)

For the columns:

sage: sum(M.columns())
(-13, -2, 3)

or

sage: sum(M.T)
(-13, -2, 3)
edit flag offensive delete link more

Comments

thank you !!!

fagui gravatar imagefagui ( 2016-06-26 13:34:40 +0100 )edit

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: 2016-06-26 10:19:07 +0100

Seen: 3,746 times

Last updated: Jun 26 '16