System Operations

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

AbsoluteTime  
CForm  
Character  
Characters  
Copy  
Date  
DeleteFile  
Directory  
Encode  
File`append  
File`display  
File`output  
File`reading  
FileDate  
FileNames  
Files`finding  
Files`manipulating  
Files`reading`data  
Files`splicing  
Files`writing  
FindList    
FortranForm  
FullForm  
Get  
InputForm  
MatrixForm  
ReadList    
ReNameFile  
Save  
SessionTime  
SetDirectory  
Short  
Sort  
Splice  
StringDrop  
StringInsert  
StringJoin  
StringLength  
StringPosition  
StringReplace  
StringReverse  
Strings`manipulating  
Strings`reading  
Strings`searching  
StringTake  
TableForm  
TeXForm  
TimeUsed  
TimeZone  
Timing  
TreeForm  
Word

Special Output Forms

Syntax (see pages 182 and 343)

CForm[expr ] writes out expr  in a form that can be used in a C program.

FortranForm[expr ]writes out expr  for Fortran.

TeXForm[expr ] writes out expr  in TeX input form.

FullForm[expr ]  gives the explicit form of expr  in functional notation.

TreeForm[expr ] gives a representation of the expression tree for expr.

InputForm[expr ] gives a one-dimensional form suitable for input to Mathematica.

Short[expr ] shows a one-line outline of expr.

TableForm[list ] prints the list in tabular form.

MatrixForm[list ] prints the list in matrix form.

Using TeXForm

TeXForm[ x^5 / 7 + y^3 - Log[y] ]

Using CForm

CForm[ x^5 / 7 + y^3 - Log[y] ]

Using FortranForm

FortranForm[ x^5 / 7 + y^3 - Log[y] ]

Using FullForm

FullForm[ x^2 / 3 + 1 / y^2 ]

Using TreeForm

TreeForm[ x^2 / 3 + 1 / y^2 ]

A Note on FullForm and TreeForm

In most output formats, the way Mathematica  prints a particular expression often bears little relation to the way in which it stores that expression internally. For most purposes, you never need to know exactly how an expression is stored.  But if you want to manipulate the structure of an expression, particularly as part of a progam, you may need to see what its internal form is.  You can do this using an output format such as FullForm or TreeForm.

Using InputForm

InputForm allows you to generate a textual form for any Mathematica  expression, suitable for editing or other kinds of textual manipulation. Note that in a Mathematica  notebook you can typically switch between output form and input form versions of a particular cell by choosing the "formatted" and "unformatted" options for the cell.

x^4 / 4 + 1 / y^2

InputForm[%]

Using Short

The second input shows how to use Short in a "postfix form".

Short[ Expand[(1 + x + y)^12]]

Expand[(1 + x + y)^12] // Short

Using TableForm

tab = Table[(i + 45)^j, {i, 3}, {j, 3}];

TableForm[tab]

Using MatrixForm

MatrixForm displays the list as a matrix, in which each "cell" has the same size.

tab = Table[(i + 45)^j, {i, 3}, {j, 3}];

MatrixForm[tab]

Working with External Files

Note: Some of these examples are for demonstration purposes only; they are machine-specific, and will yield different output (or none) on your computer.

Reading and Writing Mathematica  Files

Syntax (see pages 178 and 477)

<<filename    reads in a plain text file of Mathematica  input.

expr  >> filename    outputs expr  to a file as plain text.

expr  >>> filename    appends expr  to a file.

!!filename    displays the contents of a plain text file.

Save["filename ", f, g, ...]   saves definitions for variables or functions in a file.

Encode["source ","dest "]   writes an encoded version of the file source to the file dest .

<<dest   reads in an encoded file.

Encode["source ","dest ","key "]   encodes with a specific key.

Get["dest ", "key "]  reads in a file that was encoded with a key.

Reading, Displaying  and Writing to Files

Expand[ (x + y)^3 ] >> tmp

!!tmp

<<tmp

Appending to a File

Expand[ (x + y) ^ 4] >>> tmp

!!tmp

Input from a File

It is sometimes useful to prepare input in an external file using a text editor and then read in the file. The first line shows the contents of the file gdef and the second line inputs this file into Mathematica.

!!gdef

<<gdef

g[.23]

Saving Definitions

Note that Mathematica  automatically saves both the actual definition of f, and the definition of c on which it depends.  In a later session, you can reinstate the definitions you saved simply by reading in the file fdef.

f[x_] := x^2 + c

c = 17

Save["fdef", f]

Clear[f]

!!fdef

<<fdef

f[3]

Encoding a File

Factor[x^2 - 1] >> tmp2

Encode["tmp2", "tmp2.x"]

!!tmp2.x

<<tmp2.x

Encoding a File Using a Key

Encode["tmp2", "tmp3.x", "secret"]

!!tmp3.x

Get["tmp3.x", "secret"]

Finding and Manipulating Files

Syntax (see pages 180, 489, 492 and 500)

Directory[ ] gives your current working directory.

SetDirectory["dir "] sets your current working directory.

FileNames[ ] lists the files in your current working directory.

FileNames["form "] lists all files in your current working directory whose names match the string pattern form.

Copy["file1 ", "file2 "] copies file1  to file2 .

ReNameFile["file1 ", "file2 "] gives file1  the name file2 .

DeleteFile["file "] deletes a file.

FileDate["file "] gives the last modification date for a file.

FindList["file ", "text "] gives a list of all lines in a file that contain the specified text.

FindList[FileNames[ ], "text "] searches all files in your current directory.

Using Directory

Directory[ ]

Setting Your Directory

SetDirectory["Examples"]

Listing the File Names in the Directory

FileNames[ ]

Listing File Names of a Specified Form

FileNames[ "Test*.m"]

Copying a File

CopyFile["Test3.m", "NewTest.m"]

FileNames[ ]

Renaming a File

RenameFile["Test4.m", "TransformTest.m"]

FileNames[ ]

Deleting a File

DeleteFile["Test1.m"]

FileNames[ ]

Getting the Modification Date of a File

FileDate["Test2.m"]

Finding Text in a File

FindList["TransformTest.m", "Laplace"]

Reading Data Files

Syntax (see pages 181 and 493)

ReadList["filename ", Number] reads numbers from a file, and returns a Mathematica  list of them.

ReadList["filename ", {Number, Number}] reads numbers from a file, putting each successive pair in a list.

ReadList["filename ", Number, RecordLists -> True] reads numbers from a file, making a separate list for each line in the file.

Reading a List of Numbers

The first line displays the contents of the file numbers.data.

!!numbers.data

ReadList["numbers.data", Number]

Reading Pairs of Numbers

ReadList["numbers.data", {Number, Number}]

Making Each Line a Separate List

ReadList["numbers.data", Number,
                       RecordLists ->  True ]

Splicing into External Files

Syntax (see pages 184

Splice["infile ", "outfile "] splices Mathematica  output into infile , sending the output to outfile .

Using Splice

The file extension  .mtex causes the Mathematica  output to be put in a form suitable for input into the TeX typesetting system (see note below).

!!paper.mtex

Splice["paper.mtex"]

!!paper.tex

A Note on Splice

Text in the input file not enclosed between  <*  and *>  is copied without change to the output file. Text between  <*  and *>  is treated as Mathematica  input and is evaluated. The default format for Mathematica  output is determined by the extension of the input file name: name .mc gives output formated by CForm, name .mf by FortranForm, and name .mtex  by TeXForm.

Date and Time Functions

Syntax (see page 523)

Date[ ] gives the current local date and time in the form      {year, month, day, hour, minute, second }.

Date[z ] gives the current date and time in time zone z .

TimeZone[ ] gives the time zone assumed by your computer.

AbsoluteTime[ ] gives the total number of seconds since the beginning of January 1, 1900.

SessionTime[ ] gives the total number of seconds elapsed since the beginning of your Mathematica  session.

TimeUsed[ ] gives the total number of seconds used in your current Mathematica  session.

Timing[expr ] evaluates expr , and returns a list of the CPU time needed, together with the result obtained.

Using Date

Date[ ]

Using TimeZone

TimeZone[ ]

The Date in Another Time Zone

This gives the current time and date in time zone +9, the time zone for Japan.

Date[9]

Using AbsoluteTime

AbsoluteTime[ ]

Using SessionTime

SessionTime[ ]

Using TimeUsed

TimeUsed[ ]

Using Timing

Note that the semicolon causes the result of the calculation to be given as Null.

Timing[ 1000!;]

Operations on Strings

Manipulating Strings

Syntax (see page 376)

StringLength[s ] gives the number of characters in a string.

s1  <>  s2  <> ...  or  StringJoin[s1, s2, ...]   joins several strings together.

StringReverse[s ]  reverses the characters in a string.

StringTake[s, n ]   makes a string by taking the first n  characters from s .

StringTake[s, {n }]   takes the n th character of s .

StringTake[s, {n1, n2 }]   takes characters n1  to n2 .

StringDrop[s, n ]    makes a string by dropping the first n  characters in s .

StringDrop[ s, {n1, n2 }]  drops characters n1  through n2 .

StringInsert[s, snew, n] inserts the string snew at position n .

StringPosition[s, sub ]   gives a list of the starting and ending positions at which sub  appears as a substring of s .

StringReplace[s, {s1 -> sp1, s2 -> sp2, ...}]  replaces the si  by the corresponding spi  whenever they occur as substrings of s .

Characters[s ] gives a list of the characters in a string.

Sort[{s1, s2, ...}]   sorts a list of strings into standard "dictionary" order.

Joining Strings

"aaaa" <> "bbbbb" <> "ccccc"

Using StringLength

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

StringLength[alpha]

Using StringReverse

Note that alpha was defined in the previous example.

StringReverse[alpha]

Using StringTake

Note that negative positions are counted from the end of the string.

StringTake[alpha, 5]

StringTake[alpha, {5}]

StringTake[alpha, {-10, -2}]

Using StringDrop

Note that negative positions are counted from the end of the string.

StringDrop[alpha, 5]

StringDrop[alpha, -5]

StringDrop[alpha, {10, 15}]

Using StringInsert

Note that negative positions are counted from the end of the string.

StringInsert[alpha, "XXX", 3]

StringInsert[alpha, "XXX", -3]

Using StringPosition

newstr = "abcdabcdaabcabcd"

StringPosition[newstr, "abc"]

Using StringReplace

Note that newstr was defined in the previous example.

StringReplace[newstr, 
                  {"a" -> "XX", "bc" -> "Y"}]

Converting a String to a List

In doing some kinds of string manipulation, you may find it useful to convert strings into lists of characters, and then perform operations on these lists.

Characters["abcdefg"]

RotateLeft[%]

StringJoin[%]

Sorting a String

Sort[{"cat", "fish", "catfish", "Cat"}]

Searching and Reading Strings

Syntax (see pages 494 and 504)

ReadList[file, Character]   reads a sequence of characters from a file, and puts them in a Mathematica  list.

ReadList[file, Word]   reads a sequence of words from a file, and puts them in a Mathematica  list.

FindList["file ", "text "]   gives a list of all lines in a file that contain the specified text.

Reading Characters from a File

The first line displays the contents of the file strings.

!!strings

ReadList["strings", Character]

Reading Words from a File

ReadList["strings", Word]

Finding Strings in a File

FindList["strings", "is"]