Preface

Introduction

If you were stranded on a desert island with only your laptop (and presumably a large solar panel), what software would you want to have with you? For me the answer definitely includes the latest version of Wolfram Mathematica. Whether you are a scientist, engineer, or mathematician, a Wall Street quant, a statistician or programmer, or even an artist or musician, you will be a better one if you have this tool at your disposal. Of course, having a tool and knowing how to use it well are quite different things. That is why I wrote the Mathematica Cookbook.

I am a big fan of O’Reilly cookbooks, as these books are designed to help you solve real-world problems. Mathematica is an ideal candidate for a cookbook because it is so vast, deep, and full of traps for the novice. I was ecstatic to learn that O’Reilly was looking to publish a Mathematica cookbook and even more excited when I was chosen to be its author. I have been a user of Mathematica since version 3.0. Although that was over 13 years ago, I still remember the frustration of trying to solve problems in this system. I don’t mean this in a derogatory way. The frustration a newbie experiences when trying to learn Mathematica comes from the knowledge that you are sitting in front of a highly advanced computational platform that eventually will magnify your productivity tenfold—if you can only wrap your mind around its unfamiliar idioms. If you are a new (or even not-so-new) user of Mathematica today, you are simultaneously in a better and a much worse position than I was with version 3.0. You are in a better position because Mathematica 7.0 is vastly more powerful than 3.0 was back then. Not only has the number of available functions doubled, but Mathematica has fundamental new capabilities including dynamic interactivity, curated data sources, parallel processing, image processing, and much more. You are in a worse position because there is much more to learn!

As Mathematica grows, it remains largely unchanged in its core principles. This book is designed to help you master those core principles by presenting Mathematica in the context of real-world problems. However, my goal is not just to show you how to solve problems in Mathematica, but to show you how to do so in a way that plays to Mathematica’s strengths. This means there is an emphasis on symbolic, functional, and pattern-based styles of programming. Mathematica is a multi-paradigm programming language; you can easily write code in it that a Fortran or C programmer would have little trouble following. However, the procedural style that this entails is not likely to give you good performance. More importantly, it will often cause you to write more code than necessary and spend more time adapting that code to future problems. Stephen Wolfram has said that a correct Mathematica program is often a short Mathematica program. There is much truth to this. The truth comes from the idea that good Mathematica programs leverage the capabilities of the vast built-in library of both general-purpose and highly specialized functions. Programming in Mathematica is a search for the right combination of primitives. My hope is that this cookbook will play a role as your guide.

MathematicaCookbook.com

One risk of authoring a cookbook is that it is almost inevitable that something someone finds important will be left out. With Mathematica, this risk is a certainty because even as I wrote the book, Mathematica’s capabilities grew. However, even if you drew a line at, say, version 6.0, you would find that there are still many topics that I do not cover in the book, for various reasons. To remedy this and to create a living resource that I hope the Mathematica community will help nourish, I am launching https://oreil.ly/mathematicackbk_archive . Here you will find recipes that did not make it into this book, and more importantly, you will be able rate recipes, contribute your own, or provide alternative implementations to those found in the book or on the site.

Structure of This Book

The Mathematica Cookbook is not necessarily meant to be read from start to finish like a conventional book (although you are certainly welcome to do so!). Having said that, the chapters are organized in a purposeful way. Chapter 1 through Chapter 8 present general techniques that all users of Mathematica should know. These chapters are largely self-contained, but sometimes it is necessary to use features in one chapter that are covered more deeply in another. Cross-references within each recipe should prevent you from getting stuck. However, keep in mind that a cookbook is not the same as a tutorial, and you should also make frequent use of the Mathematica reference, tutorials, and guides that are integrated into Mathematica’s help system. Chapter 9 through Chapter 14 cover specific domains of Mathematica application. If you are the type of person who learns best by examples from your area of expertise, you will benefit from seeing the techniques of the first chapters leveraged in problems related to physics, engineering, calculus, statistics, music, finance, and more. Finally, Chapter 15 through Chapter 19 cover important techniques, extensions, and tools that make Mathematica unrivaled as a technical software development tool.

Chapter 1 covers numerics. For the most part, Mathematica simply does the right thing when computing numeric results, as you would expect. In pure mathematics, numbers are formal objects that are well behaved, but when you represent numbers in a finite discrete device like a computer, often you will need to understand issues of precision and accuracy in order to get reasonable results on certain classes of problems. Further, numbers have different representations in Mathematica (Integers, Rationals, Complex, and some exotic types like Intervals). Then there is an issue of input and presentation: Mathematica supports different base representations and different display formats. This chapter has recipes that cover all these issues, and it is wise to have some familiarity with them before using any of the numeric algorithms.

