From the Inside Flap
This book represents my Mathematica experience as a physicist and as a teacher. As a physicist, I was initially drawn to Mathematica because I was unable to carry out certain calculations in the field of solid state physics, due to the magnitude of the algebra involved. Since then, I discovered that Mathematica can be profitably used in most research problems and will significantly increase productivity by decreasing the time needed for analytical calculations, computer programming, and data analysis. As a teacher, I taught Mathematica to scientists and engineers in a workshop setting at George Mason University and the U.S. Army Research Laboratory.
Mathematica as a language is different from FORTRAN or C, in part because of Mathematica's ability to handle mathematical expressions as well as numbers, and also because Mathematica is an interpretative language. The biggest challenge to users is understanding the subtleties associated with evaluation of Mathematica expressions. For example, new users often have problems with understanding why a certain Mathematica evaluation seems to "run forever." This is often a result of the evaluation process and the code can be rewritten to run in a reasonable time. Another important point is that Mathematica is an interpretive language. As a consequence, a calculation takes longer to run in Mathematica than in a compiled language such as FORTRAN. The tradeoff is that writing the program in Mathematica requires a small fraction of the time needed to write the same program in FORTRAN or C. In this book, practical alternatives that are available to deal with long running times, including connecting to external FORTRAN or C programs using MathLink are discussed. Another common problem inevitably confronting users is the finite memory of their computer. While no book currently available discusses these practical issues, getting a grasp of these issues is absolutely essential for using Mathematica effectively in scientific and engineering applications. In this book I address evaluation and memory issues in the context of significant examples that are of interest to scientists and engineers.
Studying the solution of significant problems is the best way to learn Mathematica. Each of the examples in this book consists of many different commands. Mathematica is a large software system. (Executing the command Length Names "System'* reveals that there are 1133 built-in commands.) This book explains a large subset of commands needed to develop a conceptual understanding of the Mathematica system. When a command is used without explanation, the definition can be found in the manual, Mathematica: A System for Doing Mathematics by Computer, by Stephen Wolfram, which is the fundamental reference for Mathematica. Hereafter in this book, this manual will be referred to as the Mathematica book.
The Purpose of This Book
The purpose of this book is to teach Mathematica to scientists, engineers, students, and anyone else who uses mathematics. The examples are oriented toward the types of computations that are done by scientists and engineers. The emphasis is on understanding how Mathematica works and how to use Mathematica effectively to do realistic mathematical calculations. The examples in the book are designed to teach how to do large calculations efficiently, both symbolic and numerical, without getting trapped by the standard pitfalls that users encounter. To this end, many common errors are demonstrated and ways shown to fix them. After a user works through a significant portion of this book, he or she should be able to debug Mathematica code and carry out complex calculations efficiently.
Organization of the Book
This book consists of actual Mathematica sessions, together with in-depth tutorial discussions. Each section or subsection examines a different topic. Some sections develop basic Mathematica machinery, such as those in Chapter 1, and the examples are short. Other sections treat actual applications, as in Chapters 8, 9, and 10, and those examples are longer and more interesting mathematically. We often start a section by testing code, line-by-line. When the code appears to work, we gather it together into a function or Mathematica package. "EXAMPLE" boxes are also used to illustrate ideas and solve particular problems. Besides examples, there are boxes labeled "POINT" that contain significant points and asides as they arise.
Chapter 1 introduces the core of the Mathematica language. It begins with simple assignment of values to variables. We discuss the internal representation of Mathematica expressions, which is essential to understanding the unity of the language. The chapter also looks at how Mathematica functions can be applied to whole lists, which serve as an efficient basis for programming. The chapter also examines patterns and rules, the two basic pillars of Mathematica. A variety of standard programming constructs are presented here.
Mathematica lists are used to organize calculations and data and are the subject of Chapter 2. Lists can represent vectors, matrices, and tensors. The special attribute of built-in functions called Listable allows us to do algebraic operations using whole lists, rather than element-by-element operations, as is traditionally done in languages such as FORTRAN or C.
Chapter 3 deals with the topic of graphics. Mathematica is a complete graphics programming language. We concentrate on developing a unified concept of Mathematica graphics, while giving many examples of the types of things that are useful for the sciences. Furthermore, we describe how to make publication-quality plots and how to output device-independent PostScript graphics. The chapter concludes with examples of graphics programming, which is needed when complete control over graphics is desired or a new graphics function is to be written from scratch.
Encapsulation of program parts, the analogue of FORTRAN subroutines or C functions, is done by making variables local to a procedure. Chapter 4 deals with these issues.
Chapter 5 discusses how to define functions and the issues that are related to function evaluation. Inadequate control of evaluation is a common source of error as well as a cause of programs running forever. The types of values that symbols can carry are discussed in detail, including attributes of symbols. Also, compiled functions are introduced, which can lead to order of magnitude improvements in execution times.
Chapter 6 is a survey of the techniques used to carry out various analytic calculations. This chapter includes working with rational expressions, differentiation, integration, power series, and equations.
Chapter 7 discusses issues relating to numerical computation. The concepts of precision and accuracy of numbers are discussed in detail. Unlike most computer programming languages, Mathematica offers direct support for arbitrary precision arithmetic. High precision numbers and calculations are discussed. Examples of defining numerical functions and using them in calculations are treated in detail. Different ways to assign numerical values to symbols are also discussed.
Vectors, matrices, and tensors are the topic of Chapter 8. The functions needed for linear algebra are described. Vector analysis for solving vector field theory problems is also described. A detailed example of solving Maxwell's equations for electromagnetic wave propagation in an anistropic medium is carried out. The built-in functions available for manipulating Cartesian tensors are treated in detail. The problem of elastic waves in a piezoelectric crystal is worked out. For work with general tensors requiring the use of covariant and contravariant indices, we give an introduction to the commercially available package called MathTensor.
Differential equations are treated extensively in Chapter 9. The chapter begins by discussing the symbolic solution of ordinary differential equations using built-in functions. An extension of built-in functions is developed using the method of variation of parameters. Approximate solution of differential equations by power series solution is investigated next. High-order power series offer an excellent approximation method when a closed form solution is not possible. Next, solution of linear differential equations is presented by Laplace transforms. Finally, for when these analytic methods fail, built-in functions for numerical solution of differential equations are discussed in detail. The chapter concludes with a discussion of analytic solution of differential equations by perturbation expansion, which is often useful when there is a small parameter in the differential equation.
Chapter 10 presents an in-depth treatment of inhomogeneous boundary value problems and eigenvalue problems of ordinary differential equations. Besides some simple examples, this chapter solves several nontrivial eigenvalue problems. The chapter is divided into three sections that illustrate how to carry out an analytic solution, numerical solutions by shooting methods, and numerical solution by finite difference methods, respectively. Each section treats a nontrivial problem. As an example, we solve the classic problem of a one-dimensional quantum mechanical particle-in-a-box, described by Schrdinger equation. This constitutes a Sturm-Liouville eigenvalue problem that occurs commonly in applications. We solve this eigenvalue problem analytically for the case where there is an electric field inside the box rather than a constant potential, so the solutions of the associated differential equation are not simple exponentials but Airy functions. We then treat this problem numerically via shooting methods. However, this time the potential in the box is taken to be a constant. Finally, we revisit this problem again using finite difference methods to demonstrate its solution with matrix methods.
Chapter 11 presents an in-depth discussion of various aspects of input and output, including various ways of reading and writing to external files. Mathematica also has the capability to act as a shell or operation system, permitting manipulation of external files and directories from within Mathematica.
Mathematica can be run interactively or, if the computation takes a long time, it can be run in background or batch mode like traditional FORTRAN programs. Sometimes Mathematica can overrun your computer if it is allowed to run without restrictions on CPU time and memory. Chapter 12 deals with the available options for running and controlling Mathematica.
Chapter 13 discusses using Mathematica packages. Ultimately users will want to do some programming to write special purpose functions. These functions can be encapsulated in Mathematica packages. The concept of context and context search path is explained in detail, as is how to write and debug a package.
In Chapter 14 we discuss MathLink. Users of Mathematica typically have previously written many useful programs in FORTRAN or C. Alternatively, some new programs in FORTRAN or C can be written that will run considerably faster than the corresponding Mathematica program. In both cases, these programs can be installed into Mathematica as if the subroutines or functions in these programs were built into Mathematica. The link between Mathematica and external programs is provided with the standard distribution of Mathematica and is called MathLink. We use the MathLink template mechanism to demonstrate how to install FORTRAN subroutines and C functions into Mathematica.
The Mathematica system itself is described in the Appendix, including the breakdown between the kernel and the Notebook front end.
How to Use This Book
If you have no prior experience with Mathematica, then you should at least read parts of Chapter 1, which presents a thorough explanation of many concepts along with a basic set of commands. Chapter 1 also gives many useful examples containing standard idioms in Mathematica programming. The rest of the chapters can be read in sequence, or you can alternatively skip to the chapter or section that contains the type of calculations that are of interest to you.
If you have some prior experience with Mathematica, then you can start on any chapter that seems appropriate to the problems that you want to solve. The sequence of chapters is meant to follow logically. However, the chapters are designed to be independent of each other as much as possible.
This book is a tutorial for doing realistic calculations that are of interest to people in the fields of science and engineering. It is not a manual of Mathematica commands. The Mathematica book is an excellent encyclopedic manual with many examples. When a command is used here and not explained, it is meant to be looked up in the Mathematica book, as there is no point to reiterating what is already clearly defined there.
To get the maximum benefit, the sequences in the book should be tried on a computer running Mathematica, preferably running a Notebook front end. Some sections have problems to be solved and are useful for testing your understanding.
Finally, the most important advice: Experiment with Mathematica to see how it can be of help in your work.
Mathematica as a Tool
The Mathematica system is interactive, meaning that no compilation of programs has to be done. Instead, calculations are typically done executing one line at a time. The intermediate results can be seen just as if the calculations were done by hand.
Mathematica is powerful; it can be used to perform large interactive calculations that would not be possible with paper and pencil. Even when the calculations are of manageable size, it is often better to carry them out in Mathematica. The principal benefit of using Mathematica is that algebraic mistakes can be avoided, a problem that is all too familiar to people who do long calculations. In addition, the results can be checked quite painlessly.
The Mathematica language is concise and (arguably) intuitive. Complicated calculations can be written down in a few lines. As a result, it is easy to write down problems that will exceed the memory capacity and practical limitations associated with a computer's CPU speed. The Mathematica system is also flexible. Mathematica can interface with existing computer programs and operating systems, making Mathematica useful for interactive data analysis.
Mathematica was developed by Wolfram Research, Inc. with the first version of Mathematica announced on June 23, 1988. Mathematica is currently available on virtually all major computer platforms. Mathematica code is portable in the form of ASCII files or as Mathematica Notebook files. The Notebook files typically contain the Mathematica program code, graphics, and descriptive text. These Notebooks are portable from one computer to another one running a compatible version of Mathematica.
These features make Mathematica an ideal tool for research. In fact, the increased capabilities that Mathematica provides allow users to tackle a whole new class of research and engineering problems, which were previously inaccessible due to algebraic complexity.
Availability of Code from the Book
During the course of this book quite a few routines are developed. These routines perform many useful tasks, such as writing numbers in columns under format control so that they can be read by other programs. Another routine writes Mathematica lists as arrays, which can be input directly into FORTRAN or C programs. For convenience, the code from this book was collected and is available through MathSource, the electronic archive that is kept by Wolfram Research, Inc. MathSource contains a variety of useful Mathematica routines. The MathSource archive is free and can be accessed by Internet, either through electronic mail or by ftp (file transfer protocol), a standard program available on many computers. MathSource can be accessed by electronic mail using the electronic mail address: email@example.com. Over the Internet, MathSource can be accessed by ftp using mathsource.wri.com.
The Mathematica Version Used
The actual Mathematica sessions in this book were executed in Mathematica version 2.2 on a NeXT Station color computer with a Motorola 68040 CPU running at 25MHz, with 32 MB of RAM. The Mathematica Kernel is designed to give essentially the same results on any computer running version 2.0 or higher (except for discrepancies associated with differences in machine floating point number precision). The basic command set is that defined in the Mathematica book, which describes Mathematica version 2. The previous versions of Mathematica are considered obsolete. Version 2 has been substantially extended, and many new useful commands have been added.
Writing Programs One Line at a Time
Mathematica programs are not typically written down and then tested, as is the practice with compiled languages. Instead, the interactive nature of the Mathematica system allows us to execute one line of program code at a time. Typically, many errors are discovered and fixed during this phase. When all the code seems to work, we then put the code together into a function or a package. Hopefully, it will still work. If not, only small changes are necessary. Typically, each Mathematica program line is more powerful (and more subtle) than a line of FORTRAN or C code. The complexity of the Mathematica language makes it a good idea to write one line at a time. The Mathematica programs used as examples in this book consist of several to many lines of code. Each of these examples was written one line at a time. When you encounter Mathematica code consisting of more than one line (or nested function), I suggest you execute it one line (or function) per In line to see how the code works.
Initialization Used in Sessions
When Mathematica starts up, it attempts to read a file named init.me. Users can place into this file any commands that they want executed before the start of the session. Mathematica has a built-in messaging system that is designed to inform the user when he has misspelled, or potentially misspelled, a word. This messaging system emits warning messages whenever two symbols have similar names. These error messages can be switched off for the duration of a session by evaluating the lines:
In1:= Off General::spell; Off General::spell1;
Alternatively, the error messages can be switched off forever by including these lines in the file init.m. Mathematica reads the file init.m on start-up. We have switched off these messages so that they do not interfere with the "live sequences" shown in the text. If you try to execute the sequences shown in this book and you do not switch off these messages, then you will see error messages of the form
In2:= General::spell Out2=Possible spelling error: new symbol name "`1`" is\ similar to existing symbols `2`.
From the Back Cover
and engineering. It includes a tutorial introduction to the system. Unlike other
tutorials, examples focus on the non-trivial problems scientists and engineers
are likely to encounter in their work, problems that often push Mathematica to