mathematica cookbook - gbv

10
Mathematica Cookbook Sal Mangano O'REILLT Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo

Upload: others

Post on 07-May-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mathematica Cookbook - GBV

Mathematica Cookbook

Sal Mangano

O'REILLT Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo

Page 2: Mathematica Cookbook - GBV

Table of Contents

Preface xv

1. Numerics 1 1.0 Introduction 1 1.1 Controlling Precision and Accuracy 4

1.2 Mixing Different Numerical Types 9

1.3 Representing Numbers in Other Bases 12 1.4 Extracting the Digits of a Number 13 1.5 Working with Intervals 16 1.6 Converting Between Numerical Types 18 1.7 Displaying Numbers in Alternate Forms 20

2. Functional Programming 23 2.0 Introduction 23

2.1 Mapping Functions with More Than One Argument 32 2.2 Holding Arbitrary Arguments 36 2.3 Creating Functions That Automatically Map Over Lists 37 2.4 Mapping Multiple Functions in a Single Pass 38

2.5 Keeping Track of the Index of Each Item As You Map 41 2.6 Mapping a Function over a Moving Sublist 43 2.7 Using Prefix and Postfix Notation to Produce More

Readable Code 48 2.8 Defining Indexed Functions 51 2.9 Understanding the Use of Fold As an Alternative to Recursion 53

2.10 Incremental Construction of Lists 57 2.11 Computing Through Repeated Function Application 59 2.12 Building a Function Through Iteration 62

v

Page 3: Mathematica Cookbook - GBV

2.13 Exploiting Function Composition and Inverse Functions 63 2.14 Implementing Closures 66

2.15 Currying in Mathematica 73 2.16 Creating Functions with Default Values 77 2.17 Creating Functions That Accept Options 79

3. Data Structures 85 3.0 Introduction 85 3.1 Ensuring the Most Efficient Representation of Numerical Lists 95

3.2 Sorting Lists 97 3.3 Determining Order Without Sorting 100 3.4 Extracting the Diagonals of a Matrix 102

3.5 Constructing Matrices of Specific Structure 103

3.6 Constructing Permutation and Shift Matrices 105 3.7 Manipulating Rows and Columns of Matrices 110

3.8 Using Sparse Arrays to Conserve Memory 112 3.9 Manipulating Deeply Nested Lists Using Functions

with Level Specifications 114 3.10 Implementing Bit Vectors and Using Format to Customize

Their Presentation 119 3.11 Implementing Trees and Traversals Using Lists 121 3.12 Implementing Ordered Associative Lookup Using

a Red-Black Tree 125 3.13 Exploiting Mathematical Built-in Associative Lookup 130 3.14 Constructing Graphs Using the Combinatorica" Package 134 3.15 Using Graph Algorithms to Extract Information from Graphs 140

4. Patterns and Rule-Based Programming 145 4.0 Introduction 145

4.1 Collecting Items That Match (or Don't Match) a Pattern 151 4.2 Excluding Items That Match (or Don't Match) a Pattern 153 4.3 Counting Items That Match a Pattern 155 4.4 Replacing Parts of an Expression 157

4.5 Finding the Longest (or Shortest) Match for a Pattern 159 4.6 Implementing Algorithms in Terms of Rules 161 4.7 Debugging Infinite Loops When Using ReplaceRepeated 165

4.8 Preventing Evaluation Until Replace Is Complete 168 4.9 Manipulating Patterns with Patterns 169

vi | Table of Contents

Page 4: Mathematica Cookbook - GBV

4.10 Optimizing Rules 170 4.11 Using Patterns As a Query Language 171 4.12 Semantic Pattern Matching 177 4.13 Unification Pattern Matching 178

5. String and Text Processing 181 5.0 Introduction 181

5.1 Comparing Strings 187 5.2 Removing and Replacing Characters from Strings 188 5.3 Extracting Characters and Substrings 192

