Ask Your Question
0

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

asked 8 years ago

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

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 8 years ago

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

answered 8 years ago

nbruin gravatar image

updated 8 years ago

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.

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

Seen: 1,526 times

Last updated: Jul 04 '16