ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 17 Mar 2019 13:30:28 -0500How to get rid of the Error Message for calling a text file in SageMathhttp://ask.sagemath.org/question/45783/how-to-get-rid-of-the-error-message-for-calling-a-text-file-in-sagemath/ 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.
Fri, 15 Mar 2019 02:19:27 -0500http://ask.sagemath.org/question/45783/how-to-get-rid-of-the-error-message-for-calling-a-text-file-in-sagemath/Answer by dan_fulea for <p>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.</p>
<p>I changed the the name of the file from Matrix.txt to Matrix.sage.
I then wrote load('Matrix.sage');</p>
<p>But I got an error message which read </p>
<blockquote>
<p>Traceback (click to the left of this block for traceback)
...
IOError: did not find file 'Matrix.sage' to load or attach</p>
</blockquote>
<p>Is it possible for Sage-Math to take input a matrix which is stored in a Text File?
Can someone kindly comment?</p>
<p><strong>Note</strong>:I want to input the matrix from the text file and find its determinant in SageMath.</p>
http://ask.sagemath.org/question/45783/how-to-get-rid-of-the-error-message-for-calling-a-text-file-in-sagemath/?answer=45789#post-id-45789This 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 `r`ead a presumably existing file that lives under `C:\temp`.
Fri, 15 Mar 2019 19:09:39 -0500http://ask.sagemath.org/question/45783/how-to-get-rid-of-the-error-message-for-calling-a-text-file-in-sagemath/?answer=45789#post-id-45789Comment by nbruin for <p>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?</p>
<p>In my case, the following worked.
I stored in a file named <code>Matrix.sage</code> in the folder <code>\home\dan\temp</code> the following data:</p>
<pre><code>1234 928374 1412341 123412431
443313341 13 131134 3414315634
10945203452 2341 122 74532423
9345234 7834192 10234123 99
</code></pre>
<p>To make things simple (well, for me complicated), i will not use / import <code>csv</code> and take advantage of the offered functionality.</p>
<p>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 <code>rows</code>, used to initialize the matrix <code>A</code> in the next line.</p>
<pre><code>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()) )
</code></pre>
<p>This gives:</p>
<pre><code>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
</code></pre>
<p>On a Win* box try instead the full path to the <code>Matrix.sage</code> file, for instance</p>
<pre><code>stream = open( r'C:\temp\Matrix.sage', 'r' )
</code></pre>
<p>to open for <code>r</code>ead a presumably existing file that lives under <code>C:\temp</code>.</p>
http://ask.sagemath.org/question/45783/how-to-get-rid-of-the-error-message-for-calling-a-text-file-in-sagemath/?comment=45809#post-id-45809The 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.Sun, 17 Mar 2019 13:30:28 -0500http://ask.sagemath.org/question/45783/how-to-get-rid-of-the-error-message-for-calling-a-text-file-in-sagemath/?comment=45809#post-id-45809