5.4 Duplicating a String 196 5.5 Matching and Searching Text 198

5.6 Tokenizing Text 201 5.7 Working with Natural Language Dictionaries 202 5.8 Importing XML 209 5.9 Transforming XML Using Patterns and Rules 213

5.10 Transforming XML Using Recursive Functions (ä la XSLT) 218

5.11 Writing Parsers and Grammars in Mathematica 227

6. Two-Dimensional Graphics and Plots 237 6.0 Introduction 237 6.1 Plotting Functions in Cartesian Coordinates 238 6.2 Plotting in Polar Coordinates 247

6.3 Creating Plots Parametrically 249

6.4 Plotting Data 252 6.5 Mixing Two or More Graphs into a Single Graph 255 6.6 Displaying Multiple Graphs in a Grid 258 6.7 Creating Plots with Legends 260 6.8 Displaying 2D Geometric Shapes 263

6.9 Annotating Graphics with Text 269 6.10 Creating Custom Arrows 270

7. Three-Dimensional Plots and Graphics 275 7.0 Introduction 275 7.1 Plotting Functions of Two Variables in Cartesian Coordinates 276

7.2 Plotting Functions in Spherical Coordinates 283 7.3 Plotting Surfaces in Cylindrical Coordinates 285

7.4 Plotting 3D Surfaces Parametrically 290

Table of Contents | vii

Page 5: Mathematica Cookbook - GBV

7.5 Creating 3D Contour Plots 292 7.6 Combining 2D Contours with 3D Plots 295 7.7 Constraining Plots to Specified Regions 296 7.8 Plotting Data in 3D 298 7.9 Plotting 3D Regions Where a Predicate Is Satisfied 301

7.10 Displaying 3D Geometrical Shapes 302 7.11 Constructing Wireframe Models from Mesh 306 7.12 Controlling Viewing Geometry 309 7.13 Controlling Lighting and Surface Properties 313

7.14 Transforming 3D Graphics 317

7.15 Exploring Polyhedra 320 7.16 Importing 3D Graphics from CAD and Other 3D Software 326

8. Image Processing 329 8.0 Introduction 329 8.1 Extracting Image Information 332

8.2 Converting Images from RGB Color Space to HSV Color Space 335 8.3 Enhancing Images Using Histogram Equalization 341 8.4 Correcting Images Using Histogram Specification 347

8.5 Sharpening Images Using Laplacian Transforms 351 8.6 Sharpening and Smoothing with Fourier Transforms 356

8.7 Detecting Edges in Images 361 8.8 Image Recognition Using Eigenvectors (Eigenimages) 365

9. Audio and Music Processing 373 9.0 Introduction 373

9.1 Creating Musical Notes 374

9.2 Creating a Scale or a Melody 375 9.3 Adding Rhythm to a Melody 376 9.4 Controlling the Volume 377

9.5 Creating Chords 378 9.6 Playing a Chord Progression 379 9.7 Writing Music with Traditional Chord Notation 380

9.8 Creating Percussion Grooves 384 9.9 Creating More Complex Percussion Grooves 386

9.10 Exporting MIDI files 389 9.11 Playing Functions As Sound 390

viii | Table of Contents

Page 6: Mathematica Cookbook - GBV

9.12 Adding Tremolo 392 9.13 Adding Vibrato 393 9.14 Applying an Envelope to a Signal 394

9.15 Exploring Alternate Tunings 397 9.16 Importing Digital Sound Files 403

9.17 Analyzing Digital Sound Files 405 9.18 Slicing a Sample 408

10. Algebra 413 10.0 Introduction 413

10.1 Solving Algebraic Equations 414 10.2 Finding a Polynomial from a Given Root 415 10.3 Transforming Expressions to Other Forms 416

10.4 Generating Polynomials 419 10.5 Decomposing Polynomials into Their Constituent Parts 420 10.6 Dividing Polynomials by Other Polynomials 422

