Maple 7 Programming Guide
Total Page:16
File Type:pdf, Size:1020Kb
Maple 7 Programming Guide M. B. Monagan K. O. Geddes K. M. Heal G. Labahn S. M. Vorkoetter J. McCarron P. DeMarco c 2001 by Waterloo Maple Inc. ii • Waterloo Maple Inc. 57 Erb Street West Waterloo, ON N2L 6C2 Canada Maple and Maple V are registered trademarks of Waterloo Maple Inc. c 2001, 2000, 1998, 1996 by Waterloo Maple Inc. All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the copyright holder, except 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. Contents 1 Introduction 1 1.1 Getting Started ........................ 2 Locals and Globals ...................... 7 Inputs, Parameters, Arguments ............... 8 1.2 Basic Programming Constructs ............... 11 The Assignment Statement ................. 11 The for Loop......................... 13 The Conditional Statement ................. 15 The while Loop....................... 19 Modularization ........................ 20 Recursive Procedures ..................... 22 Exercise ............................ 24 1.3 Basic Data Structures .................... 25 Exercise ............................ 26 Exercise ............................ 28 A MEMBER Procedure ..................... 28 Exercise ............................ 29 Binary Search ......................... 29 Exercises ........................... 30 Plotting the Roots of a Polynomial ............. 31 1.4 Computing with Formulæ .................. 33 The Height of a Polynomial ................. 34 Exercise ............................ 36 The Chebyshev Polynomials, Tn(x)............. 36 Exercise ............................ 37 Integration by Parts ..................... 37 Exercises ........................... 39 Computing with Symbolic Parameters ........... 39 Exercise ............................ 42 1.5 Conclusion .......................... 42 iii iv • Contents 2 Fundamentals 45 2.1 Evaluation Rules ....................... 46 Parameters .......................... 47 Local Variables ........................ 50 Global Variables ....................... 51 Exceptions .......................... 52 2.2 Nested Procedures ...................... 54 Local Versus Global Variables ................ 55 The Quick-Sort Algorithm .................. 56 Creating a Uniform Random Number Generator ..... 59 2.3Types............................. 62 Types that Modify Evaluation Rules ............ 62 Structured Types ....................... 66 Type Matching ........................ 68 2.4 Choosing a Data Structure: Connected Graphs ...... 70 Exercises ........................... 75 2.5 Remember Tables ....................... 76 The remember Option .................... 76 Adding Entries Explicitly .................. 77 Removing Entries from a Remember Table ......... 78 2.6 Conclusion .......................... 79 3 Advanced Programming 81 3.1 Procedures Which Return Procedures ........... 82 Creating a Newton Iteration ................. 82 A Shift Operator ....................... 85 3.2 When Local Variables Leave Home ............. 86 Creating the Cartesian Product of a Sequence of Sets . 89 Exercises ........................... 94 3.3 Interactive Input ....................... 94 Reading Strings from the Terminal ............. 95 Reading Expressions from the Terminal .......... 95 Converting Strings to Expressions ............. 97 3.4 Extending Maple ....................... 98 Defining New Types ..................... 99 Exercises ........................... 100 Neutral Operators ...................... 100 Exercise ............................ 104 Extending Certain Commands ............... 106 3.5 Conclusion .......................... 109 Contents • v 4 The Maple Language 111 4.1 Language Elements ...................... 113 The Character Set ...................... 113 Tokens............................. 114 Token Separators ....................... 118 4.2 Escape Characters ...................... 122 4.3 Statements .......................... 122 The Assignment Statement ................. 123 Unassignment: Clearing a Name .............. 128 The Selection Statement ................... 130 The Repetition Statement .................. 133 The read and save Statements ............... 138 4.4 Expressions .......................... 139 Expression Trees: Internal Representation ......... 140 The Types and Operands of Integers, Strings, Indexed Names, and Concatenations ............. 144 Fractions and Rational Numbers .............. 147 Floating-Point (Decimal) Numbers ............. 148 Complex Numerical Constants ............... 151 Labels............................. 153 Sequences ........................... 154 Sets and Lists ......................... 158 Functions ........................... 160 The Arithmetic Operators .................. 165 Non-Commutative Multiplication .............. 168 The Composition Operators ................. 169 The Ditto Operators ..................... 170 The Factorial Operator ................... 170 The mod Operator ...................... 171 The Neutral Operators .................... 172 Relations and Logical Operators .............. 173 Tables ............................. 178 Series ............................. 180 Ranges ............................. 182 Unevaluated Expressions ................... 184 Constants ........................... 186 Structured Types ....................... 187 4.5 Useful Looping Constructs .................. 190 The map, select, remove, and selectremove Commands 191 The zip Command ...................... 194 The seq, add, and mul Commands ............. 195 vi • Contents 4.6 Substitution .......................... 197 4.7 Conclusion .......................... 200 5 Procedures 201 5.1 Procedure Definitions .................... 201 Mapping Notation ...................... 202 Unnamed Procedures and Their Combinations ...... 203 Procedure Simplification ................... 204 5.2 Parameter Passing ...................... 204 Declared Parameters ..................... 206 The Sequence of Arguments ................. 207 5.3 Local and Global Variables ................. 208 Evaluation of Local Variables ................ 210 5.4 Procedure Options and the Description Field ....... 212 Options ............................ 212 The Description Field .................... 214 5.5 The Value Returned by a Procedure ............ 215 Assigning Values to Parameters ............... 215 Explicit Returns ....................... 218 Error Returns ......................... 219 Trapping Exceptions ..................... 221 Returning Unevaluated .................... 225 Exercise ............................ 227 5.6 The Procedure Object .................... 227 Last Name Evaluation .................... 227 The Type and Operands of a Procedure .......... 228 Saving and Retrieving Procedures ............. 231 5.7 Explorations ......................... 232 Exercises ........................... 232 5.8 Conclusion .......................... 233 6 Programming with Modules 235 About This Chapter ..................... 238 6.1 Syntax and Semantics .................... 238 The Module Body ...................... 239 Module Parameters ...................... 240 Named Modules ....................... 240 Declarations .......................... 242 Exported Local Variables .................. 244 Module Options ........................ 249 Implicit Scoping Rules .................... 249 Contents • vii Lexical Scoping Rules .................... 249 Modules and Types ...................... 251 Example: A Symbolic Differentiator ............ 252 6.2 Records ............................ 263 6.3 Packages ............................ 266 What Is a Package? ..................... 266 Example: The LinkedList Package............. 268 Example: A Code Coverage Profiling Package ....... 275 Example: The Shapes Package ............... 282 6.4 The use Statement ...................... 291 Operator Rebinding ..................... 294 6.5 Modeling Objects ....................... 297 Example: Priority Queues .................. 300 An Object-oriented Shapes Package ............ 304 6.6 Interfaces and Implementations ............... 306 Interfaces ........................... 307 Example: Generic Graph Algorithms ............ 312 Example: Quotient Fields .................. 318 Example: A Generic Group Implementation ........ 327 6.7 Conclusion .......................... 348 7 Debugging Maple Programs 349 7.1 A Tutorial Example ..................... 349 Numbering the Procedure Statements I .......... 350 Invoking the Debugger I ................... 351 Controlling Execution of a Procedure during Debugging I 353 Invoking the Debugger II .................. 357 7.2 Maple Debugger Commands ................. 361 Numbering the Procedure Statements II .......... 361 Invoking the Debugger III .................. 362 Controlling Execution of a Procedure during Debugging II 371 Changing the State of a Procedure during Debugging . 371 Examining the State of a Procedure during Debugging . 374 Using Top-Level Commands at the Debugger Prompt . 379 Restrictions .......................... 379 7.3 Detecting