Functional programming is a style of Mathematica development that most seasoned users prefer. Chapter 2 dives deeply into functional programming, Mathematica style. Because Mathematica was designed to support multiple development paradigms, its functional programming abilities are not as pure as languages like Haskell. This is actually a big plus, because if you are using Mathematica chances are you are solving a problem, and it’s the solution rather than the aesthetics that is foremost in your mind. Mathematica programmers prefer the functional style because it leads to efficient programs. It also leads to elegant programs. In the context of programming, elegant means the combination of brevity, power, and clarity. There is an amazing sense of intellectual satisfaction that comes from finding a concise functional solution, and this feeling creates the positive feedback that will draw you into Mathematica. However, this style is often mysterious to people who come to Mathematica from other languages like Fortran, C, MATLAB, or Microsoft Excel. I think this chapter will help you discover the rewards of the functional style.

Chapter 3 presents Mathematica data structures, which are largely built on the foundation of lists. From lists, Mathematica derives matrices and higher order tensors, sparse matrices, and more. Knowing how to manipulate these structures is essential for almost any application of Mathematica. This is obvious if you are doing linear algebra, but list processing is integral to almost every facet of use. This chapter also shows how to implement other types of data structures, such as a dictionary that leverages the fast associative look-up that is part of Mathematica’s evaluation engine.

Pattern-based programming revolves around pattern matching and transformation. Chapter 4 introduces Mathematica’s rich pattern-based techniques. Patterns are not a feature of most mainstream languages, but they are tremendously powerful and essential if you hope to accomplish anything nontrivial in Mathematica. Of all the techniques at your disposal, pattern matching and replacement is the one most likely to yield the “wow” reaction you get when you see a seemingly simple looking piece of code do something not so simple. To whet your appetite, here is one of my favorites.

Structure of This Book

In this little ditty by Frank Zizza (which won a programming contest at the 1990 Wolfram conference), the goal is to take a list and return the list in run length encoded form. Don’t worry if this code seems cryptic; it won’t after you have recipes from Chapter 2 and Chapter 4 under your belt. For example, input {1, 1, 2, 2, 2, 1, 3, 3, 3, 3} should produce {{1, 2}, {2, 3}, {1, 1}, {3, 4}}.

In[191]:= runEncode[{1, 1, 2, 2, 2, 1, 3, 3, 3, 3}]
Out[191]= {{1, 2}, {2, 3}, {1, 1}, {3, 4}}

Although you can create small solutions to this problem in languages like Python or Ruby, I find this solution compelling because it contains no explicit looping construct and, once you learn to read it, contains a very explicit and statement of the problem.

Chapter 5 covers string manipulation which is more important than you might first guess for a language that is primarily associated with numeric and symbolic mathematics. Mathematica has a rich set of string manipulation primitives that include all the typical functions you expect (StringLength, StringReplace, StringInsert and so forth) plus an extension of its pattern language specifically designed for strings and including regular expression based transformations.

The next two chapters explore one of Mathematica’s best capabilities, integrated graphics. Chapter 6 dives into two-dimensional plots and graphics. There are many packages that let you create plots, but few are so seamlessly integrated into the same development environment where you write code. This integration is an amazing productivity tool. I frequently find myself using Plot and other graphing functions simply as a means to help me understand an equation or algorithm I am developing and not necessarily because I am creating a presentation to be viewed by others. The fact that functions like Plot, ListPlot, and ParametricPlot give good results with little effort means they can become part of your day-to-day interaction with Mathematica. But if you need professionally designed graphics for an important paper or presentation, you will not be disappointed, because there are options to customize every aspect of the presentation.

Chapter 7 builds on the preceding chapter by moving into the more sexy domain of 3D graphics and plots. Plotting in 3D provides you with additional visualization and interaction capabilities. All 3D graphics can be rotated, panned, and zoomed interactively. There are also many sophisticated options that let you adjust every aspect of the plot, including shading, mesh, coloring, camera angles, how light reflects off the surface, and so on. Not every user will want to tweak all of these settings, but if you are a graphic artist or aficionado you will have a lot of fun once you master all the options. This chapter will give you a leg up on this important dimension of Mathematica’s power.

Chapter 8, the first of the special-purpose chapters, covers image processing. Native image processing functions were added in Mathematica 7.0, and you can have quite a bit of fun transforming images programmatically as you do by hand with software such as Photoshop. This chapter also shows some advanced image-processing techniques for which I wrote a large part of the algorithms in Mathematica rather than relying on the built-in functions. This provides readers who are interested in image processing with a guide to approaching image algorithm development in Mathematica, and also provides some deeper insight for those who know little about these algorithms

