Maple Programming Guide L. Bernardin P. Chin P. DeMarco K. O. Geddes D. E. G. H
Maple Programming Guide L. Bernardin P. Chin P. DeMarco K. O. Geddes D. E. G. Hare K. M. Heal G. Labahn J. P. May J. McCarron M. B. Monagan D. Ohashi S. M. Vorkoetter Copyright © Maplesoft, a division of Waterloo Maple Inc. 2011 Maple Programming Guide by L. Bernardin, P. Chin, P. DeMarco, K. O. Geddes, D. E. G. Hare, K. M. Heal, G. Labahn, J. P. May, J. McCarron, M. B. Monagan, D. Ohashi, and S. M. Vorkoetter Copyright Maplesoft, Maple, MapleNet, MaplePrimes, Maplet, Maple T.A., and OpenMaple are all trademarks of Waterloo Maple Inc. © Maplesoft, a division of Waterloo Maple Inc. 1996-2011. All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transcribed, in any form or by any means — electronic, mechanical, photocopying, recording, or otherwise. Information in this document is subject to change without notice and does not represent a commitment on the part of the vendor. The software described in this document is furnished under a license agreement and may be used or copied only in accordance with the agreement. It is against the law to copy the software on any medium except as specifically allowed in the agreement. Adobe and Acrobat are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries. Java is a registered trademarks of Oracle and/or its affiliates. MATLAB is a registered trademark of The MathWorks, Inc. Microsoft and Windows are registered trademarks of Microsoft Corporation. NAG is a registered trademark of The Numerical Algorithms Group Ltd. All other trademarks are the property of their respective owners. This document was produced using a special version of Maple and DocBook. Printed in Canada ISBN 978-1-926902-08-1 Contents Preface ....................................................................................................... xxi 1 Introduction to Programming in Maple .............................................................. 1 1.1 In This Chapter ...................................................................................... 1 1.2 The Maple Software ................................................................................ 1 The User Interface .................................................................................. 1 The Computation Engine .......................................................................... 1 1.3 Maple Statements ................................................................................... 2 Getting Help .......................................................................................... 2 Displaying a Text String ........................................................................... 2 Performing an Arithmetic Operation ........................................................... 3 Assigning to a Name ............................................................................... 3 Using Maple Library Commands .............................................................. 3 1.4 Procedures ............................................................................................ 4 Defining a Simple Procedure ..................................................................... 4 Entering a Procedure Definition ................................................................. 4 Adding Comments to a Procedure .............................................................. 7 Calling a Procedure ................................................................................. 8 Maple Library Commands, Built-In Commands, and User-Defined Proced- ures ....................................................................................................... 8 Full Evaluation and Last Name Evaluation .................................................. 9 Viewing Procedure Definitions and Maple Library Code ............................... 10 1.5 Interrupting Computations and Clearing the Internal Memory ........................ 11 Interrupting a Maple Computation ............................................................ 11 Clearing the Maple Internal Memory ........................................................ 12 1.6 Avoiding Common Problems ................................................................... 12 Unexpected End of Statement .................................................................. 12 Missing Operator .................................................................................. 13 Invalid, Wrong Number or Type of Arguments ........................................... 14 Unbalanced Parentheses ......................................................................... 14 Assignment Versus Equality .................................................................... 14 1.7 Exercises ............................................................................................. 16 2 Maple Language Elements ............................................................................ 17 2.1 In This Chapter ..................................................................................... 17 2.2 Character Set ....................................................................................... 17 2.3 Tokens ................................................................................................ 18 Reserved Words .................................................................................... 18 Programming-Language Operators ........................................................... 19 Names ................................................................................................ 22 2.4 Natural Integers .................................................................................... 23 2.5 Strings ................................................................................................ 24 Length of a String ................................................................................. 24 iii Substrings ............................................................................................ 24 Searching a String ................................................................................. 25 String Concatenation ............................................................................. 25 Mutability of Strings .............................................................................. 26 Special Characters in Strings ................................................................... 27 Parsing Strings ..................................................................................... 27 Converting Expressions to Strings ............................................................ 28 2.6 Using Special Characters ........................................................................ 29 Token Separators .................................................................................. 29 Blank Spaces, New Lines, Comments, and Continuation ............................... 29 Punctuation Marks ................................................................................ 30 Escape Characters ................................................................................. 33 2.7 Types and Operands .............................................................................. 34 DAGs ................................................................................................. 34 Maple Types ........................................................................................ 35 Operands and op ................................................................................... 36 2.8 Avoiding Common Problems ................................................................... 40 Attempting to Assign to a Protected Name ................................................. 40 Invalid Left-Hand Assignment ................................................................. 41 Incorrect Syntax in Parse ........................................................................ 41 White Space Characters within a Token ..................................................... 41 Incorrect Use of Double and Single Quotes ................................................ 41 Avoid Using Maple Keywords as Names ................................................... 42 2.9 Exercises ............................................................................................. 43 3 Maple Expressions ....................................................................................... 45 3.1 In This Chapter ..................................................................................... 45 3.2 Introduction ......................................................................................... 45 Expressions and Statements .................................................................... 45 Automatic Simplification and Evaluation ................................................... 45 Syntax and Constructors ........................................................................ 45 3.3 Names ................................................................................................ 47 Creating Names: Lexical Conventions ....................................................... 48 3.4 Unevaluated Expressions ........................................................................ 52 Protecting Names and Options ................................................................. 53 Generic Expressions .............................................................................. 54 Pass by Reference ................................................................................. 54 Displaying the Original Command ............................................................ 55 Unassigning Names ............................................................................... 55 Evaluation and Automatic Simplification ................................................... 56 Example: Defining a Procedure that Is Returned Unevaluated ........................ 57 3.5 Numbers ............................................................................................. 59 Integers ............................................................................................... 59 Fractions ............................................................................................. 60 iv • Contents Floats .................................................................................................. 61 Complex Numbers ................................................................................ 65 3.6 Indexed Expressions .............................................................................. 69 3.7 Member Selection ................................................................................. 74 3.8 Functions ............................................................................................ 75 Calls to Procedures ................................................................................ 77 3.9 Arithmetic Expressions .......................................................................... 77 Arithmetic Operators ............................................................................. 77 Noncommutative Multiplication ............................................................... 90 Factorials ............................................................................................. 92 Forming Sums and Products .................................................................... 93 3.10 Boolean and Relational Expressions ........................................................ 94 Boolean Constants ................................................................................. 94 Boolean Operators ................................................................................. 94 Relational Operators .............................................................................. 98 Efficient Boolean Iteration .................................................................... 102 3.11 Expressions for Data Structures ............................................................ 103 Sequences .......................................................................................... 103 Lists .................................................................................................. 105 Sets .................................................................................................. 107 Tables ............................................................................................... 107 Rectangular Tables .............................................................................. 108 3.12 Set-Theoretic Expressions ................................................................... 109 Membership ....................................................................................... 110 Set Inclusion ....................................................................................... 110 Other Binary Operators for Sets ............................................................. 111 3.13 Other Expressions .............................................................................. 113 Functional Operators ............................................................................ 113 Composition ....................................................................................... 114 Neutral Operators ................................................................................ 116 Ranges .............................................................................................. 116 The Concatenation Operator .................................................................. 119 The Double Colon Operator ................................................................... 121 Series ................................................................................................ 122 3.14 Attributes ......................................................................................... 124 3.15 Using Expressions ............................................................................. 126 Evaluating and Simplifying Expressions .................................................. 126 Substituting Subexpressions .................................................................. 127 Structured Types ................................................................................. 130 3.16 Exercises ......................................................................................... 134 4 Basic Data Structures ................................................................................. 135 4.1 In This Chapter ................................................................................... 135 4.2 Introduction ....................................................................................... 135 Contents • v 4.3 Immutable Data Structures .................................................................... 135 Lists .................................................................................................. 136 Sets .................................................................................................. 142 4.4 Mutable Data Structures ....................................................................... 147 Tables ............................................................................................... 147 Arrays ............................................................................................... 155 4.5 Other Data Structure Operations ............................................................. 165 Filtering Data Structure Elements ........................................................... 165 Converting Data Structures .................................................................... 165 4.6 Other Data Structures ........................................................................... 166 Records ............................................................................................. 166 Stacks ............................................................................................... 168 Queues .............................................................................................. 171 4.7 Data Structure Performance Comparisons ................................................ 174 Indexing ............................................................................................ 174 Membership ....................................................................................... 175 Building a Collection of Data ................................................................. 176 4.8 Avoiding Common Problems ................................................................. 177 Passing Sequences into Functions ........................................................... 177 Incorrect Index Values .......................................................................... 177 Do Not Treat Lists and Sets as Mutable ................................................... 178 4.9 Exercises ........................................................................................... 179 5 Maple Statements ...................................................................................... 181 5.1 In This Chapter ................................................................................... 181 5.2 Introduction ....................................................................................... 181 5.3 Statement Separators ............................................................................ 181 5.4 Expression Statements .......................................................................... 182 5.5 Assignments ....................................................................................... 182 Multiple Assignment ............................................................................ 183 5.6 Flow Control ...................................................................................... 185 Sequencing ........................................................................................ 185 Branching .......................................................................................... 185 Loops ................................................................................................ 189 Looping Commands ............................................................................. 195 Non-Local Flow Control ....................................................................... 197 5.7 The use Statement ............................................................................... 201 5.8 Other Statements ................................................................................. 205 The quit Statement ............................................................................... 205 The save Statement .............................................................................. 205 The read Statement .............................................................................. 206 5.9 Exercises ........................................................................................... 207 6 Procedures ................................................................................................ 209 6.1 Terminology ....................................................................................... 209 vi • Contents 6.2 Defining and Executing Procedures ........................................................ 210 6.3 Parameter Declarations ......................................................................... 211 Required Positional Parameters .............................................................. 211 Optional Ordered Parameters ................................................................. 212 Expected Ordered Parameters ................................................................ 214 Keyword Parameters ............................................................................ 214 The End-of-Parameters Marker .............................................................. 216 Default Value Dependencies .................................................................. 217 Parameter Modifiers ............................................................................. 218 Procedures without Declared Parameters .................................................. 222 6.4 Return Type ....................................................................................... 222 6.5 The Procedure Body ............................................................................ 223 Description ........................................................................................ 223 Options ............................................................................................. 224 Variables in Procedures ......................................................................... 230 Non-Variable Name Bindings ................................................................ 233 The Statement Sequence ....................................................................... 234 Referring to Parameters within the Procedure Body .................................... 234 6.6 How Procedures Are Executed ............................................................... 241 Binding of Arguments to Parameters ....................................................... 242 Statement Sequence Interpretation .......................................................... 246 6.7 Using Data Structures with Procedures .................................................... 254 Passing Data Structures to Procedures ..................................................... 254 Returning Data Structures from Procedures .............................................. 255 Example: Computing an Average ............................................................ 256 Example: Binary Search ....................................................................... 257 Example: Plotting the Roots of a Polynomial ............................................ 258 6.8 Writing Usable and Maintainable Procedures ............................................ 262 Formatting Procedures for Readability ..................................................... 262 Commenting Your Code ....................................................................... 263 6.9 Other Methods for Creating Procedures ................................................... 265 Functional Operators: Mapping Notation .................................................. 265 The unapply Function ........................................................................... 266 Anonymous Procedures ........................................................................ 268 6.10 Recursion ......................................................................................... 269 6.11 Procedures that Return Procedures ........................................................ 271 Example: Creating a Newton Iteration ..................................................... 272 Example: A Shift Operator .................................................................... 274 6.12 The Procedure Object ......................................................................... 276 The procedure Type ............................................................................. 276 Procedure Operands ............................................................................. 277 6.13 Exercises ......................................................................................... 279 7 Numerical Programming in Maple ................................................................ 281 Contents • vii 7.1 In This Chapter ................................................................................... 281 7.2 Numeric Types in Maple ....................................................................... 281 Integers ............................................................................................. 281 Rationals ........................................................................................... 283 Floating-Point Numbers ........................................................................ 285 Hardware Floating-Point Numbers .......................................................... 286 Extended Numeric Types ...................................................................... 287 Complex Numbers ............................................................................... 288 Non-numeric Constants ........................................................................ 290 7.3 More about Floating-Point Numbers in Maple .......................................... 290 Representation of Floating-Point Numbers in Maple ................................... 290 Precision and Accuracy ........................................................................ 292 Floating-Point Contagion ...................................................................... 294 More on the Floating-Point Model .......................................................... 297 7.4 Maple Commands for Numerical Computing ............................................ 298 The evalf Command ............................................................................. 298 Numeric Solvers .................................................................................. 302 The evalhf Command ........................................................................... 302 Numerical Linear Algebra ..................................................................... uploads/s3/ maple15-programmingguide.pdf
Documents similaires
-
18
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Aoû 02, 2022
- Catégorie Creative Arts / Ar...
- Langue French
- Taille du fichier 10.0508MB