Mathematica Cookbook

Book description

Mathematica Cookbook helps you master the application's core principles by walking you through real-world problems. Ideal for browsing, this book includes recipes for working with numerics, data structures, algebraic equations, calculus, and statistics. You'll also venture into exotic territory with recipes for data visualization using 2D and 3D graphic tools, image processing, and music.

Although Mathematica 7 is a highly advanced computational platform, the recipes in this book make it accessible to everyone -- whether you're working on high school algebra, simple graphs, PhD-level computation, financial analysis, or advanced engineering models.

  • Learn how to use Mathematica at a higher level with functional programming and pattern matching
  • Delve into the rich library of functions for string and structured text manipulation
  • Learn how to apply the tools to physics and engineering problems
  • Draw on Mathematica's access to physics, chemistry, and biology data
  • Get techniques for solving equations in computational finance
  • Learn how to use Mathematica for sophisticated image processing
  • Process music and audio as musical notes, analog waveforms, or digital sound samples
Visit the catalog page for Mathematica CookbookVisit the errata page for Mathematica Cookbook

Table of contents

  1. Dedication
  2. Special Upgrade Offer
  3. Preface
    1. Introduction
    2. MathematicaCookbook.com
    3. Structure of This Book
    4. Acknowledgments
    5. Conventions Used in This Book
    6. Using Code Examples
    7. Safari® Enabled
    8. How to Contact Us
  4. 1. Numerics
    1. 1.0 Introduction
    2. 1.1 Controlling Precision and Accuracy
    3. 1.2 Mixing Different Numerical Types
    4. 1.3 Representing Numbers in Other Bases
    5. 1.4 Extracting the Digits of a Number
    6. 1.5 Working with Intervals
    7. 1.6 Converting Between Numerical Types
    8. 1.7 Displaying Numbers in Alternate Forms
  5. 2. Functional Programming
    1. 2.0 Introduction
    2. 2.1 Mapping Functions with More Than One Argument
    3. 2.2 Holding Arbitrary Arguments
    4. 2.3 Creating Functions That Automatically Map Over Lists
    5. 2.4 Mapping Multiple Functions in a Single Pass
    6. 2.5 Keeping Track of the Index of Each Item As You Map
    7. 2.6 Mapping a Function over a Moving Sublist
    8. 2.7 Using Prefix and Postfix Notation to Produce More Readable Code
    9. 2.8 Defining Indexed Functions
    10. 2.9 Understanding the Use of Fold As an Alternative to Recursion
    11. 2.10 Incremental Construction of Lists
    12. 2.11 Computing Through Repeated Function Application
    13. 2.12 Building a Function Through Iteration
    14. 2.13 Exploiting Function Composition and Inverse Functions
    15. 2.14 Implementing Closures
    16. 2.15 Currying in Mathematica
    17. 2.16 Creating Functions with Default Values
    18. 2.17 Creating Functions That Accept Options
  6. 3. Data Structures
    1. 3.0 Introduction
    2. 3.1 Ensuring the Most Efficient Representation of Numerical Lists
    3. 3.2 Sorting Lists
    4. 3.3 Determining Order Without Sorting
    5. 3.4 Extracting the Diagonals of a Matrix
    6. 3.5 Constructing Matrices of Specific Structure
    7. 3.6 Constructing Permutation and Shift Matrices
    8. 3.7 Manipulating Rows and Columns of Matrices
    9. 3.8 Using Sparse Arrays to Conserve Memory
    10. 3.9 Manipulating Deeply Nested Lists Using Functions with Level Specifications
    11. 3.10 Implementing Bit Vectors and Using Format to Customize Their Presentation
    12. 3.11 Implementing Trees and Traversals Using Lists
    13. 3.12 Implementing Ordered Associative Lookup Using a Red-Black Tree
    14. 3.13 Exploiting Mathematica’s Built-In Associative Lookup
    15. 3.14 Constructing Graphs Using the Combinatorica’ Package
    16. 3.15 Using Graph Algorithms to Extract Information from Graphs
  7. 4. Patterns and Rule-Based Programming
    1. 4.0 Introduction
    2. 4.1 Collecting Items That Match (or Don’t Match) a Pattern
    3. 4.2 Excluding Items That Match (or Don’t Match) a Pattern
    4. 4.3 Counting Items That Match a Pattern
    5. 4.4 Replacing Parts of an Expression
    6. 4.5 Finding the Longest (or Shortest) Match for a Pattern
    7. 4.6 Implementing Algorithms in Terms of Rules
    8. 4.7 Debugging Infinite Loops When Using ReplaceRepeated
    9. 4.8 Preventing Evaluation Until Replace Is Complete
    10. 4.9 Manipulating Patterns with Patterns
    11. 4.10 Optimizing Rules
    12. 4.11 Using Patterns As a Query Language
    13. 4.12 Semantic Pattern Matching
    14. 4.13 Unification Pattern Matching
  8. 5. String and Text Processing
    1. 5.0 Introduction
    2. 5.1 Comparing Strings
    3. 5.2 Removing and Replacing Characters from Strings
    4. 5.3 Extracting Characters and Substrings
    5. 5.4 Duplicating a String
    6. 5.5 Matching and Searching Text
    7. 5.6 Tokenizing Text
    8. 5.7 Working with Natural Language Dictionaries
    9. 5.8 Importing XML
    10. 5.9 Transforming XML Using Patterns and Rules
    11. 5.10 Transforming XML Using Recursive Functions (à la XSLT)
    12. 5.11 Writing Parsers and Grammars in Mathematica
  9. 6. Two-Dimensional Graphics and Plots
    1. 6.0 Introduction
    2. 6.1 Plotting Functions in Cartesian Coordinates
    3. 6.2 Plotting in Polar Coordinates
    4. 6.3 Creating Plots Parametrically
    5. 6.4 Plotting Data
    6. 6.5 Mixing Two or More Graphs into a Single Graph
    7. 6.6 Displaying Multiple Graphs in a Grid
    8. 6.7 Creating Plots with Legends
    9. 6.8 Displaying 2D Geometric Shapes
    10. 6.9 Annotating Graphics with Text
    11. 6.10 Creating Custom Arrows
  10. 7. Three-Dimensional Graphics and Plots
    1. 7.0 Introduction
    2. 7.1 Plotting Functions of Two Variables in Cartesian Coordinates
    3. 7.2 Plotting Functions in Spherical Coordinates
    4. 7.3 Plotting Surfaces in Cylindrical Coordinates
    5. 7.4 Plotting 3D Surfaces Parametrically
    6. 7.5 Creating 3D Contour Plots
    7. 7.6 Combining 2D Contours with 3D Plots
    8. 7.7 Constraining Plots to Specified Regions
    9. 7.8 Plotting Data in 3D
    10. 7.9 Plotting 3D Regions Where a Predicate Is Satisfied
    11. 7.10 Displaying 3D Geometrical Shapes
    12. 7.11 Constructing Wireframe Models from Mesh
    13. 7.12 Controlling Viewing Geometry
    14. 7.13 Controlling Lighting and Surface Properties
    15. 7.14 Transforming 3D Graphics
    16. 7.15 Exploring Polyhedra
    17. 7.16 Importing 3D Graphics from CAD and Other 3D Software
  11. 8. Image Processing
    1. 8.0 Introduction
    2. 8.1 Extracting Image Information
    3. 8.2 Converting Images from RGB Color Space to HSV Color Space
    4. 8.3 Enhancing Images Using Histogram Equalization
    5. 8.4 Correcting Images Using Histogram Specification
    6. 8.5 Sharpening Images Using Laplacian Transforms
    7. 8.6 Sharpening and Smoothing with Fourier Transforms
    8. 8.7 Detecting Edges in Images
    9. 8.8 Image Recognition Using Eigenvectors (Eigenimages)
  12. 9. Audio and Music Processing
    1. 9.0 Introduction
    2. 9.1 Creating Musical Notes
    3. 9.2 Creating a Scale or a Melody
    4. 9.3 Adding Rhythm to a Melody
    5. 9.4 Controlling the Volume
    6. 9.5 Creating Chords
    7. 9.6 Playing a Chord Progression
    8. 9.7 Writing Music with Traditional Chord Notation
    9. 9.8 Creating Percussion Grooves
    10. 9.9 Creating More Complex Percussion Grooves
    11. 9.10 Exporting MIDI files
    12. 9.11 Playing Functions As Sound
    13. 9.12 Adding Tremolo
    14. 9.13 Adding Vibrato
    15. 9.14 Applying an Envelope to a Signal
    16. 9.15 Exploring Alternate Tunings
    17. 9.16 Importing Digital Sound Files
    18. 9.17 Analyzing Digital Sound Files
    19. 9.18 Slicing a Sample
  13. 10. Algebra
    1. 10.0 Introduction
    2. 10.1 Solving Algebraic Equations
    3. 10.2 Finding a Polynomial from a Given Root
    4. 10.3 Transforming Expressions to Other Forms
    5. 10.4 Generating Polynomials
    6. 10.5 Decomposing Polynomials into Their Constituent Parts
    7. 10.6 Dividing Polynomials by Other Polynomials
  14. 11. Calculus: Continuous and Discrete
    1. 11.0 Introduction
    2. 11.1 Computing Limits
    3. 11.2 Working with Piecewise Functions
    4. 11.3 Using Power Series Representations
    5. 11.4 Differentiating Functions
    6. 11.5 Integration
    7. 11.6 Solving Differential Equations
    8. 11.7 Solving Minima and Maxima Problems
    9. 11.8 Solving Vector Calculus Problems
    10. 11.9 Solving Problems Involving Sums and Products
    11. 11.10 Solving Difference Equations
    12. 11.11 Generating Functions and Sequence Recognition
  15. 12. Statistics and Data Analysis
    1. 12.0 Introduction
    2. 12.1 Computing Common Statistical Metrics of Numerical and Symbolic Data
    3. 12.2 Generating Pseudorandom Numbers with a Given Distribution
    4. 12.3 Working with Probability Distributions
    5. 12.4 Demonstrating the Central Limit Theorem
    6. 12.5 Computing Covariance and Correlation of Vectors and Matrices
    7. 12.6 Measuring the Shape of Data
    8. 12.7 Finding and Adjusting for Outliers
    9. 12.8 Fitting Data Using a Linear Model
    10. 12.9 Fitting Data Using a Nonlinear Model
    11. 12.10 Creating Interpolation Functions from Data
    12. 12.11 Testing for Statistically Significant Difference Between Groups Using ANOVA
    13. 12.12 Hypothesis Testing with Categorical Data
    14. 12.13 Grouping Data into Clusters
    15. 12.14 Creating Common Statistical Plots
    16. 12.15 Quasi-Random Number Generation
    17. 12.16 Creating Stochastic Simulations
  16. 13. Science and Engineering
    1. 13.0 Introduction
    2. 13.1 Working with Element Data
    3. 13.2 Working with Chemical Data
    4. 13.3 Working with Particle Data
    5. 13.4 Working with Genetic Data and Protein Data
    6. 13.5 Modeling Predator-Prey Dynamics
    7. 13.6 Solving Basic Rigid Bodies Problems
    8. 13.7 Solving Problems in Kinematics
    9. 13.8 Computing Normal Modes for Coupled Mass Problems
    10. 13.9 Modeling a Vibrating String
    11. 13.10 Modeling Electrical Circuits
    12. 13.11 Modeling Truss Structures Using the Finite Element Method
  17. 14. Financial Engineering
    1. 14.0 Introduction
    2. 14.1 Leveraging Mathematica’s Bundled Financial Data
    3. 14.2 Importing Financial Data from Websites
    4. 14.3 Present Value of Future Cash Flows
    5. 14.4 Interest Rate Sensitivity of Bonds
    6. 14.5 Constructing and Manipulating Yield Curves
    7. 14.6 Black-Scholes for European Option Pricing
    8. 14.7 Computing the Implied Volatility of Financial Derivatives
    9. 14.8 Speeding Up NDSolve When Solving Black-Scholes and Other PDEs
    10. 14.9 Developing an Explicit Finite Difference Method for the Black-Scholes Formula
    11. 14.10 Compiling an Implementation of Explicit Trinomial for Fast Pricing of American Options
    12. 14.11 Modeling the Value-at-Risk of a Portfolio Using Monte Carlo and Other Methods
    13. 14.12 Visualizing Trees for Interest-Rate Sensitive Instruments
  18. 15. Interactivity
    1. 15.0 Introduction
    2. 15.1 Manipulating a Variable
    3. 15.2 Manipulating a Symbolic Expression
    4. 15.3 Manipulating a Plot
    5. 15.4 Creating Expressions for Which Value Dynamically Updates
    6. 15.5 Intercepting the Values of a Control Attached to a Dynamic Expression
    7. 15.6 Controlling Updates of Dynamic Values
    8. 15.7 Using DynamicModule As a Scoping Construct in Interactive Notebooks
    9. 15.8 Using Scratch Variables with DynamicModule to Balance Speed Versus Space
    10. 15.9 Making a Manipulate Self-Contained
    11. 15.10 Remembering the Values Found Using Manipulate
    12. 15.11 Improving Performance of Manipulate by Segregating Fast and Slow Operations
    13. 15.12 Localizing a Function in a Manipulate
    14. 15.13 Sharing DynamicModule Variables across Cell or Window Boundaries
    15. 15.14 Creating Your Own Custom Controls
    16. 15.15 Animating an Expression
    17. 15.16 Creating Custom Interfaces
    18. 15.17 Managing a Large Number of Controls in Limited Screen Real Estate
  19. 16. Parallel Mathematica
    1. 16.0 Introduction
    2. 16.1 Configuring Local Kernels
    3. 16.2 Configuring Remote Services Kernels
    4. 16.3 Sending a Command to Multiple Kernels for Parallel Evaluation
    5. 16.4 Automatically Parallelizing Existing Serial Expressions
    6. 16.5 Distributing Data Segments in Parallel and Combining the Results
    7. 16.6 Implementing Data-Parallel Algorithms by Using ParallelMap
    8. 16.7 Decomposing a Problem into Parallel Data Sets
    9. 16.8 Choosing an Appropriate Distribution Method
    10. 16.9 Running Different Algorithms in Parallel and Accepting the First to Complete
    11. 16.10 Sharing Data Between Parallel Kernels
    12. 16.11 Preventing Race Conditions When Multiple Kernels Access a Shared Resource
    13. 16.12 Organizing Parallel Processing Operations Using a Pipeline Approach
    14. 16.13 Processing a Massive Number of Files Using the Map-Reduce Technique
    15. 16.14 Diagnosing Parallel Processing Performance
    16. 16.15 Measuring the Overhead of Parallelization in Your Environment
  20. 17. Interfacing Mathematica
    1. 17.0 Introduction
    2. 17.1 Calling External Command Line Programs from Mathematica
    3. 17.2 Launching Windows Programs from Mathematica
    4. 17.3 Connecting the Frontend to a Remote Kernel
    5. 17.4 Using Mathematica with C and C++
    6. 17.5 Using Mathematica with Java
    7. 17.6 Using Mathematica to Interact with Microsoft’s .NET Framework
    8. 17.7 Using the Mathematica Kernel from a .NET Application
    9. 17.8 Querying a Database
    10. 17.9 Updating a Database
    11. 17.10 Introspection of Databases
  21. 18. Tricks of the Trade
    1. 18.0 Introduction
    2. 18.1 Cleaning Up During Incremental Development
    3. 18.2 Modifying Built-in Functions and Constants
    4. 18.3 Locating Undocumented Functions
    5. 18.4 Packaging Your Mathematica Solutions into Libraries for Others to Use
    6. 18.5 Compiling Functions to Improve Performance
    7. 18.6 Automating and Standardizing the Appearance of Notebooks Using Stylesheets
    8. 18.7 Transforming Notebooks into Other Forms
    9. 18.8 Calling into the Mathematica Frontend
    10. 18.9 Initializing and Cleaning Up Automatically
    11. 18.10 Customizing Frontend User Interaction
  22. 19. Debugging and Testing
    1. 19.0 Introduction
    2. 19.1 Printing as the First Recourse to Debugging
    3. 19.2 Debugging Functions Called Many Times
    4. 19.3 Stack Tracing to Debug Recursive Functions
    5. 19.4 Taming Trace to Extract Useful Debugging Information
    6. 19.5 Creating a Poor Man’s Mathematica Debugger
    7. 19.6 Debugging Built-In Functions with Evaluation and Step Monitors
    8. 19.7 Visual Debugging with Wolfram Workbench
    9. 19.8 Writing Unit Tests to Help Ensure Correctness of Your Code
    10. 19.9 Creating MUnit Tests Where Success Is Not Based on Equality Testing
    11. 19.10 Organizing and Controlling MUnit Tests and Test Suites
    12. 19.11 Integrating Wolfram Workbench’s MUnit Package into the Frontend
  23. A. About the Author
  24. Index
  25. About the Author
  26. Colophon
  27. Special Upgrade Offer
  28. Copyright

Product information

  • Title: Mathematica Cookbook
  • Author(s): Sal Mangano
  • Release date: May 2010
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596520991