Preface to Introduction to Programming with Mathematica Published by TELOS/Springer-Verlag Publishers Due out: August 1993 ISBN: 0-387-94048-0 (3-540-94048-0) Technical Computing for the Rest of Us Computers have wrought a fundamental change in the nature of research, and in education in science and engineering. Experimentalists routinely use computers to collect and analyze data while theoreticians use computers to manipulate equations numerically and symbolically. For both, computer simulation studies have become an indispensable investigative tool. In response, technical education is changing to incorporate the use of computers into the curriculum both as a topic and as a medium for the presentation of technical material. The use of computers in all of these areas has been accelerating rapidly as computer hardware development has progressed to the point where every person can afford his or her own self-contained computing environment. It even seems likely that the struggle by universities and colleges to obtain funds to establish computer laboratories of desktop computers will be obviated by students bringing their own powerful notebook computers to the classroom. Yet, even as the the hardware hurdle has been overcome, another obstacle has held back this `democratic' spread of technical computing power. That hindrance has been the paucity of software that is both powerful and user-friendly. While there are software packages for carrying out various kinds of technical computing, there are few that provide a fully integrated technical computing environment, including a programming language suitable for individuals who are not full-time programmers but who need to create programs for their work. Mathematica provides such an environment. It includes: - Built-in mathematical and graphical capabilities, that are both powerful and flexible. - A programming language that can be used to extend its capabilities virtually without limit. That language is interactive, has the capability to perform both numeric and symbolic manipulation, makes broad use of pattern matching, and supports the functional style of programming favored by many computer scientists (while incorporating constructs for more conventional programming styles). - Extensive on-line help facilities. - The ability to connect Mathematica to other computing environments and other languages. - An interface that allows text and graphics to appear together in documents. In this book, we focus on the Mathematica programming language. While there are many books, including the reference manual by Stephen Wolfram [Wolfram, 1992], that discuss various aspects of Mathematica, there has been a need for a text explaining how to use the underlying programming language so that Mathematica's capabilities can be fully utilized. This is the first book that explains Mathematica's programming language for the beginning programmer, emphasizing the programming styles that are most efficient and idiomatic in Mathematica: functional and transformational (or rule-based). The Audience for this Book This book was written for two distinct groups of readers: - Mathematica users who have no prior programming experience. We start from the very beginning, explaining how to build `nested function calls', create `anonymous' functions, and use `higher-order' functions. Recursive and iterative programming are also explained. We include in the category of beginners, Mathematica users who have programmed in conventional languages such as Fortran, C, BASIC and Pascal. In many instances, these programmers will write Mathematica programs in a procedural style, producing code that looks like `Fortran in Mathematica.' By understanding how Mathematica programs can be constructed using the functional and rule-based styles of programming, much simpler and more efficient code can be written. - Those who want to learn to program, and would like a friendly and useful language to start with. Most low-level languages, like C and Fortran, require too many lines of complicated code in order to do something interesting while many high-level languages, like LISP, use an unnatural syntax and are difficult to master. Mathematica has a more natural syntax and provides very high-level built-in operations with which one can do a lot of interesting things right away. How to Use This Book There are several ways in which this book may be useful: - As a primary text in an introductory course on programming. Perhaps the most obvious use would be in a introductory computer science course for students in science, engineering, or mathematics, who would solve technical problems of interest to them while at the same time learning to program. The Mathematica programming language supports many programming styles that can be transferred to other programming languages. Experience shows that the programming skills learned using Mathematica can be transferred to conventional programming languages. - As a supplemental text in a course in which Mathematica is being used as a tool for studying another technical subject. In these kinds of courses, the principles of Mathematica programming can be introduced as needed during the course, although we have found that a structured introduction to Mathematica programming at the beginning of the semester works best. - As a self-study book, particularly for Mathematica users who need to use its programming capabilities more fully or who are interested in understanding how the Mathematica programming language works. Conventions Used in This Book All input and output in this book appear in a different font from the regular text. This is true for examples that appear in the middle of text, such as Expand[(a + b)^4], as well as displayed Mathematica code. So, for example, lines of input (what you type at your computer) appear as: In[1]:= 3 + 5 whereas all output (what Mathematica prints on your computer screen) appears in a slightly lighter font than the input: Out[1]= 8 You do not type the prompts In[1]:= or Out[1]=; Mathematica will do that for you. In addition to being placed on the floppy diskette, all of the programs that are defined in this book, are indexed in two different ways --- under the program's name and also under the heading `Programs'. So, for example, the function runEncode defined on page 134, is listed in the index under runEncode and also as Programs, runEncode. How to Get Help The material in this book has been made available in an electronic form. Mathematica Notebooks containing most of the examples and exercises are included on a floppy diskette on the inside back cover of this book. The diskette also contains files with Mathematica programs for users without the Notebook interface. This diskette is a 1.44" high-density disk that is formatted in an IBM-compatible format. It should be readable by any IBM-compatible computer, as well as by any UNIX machine, NeXT computer, or Macintosh computer (with the Apple File Exchange program that comes with each Macintosh operating system). In addition to the diskette, these materials are available from Wolfram Research's MathSource, an electronic distribution service of Mathematica materials. To obtain information about MathSource, send an email message to mathsource@wri.com containing the one-line message help intro. If you just wish to obtain the materials from this book, then send the message: find 0204-938 The floppy diskette contains a file ReadMe.txt that describes the MathSource service in greater detail, including instructions on how to query and obtain any of the materials it provides. A Final Note to The Reader While the basic aspects of Mathematica programming are discussed in this book, there are a great many more things that can be said about Mathematica and the Mathematica programming language. The most comprehensive reference source is the Mathematica manual: Mathematica, A System for Doing Mathematics by Computer [Wolfram, 1992]. MathSource contains hundreds of programming examples and Notebooks from all areas of science and engineering. These materials can be obtained by sending electronic mail to mathsource@wri.com. A description of how to access the materials on MathSource is contained on the floppy diskette included with this book. For those with access to the Internet, two electronic mailgroups for Mathematica users exists --- mathgroup and sci.math.symbolic. These are good places to get answers to frequently asked questions and to pose your own questions to hundreds of subscribers from around the world. To subscribe, send an email message to mathgroup-request@yoda.physics.unc.edu or to sci.math.symbolic asking to be added to the respective lists. Mathematica in Education is a quarterly newsletter/journal published by TELOS/Springer-Verlag that includes articles and notes about the use of Mathematica in the classroom. It includes a regular column on programming and also a student column. To subscribe, send a request to Mathematica in Education, Department of Mathematics, Sonoma State University, Rohnert Park, CA 94928, or send email to MathInEd@groucho.sonoma.edu. The Mathematica Journal is a quarterly journal that publishes articles about all aspects of Mathematica. For subscription information, contact Miller Freeman, Inc., 600 Harrison Street, San Francisco, CA 415-905-2334. Finally, there are dozens of books that are about Mathematica or that use Mathematica to teach a subject. A complete list (as of the publication of this book) can be found in the bibliography at the end of this text. Colophon This book was produced using LaTeX from original TeX source files and Mathematica Notebooks. The Notebooks were converted to TeX files using the nb2tex converter available from MathSource. All graphics were produced in Mathematica and imported to the TeX documents by means of the options available in the epsf.sty style file. PostScript files were generated with dvips and cut into individual page PostScript files before being output to paper. The fonts used for this book are Melior (for all text), Optima (for display and heads), and Rockwell (for Mathematica code). The choice of Rockwell was a bit non-standard as it is not a monospaced font. The standard font for the computer voice has been Courier, but with the new typesetting capabilities of Mathematica (starting in Version 2.3), we felt it was time to make a break from tradition and start displaying the computer voice in a more pleasing manner. We welcome comments and criticism related to this decision. The Authors: Richard J. Gaylord Department of Materials Science University of Illinois Urbana, IL 61801 gaylord@ux1.cso.uiuc.edu Samuel N. Kamin Department of Computer Science University of Illinois Urbana, IL 61801 kamin@cs.uiuc.edu Paul R. Wellin Department of Mathematics Sonoma State University Rohnert Park, CA 94928 wellin@sonoma.edu June 1993