11. Calculus: Continuous and Discrete 425 11.0 Introduction 425 11.1 Computing Limits 426

11.2 Working with Piecewise Functions 427 11.3 Using Power Series Representations 429 11.4 Differentiating Functions 431

11.5 Integration 435 11.6 Solving Differential Equations 438

11.7 Solving Minima and Maxima Problems 441 11.8 Solving Vector Calculus Problems 443 11.9 Solving Problems Involving Sums and Products 447

11.10 Solving Difference Equations 450

11.11 Generating Functions and Sequence Recognition 452

12. Statistics and Data Analysis 455 12.0 Introduction 455 12.1 Computing Common Statistical Metrics of Numerical

and Symbolic Data 456 12.2 Generating Pseudorandom Numbers with a Given Distribution 459 12.3 Working with Probability Distributions 461 12.4 Demonstrating the Central Limit Theorem 464

Table of Contents I ix

Page 7: Mathematica Cookbook - GBV

12.5 Computing Covariance and Correlation of Vectors and Matrices 466

12.6 Measuring the Shape of Data 468 12.7 Finding and Adjusting for Outliers 471 12.8 Fitting Data Using a Linear Model 472 12.9 Fitting Data Using a Nonlinear Model 475

12.10 Creating Interpolation Functions from Data 477 12.11 Testing for Statistically Significant Difference Between

Groups Using ANOVA 479 12.12 Hypothesis Testing with Categorical Data 483 12.13 Grouping Data into Clusters 486 12.14 Creating Common Statistical Plots 492 12.15 Quasi-Random Number Generation 496 12.16 Creating Stochastic Simulations 499

13. Science and Engineering 505 13.0 Introduction 505

13.1 Working with Element Data 507

13.2 Working with Chemical Data 510 13.3 Working with Particle Data 513 13.4 Working with Genetic Data and Protein Data 516 13.5 Modeling Predator-Prey Dynamics 519 13.6 Solving Basic Rigid Bodies Problems 522

13.7 Solving Problems in Kinematics 524 13.8 Computing Normal Modes for Coupled Mass Problems 530 13.9 Modeling a Vibrating String 533

13.10 Modeling Electrical Circuits 537

13.11 Modeling Truss Structures Using the Finite Element Method 539

14. Financial Engineering 549 14.0 Introduction 549 14.1 Leveraging Mathematica's Bundled Financial Data 552

14.2 Importing Financial Data from Websites 557 14.3 Present Value of Future Cash Flows 559 14.4 Interest Rate Sensitivity of Bonds 561

14.5 Constructing and Manipulating Yield Curves 563

x | Table of Contents

Page 8: Mathematica Cookbook - GBV

14.6 Black-Scholes for European Option Pricing 565 14.7 Computing the Implied Volatility of Financial Derivatives 573

14.8 Speeding Up NDSolve When Solving Black-Scholes and Other PDEs 574

14.9 Developing an Explicit Finite Difference Method for the Black-Scholes Formula 578

14.10 Compiling an Implementation of Explicit Trinomial for Fast Pricing of American Options 583

14.11 Modeling the Value-at-Risk of a Portfolio Using Monte Carlo and Other Methods 585

14.12 Visualizing Trees for Interest-Rate Sensitive Instruments 587

15. Interactivity 593 15.0 Introduction 593

15.1 Manipulating a Variable 594 15.2 Manipulating a Symbolic Expression 598

15.3 Manipulating a Plot 600

15.4 Creating Expressions for Which Value Dynamically Updates 604 15.5 Intercepting the Values of a Control Attached

to a Dynamic Expression 607 15.6 Controlling Updates of Dynamic Values 609 15.7 Using DynamicModule As a Scoping Construct

in Interactive Notebooks 611 15.8 Using Scratch Variables with DynamicModule

to Balance Speed Versus Space 613 15.9 Making a Manipulate Self-Contained 615

