Maple Introductory Programming Guide M. B. Monagan - K. O. Geddes - K. M. Heal G. Labahn S. - M. Vorkoetter - J. McCarron P. DeMarco Contents Preface 1 Audience 1 Worksheet Graphical Interface 1 Manual Set . 2 Conventions . 2 The Maple Programming Language . 3 Using This Book . 3 Customer Feedback . 4 1 Introduction to Programming in Maple 5 In This Chapter . 5 1.1 The Maple Software . 5 1.2 Maple Statements 6 Getting Started 6 Maple Statements 7 Getting Help . 7 Displaying a Text String . 7 Performing an Arithmetic Operation 8 Assigning to a Name . 8 Using Maple Library Routines 9 1.3 Procedures 9 Deßning a Simple Procedure 9 Maple Library Routines, Built-In Routines, and User- Deßned Procedures 13 1.4 Interrupting Computations and Clearing the Internal Mem- ory . 16 Interrupting a Maple Computation . 16 Clearing the Maple Internal Memory 17 1.5 Troubleshooting 17 Unexpected End of Statement 17 Missing Operator . 18 Invalid, Wrong Number or Type of Arguments 19 Unbalanced Parentheses . 19 Assignment Versus Equality . 20 1.6 Exercises . 21 1.7 Conclusion 21 2 Maple Language Elements 23 In This Chapter . 23 2.1 Character Set . 23 2.2 Tokens . 24 Reserved Words 24 Programming-Language Operators . 25 Names . 26 Strings . 38 Natural Integers . 46 2.3 Using Special Characters . 46 Token Separators . 46 Blank Spaces, New Lines, Comments, and Continuation . 46 Punctuation Marks 48 Escape Characters 51 2.4 Types and Operands . 51 Integers 52 Strings . 53 Names . 53 Concatenations 55 2.5 Troubleshooting 55 Attempting to Assign to a Protected Name 55 Invalid Left-Hand Assignment 56 Incorrect Syntax in Parse 56 White Space Characters within a Token 56 Incorrect Use of Double and Single Quotes . 57 2.6 Exercises . 58 2.7 Conclusion 583 Maple Expressions and Statements 59 In This Chapter . 59 3.1 Syntax and Semantics 59 Syntax . 59 Semantics . 60 3.2 Expressions 61 Constants . 61 Operators . 70 Data Structures 89 Functions . 92 3.3 Using Expressions 96 Investigating the Parts of an Expression 96 Evaluating and Simplifying Expressions 100 Unevaluated Expressions . 100 Substituting Subexpressions . 103 Displaying Large Expressions: Labels 105 Structured Types . 106 3.4 Statements 110 The Assignment Statement . 111 Selection Statements . 114 Repetition Statements 115 The read and save Statements . 117 The break and next Statements 118 The error and return Statements . 118 The use Statement 119 The quit Statement . 119 3.5 Troubleshooting 119 Syntax Errors . 120 Reserved Word Unexpected . 120 Break or Next Not in Loop . 121 3.6 Exercises . 121 3.7 Conclusion 122 4 Basic Data Structures 123 In This Chapter . 123 4.1 Sets 123 Manipulating a Set 124 4.2 Lists 126 Manipulating a List . 128 4.3 Tables . 133 An Alternate Method for Generating a Table . 135 Table Evaluation Rules . 135 Manipulating a Table 136 4.4 arrays and Arrays 141 Evaluation Rules for arrays . 144 Manipulating arrays and Arrays . 145 4.5 Selecting Elements from a Data Structure . 152 In This Chapter . 1936.1 Deßning a Procedure . 193 Naming a Procedure . 194 Executing a Procedure 194 6.2 Procedure Components . 196 Formal Parameters 196 Variables . 201 Procedure Options 206 Procedure Description 214 Procedure Body Statements . 215 6.3 Procedure Interpretation . 215 6.4 Procedure Return Values 217 Explicit Returns . 217 Error Returns . 219 Returning Values through Parameters . 220 Returning Unevaluated . 222 6.5 Documenting Your Procedures . 225 Indicating Copyright . 225 Formatting Code for Readability 225 Adding Comments 226 Including a Procedure Description . 226 Creating Help Pages . 227 6.6 Saving and Retrieving Procedures . 227 6.7 Viewing Maple Library Source Code 228 Special Evaluation Rules for Procedures 228 Displaying Maple Library Source Code . 229 6.8 Alternate Methods for Deßning a Procedure 231 Functional Operators: Mapping Notation . 231 The unapply Command . 232 Unnamed Procedures and the map Command . 232 Building a List of Arguments 234 6.9 The Procedure Object 236 The procedure Type . 236 Procedure Operands . 237 6.10 Using Data Structures to Solve Problems . 239 Computing an Average 239 Testing for Membership . 241 Performing a Binary Search . 242 Plotting the Roots of a Polynomial . 243 Connected Graphs 246 6.11 Troubleshooting 251 Missing Argument 251 Incorrect Argument Type 252 Implicitly Declared Local Variable . 252 Understanding Names, Functions, and Remember Tables . 253 6.12 Exercises . 255 6.13 Conclusion 256 7 Input and Output 257 In This Chapter . 257 7.1 Screen and Keyboard I/O 257 Printing Output to the Screen 257 Interactive Input . 264 7.2 Standard Disk I/O 269 Readable File Formats in Maple 270 Using Filenames in Maple Statements . 271 Reading and Saving Data in Maple . 271 Importing Data from Files 274 Exporting Data to Files . 278 7.3 Troubleshooting 280 Syntax Error in readstat 280 Extra Characters at End of Parsed String . 280 Unable to Read Filename 281 7.4 Exercises . 281 7.5 Conclusion 282 8 Debugging and Eáciency 283 In This Chapter . 283 8.1 A Tutorial Example . 284 Numbering the Procedure Statements I 285 Invoking the Debugger I . 285 Controlling the Execution of a Procedure During Debug- ging I . 287 Invoking the Debugger II 292 8.2 Maple Debugger Commands . 296 Numbering the Procedure Statements II 296 Invoking the Debugger III 296 Controlling the Execution of a Procedure During Debug- ging II . 305 Changing the State of a Procedure During Debugging 306 Examining the State of a Procedure During Debugging 309 Using Top-Level Commands at the Debugger Prompt 314 Restrictions 3148.3 Detecting Errors . 315 Tracing a Procedure . 315 Using Assertions . 320 Handling Exceptions . 323 Checking Syntax . 329 8.4 Creating Eácient Programs . 330 Displaying Time and Memory Statistics 330 Proßling a Procedure . 332 8.5 Managing Resources . 334 Setting a Time Limit on Computations . 334 Garbage Collection 335 Communicating with the Kernel 336 8.6 Exercises . 336 8.7 Conclusion 337 9 Introduction to the Maplet User Interface Customization System 339 In This Chapter . 339 9.1 Uses of Maplet Applications . 339 Custom Maple Calculators 340 Interfaces to Maple Packages and Routines 341 Queries 342 Messages . 342 9.2 The Maplets Package . 342 Elements . 343 Examples . 343 Tools 343 Display 343 9.3 Terminology 343 9.4 Elements . 344 Window Body Elements . 344 Layout Elements . 353 MenuBar Elements 354 ToolBar Elements 355 Command Elements . 356 Dialog Elements . 357 Other Elements 361 Reference Options 363 9.5 Example Maplet Applications 363 Linear Algebra Subpackage . 364 Using the Example Maplet Applications 364 9.6 Tools 366 Maplet System Global Variables 367 9.7 Running a Maplet Application . 367 9.8 Writing a Maplet Application 368 Deßning a Maplet Application . 368 Maplet Application Programming Style Guidelines 368 9.9 After Reading This Chapter . 369 9.10 Conclusion 370 Index 371