Ask Your Question
1

maximum element of a matrix

asked 2013-07-10 22:44:26 -0600

updated 2015-01-13 14:27:59 -0600

FrédéricC gravatar image

How to determine the maximum element of a matrix? For "matrix_integer_dense" there is height() [1]. For numpy matrix there is max function. How to do it in Sage matrices with one single "max" like instruction ?

[1] http://www.sagemath.org/doc/reference...

[2] Alternetive: m.numpy().max()

edit retag flag offensive close merge delete

Comments

I do not think there is any... an other alternative would be `max(m.list())` which I do not like because it explicitely build the list of the entries of m.

vdelecroix gravatar imagevdelecroix ( 2013-07-10 23:48:48 -0600 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2013-07-11 11:55:33 -0600

tmonteil gravatar image

updated 2013-07-11 12:22:24 -0600

Not a single command, but you can try the following one-line possibilities:

sage: size = 1000
sage: m = random_matrix(RDF, size)  

sage: max(max(i for i in rows) for rows in m)
sage: max(m[i,j] for i in range(size) for j in range(size))
sage: max(m.list())
sage: max(m._list())

From the slowest to the fastest (5.19 s, 3.1 s, 2.12 s, 2.08 s).

edit flag offensive delete link more

Comments

You forget "m.numpy().max()" which is around 2ms and is definitely the best solution in that case. The reason is that a matrix over RDF is a wrapper to a numpy matrix.

vdelecroix gravatar imagevdelecroix ( 2013-07-12 01:24:33 -0600 )edit

You are right, and the bad timings of the privious methods could come from there too, here are the timings for a matrix over `RR`: sage: size = 1000 sage: m = random_matrix(RR, size) m.numpy().max() # 2.14 s max(max(i for i in rows) for rows in m) # 862 ms max(m[i,j] for i in range(size) for j in range(size)) # 414 ms max(m.list()) # 232 ms max(m._list()) # 137 ms

tmonteil gravatar imagetmonteil ( 2013-07-15 10:08:40 -0600 )edit

And over `ZZ`, from the slowest to the fastest: sage: m = random_matrix(ZZ, size) m.numpy().max() # 2.53 s max(max(i for i in rows) for rows in m) # 1.03 s max(m.list()) # 430 ms max(m[i,j] for i in range(size) for j in range(size)) # 365 ms max(m._list()) # 359 ms

tmonteil gravatar imagetmonteil ( 2013-07-15 10:20:15 -0600 )edit
0

answered 2013-07-11 01:42:41 -0600

M. H. M. gravatar image

updated 2013-07-11 01:45:34 -0600

I tried to do this a while ago but couldn't do it in one step. I just went through all the columns and got their maximum. I also needed to find the location of the maximum.

Example for a matrix M with type 'sage.matrix.matrix_real_double_dense.Matrix_real_double_dense'

MAX_val=0
for i in range(len(M.columns())):
  if (MAX_val< max(M.column(i))):
    MAX_val=max(M.column(i))

MAX_val is then the maximum value in the matrix. I hope it was helpful. I ll follow to see if there is a better way.

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

Stats

Asked: 2013-07-10 22:44:26 -0600

Seen: 850 times

Last updated: Jul 11 '13