15.10 Remembering the Values Found Using Manipulate 618 15.11 Improving Performance of Manipulate by Segregating Fast

and Slow Operations - 619

15.12 Localizing a Function in a Manipulate 622

15.13 Sharing DynamicModule Variables across Cell or Window Boundaries 624

15.14 Creating Your Own Custom Controls 625 15.15 Animating an Expression 627 15.16 Creating Custom Interfaces 630 15.17 Managing a Large Number of Controls in Limited

Screen Real Estate 633

Table of Contents | xi

Page 9: Mathematica Cookbook - GBV

16. Parallel Mathematica 641 16.0 Introduction 641 16.1 Configuring Local Kernels 643 16.2 Configuring Remote Services Kernels 646 16.3 Sending a Command to Multiple Kernels for Parallel Evaluation 648 16.4 Automatically Parallelizing Existing Serial Expressions 651 16.5 Distributing Data Segments in Parallel and Combining

the Results 653 16.6 Implementing Data-Parallel Algorithms by Using ParallelMap 654 16.7 Decomposing a Problem into Parallel Data Sets 656 16.8 Choosing an Appropriate Distribution Method 658 16.9 Running Different Algorithms in Parallel and Accepting

the First to Complete 661 16.10 Sharing Data Between Parallel Kernels 662 16.11 Preventing Race Conditions When Multiple Kernels

Access a Shared Resource 663 16.12 Organizing Parallel Processing Operations Using

a Pipeline Approach 665 16.13 Processing a Massive Number of Files Using

the Map-Reduce Technique 669 16.14 Diagnosing Parallel Processing Performance 678 16.15 Measuring the Overhead of Parallelization

in Your Environment 686

17. Interfacing Mathematica 689 17.0 Introduction 689 17.1 Calling External Command Line Programs from Mathematica 690 17.2 Launching Windows Programs from Mathematica 691 17.3 Connecting the Frontend to a Remote Kernel 692 17.4 Using Mathematica with С and C++ 694 17.5 Using Mathematica with Java 700 17.6 Using Mathematica to Interact with Microsoft's

.NET Framework 707 17.7 Using the Mathematica Kernelfrom a .NET Application 709 17.8 Querying a Database 711 17.9 Updating a Database 714

17.10 Introspection of Databases 715

xii | Table of Contents

Page 10: Mathematica Cookbook - GBV

18. Tricks of the Trade 719 18.0 Introduction 719 18.1 Cleaning Up During Incremental Development 720 18.2 Modifying Built-in Functions and Constants 721

18.3 Locating Undocumented Functions 723

18.4 Packaging Your Mathematica Solutions into Libraries for Others to Use 725

18.5 Compiling Functions to Improve Performance 727 18.6 Automating and Standardizing the Appearance

of Notebooks Using Stylesheets 728 18.7 Transforming Notebooks into Other Forms 733 18.8 Calling into the Mathematica Frontend 736 18.9 Initializing and Cleaning Up Automatically 737

18.10 Customizing Frontend User Interaction 739

19. Debugging and Testing 741 19.0 Introduction 741

19.1 Printing as the First Recourse to Debugging 743 19.2 Debugging Functions Called Many Times 746 19.3 Stack Tracing to Debug Recursive Functions 747

19.4 Taming Trace to Extract Useful Debugging Information 749 19.5 Creating a Poor Man's Mathematica Debugger 753 19.6 Debugging Built-in Functions with Evaluation

and Step Monitors 756 19.7 Visual Debugging with Wolfram Workbench 758 19.8 Writing Unit Tests to Help Ensure Correctness of Your Code 762 19.9 Creating MUnit Tests Where Success Is Not Based

on Equality Testing 765 19.10 Organizing and Controlling MUnit Tests and Test Suites 767 19.11 Integrating Wolfram Workbench's MUnit Package

into the Frontend 769

Index 777

Table of Contents | xiii