Maple Introductory Programming Guide
Total Page:16
File Type:pdf, Size:1020Kb
Maple Introductory Programming Guide M. B. Monagan K. O. Geddes K. M. Heal G. Labahn S. M. Vorkoetter J. McCarron P. DeMarco Maplesoft, a division of Waterloo Maple Inc. 2009. ii Maplesoft, Maple, and Maplet are all trademarks of Waterloo Maple Inc. © Maplesoft, a division of Waterloo Maple Inc. 2009. All rights reserved. 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. Windows is a registered trademark of Microsoft Corporation. Java and all Java based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Maplesoft is independent of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. This document was produced using a special version of Maple that reads and updates LaTeX files. Printed in Canada ISBN 978-1-897310-73-1 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 Defining a Simple Procedure . 9 Maple Library Routines, Built-In Routines, and User- Defined 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 iii iv Contents ¯ 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 . 58 3 Maple Expressions and Statements 59 In This Chapter . 59 3.1 Syntax and Semantics . 59 Syntax . 59 Semantics . 60 3.2 Expressions . 61 Contents v ¯ 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 vi Contents ¯ The Selection Operation [] . 152 The op Command . 155 The select, remove, and selectremove Commands . 156 4.6 Converting Between Data Structures . 158 Converting a Sequence to a Set or a List . 158 Converting Other Data Structures . 159 4.7 Other Maple Data Structures . 159 Stacks . 159 Queues . 161 4.8 Troubleshooting . 162 Wrong Number of Parameters in Function . 163 Invalid Subscript Selector . 163 Requires Range or Initialization List for Building arrays . 164 Error in array Bound or Array Index out of Range . 164 4.9 Exercises . 165 4.10 Conclusion . 166 5 Flow Control 167 In This Chapter . 167 5.1 Selection and Conditional Execution . 167 The if Statement . 167 The `if` Operator . 171 5.2 Repetition . 172 The for Loop . 173 The while Loop . 177 Control within Loops . 178 5.3 Looping Commands . 180 The map Command . 180 The select, remove, and selectremove Commands . 181 The zip Command . 182 The seq, add, and mul Commands . 183 Using Specialized Looping Commands . 185 5.4 Troubleshooting . 187 Cannot Evaluate Boolean in if Statement . 188 Value in Loop Must Be Numeric or Character . 188 Variable Previously Assigned . 189 Wrong Parameters in Function $ . 190 5.5 Exercises . 191 5.6 Conclusion . 191 6 Maple Procedures 193 Contents vii ¯ In This Chapter . ..