Printed from the Mathematica Help Browser Library of Congress Cataloging-in-Publication Data Wolfram, Stephen, 1959 – Mathematica book / Stephen Wolfram — 5thed p cm Includes index ISBN 1–57955–022–3 (hardbound) Mathematica (Computer file) Mathematics—Dataprocessing I Title QA76.95.W65 2003 510 285 5369—dc21XX–XXXXX CIP Comments on this book will be welcomed at: comments@wolfram.com In publications that refer to the Mathematica system, please cite this book as: Stephen Wolfram, The Mathematica Book, 5th ed (Wolfram Media, 2003) First and second editions published by Addison-Wesley Publishing Company under the title Mathematica: A System for Doing Mathematics by Computer Third and fourth editions co-published by Wolfram Media and Cambridge University Press Published by: ISBN 1–57955–022–3 Wolfram Media, Inc web: www.wolfram–media.com; +1–217–398–9090; fax: +1–217–398–9095 email: info@wolfram–media.com phone: mail: 100 Trade Center Drive, Champaign, IL 61820, USA Copyright © 1988, 1991, 1996, 1999, 2003 by Wolfram Research, Inc All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser Wolfram Research is the holder of the copyright to the Mathematica software system described in this book, including without limitation such aspects of the system as its code, structure, sequence, organization, “look and feel”, programming language and compilation of command names Use of the system unless pursuant to the terms of a license granted by Wolfram Research or as otherwise authorized by law is an infringement of the copyright The author, Wolfram Research, Inc and Wolfram Media, Inc make no representations, express or implied, with respect to this documentation or the software it describes, including without limitations, any implied warranties of merchantability or fitness for a particular purpose, all of which are expressly disclaimed Users should be aware that included in the terms and conditions under which Wolfram Research is willing to license Mathematica is a provision that the author, Wolfram Research, Wolfram Media, and their distribution licensees, distributors and dealers shall in no event be liable for any indirect, incidental or consequential damages, and that liability for direct damages shall be limited to the amount of the purchase price paid for Mathematica In addition to the foregoing, users should recognize that all complex software systems and their documentation contain errors and omissions The author, Wolfram Research and Wolfram Media shall not be responsible under any circumstances for providing information on or corrections to errors and omissions discovered at any time in this book or the software it describes, whether or not they are aware of the errors or omissions The author, Wolfram Research and Wolfram Media not recommend the use of the software described in this book for applications in which errors or omissions could threaten life, injury or significant loss Mathematica, MathLink and MathSource are registered trademarks of Wolfram Research J/Link, MathLM, MathReader, NET/Link, Notebooks and webMathematica are trademarks of Wolfram Research All other trademarks used are the property of their respective owners Mathematica is not associated with Mathematica Policy Research, Inc or MathTech, Inc Printed in the United States of America (¶) Acid-free paper 15 14 13 12 11 10 ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser About the Author Stephen Wolfram is the creator of Mathematica, and a well-known scientist He is widely regarded as the most important innovator in technical computing today, as well as one of the world's most original research scientists Born in London in 1959, he was educated at Eton, Oxford and Caltech He published his first scientific paper at the age of fifteen, and had received his PhD in theoretical physics from Caltech by the age of twenty Wolfram's early scientific work was mainly in high-energy physics, quantum field theory and cosmology, and included several now-classic results Having started to use computers in 1973, Wolfram rapidly became a leader in the emerging field of scientific computing, and in 1979 he began the construction of SMP—the first modern computer algebra system—which he released commercially in 1981 In recognition of his early work in physics and computing, Wolfram became in 1981 the youngest recipient of a MacArthur Prize Fellowship Late in 1981, Wolfram then set out on an ambitious new direction in science: to develop a general theory of complexity in nature Wolfram's key idea was to use computer experiments to study the behavior of simple computer programs known as cellular automata And in 1982 he made the first in a series of startling discoveries about the origins of complexity The publication of Wolfram's papers on cellular automata led to a major shift in scientific thinking, and laid the groundwork for a new field of science that Wolfram named “complex systems research” Through the mid-1980s, Wolfram continued his work on complexity, discovering a number of fundamental connections between computation and nature, and inventing such concepts as computational irreducibility Wolfram's work led to a wide range of applications—and provided the main scientific foundations for the popular movements known as complexity theory and artificial life Wolfram himself used his ideas to develop a new randomness generation system and a new approach to computational fluid dynamics—bothof which are now in widespread use Following his scientific work on complex systems research, Wolfram in 1986 founded the first research center and first journal in the field Then, after a highly successful career in academia—first at Caltech, then at the Institute for Advanced Study in Princeton, and finally as Professor of Physics, Mathematics and Computer Science at the University of Illinois—Wolfram launched Wolfram Research, Inc Wolfram began the development of Mathematica in late 1986 The first version of Mathematica was released on June 23, 1988, and was immediately hailed as a major advance in computing In the years that followed, the popularity of Mathematica grew rapidly, and Wolfram Research became established as a world leader in the software industry, widely recognized for excellence in both technology and business Wolfram has been president and CEO of Wolfram Research since its inception, and continues to be personally responsible for the overall design of its core technology Following the release of Mathematica Version in 1991, Wolfram began to divide his time between Mathematica development and scientific research Building on his work from the mid-1980s, and now with Mathematica as a tool, Wolfram made a rapid succession of major new discoveries By the mid-1990s his discoveries led him to develop a fundamentally new conceptual framework, which he then spent the remainder of the 1990s applying not only to new kinds of questions, but also to many existing foundational problems in physics, biology, computer science, mathematics and several other fields After more than ten years of highly concentrated work, Wolfram finally described his achievements in his 1200-page book A New Kind of Science Released on May 14, 2002, the book was widely acclaimed and immediately became a bestseller Its publication has been seen as initiating a paradigm shift of historic importance in science In addition to leading Wolfram Research to break new ground with innovative technology, Wolfram is now developing a series of research and educational initiatives in the science he has created Other books by Stephen Wolfram: è Cellular Automata and Complexity: Collected Papers (1993) ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser è A New Kind of Science (2002) Author's website: www.stephenwolfram.com Author's address: email: s.wolfram@wolfram.com mail: c/o Wolfram Research, Inc 100 Trade Center Drive Champaign, IL 61820, USA For comments on this book or Mathematica send email to comments@wolfram.com ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser About Mathematica Mathematica is the world's only fully integrated environment for technical computing First released in 1988, it has had a profound effect on the way computers are used in many technical and other fields It is often said that the release of Mathematica marked the beginning of modern technical computing Ever since the 1960s individual packages had existed for specific numerical, algebraic, graphical and other tasks But the visionary concept of Mathematica was to create once and for all a single system that could handle all the various aspects of technical computing in a coherent and unified way The key intellectual advance that made this possible was the invention of a new kind of symbolic computer language that could for the first time manipulate the very wide range of objects involved in technical computing using only a fairly small number of basic primitives When Mathematica Version was released, the New York Times wrote that “the importance of the program cannot be overlooked”, and Business Week later ranked Mathematica among the ten most important new products of the year Mathematica was also hailed in the technical community as a major intellectual and practical revolution At first, Mathematica's impact was felt mainly in the physical sciences, engineering and mathematics But over the years, Mathematica has become important in a remarkably wide range of fields Mathematica is used today throughout the sciences—physical, biological, social and other—and counts many of the world's foremost scientists among its enthusiastic supporters It has played a crucial role in many important discoveries, and has been the basis for thousands of technical papers In engineering, Mathematica has become a standard tool for both development and production, and by now many of the world's important new products rely at one stage or another in their design on Mathematica In commerce, Mathematica has played a significant role in the growth of sophisticated financial modeling, as well as being widely used in many kinds of general planning and analysis Mathematica has also emerged as an important tool in computer science and software development: its language component is widely used as a research, prototyping and interface environment The largest part of Mathematica's user community consists of technical professionals But Mathematica is also heavily used in education, and there are now many hundreds of courses—from high school to graduate school—based on it In addition, with the availability of student versions, Mathematica has become an important tool for both technical and non-technical students around the world The diversity of Mathematica's user base is striking It spans all continents, ages from below ten up, and includes for example artists, composers, linguists and lawyers There are also many hobbyists from all walks of life who use Mathematica to further their interests in science, mathematics and computing Ever since Mathematica was first released, its user base has grown steadily, and by now the total number of users is above a million Mathematica has become a standard in a great many organizations, and it is used today in all of the Fortune 50 companies, all of the 15 major departments of the U.S government, and all of the 50 largest universities in the world At a technical level, Mathematica is widely regarded as a major feat of software engineering It is one of the largest single application programs ever developed, and it contains a vast array of novel algorithms and important technical innovations Among its core innovations are its interconnected algorithm knowledgebase, and its concepts of symbolic programming and of document-centered interfaces The development of Mathematica has been carried out at Wolfram Research by a world-class team led by Stephen Wolfram The success of Mathematica has fueled the continuing growth of Wolfram Research, and has allowed a large community of independent Mathematica-related businesses to develop There are today well over a hundred specialized commercial packages available for Mathematica, as well as more than three hundred books devoted to the system ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser New in Version Mathematica Version introduces important extensions to the Mathematica system, especially in scope and scalability of numeric and symbolic computation Building on the core language and extensive algorithm knowledgebase of Mathematica, Version introduces a new generation of advanced algorithms for a wide range of numeric and symbolic operations Numerical computation † Major optimization of dense numerical linear algebra † New optimized sparse numerical linear algebra † Support for optimized arbitrary-precision linear algebra † Generalized eigenvalues and singular value decomposition † LinearSolveFunction for repeated linear-system solving † p norms for vectors and matrices † Built-in MatrixRank for exact and approximate matrices † Support for large-scale linear programming, with interior point methods † New methods and array variable support in FindRoot and FindMinimum † FindFit for full nonlinear curve fitting † Constrained global optimization with NMinimize † Support for n -dimensional PDEs in NDSolve † Support for differential-algebraic equations in NDSolve † Support for vector and array-valued functions in NDSolve † Highly extensive collection of automatically-accessible algorithms in NDSolve † Finer precision and accuracy control for arbitrary-precision numbers † Higher-efficiency big number arithmetic, including processor-specific optimization † Enhanced algorithms for number theoretical operations including GCD and FactorInteger † Direct support for high-performance basic statistics functions Symbolic computation † Solutions to mixed systems of equations and inequalities in Reduce † Complete solving of polynomial systems over real or complex numbers † Solving large classes of Diophantine equations † ForAll and Exists quantifiers and quantifier elimination † Representation of discrete and continuous algebraic and transcendental solution sets † FindInstance for finding instances of solutions over different domains ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser † Exact constrained minimization over real and integer domains † Integrated support for assumptions using Assuming and Refine † RSolve for solving recurrence equations † Support for nonlinear, partial and q difference equations and systems † Full solutions to systems of rational ordinary differential equations † Support for differential-algebraic equations † CoefficientArrays for converting systems of equations to tensors Programming and Core System † Integrated language support for sparse arrays † New list programming with Sow and Reap † EvaluationMonitor and StepMonitor for algorithm monitoring † Enhanced timing measurement, including AbsoluteTiming † Major performance enhancements for MathLink † Optimization for 64-bit operating systems and architectures † Support for computations in full 64-bit address spaces Interfaces † Support for more than 50 import and export formats † High efficiency import and export of tabular data † PNG, SVG and DICOM graphics and imaging formats † Import and export of sparse matrix formats † MPS linear programming format † Cascading style sheets and XHTML for notebook exporting † Preview version of NET/Link for integration with NET Notebook Interface † Enhanced Help Browser design † Automatic copy/paste switching for Windows † Enhanced support for slide show presentation † AuthorTools support for notebook diffs Standard Add-on Packages † Statistical plots and graphics ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser † Algebraic number fields New in Versions 4.1 and 4.2 † Enhanced pattern matching of sequence objects † Enhanced optimizer for built-in Mathematica compiler † Enhanced continued fraction computation † Greatly enhanced DSolve † Additional TraditionalForm formats † Efficiency increases for multivariate polynomial operations † Support for import and export of DXF, STL, FITS and STDS data formats † Full support for CSV format import and export † Support for UTF character encodings † Extensive support for XML, including SymbolicXML subsystem and NotebookML † Native support for evaluation and formatting of Nand and Nor † High-efficiency CellularAutomaton function † J/Link MathLink-based Java capabilities † MathMLForm and extended MathML support † Extended simplification of Floor, Erf, ProductLog and related functions † Integration over regions defined by inequalities † Integration of piecewise functions † Standard package for visualization of regions defined by inequalities † ANOVA standard add-on package † Enhanced Combinatorica add-on package † AuthorTools notebook authoring environment ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser The Role of This Book The Scope of the Book This book is intended to be a complete introduction to Mathematica It describes essentially all the capabilities of Mathematica, and assumes no prior knowledge of the system In most uses of Mathematica, you will need to know only a small part of the system This book is organized to make it easy for you to learn the part you need for a particular calculation In many cases, for example, you may be able to set up your calculation simply by adapting some appropriate examples from the book You should understand, however, that the examples in this book are chosen primarily for their simplicity, rather than to correspond to realistic calculations in particular application areas There are many other publications that discuss Mathematica from the viewpoint of particular classes of applications In some cases, you may find it better to read one of these publications first, and read this book only when you need a more general perspective on Mathematica Mathematica is a system built on a fairly small set of very powerful principles This book describes those principles, but by no means spells out all of their implications In particular, while the book describes the elements that go into Mathematica programs, it does not give detailed examples of complete programs For those, you should look at other publications The Mathematica System Described in the Book This book describes the standard Mathematica kernel, as it exists on all computers that run Mathematica Most major supported features of the kernel in Mathematica Version are covered in this book Many of the important features of the front end are also discussed Mathematica is an open software system that can be customized in a wide variety of ways It is important to realize that this book covers only the full basic Mathematica system If your system is customized in some way, then it may behave differently from what is described in the book The most common form of customization is the addition of various Mathematica function definitions These may come, for example, from loading a Mathematica package Sometimes the definitions may actually modify the behavior of functions described in this book In other cases, the definitions may simply add a collection of new functions that are not described in the book In certain applications, it may be primarily these new functions that you use, rather than the standard ones described in the book This book describes what to when you interact directly with the standard Mathematica kernel and notebook front end Sometimes, however, you may not be using the standard Mathematica system directly Instead, Mathematica may be an embedded component of another system that you are using This system may for example call on Mathematica only for certain computations, and may hide the details of those computations from you Most of what is in this book will only be useful if you can give explicit input to Mathematica If all of your input is substantially modified by the system you are using, then you must rely on the documentation for that system Additional Mathematica Documentation For all standard versions of Mathematica, the following is available in printed form, and can be ordered from Wolfram Research: ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser A.13 Incompatible Changes A.13.1 Since Version Every new version of Mathematica contains many new features But careful design from the outset has allowed nearly total compatibility to be maintained between all versions As a result, almost any program written, say, for Mathematica Version in 1988 should be able to run without change in Mathematica Version 5—though it will often run considerably faster One inevitable problem, however, is that if a program uses names that begin with upper-case letters, then it is possible that since the version when the program was first written, built-in functions may have been added to Mathematica whose names conflict with those used in the program In addition, to maintain the overall coherence of Mathematica a few functions that existed in earlier versions have gradually been dropped—first becoming undocumented, and later generating warning messages if used Furthermore, it has in a few rare cases been necessary to makes changes to particular functions that are not compatible with their earlier operation This section lists all major incompatible changes from Mathematica Version onward A.13.2 Between Versions and † 260 new built-in objects have been added, some of whose names may conflict with names already being used † Accumulate has been superseded by FoldList; Fold has been added † Condition (/;) can now be used in individual patterns as well as in complete rules, and does not evaluate by default † The functionality of Release has been split between Evaluate and ReleaseHold † Compose has been superseded by Composition † Debug has been superseded by Trace and related functions † Power no longer automatically makes transformations such as Sqrt[x^2]Ø x † Limit now by default remains unevaluated if it encounters an unknown function † Mod now handles only numbers; PolynomialMod handles polynomials † CellArray has been superseded by Raster and RasterArray † FontForm takes a slightly different form of font specification † Framed has been superseded by Frame and related options † ContourLevels and ContourSpacing have been superseded by Contours † Plot3Matrix has been superseded by ViewCenter and ViewVertical † FromASCII and ToASCII have been superseded by FromCharacterCode and ToCharacterCode respectively † Alias has been superseded by $PreRead † ResetMedium has been subsumed in SetOptions, and $$Media has been superseded by Streams † StartProcess has been superseded by Install and by MathLink † Additional parts devoted to Mathematica as a programming language, and to examples of Mathematica packages, have been dropped from The Mathematica Book ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser A.13.3 Between Versions and † 259 new built-in objects have been added, some of whose names may conflict with names already being used † N[expr, n] now always tries to give n digits of precision if possible, rather than simply starting with n digits of precision † All expressions containing only numeric functions and numerical constants are now converted to approximate numerical form whenever they contain any approximate numbers † Many expressions involving exact numbers that used to remain unevaluated are now evaluated Example: Floor[(7/3)^20] † Plus and Times now apply built-in rules before user-defined ones, so it is no longer possible to make definitions such as 2+2=5 † The operator precedence for and ** has been changed so as to be below ^ This has the consequence that expressions previously written in InputForm as a b ^ n must now be written as (a b)^n V2Get[file] will read a file using old operator precedences † ỵ^ is now an operator used to generate a superscript Raw octal codes must be used instead of ỵ^A for inputting control characters † In Mathematica notebooks, several built-in Mathematica functions are now output by default using special characters Example: x->y is output as xØ y in StandardForm † More sophisticated definite integrals now yield explicit If constructs unless the option setting GenerateConditions-> False is used † HeldPart[expr, i, j, …] has been superseded by Extract[expr, i, j, … < , Hold] † Literal[pattern] has been replaced by HoldPattern[pattern] Verbatim[pattern] has been introduced Functions like DownValues return their results wrapped in HoldPattern rather than Literal † ReplaceHeldPart[expr, new, pos] has been superseded by ReplacePart[expr, Hold[new], pos, 1] † ToHeldExpression[expr] has been superseded by ToExpression[expr, form, Hold] † Trig as an option to algebraic manipulation functions has been superseded by the explicit functions TrigExpand, TrigFac tor and TrigReduce † AlgebraicRules has been superseded by PolynomialReduce † The option LegendreType has been superseded by an additional optional argument to LegendreP and LegendreQ † WeierstrassP[u, g2 , g3 < ] now takes g2 and g3 in a list † $Letters and $StringOrder now have built-in values only, but these handle all possible Mathematica characters † StringByteCount is no longer supported † Arbitrary-precision approximate real numbers are now given by default as digits`prec in InputForm This behavior is controlled by $NumberMarks † Large approximate real numbers are now given by default as digits*^exponent in InputForm † HomeDirectory[ ] has been replaced by $HomeDirectory † Dump has been superseded by DumpSave † $PipeSupported and $LinkSupported are now obsolete, since all computer systems support pipes and links † LinkOpen has been superseded by LinkCreate, LinkConnect and LinkLaunch † Subscripted has been superseded by RowBox, SubscriptBox, etc † Subscript and Superscript now represent complete subscripted and superscripted quantities, not just subscripts and superscripts † FontForm and DefaultFont have been superseded by StyleForm and TextStyle In the notebook front end, changes that were made include: † The file format for notebooks has been completely changed in order to support new notebook capabilities † Notebook files are now by default given nb rather than ma extensions; mb files are now superfluous ©1988-2003 Wolfram Research, Inc All rights reserved Printed from the Mathematica Help Browser † The front end will automatically ask to convert any old notebook that you tell it to open † The kernel command NotebookConvert can be used to convert notebook files from Version to Version format † The default format type for input cells is now StandardForm rather than InputForm † The organization of style sheets, as well as the settings for some default styles, have been changed † Some command key equivalents for menu items have been rearranged A.13.4 Between Versions and † 61 new built-in objects have been added, some of whose names may conflict with names already being used † N[0] now yields a machine-precision zero rather than an exact zero † FullOptions has been superseded by AbsoluteOptions, which yields results in the same form as Options † Element[x, y] or x ∈ y now has built-in evaluation rules † The symbols I and E are now output in StandardForm as  (ỵ[ImaginaryI]) and ‰ (ỵ[ExponentialE]) respectively † A new second argument has been added to CompiledFunction to allow easier manipulation and composition of compiled functions A.13.5 Between Versions and † 44 completely new built-in objects have been added, some of whose names may conflict with names already being used † Precision and Accuracy now return exact measures of uncertainty in numbers, not just estimates of integer numbers of digits † Precision now returns the symbol MachinePrecision for machine numbers, rather than the numerical value $Machine Precision † N[expr, MachinePrecision] is now used for numerical evaluation with machine numbers; N[expr, $MachinePreci sion] generates arbitrary-precision numbers † ConstrainedMin and ConstrainedMax have been superseded by Minimize, Maximize, NMinimize and NMaximize † SingularValues has been superseded by SingularValueList and SingularValueDecomposition Singular ValueDecomposition uses a different and more complete definition † FindRoot[f, x, x0 , x1