Ask Your Question
0

how to convert a sage 1-row matrix into tuple ?

asked 2016-07-03 23:41:39 -0500

fagui gravatar image

import numpy as np

a=random_matrix(ZZ,1,5)

tuple(np.array(a).tolist()[0])

is there something more native ?

motivation: sagematrix,it seems, are not "hashable" and cannot be used as keys in lists to do groupby/countby

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2016-07-04 04:27:06 -0500

tmonteil gravatar image

You can do:

sage: a=random_matrix(ZZ,1,5)
sage: a
[-62   3   3   1   1]
sage: tuple(a.row(0))
(-62, 3, 3, 1, 1)
sage: hash(tuple(a.row(0)))
-3691849509262070773
edit flag offensive delete link more
2

answered 2016-07-04 10:44:56 -0500

nbruin gravatar image

updated 2016-07-04 16:10:26 -0500

Concerning your

motivation: sagematrix,it seems, are not "hashable" and cannot be used as keys in lists to do groupby/countby

I'd like to make a comment. Matrices in sage are, by default, mutable and hence (just like lists in python) not hashable. You can set a matrix to be immutable, and if its entries are hashable, the matrix itself will also be hashable.

sage: A=random_matrix(ZZ,1,5)
sage: hash(A)
TypeError: mutable matrices are unhashable
sage: A.set_immutable()
sage: hash(A)
-4

If you're OK with your matrices being immutable, this can save you having to copy the entries into tuples.

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: 2016-07-03 23:41:39 -0500

Seen: 66 times

Last updated: Jul 04 '16