Baixe Maple guide progammation e outras Manuais, Projetos, Pesquisas em PDF para Matemática Computacional, somente na Docsity!
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.
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-
List of Tables
- 1 Introduction to Programming in Maple Preface xxi
- 1.1 In This Chapter
- 1.2 The Maple Software - The User Interface - The Computation Engine
- 1.3 Maple Statements - Getting Help - Displaying a Text String - Performing an Arithmetic Operation - Assigning to a Name - Using Maple Library Commands
- 1.4 Procedures - Defining a Simple Procedure - Entering a Procedure Definition - Adding Comments to a Procedure - Calling a Procedure
- ures Maple Library Commands, Built-In Commands, and User-Defined Proced-
- Full Evaluation and Last Name Evaluation
- Viewing Procedure Definitions and Maple Library Code
- 1.5 Interrupting Computations and Clearing the Internal Memory - Interrupting a Maple Computation - Clearing the Maple Internal Memory
- 1.6 Avoiding Common Problems - Unexpected End of Statement - Missing Operator - Invalid, Wrong Number or Type of Arguments - Unbalanced Parentheses - Assignment Versus Equality
- 1.7 Exercises
- 2 Maple Language Elements
- 2.1 In This Chapter
- 2.2 Character Set
- 2.3 Tokens - Reserved Words - Programming-Language Operators - Names
- 2.4 Natural Integers
- 2.5 Strings - Length of a String
- Substrings
- Searching a String
- String Concatenation
- Mutability of Strings
- Special Characters in Strings
- Parsing Strings
- Converting Expressions to Strings
- 2.6 Using Special Characters
- Token Separators
- Blank Spaces, New Lines, Comments, and Continuation
- Punctuation Marks
- Escape Characters
- 2.7 Types and Operands
- DAGs
- Maple Types
- Operands and op
- 2.8 Avoiding Common Problems
- Attempting to Assign to a Protected Name
- Invalid Left-Hand Assignment
- Incorrect Syntax in Parse
- White Space Characters within a Token
- Incorrect Use of Double and Single Quotes
- Avoid Using Maple Keywords as Names
- 2.9 Exercises
- 3 Maple Expressions
- 3.1 In This Chapter
- 3.2 Introduction
- Expressions and Statements
- Automatic Simplification and Evaluation
- Syntax and Constructors
- 3.3 Names
- Creating Names: Lexical Conventions
- 3.4 Unevaluated Expressions
- Protecting Names and Options
- Generic Expressions
- Pass by Reference
- Displaying the Original Command
- Unassigning Names
- Evaluation and Automatic Simplification
- Example: Defining a Procedure that Is Returned Unevaluated
- 3.5 Numbers
- Integers
- Fractions
- Floats
- Complex Numbers
- 3.6 Indexed Expressions
- 3.7 Member Selection
- 3.8 Functions
- 3.9 Arithmetic Expressions
- Arithmetic Operators
- Noncommutative Multiplication
- Factorials
- Forming Sums and Products
- 3.10 Boolean and Relational Expressions
- Boolean Constants
- Boolean Operators
- Relational Operators
- Efficient Boolean Iteration
- 3.11 Expressions for Data Structures
- Sequences
- Lists
- Sets
- Tables
- Rectangular Tables
- 3.12 Set-Theoretic Expressions
- Membership
- Set Inclusion
- Other Binary Operators for Sets
- 3.13 Other Expressions
- Functional Operators
- Composition
- Neutral Operators
- Ranges
- The Concatenation Operator
- The Double Colon Operator
- Series
- 3.14 Attributes
- 3.15 Using Expressions
- Evaluating and Simplifying Expressions
- Substituting Subexpressions
- Structured Types
- 3.16 Exercises
- 4 Basic Data Structures
- 4.1 In This Chapter
- 4.2 Introduction
- 4.3 Immutable Data Structures
- 4.4 Mutable Data Structures
- 4.5 Other Data Structure Operations
- Filtering Data Structure Elements
- Converting Data Structures
- 4.6 Other Data Structures
- 4.7 Data Structure Performance Comparisons
- Indexing
- Membership
- Building a Collection of Data
- 4.8 Avoiding Common Problems
- Passing Sequences into Functions
- Incorrect Index Values
- Do Not Treat Lists and Sets as Mutable
- 4.9 Exercises
- 5 Maple Statements
- 5.1 In This Chapter
- 5.2 Introduction
- 5.3 Statement Separators
- 5.4 Expression Statements
- 5.5 Assignments
- 5.6 Flow Control
- Sequencing
- Branching
- Loops
- Looping Commands
- Non-Local Flow Control
- 5.7 The use Statement
- 5.8 Other Statements
- The quit Statement
- The save Statement
- The read Statement
- 5.9 Exercises
- 6 Procedures
- 6.1 Terminology
- 6.2 Defining and Executing Procedures
- 6.3 Parameter Declarations
- Required Positional Parameters
- Optional Ordered Parameters
- Expected Ordered Parameters
- Keyword Parameters
- The End-of-Parameters Marker
- Default Value Dependencies
- Parameter Modifiers
- Procedures without Declared Parameters
- 6.4 Return Type
- 6.5 The Procedure Body
- Description
- Options
- Variables in Procedures
- Non-Variable Name Bindings
- The Statement Sequence
- Referring to Parameters within the Procedure Body
- 6.6 How Procedures Are Executed
- Binding of Arguments to Parameters
- Statement Sequence Interpretation
- 6.7 Using Data Structures with Procedures
- Passing Data Structures to Procedures
- Returning Data Structures from Procedures
- Example: Computing an Average
- Example: Binary Search
- Example: Plotting the Roots of a Polynomial
- 6.8 Writing Usable and Maintainable Procedures
- Formatting Procedures for Readability
- Commenting Your Code
- 6.9 Other Methods for Creating Procedures
- Functional Operators: Mapping Notation
- The unapply Function
- Anonymous Procedures
- 6.10 Recursion
- 6.11 Procedures that Return Procedures
- Example: Creating a Newton Iteration
- Example: A Shift Operator
- 6.12 The Procedure Object
- The procedure Type
- Procedure Operands
- 6.13 Exercises
- 7 Numerical Programming in Maple
- 7.1 In This Chapter
- 7.2 Numeric Types in Maple
- Integers
- Rationals
- Floating-Point Numbers
- Hardware Floating-Point Numbers
- Extended Numeric Types
- Complex Numbers
- Non-numeric Constants
- 7.3 More about Floating-Point Numbers in Maple
- Representation of Floating-Point Numbers in Maple
- Precision and Accuracy
- Floating-Point Contagion
- More on the Floating-Point Model
- 7.4 Maple Commands for Numerical Computing
- The evalf Command
- Numeric Solvers
- The evalhf Command
- Numerical Linear Algebra
- 7.5 Writing Efficient Numerical Programs
- Writing Flexible Numerical Procedures
- Example: Newton Iteration
- Example: Jacobi Iteration
- 8 Programming with Modules
- 8.1 In This Chapter
- 8.2 Introduction
- Encapsulation
- Creating a Custom Maple Package
- Creating Objects
- Creating Generic Programs
- 8.3 A Simple Example
- 8.4 Syntax and Semantics
- The Module Definition
- The Module Body
- Module Parameters
- Named Modules
- Declarations
- Exported Local Variables
- Module Options
- Special Exports
- Implicit Scoping Rules
- Lexical Scoping Rules
- Modules and Types
- 8.5 Records
- Creating Records
- Record Types
- Using Records to Represent Quaternions
- Object Inheritance
- 8.6 Modules and use Statements
- 8.7 Modeling Objects
- Objects and Constructors
- Example: Complex Number Constructor
- Effect of Immutable Local States
- 8.8 Interfaces and Implementations
- Generic Programming as a Good Software Engineering Practice
- Distinction between Local and Exported Variables
- Interfaces
- A Package for Manipulating Interfaces
- The load Option
- 9 Input and Output
- 9.1 In This Chapter
- 9.2 Introduction
- 9.3 Input and Output in the Worksheet
- Interfaces
- Interactive Output
- Interactive Input
- Customization
- 9.4 Input and Output with Files
- Introduction
- Working with General Files
- Importing and Exporting Numerical Data
- Files Used by Maple
- 9.5 Reading and Writing Formatted Data
- The scanf and printf Commands
- Format Strings
- Related Commands
- 9.6 Useful Utilities
- The StringTools Package
- Conversion Commands
- 9.7 2-D Math
- Introduction
- The Typesetting Package
- Additional Tips
- 9.8 Exercises
- 10 Writing Packages
- 10.1 In This Chapter
- 10.2 What Is a Package
- Packages in the Standard Library
- Packages Are Modules
- Package Exports
- Using Packages Interactively
- 10.3 Writing Maple Packages By Using Modules
- A Simple Example
- Custom Libraries
- 10.4 A Larger Example
- ModuleLoad
- The Preprocessor and Structured Source Files
- Subpackages
- 10.5 Example: A Shapes Package
- Source Code Organization
- Package Architecture
- The Package API
- The make Procedure
- The area Procedure
- The circumference Procedure
- Shape Representation
- Procedure Dispatching
- Dispatching on Submodule Exports
- Conditional Dispatching
- Table-based Dispatching
- Shape-specific Submodules
- The point Submodule
- The circle Submodule
- 11 Graphics
- 11.1 In This Chapter
- 11.2 Introduction
- Plots in Maple
- Generating a Plot
- 11.3 The Plot Library
- Generating 2-D and 3-D Plots
- Plotting Points, Polygons, and Text
- Combining Plots
- Specialty Plots
- Other Packages
- 11.4 Programming with Plots
- A 2-D Example
- A 3-D Example
- 11.5 Data Structures
- Types of Data Structures
- Creating Plot Structures
- Altering Plot Structures
- 11.6 Customizing Plots
- Controlling the Sampling
- Colors
- View
- Typesetting
- Axes and Gridlines
- Coordinate Systems
- Setting Options
- 11.7 Animations
- Building an Animation with plots:-display
- The plots:-animate command
- 3-D Animations with the viewpoint Option
- Other Animation Commands
- Displaying an Animation as an Array of Plots
- 11.8 Miscellaneous Topics
- Efficiency in Plotting
- Interfaces and Devices
- 11.9 Avoiding Common Problems
- Mixing Expression and Operator Forms
- Generating Non-numeric Data
- 12 Programming Interactive Elements
- 12.1 In This Chapter
- 12.2 Programming Embedded Components
- Adding Embedded Components to a Document
- Editing Component Properties
- Example: Creating a Tic-Tac-Toe Game
- Retrieving and Updating Component Properties
- Using the GetProperty Command to Retrieve Properties
- Using the SetProperty Command to Update Properties
- Using the Do Command to Retrieve and Update Component Properties
- 12.3 Programming Maplets
- Layout Managers
- Box Layout
- Grid Layout
- Border Layout
- 13 Advanced Connectivity
- 13.1 In This Chapter
- Connecting to the Maple Engine
- Using External Libraries in Maple
- Connecting Maple to Another Program
- Code Generation
- 13.2 MapleNet
- Computation on Demand
- Embedding a Maple Application in a Web Application
- 13.3 OpenMaple
- Runtime Environment Prerequisites
- Interface Overview
- C/C++ Example
- C# Example
- Java Example
- Visual Basic 6 Example
- Visual Basic .NET Example
- Memory Usage
- 13.4 The Maple Command-line Interface
- Batch Files
- Directing Input to a Pipeline
- Specifying Start-up Commands
- 13.5 External Calling: Using Compiled Code in Maple
- Calling a Function in a Dynamic-link Library
- Specifying Parameter Types for Function Specifications
- Scalar Data Formats
- Structured Data Formats
- External Function Interface
- Specifying Parameter Passing Conventions
- Generating Wrappers Automatically
- Passing Arguments by Reference
- External API
- System Integrity
- 13.6 Accessing Data over a Network with TCP/IP Sockets
- Socket Server
- Socket Client
- 13.7 Code Generation
- Calling CodeGeneration Commands
- Notes on Code Translation
- Translation Process
- Example 1: Translating a Procedure to Java
- Example 2: Translating a Procedure to C
- Example 3: Translating a Procedure to Fortran
- Example 4: Translating an Expression to MATLAB
- Example 5: Translating a Procedure to Visual Basic
- Example 6: Using the defaulttype and deducetypes Options
- Example 7: Using the declare Option
- The Intermediate Code
- Extending the CodeGeneration Translation Facilities
- The Printing Phase
- Defining a Custom Translator
- Using a Printer Module
- Language Translator Definition
- Using the Define Command
- Creating a Language Definition Module
- Using a New Translator
- 13.8 CAD Connectivity
- 13.9 Maple Plug-in for Excel
- 13.10 Connecting MATLAB and Maple
- Accessing the MATLAB Computation Engine from Maple
- Accessing the Maple Computational Engine from MATLAB
- 14 Parallel Programming
- 14.1 In This Chapter
- 14.2 Introduction
- 14.3 Introduction to Parallel Programming with Tasks
- Parallel Execution
- Controlling Parallel Execution
- 14.4 Task Programming Model
- Tasks
- The Task Tree
- Starting Tasks
- Task Management
- 14.5 Examples
- 14.6 Limitations of Parallel Programming
- Library Code
- Maple Interpreter
- 14.7 Avoiding Common Problems
- Every Execution Order Will Happen
- Lock around All Accesses
- Debugging Parallel Code
- 14.8 Introduction to Grid Programming
- Starting a Grid-Based Computation
- Communicating between Nodes
- 14.9 Grid Examples
- Computing a Mandelbrot Set
- 14.10 The Grid Computing Toolbox
- 14.11 Limitations
- Memory Usage
- Cost of Communication
- Load Balancing
- 14.12 Troubleshooting
- Deadlocking
- 'libname' and Other Engine Variables
- Missing Functions
- 15 Testing, Debugging, and Efficiency
- 15.1 In This Chapter
- 15.2 The Maple Debugger: A Tutorial Example
- Example
- Numbering the Procedure Statements I
- Invoking the Debugger I
- Setting a Breakpoint
- Controlling the Execution of a Procedure during Debugging I
- Invoking the Debugger II
- Setting a Watchpoint
- 15.3 Maple Debugger Commands
- Numbering the Procedure Statements II
- Invoking the Debugger III
- Controlling the Execution of a Procedure during Debugging II
- Changing the State of a Procedure during Debugging
- Examining the State of a Procedure during Debugging
- Using Top-Level Commands at the Debugger Prompt
- Restrictions
- 15.4 Detecting Errors
- Tracing a Procedure
- Using Assertions
- Handling Exceptions
- Checking Syntax
- 15.5 Creating Efficient Programs
- Displaying Time and Memory Statistics
- Profiling a Procedure
- 15.6 Managing Resources
- Setting a Time Limit on Computations
- Garbage Collection
- Other Kernel Options for Managing Resources
- 15.7 Testing Your Code
- Verifying Results with verify
- A Simple Test Harness
- Writing Good Tests
- Test Coverage
- 15.8 Exercises
- A Internal Representation
- A.1 Internal Functions
- Algebraic Functions
- Algebraic Service Functions
- Data Structure Manipulation Functions
- General Service Functions
- A.2 Flow of Control
- A.3 Internal Representations of Data Types
- AND: Logical AND
- ASSIGN: Assignment Statement
- BINARY: Binary Object
- BREAK: Break Statement
- CATENATE: Name Concatenation
- COMPLEX: Complex Value
- CONTROL: Communications Control Structure
- DCOLON: Type Specification or Test
- DEBUG: Debug
- EQUATION: Equation or Test for Equality
- ERROR: Error Statement
- EXPSEQ: Expression Sequence
- FLOAT: Software Floating-Point Number
- FOR: For/While Loop Statement
- FOREIGN: Foreign Data
- FUNCTION: Function Call
- GARBAGE: Garbage
- HFLOAT: Hardware Float
- IF: If Statement
- IMPLIES: Logical IMPLIES
- INEQUAT: Not Equal or Test for Inequality
- INTNEG: Negative Integer
- INTPOS: Positive Integer
- LESSEQ: Less Than or Equal
- LESSTHAN: Less Than
- LEXICAL: Lexically Scoped Variable within an Expression
- LIST: List
- LOCAL: Local Variable within an Expression
- MEMBER: Module Member
- MODDEF: Module Definition
- MODULE: Module Instance
- NAME: Identifier
- NEXT: Next Statement
- NOT: Logical NOT
- OR: Logical OR
- PARAM: Procedure Parameter in an Expression
- POLY: Multivariate Polynomials with Integer Coefficients
- POWER: Power
- PROC: Procedure Definition
- PROD: Product, Quotient, Power
- RANGE: Range
- RATIONAL: Rational
- READ: Read Statement
- RETURN: Return Statement
- RTABLE: Rectangular Table
- SAVE: Save Statement
- SDPOLY: Sparse Distributed Multivariate Polynomial
- SERIES: Series
- SET: Set
- STATSEQ: Statement Sequence
- STOP: Quit Statement
- STRING: Character String
- SUM: Sum, Difference
- TABLE: Table
- TABLEREF: Table Reference
- TRY: Try Statement
- UNEVAL: Unevaluated Expression
- USE: Use Statement
- XOR: Logical Exclusive-Or
- ZPPOLY: Polynomials with Integer Coefficients modulo n
- A.4 Hashing in Maple
- Basic Hash Tables
- Dynamic Hash Tables
- Cache Hash Tables
- The Simplification Table
- The Name Table
- Remember Tables
- Maple Language Arrays and Tables
- Maple Language Rectangular Tables
- Portability
- Index
- Figure 1.1: Maple Toolbar List of Figures
- Figure 2.1: Expression Tree
- Figure 2.2: Expression DAG
- Figure 2.3: Actual Expression DAG
- Figure 3.1: expr DAG
- Figure 3.2: subsop Example DAGs
- Figure 10.1: Organization of Package Source Files
- Figure 10.2: Design of Package
- Figure 12.1: Code Region for an Embedded Component
- Figure 12.2: Border Layout Diagram
- Figure 13.1: Maple in Excel
- Figure 15.1: The Maple Debugger in the Standard Interface
- Table 2.1: Special Characters
- Table 2.2: Reserved Keywords
- Table 2.3: Binary Operators
- Table 2.4: Unary Operators
- Table 2.5: Element-wise Operators
- Table 2.6: Token Separators
- Table 2.7: Subtype
- Table 3.1: Initially Known Names
- Table 5.1: Operators That Can Be Rebound
- Table 6.1: Procedure Operands
- Table 7.1: Floating-Point Contagion Rules
- Table 10.1: RandomnessTests
- Table 13.1: Basic Data Types
- Table 13.2: Compound Data Types
- Table 13.3: Printer Commands
- Table 15.1: sieveTest.mpl
- Table 15.2: sieveTest2.mpl
- Table 15.3: Modified sieveTest2.mpl
- Table A.1: Maple Structures
xx • List of Tables