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