Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4987

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>reading files</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_getting_spinput_spfrom_spthe_spuser.html">next</a> | <a href="_printing_spto_spthe_spscreen.html">previous</a> | <a href="_getting_spinput_spfrom_spthe_spuser.html">forward</a> | <a href="_printing_spto_spthe_spscreen.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_reading_spfiles.html" title="">reading files</a></div>
<hr/>
<div><h1>reading files</h1>
<div>Sometimes a file will contain a single expression whose value you wish to have access to.  For example, it might be a polynomial produced by another program.  The function <a href="_get.html" title="get the contents of a file">get</a> can be used to obtain the entire contents of a file as a single string.  We illustrate this here with a file whose name is <tt>expression</tt>.<p/>
First we create the file by writing the desired text to it.<table class="examples"><tr><td><pre>i1 : fn = temporaryFileName()

o1 = /tmp/M2-13413-1</pre>
</td></tr>
<tr><td><pre>i2 : fn &lt;&lt; "z^6+3*x*z^4+6*y*z^4+3*x^2*z^2+12*x*y*z^2+12*y^2*z^2+x^3+6*x^2*y+12*x*y^2+8*y^3" &lt;&lt; endl &lt;&lt; close

o2 = /tmp/M2-13413-1

o2 : File</pre>
</td></tr>
</table>
Now we get the contents of the file, as a single string.<table class="examples"><tr><td><pre>i3 : get fn

o3 = z^6+3*x*z^4+6*y*z^4+3*x^2*z^2+12*x*y*z^2+12*y^2*z^2+x^3+6*x^2*y+12*x*y^2
     +8*y^3</pre>
</td></tr>
</table>
This isn't quite what you want, because a string containing a description of a polynomial is not the same as a polynomial.  We may use <a href="_value.html" title="evaluate">value</a> to evaluate the string and produce the corresponding polynomial, after first setting up a polynomial ring to contain it.<table class="examples"><tr><td><pre>i4 : R = ZZ/101[x,y,z]

o4 = R

o4 : PolynomialRing</pre>
</td></tr>
<tr><td><pre>i5 : f = value get fn

      6       4       4     2 2          2      2 2    3     2         2  
o5 = z  + 3x*z  + 6y*z  + 3x z  + 12x*y*z  + 12y z  + x  + 6x y + 12x*y  +
     ------------------------------------------------------------------------
       3
     8y

o5 : R</pre>
</td></tr>
<tr><td><pre>i6 : factor f

       2          3
o6 = (z  + x + 2y)

o6 : Expression of class Product</pre>
</td></tr>
</table>
Often a file will contain code written in the Macaulay2 language.  Let's create such a file.<table class="examples"><tr><td><pre>i7 : fn &lt;&lt; "sample = 2^100
     print sample
     " &lt;&lt; close

o7 = /tmp/M2-13413-1

o7 : File</pre>
</td></tr>
</table>
Now verify that it contains the desired text with <a href="_get.html" title="get the contents of a file">get</a>.<table class="examples"><tr><td><pre>i8 : get fn

o8 = sample = 2^100
     print sample</pre>
</td></tr>
</table>
To load and execute that code, use <a href="_load.html" title="read Macaulay2 commands">load</a>.  This is the function you will use most often for using bits of code you have previously written, unless you have incorporated them into a package, in which case you would use <a href="_load__Package.html" title="load a package">loadPackage</a>.<table class="examples"><tr><td><pre>i9 : load fn
1267650600228229401496703205376</pre>
</td></tr>
</table>
The command <a href="_needs.html" title="read Macaulay2 commands if necessary">needs</a> can be used to load a file only if it hasn't already been loaded.<table class="examples"><tr><td><pre>i10 : needs fn</pre>
</td></tr>
</table>
For debugging or display purposes, it is sometimes useful to be able to simulate entering the lines of a file one by one, so they appear on the screen along with prompts and output lines.  We use <a href="_input.html" title="read Macaulay2 commands and echo">input</a> for this.<table class="examples"><tr><td><pre>i11 : input fn</pre>
</td></tr>
</table>
There are other ways to manipulate the contents of a file with multiple lines.  First, let's use <a href="_peek.html" title="examine contents of an object">peek</a> to observe the extent of this string returned by <a href="_get.html" title="get the contents of a file">get</a>.<table class="examples"><tr><td><pre>ii12 : sample = 2^100

oo12 = 1267650600228229401496703205376</pre>
</td></tr>
</table>
The resulting string has newlines in it; we can use <a href="_lines.html" title="split a string into lines">lines</a> to break it apart into a list of strings, with one row in each string.<table class="examples"><tr><td><pre>ii13 : print sample
1267650600228229401496703205376</pre>
</td></tr>
</table>
We may use <a href="_peek.html" title="examine contents of an object">peek</a> to observe the extent of these strings.<table class="examples"><tr><td><pre>ii14 : </pre>
</td></tr>
</table>
We could even use <a href="_stack.html" title="join nets or string vertically">stack</a> to assemble the lines of the file into a net.<table class="examples"><tr><td><pre>i15 : peek get fn

o15 = "sample = 2^100
      print sample
      "</pre>
</td></tr>
</table>
Now let's remove that file.<table class="examples"><tr><td><pre>i16 : lines get fn

o16 = {sample = 2^100, print sample}

o16 : List</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>