Chapter 9 will give you respite from all the eye-candy by providing some ear-candy. You may not know it, but Mathematica is quite the musician, and I was happy to have John Kiehl, a professional musician and recording studio owner, write this chapter for you. Mathematica can turn functions into sound to play notes, chords, and electronic versions of a variety of musical instruments. Further, it can import MIDI files and other audio formats. You can even perform various operations on sound such as Fourier transforms. There really are few limits, and John is an experienced guide who provides lots of recipes for the musically inclined to expand upon and the not-so-musically inclined to educate themselves with (or just play with for fun). This chapter is available for your immediate listening pleasure at http://www.oreilly.com/catalog/9780596521004.

Chapter 10 returns to more mathematical fare by exploring Mathematica’s formidable abilities in symbolic math. This chapter focuses on algebraic manipulation and solutions to equations. Many of the recipes show techniques for massaging results produced by Mathematica into equivalent but sometimes more desirable forms.

Symbolic and numerical calculus is what most people think about when they think about Mathematica, and Chapter 11 dives into Mathematica’s formidable (many say unrivaled) capabilities in this domain. Here you will see recipes related to computing limits, derivatives, integrals, vector calculus, and the solutions to differential equations. The chapter also covers the increasingly important domain of discrete calculus, including sums, products, and difference equations.

There is high probability that the average technical person will need to do some statistics! Puns aside, Chapter 12 has recipes that will help you get a handle on Mathematica’s formidable statistical capabilities, which rival those of specialized stats packages. This chapter has recipes for common statistical measures, probability distributions, data fitting, interpolation, and more sophisticated tools like ANOVA. It also introduces stochastic simulation.

Chapter 13 enters the realm of applied math by showcasing physics and engineering. These domains are extremely broad, so rather than attempting to cover a large swath, I cherry pick recipes that show applications of the mathematical tools discussed in preceding chapters. I also include recipes that demonstrate general techniques for organizing programs that have many variables. In addition, this chapter shows how educators and others can draw on Mathematica’s access to curated data related to physics, chemistry, and biology.

Chapter 14 jumps bravely into the risky business of numerical finance. The goal of this chapter is to show quants and others interested in mathematical finance how to leverage Mathematica’s strengths in applying common financial algorithms. This chapter presents problems of mild to moderate sophistication so that the illustration of Mathematica techniques is not lost in the complexity of modern computational finance. A large part of this chapter is the result of the efforts of Andreas Lauschke, who is expert in both computational finance and Mathematica.

Version 6.0 brought new excitement to the Mathematica world with the addition of dynamic interactivity. For the first time a Mathematica user had the capability to create notebook output that changed in response to changes in underlying variables. In many ways this ability parallels the new types of dynamic web pages that emerged around the same time (so-called Web 2.0)—but I digress. Chapter 15 introduces the primitives underlying this new dynamic interactivity. Amazingly, there are just three main ingredients to this capability: Manipulate, Dynamic and DynamicModule. As with many of Mathematica’s advanced features, you will master the easy use cases immediately, because the primitives just do the right thing. More advanced application will require some steep learning, but this chapter has many recipes that will help you get there. For your immediate gratification, this chapter is available at http://www.oreilly.com/catalog/9780596521004.

Computers with multiple cores (processing elements) are commonplace; there is a good chance you own a computer with at least two cores, and if you bought one recently, perhaps even four or more. My Mac Pro has eight. Mathematica stays ahead of this trend by bundling Parallel Processing with version 7.0. Chapter 16 contains recipes that show you how to use these features. Mathematica makes it easy to add parallelism to your programs, but this does not mean your algorithms will run four times faster if you have four processors. To get any speed increase at all, you need to understand how the parallel primitives work and how they can be tuned. The recipes in this chapter show you how to configure parallelism, parallelize existing serial programs, and also implement more sophisticated parallel techniques like Map-Reduce and parallel pipelines.

As powerful as Mathematica is, there are times when you need something else. Chapter 17 will show you how to interface Mathematica with other languages and programs. Here, programmers will learn how to integrate code written in C, Java, and .NET languages. This chapter also has recipes for integrating Mathematica with database systems and third-party tools like spreadsheets.

Chapter 18 includes material that every Mathematica user should know but that did not quite fit anywhere else in the book. Here I introduce recipes on performance, packaging, stylesheets, and other important techniques.

