First time here? Check out the FAQ!

Ask Your Question
2

How to use the output matrix as an input in Sage

asked 3 years ago

dasdipayan9038 gravatar image

updated 3 years ago

Suppose [1 2 3] [4 5 6] [5 6 7] is a matrix I get as an output in a file. How can I use the matrix as an input to a different code? It seems the matrix output doesn't have the same input structure in Sagemath.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 3 years ago

You can try the sage_input function:

sage: m = matrix(QQ, [[1,2,3], [4,5,6], [7,8,9]])
sage: m
[1 2 3]
[4 5 6]
[7 8 9]
sage: sage_input(m)
matrix(QQ, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Save the last line to a file instead of just

[1 2 3]
[4 5 6]
[7 8 9]

The sage_input function is documented here

Preview: (hide)
link
2

answered 3 years ago

Max Alekseyev gravatar image

updated 3 years ago

For example, you can parse such matrix printout as

s = '[1 2 3] [4 5 6] [5 6 7]'
M = Matrix(QQ, eval('['+s.replace(' ',',')+']') )
print(M)
Preview: (hide)
link

Comments

Thank you very much. But how should I use it from a file?

dasdipayan9038 gravatar imagedasdipayan9038 ( 3 years ago )

load file's content to a string and proceed as above.

Max Alekseyev gravatar imageMax Alekseyev ( 3 years ago )

For a large matrix (say of order 1000 and elements in range(2^15)), this space can be tricky. For instance, there are non-uniform spaces. Thus it might be a little difficult just to use replace space with a comma to define the matrix. Is there any way [2 4 6] can be treated as a vector [2,4,6] in sage?

dasdipayan9038 gravatar imagedasdipayan9038 ( 3 years ago )

It's easy to make spacing uniform by replacing two spaces with one until all such paired spaces are gone.

Max Alekseyev gravatar imageMax Alekseyev ( 3 years ago )

Thanks. I could able to do the uniformity of spacing using notepad.

dasdipayan9038 gravatar imagedasdipayan9038 ( 3 years ago )

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

Seen: 617 times

Last updated: Nov 09 '21