Ask Your Question
0

How to get rid of the Error Message for calling a text file in SageMath

asked 2019-03-15 08:19:27 +0100

Captcha gravatar image

I want to input a matrix from a text file and run it in SageMath. The name of the file is Matrix.It is located in the D-drive and it contains a square matrix of order 4.

I changed the the name of the file from Matrix.txt to Matrix.sage. I then wrote load('Matrix.sage');

But I got an error message which read

Traceback (click to the left of this block for traceback) ... IOError: did not find file 'Matrix.sage' to load or attach

Is it possible for Sage-Math to take input a matrix which is stored in a Text File? Can someone kindly comment?

Note:I want to input the matrix from the text file and find its determinant in SageMath.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-03-16 01:09:39 +0100

dan_fulea gravatar image

This is a linux box answering. First of all, how does the matrix look like? Is it a piece of code? Is it a csv type?

In my case, the following worked. I stored in a file named Matrix.sage in the folder \home\dan\temp the following data:

1234 928374 1412341 123412431
443313341 13 131134 3414315634
10945203452 2341 122 74532423
9345234 7834192 10234123 99

To make things simple (well, for me complicated), i will not use / import csv and take advantage of the offered functionality.

Then the following code parses the lines of the data file line by line, splits the entries w.r.t. the blank (my choice of a delimiter), puts each row obtained in this way in a list, and appends the rows one by one to the variable rows, used to initialize the matrix A in the next line.

stream = open( r'/home/dan/temp/Matrix.sage', 'r' )
rows = []
for line in stream.readlines():
    rows.append( [ ZZ(entry) for entry in line.strip().split(' ')] )
A = matrix( ZZ, 4, 4, rows )
print( 'The matrix A is as follows:\n{!r}'.format(A) )
print( '\n\ndet(A) = {} = {}'.format(A.det(), A.det().factor()) )

This gives:

The matrix A is as follows:
[       1234      928374     1412341   123412431]
[  443313341          13      131134  3414315634]
[10945203452        2341         122    74532423]
[    9345234     7834192    10234123          99]


det(A) = 56986363083817785658295994231136 = 2^5 * 7 * 61 * 1136790418207 * 3668704083691007

On a Win* box try instead the full path to the Matrix.sage file, for instance

stream = open( r'C:\temp\Matrix.sage', 'r' )

to open for read a presumably existing file that lives under C:\temp.

edit flag offensive delete link more

Comments

The advice for locating the file is all very sound, but in Python, you're much better off using something like csv.reader to read csv files, rather than to parse them yourself. Funny variations can arise and they are much better detected and dealt with by library code.

nbruin gravatar imagenbruin ( 2019-03-17 19:30:28 +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: 2019-03-15 08:19:27 +0100

Seen: 288 times

Last updated: Mar 16 '19