Last but by no means least, you will want to know how to debug your way out of those nasty situations where you just can’t figure out why you are getting strange error messages or bizarre results. Chapter 19 presents debugging techniques and, possibly more important, unit testing techniques. An important part of this chapter is Wolfram Workbench, the alternative development environment based on Eclipse (an open source IDE designed to be customizable to different languages).

Acknowledgments

The Mathematica Cookbook was one of my most challenging projects and it is not something I could have accomplished without the support of many people. Although I would have never survived without this help, any problems, errors, or omissions in the final product are mine alone.

First I must thank Maryka Baraka of Wolfram Research for turning me on to this project, arranging my first visit to Wolfram Research, and most importantly, for introducing me to many valuable people on the Wolfram staff. Over the course of this project, Maryka fielded dozens of queries and directed them to where they needed to go in the Wolfram organization. Her advice and support were invaluable. Maryka, you’re the best!

Theo Gray of Wolfram answered many questions and provided much support, including a personal tutorial of some of the advanced features of Mathematica’s Manipulate and Dynamic functionality. He was also instrumental in my visiting Wolfram so that I could preview Mathematica 7 before its release. This improved the book tremendously.

Many other Wolfram folks answered questions and provided valuable suggestions and support. So many, in fact, that I fear I may leave someone out. At the risk of that, I must personally acknowledge Larry Adelston, Chris Carlson, Joy Costa, Lou D’Andria, John Fultz, Roger Germundsson, Bradley Harden, Jason Harris, Tom Wickham Jones, Andre Kuzniarek, Misty Moseley, Peter Overmann, Schoeller Porter, Michael Trott, and Eric W. Weisstein. Also, thanks to Stephen Wolfram for creating and nurturing Mathematica for all these years.

Working with O’Reilly for the second time has proved to be just as rewarding as the first. First and foremost, a big thank you to my editor, Michael Loukides, whose support and patience were without bound (even when I whined about this and that as we nitpicking authors so often do!). Michael provided great perspective as he read and commented on each chapter, and he helped me to understand many points that were awkward or would trip up a beginner. In addition, Abby Fox, Keith Fahlgren, and Adam Witwer provided valuable help in the early stages of production. Also thanks to Simon St. Laurent, the editor of my first cookbook, for his glowing recommendation.

Instrumental to improving the quality of many recipes were the technical critique and corrections from Larry Stead, Gregory Frascadore, and Andreas Lauschke. Not only did these reviewers correct mistakes, they also offered better implementations for several recipes. Again, remaining defects or inadequacies are the fault of the author alone.

Portions of this book would simply not exist without generous contributions from Thomas Weber (Weber and Partner), Chris Carlson (Wolfram), and Ulises Cervantes-Pimentel (Wolfram). Special thanks to John Kiehl (Soundtrack Studios), whose unique combination of musical and Mathematica abilities resulted in the entertaining and educational music and audio processing chapter. Special thanks also to Andreas Lauschke (Andreas Lauschke Consulting) for much of the material in the financial engineering chapter.

I must also thank Kirsten Dennison and her staff at Precision Graphics for arriving to solve the final production issues and seeing this book into print. Also, thanks again to Larry Adelston of Wolfram for assisting Kirsten’s team with stylesheet production and other automation.

Thanks also to my colleagues who offered both moral and technical support during this project. In particular I would like to thank Doug Finke, whose conversations are always uplifting, and Kalani Thielen, who always seems to know what I need every time I ask.

Although the folks above were crucial, the most important ingredients that went into this cookbook were the love, patience, and support of my family. In particular, thanks to my wonderful wife Wanda, for enduring a second book project that never seemed to be coming to an end, and for all the things small and large that she did for me and our family when I was busy working on it. She deserves more than I can ever put into words. Also to my two boys, Leonardo and Salvatore, who had to give up quite a bit of our personal play time so I could get this beast out the door. This book is dedicated to you guys. Thanks also to my parents, family, and friends who provided so much support throughout my life and who cheered me on and lifted my spirits.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

  • Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

  • Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

  • Shows commands or other text that should be typed literally by the user.

Constant width italic

  • Shows text that should be replaced with user-supplied values or by values determined by context.

Note

Conventions Used in This Book

This icon signifies a tip, suggestion, or general note.

Note

Conventions Used in This Book

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does not require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Mathematica Cookbook by Salvatore Mangano. Copyright 2010 O’Reilly Media, Inc., 978-0-596-52099-1.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Enabled

Note

Safari® Enabled

Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com .

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
North Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707 829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596521004

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:

http://www.oreilly.com

Get Mathematica Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.