Table Of ContentMaple V
Programming Guide
Springer Science+Business Media, LLC
V
Maple
Programming Guide
M. B. Monagan K. O. Geddes K. M. Heal
G. Labahn S. M. Vorkoetter
With the Assistance of J. S. Devitt, M. L. Hansen,
D. Redfern, K. M. Rickard
Waterloo Maple Inc.
450 Phillip St.
Waterloo, ON N2L 5J2
Canada
Maple and Maple V are registered trademarks of Waterloo Maple Ine.
Printed on acid-free paper.
© 1998,1996 Springer Science+Business Media New York
Originally published by Waterloo Maple Inc. in 1998
AII rights reserved. This work may not be translated or copied in whole or in part without
the written permission of the copyright holder, exeept for brief excerpts in connection with
reviews or scholarly analysis. Use in connection with any form of information storage and
retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology
now known or hereafter developed is forbidden.
The use of general descriptive names, trade names, trademarks, etc., in this publication,
even if the former are not especially identified, is not to be taken as a sign that such names,
as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used
freely by anyone.
Camera-ready copy prepared using Springer-Verlag's TEX macros.
987654321
ISBN 978-0-387-98400-1 ISBN 978-1-4612-2216-3 (eBook)
DOI 10.1007/978-1-4612-2216-3
Contents
1. Introduction 1
1.1 Getting Started 2
Locals and Globals 6
Inputs, Parameters, Arguments 8
1.2 Basic Programming Constructs 10
The Assignment Statement 10
The for Loop 12
The Conditional Statement 13
The while Loop 18
Modularization 19
Recursive Procedures 20
Exercise 23
1.3 Basic Data Structures 23
Exercise 24
Exercise 26
A MEMBER Procedure 26
Exercise 27
Binary Search 27
Exercises 29
Plotting the Roots of a Polynomial 29
1.4 Computing with Formulae 31
The Height of a Polynomial 32
Exercise 34
The Chebyshev Polynomials, Tn (x) 34
Exercise 35
Integration by Parts 35
Exercise 37
v
vi • Contents
Computing with Symbolic Parameters 37
Exercise 40
2. Fundamentals 41
2.1 Evaluation Rules 42
Parameters 43
Local Variables 46
Global Variables 47
Exceptions 48
2.2 Nested Procedures 49
Local or Global? 50
The Quick-Sort Algorithm 51
Creating a Uniform Random Number Generator 54
2.3 Types 56
Types that Modify Evaluation Rules 56
Structured Types 60
Type Matching 61
2.4 Choosing a Data Structure: Connected Graphs 63
Exercises 68
2.5 Remember Tables 68
The remember Option 69
Adding Entries Explicitly 70
Removing Entries from a Remember Table 71
2.6 Conclusion 72
3. Advanced Programming 74
3.1 Procedures Which Return Procedures 75
Creating a Newton Iteration 75
A Shift Operator 77
3.2 When Local Variables Leave Home 79
Creating the Cartesian Product of a Sequence of Sets 81
Exercises 85
3.3 Interactive Input 86
Reading Strings from the Terminal 86
Reading Expressions from the Terminal 87
Converting Strings to Expressions 88
3.4 Extending Maple 89
Defining New Types 89
Exercises 91
Neutral Operators 91
Exercise 94
Extending Certain Commands 96
Contents • vii
3.5 Writing Your Own Packages 98
Package Initialization 100
Making Your Own Library 102
3.6 Conclusion 105
4. The Maple Language 107
4.1 Language Elements 109
The Character Set 109
Tokens 109
Token Separators 113
4.2 Escape Characters 116
4.3 Statements 116
The Assignment Statement 117
Unassignment: Clearing a Name 121
The Selection Statement 123
The Repetition Statement 125
The read and save Statements 129
4.4 Expressions 131
Expression Trees: Internal Representation 131
The Types and Operands of Integers, Strings, Indexed
Names, and Concatenations 135
Fractions and Rational Numbers 137
Floating-Point (Decimal) Numbers 137
Complex Numerical Constants 140
Labels 141
Sequences 142
Sets and Lists 145
Functions 148
The Arithmetic Operators 151
Non-Commutative Multiplication 153
The Composition Operators 154
The Ditto Operators 155
The Factorial Operator 155
The mod Operator 156
The Neutral Operators 157
Relations and Logical Operators 158
Arrays and Tables 162
Series 164
Ranges 166
Unevaluated Expressions 167
Constants 169
Structured Types 169
viii • Contents
4.5 Useful Looping Constructs 172
The map, select, and remove Commands 172
The zip Command 175
The seq, add, and mul Commands 176
4.6 Substitution 178
4.7 Conclusion 180
5. Procedures 181
5.1 Procedure Definitions 181
Mapping Notation 182
Unnamed Procedures and Their Combinations 183
Procedure Simplification 184
5.2 Parameter Passing 184
Declared Parameters 185
The Sequence of Arguments 186
5.3 Local and Global Variables 187
Evaluation of Local Variables 189
5.4 Procedure Options and the Description Field 191
Options 191
The Description Field 194
5.5 The Value Returned by a Procedure 194
Assigning Values to Parameters 195
Explicit Returns 197
Error Returns 198
Trapping Errors 199
Returning Unevaluated 200
Exercise 202
5.6 The Procedure Object 202
Last Name Evaluation 202
The Type and Operands of a Procedure 203
Saving and Retrieving Procedures 205
5.7 Explorations 206
Exercises 206
5.8 Conclusion 207
6. Debugging Maple Programs 209
6.1 A Tutorial Example 209
6.2 Invoking the Debugger 218
Displaying the Statements of a Procedure 218
Breakpoints 219
Watchpoints 222
Error Watchpoints 223
Contents • ix
6.3 Examining and Changing the State of the System 226
6.4 Controlling Execution 232
6.5 Restrictions 236
7. Numerical Programming in Maple 237
7.1 The Basics of evalf 238
7.2 Hardware Floating-Point Numbers 241
Newton Iterations 243
Computing with Arrays of Numbers 245
7.3 Floating-Point Models in Maple 247
Software Floats 248
Hardware Floats 249
Roundoff Error 249
7.4 Extending the evalf Command 251
Defining Your Own Constants 251
Defining Your Own Functions 253
7.5 Using the Matlab Package 255
7.6 Conclusion 256
8. Programming with Maple Graphics 257
8.1 Basic Plot Functions 257
8.2 Programming with Plotting Library Functions 261
Plotting a Loop 261
A Ribbon Plot Procedure 263
8.3 Maple's Plotting Data Structures 266
The PLOT Data Structure 268
A Sum Plot 271
The PLOT3D Data Structure 273
8.4 Programming with Plot Data Structures 277
Writing Graphic Primitives 277
Plotting Gears 279
Polygon Meshes 283
8.5 Programming with the plottools Package 285
A Pie Chart 286
A Dropshadow Procedure 288
Creating a Tiling 289
A Smith Chart 291
Modifying Polygon Meshes 292
8.6 Example: Vector Field Plots 297
8.7 Generating Grids of Points 309
8.8 Animation 313