Thông tin tài liệu
Maple
Advanced 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. 1996-2008.
ii
Maplesoft, Maple, and Maplet are all trademarks of Waterloo Maple Inc.
© Maplesoft, a division of Waterloo Maple Inc. 1996-2008. 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-47-2
Contents
Preface 1
Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Worksheet Graphical Interface . . . . . . . . . . . . . . . . . . 2
Manual Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Customer Feedback . . . . . . . . . . . . . . . . . . . . . . . . . 3
1 Procedures, Variables, and Extending Maple 5
Prerequisite Knowledge . . . . . . . . . . . . . . . . . . . 5
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 Nested Procedures . . . . . . . . . . . . . . . . . . . . . . 5
Scoping Rules . . . . . . . . . . . . . . . . . . . . . . . . . 6
Local Versus Global Variables . . . . . . . . . . . . . . . . 6
The Quick-Sort Algorithm . . . . . . . . . . . . . . . . . . 8
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Creating a Uniform Random Numb er Generator . . . . . 11
1.2 Procedures That Return Procedures . . . . . . . . . . . . 14
Conveying Values . . . . . . . . . . . . . . . . . . . . . . . 14
Creating a Newton Iteration . . . . . . . . . . . . . . . . . 14
Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
A Shift Operator . . . . . . . . . . . . . . . . . . . . . . . 17
1.3 Local Variables and Invoking Procedures . . . . . . . . . . 19
Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Procedure as a Returned Object . . . . . . . . . . . . . . 22
Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.4 Interactive Input . . . . . . . . . . . . . . . . . . . . . . . 27
iii
iv • Contents
Reading Strings from the Terminal . . . . . . . . . . . . . 27
Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Reading Expressions from the Terminal . . . . . . . . . . 28
Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Converting Strings to Expressions . . . . . . . . . . . . . 30
1.5 Extending Maple . . . . . . . . . . . . . . . . . . . . . . . 31
Defining New Typ es . . . . . . . . . . . . . . . . . . . . . 31
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Neutral Operators . . . . . . . . . . . . . . . . . . . . . . 33
Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Extending Commands . . . . . . . . . . . . . . . . . . . . 39
1.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2 Programming with Modules 43
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Module Versus Procedure . . . . . . . . . . . . . . . . . . 45
Accessing Module Exports . . . . . . . . . . . . . . . . . . 46
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . 46
2.1 Syntax and Semantics . . . . . . . . . . . . . . . . . . . . 47
The Module Definition . . . . . . . . . . . . . . . . . . . . 47
The Module Body . . . . . . . . . . . . . . . . . . . . . . 48
Module Parameters . . . . . . . . . . . . . . . . . . . . . . 48
Named Modules . . . . . . . . . . . . . . . . . . . . . . . 48
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Exported Local Variables . . . . . . . . . . . . . . . . . . 52
Module Options . . . . . . . . . . . . . . . . . . . . . . . . 57
Implicit Scoping Rules . . . . . . . . . . . . . . . . . . . . 58
Lexical Scoping Rules . . . . . . . . . . . . . . . . . . . . 58
Modules and Types . . . . . . . . . . . . . . . . . . . . . . 60
Example: A Symb olic Differentiator . . . . . . . . . . . . 61
2.2 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.3 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
What Is a Package . . . . . . . . . . . . . . . . . . . . . . 78
Writing Maple Packages by Using Modules . . . . . . . . 80
The LinkedList Package . . . . . . . . . . . . . . . . . . 80
Code Coverage Profiling Package . . . . . . . . . . . . . . 87
The Shapes Package . . . . . . . . . . . . . . . . . . . . . 95
2.4 The use Statement . . . . . . . . . . . . . . . . . . . . . . 103
Operator Rebinding . . . . . . . . . . . . . . . . . . . . . 106
Contents • v
2.5 Modeling Objects . . . . . . . . . . . . . . . . . . . . . . . 108
Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . 111
An Object-oriented Shapes Package . . . . . . . . . . . . 115
2.6 Interfaces and Implementations . . . . . . . . . . . . . . . 117
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Generic Graph Algorithms . . . . . . . . . . . . . . . . . . 124
Quotient Fields . . . . . . . . . . . . . . . . . . . . . . . . 129
A Generic Group Implementation . . . . . . . . . . . . . . 138
2.7 Extended Example: A Search Engine . . . . . . . . . . . . 159
Intro duction to Searching . . . . . . . . . . . . . . . . . . 159
Inverted Term Occurrence Indexing . . . . . . . . . . . . . 161
The Vector Space Model . . . . . . . . . . . . . . . . . . . 164
Term Weighting . . . . . . . . . . . . . . . . . . . . . . . . 167
Building a Search Engine Package . . . . . . . . . . . . . 168
Latent Semantic Analysis . . . . . . . . . . . . . . . . . . 172
The Search Engine Package . . . . . . . . . . . . . . . . . 173
Using the Package . . . . . . . . . . . . . . . . . . . . . . 180
2.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 184
3 Input and Output 185
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . 185
3.1 A Tutorial Example . . . . . . . . . . . . . . . . . . . . . 186
3.2 File Types and Modes . . . . . . . . . . . . . . . . . . . . 190
Buffered Files versus Unbuffered Files . . . . . . . . . . . 190
Text Files versus Binary Files . . . . . . . . . . . . . . . . 190
Read Mode versus Write Mode . . . . . . . . . . . . . . . 191
The default and terminal Files . . . . . . . . . . . . . . 191
3.3 File Descriptors versus File Names . . . . . . . . . . . . . 192
3.4 File Manipulation Commands . . . . . . . . . . . . . . . . 193
Opening and Closing Files . . . . . . . . . . . . . . . . . . 193
Position Determination and Adjustment . . . . . . . . . . 194
Detecting the End of a File . . . . . . . . . . . . . . . . . 195
Determining File Status . . . . . . . . . . . . . . . . . . . 195
Removing Files . . . . . . . . . . . . . . . . . . . . . . . . 196
3.5 Input Commands . . . . . . . . . . . . . . . . . . . . . . . 197
Reading Text Lines from a File . . . . . . . . . . . . . . . 197
Reading Arbitrary Bytes from a File . . . . . . . . . . . . 197
Formatted Input . . . . . . . . . . . . . . . . . . . . . . . 198
Reading Maple Statements . . . . . . . . . . . . . . . . . 204
Reading Tabular Data . . . . . . . . . . . . . . . . . . . . 204
3.6 Output Commands . . . . . . . . . . . . . . . . . . . . . . 206
vi • Contents
Configuring Output Parameters Using the interface Com-
mand . . . . . . . . . . . . . . . . . . . . . . . . . 206
One-Dimensional Expression Output . . . . . . . . . . . . 206
Two-Dimensional Expression Output . . . . . . . . . . . . 207
Writing Maple Strings to a File . . . . . . . . . . . . . . . 210
Writing Bytes to a File . . . . . . . . . . . . . . . . . . . . 211
Formatted Output . . . . . . . . . . . . . . . . . . . . . . 211
Writing Tabular Data . . . . . . . . . . . . . . . . . . . . 215
Flushing a Buffered File . . . . . . . . . . . . . . . . . . . 217
Redirecting the default Output Stream . . . . . . . . . . 217
3.7 Conversion Commands . . . . . . . . . . . . . . . . . . . . 218
Conversion between Strings and Lists of Integers . . . . . 218
Parsing Maple Expressions and Statements . . . . . . . . 219
Formatted Conversion to and from Strings . . . . . . . . . 220
3.8 Notes to C Programmers . . . . . . . . . . . . . . . . . . . 221
3.9 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 222
4 Numerical Programming in Maple 223
Floating-Point Calculations . . . . . . . . . . . . . . . . . 223
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . 223
Why Use Numerical Computations . . . . . . . . . . . . . 223
4.1 The Basics of evalf . . . . . . . . . . . . . . . . . . . . . 224
4.2 Hardware Floating-Point Numbers . . . . . . . . . . . . . 227
Newton’s Method . . . . . . . . . . . . . . . . . . . . . . . 230
Computing with Arrays of Numbers . . . . . . . . . . . . 232
4.3 Floating-Point Models in Maple . . . . . . . . . . . . . . . 235
Software Floats . . . . . . . . . . . . . . . . . . . . . . . . 235
Roundoff Error . . . . . . . . . . . . . . . . . . . . . . . . 236
4.4 Extending the evalf Command . . . . . . . . . . . . . . . 238
Defining New Constants . . . . . . . . . . . . . . . . . . . 238
Defining New Functions . . . . . . . . . . . . . . . . . . . 240
4.5 Using the Matlab Package . . . . . . . . . . . . . . . . . . 243
4.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 244
5 Programming with Maple Graphics 245
Maple Plots . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Creating Plotting Procedures . . . . . . . . . . . . . . . . 245
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . 245
5.1 Basic Plotting Procedures . . . . . . . . . . . . . . . . . . 246
Altering a Plot . . . . . . . . . . . . . . . . . . . . . . . . 248
5.2 Programming with Plotting Library Procedures . . . . . . 249
Contents • vii
Plotting a Loop . . . . . . . . . . . . . . . . . . . . . . . . 249
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
A Ribbon Plot Procedure . . . . . . . . . . . . . . . . . . 251
5.3 Maple Plot Data Structures . . . . . . . . . . . . . . . . . 254
The PLOT Data Structure . . . . . . . . . . . . . . . . . . 256
Arguments Inside a PLOT Structure . . . . . . . . . . . . . 257
A Sum Plot . . . . . . . . . . . . . . . . . . . . . . . . . . 259
The PLOT3D Data Structure . . . . . . . . . . . . . . . . . 262
Objects Inside a PLOT3D Data Structure . . . . . . . . . . 264
5.4 Programming with Plot Data Structures . . . . . . . . . . 266
Writing Graphic Primitives . . . . . . . . . . . . . . . . . 266
Plotting Gears . . . . . . . . . . . . . . . . . . . . . . . . 268
Polygon Meshes . . . . . . . . . . . . . . . . . . . . . . . . 272
5.5 Programming with the plottools Package . . . . . . . . 273
A Pie Chart . . . . . . . . . . . . . . . . . . . . . . . . . . 275
A Dropshadow Pro cedure . . . . . . . . . . . . . . . . . . 276
Creating a Tiling . . . . . . . . . . . . . . . . . . . . . . . 278
A Smith Chart . . . . . . . . . . . . . . . . . . . . . . . . 280
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Modifying Polygon Meshes . . . . . . . . . . . . . . . . . 281
5.6 Vector Field Plots . . . . . . . . . . . . . . . . . . . . . . 286
Drawing a Vector . . . . . . . . . . . . . . . . . . . . . . . 286
Generating a Vector Plot Field . . . . . . . . . . . . . . . 288
5.7 Generating Grids of Points . . . . . . . . . . . . . . . . . 296
5.8 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Animation in Static Form . . . . . . . . . . . . . . . . . . 302
Graphical Object as Input . . . . . . . . . . . . . . . . . . 302
Methods for Creating Animations . . . . . . . . . . . . . . 303
Two and Three Dimensions . . . . . . . . . . . . . . . . . 305
Demonstrating Physical Objects in Motion . . . . . . . . 306
5.9 Programming with Color . . . . . . . . . . . . . . . . . . . 308
Generating Color Tables . . . . . . . . . . . . . . . . . . . 309
Using Animation . . . . . . . . . . . . . . . . . . . . . . . 310
Adding Color Information to Plots . . . . . . . . . . . . . 312
Creating A Chess Board Plot . . . . . . . . . . . . . . . . 315
5.10 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 316
6 Advanced Connectivity 319
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . 319
Code Generation . . . . . . . . . . . . . . . . . . . . . . . 319
External Calling: Using Compiled Code in Maple . . . . . 319
viii • Contents
OpenMaple: Using Maple in Compiled Code . . . . . . . . 319
6.1 Code Generation . . . . . . . . . . . . . . . . . . . . . . . 319
The CodeGeneration Package . . . . . . . . . . . . . . . . 319
Calling CodeGeneration Functions . . . . . . . . . . . . . 320
Translation Process . . . . . . . . . . . . . . . . . . . . . . 321
Extending the CodeGeneration Translation Facilities . . . 324
Defining a Custom Translator . . . . . . . . . . . . . . . . 325
6.2 External Calling: Using Compiled Code in Maple . . . . . 329
Method 1: Calling External Functions . . . . . . . . . . . 331
External Definition . . . . . . . . . . . . . . . . . . . . . . 333
Typ e Specification . . . . . . . . . . . . . . . . . . . . . . 334
Scalar Data Formats . . . . . . . . . . . . . . . . . . . . . 335
Structured Data Formats . . . . . . . . . . . . . . . . . . 335
Specifying Argument Passing Conventions . . . . . . . . . 337
Method 2: Generating Wrappers . . . . . . . . . . . . . . 337
Additional Typ es and Options . . . . . . . . . . . . . . . 338
Structured Data Formats . . . . . . . . . . . . . . . . . . 338
Enumerated Types . . . . . . . . . . . . . . . . . . . . . . 338
Procedure Call Formats . . . . . . . . . . . . . . . . . . . 339
Call by Reference . . . . . . . . . . . . . . . . . . . . . . . 339
Array Options . . . . . . . . . . . . . . . . . . . . . . . . 339
Non-passed Arguments . . . . . . . . . . . . . . . . . . . . 340
Argument Checking and Efficiency Considerations . . . . 341
Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Compiler Options . . . . . . . . . . . . . . . . . . . . . . . 343
Evaluation Rules . . . . . . . . . . . . . . . . . . . . . . . 347
Method 3: Customizing Wrappers . . . . . . . . . . . . . . 349
External Function Entry Point . . . . . . . . . . . . . . . 349
Inspecting Automatically Generated Wrappers . . . . . . 351
External API . . . . . . . . . . . . . . . . . . . . . . . . . 355
System Integrity . . . . . . . . . . . . . . . . . . . . . . . 373
6.3 OpenMaple: Using Maple in Compiled Code . . . . . . . . 373
Interface Overview . . . . . . . . . . . . . . . . . . . . . . 374
Call-back Functions . . . . . . . . . . . . . . . . . . . . . 379
Maple Online Help Database . . . . . . . . . . . . . . . . 385
Technical Issues . . . . . . . . . . . . . . . . . . . . . . . . 388
File Structure . . . . . . . . . . . . . . . . . . . . . . . . . 388
Building the Sample Program . . . . . . . . . . . . . . . . 389
6.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . 391
A Internal Representation and Manipulation 395
Contents • ix
A.1 Internal Organization . . . . . . . . . . . . . . . . . . . . 395
Components . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Internal Functions . . . . . . . . . . . . . . . . . . . . . . 396
Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . 397
A.2 Internal Representations of Data Types . . . . . . . . . . 398
Logical AND . . . . . . . . . . . . . . . . . . . . . . . . . 399
Assignment Statement . . . . . . . . . . . . . . . . . . . . 399
Binary Object . . . . . . . . . . . . . . . . . . . . . . . . . 399
Break Statement . . . . . . . . . . . . . . . . . . . . . . . 399
Name Concatenation . . . . . . . . . . . . . . . . . . . . . 400
Complex Value . . . . . . . . . . . . . . . . . . . . . . . . 400
Communications Control Structure . . . . . . . . . . . . . 400
Typ e Specification or Test . . . . . . . . . . . . . . . . . . 401
Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Equation or Test for Equality . . . . . . . . . . . . . . . . 401
Error Statement . . . . . . . . . . . . . . . . . . . . . . . 401
Expression Sequence . . . . . . . . . . . . . . . . . . . . . 402
Floating-Point Number . . . . . . . . . . . . . . . . . . . . 402
For/While Loop Statement . . . . . . . . . . . . . . . . . 402
Foreign Data . . . . . . . . . . . . . . . . . . . . . . . . . 403
Function Call . . . . . . . . . . . . . . . . . . . . . . . . . 404
Garbage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Hardware Float . . . . . . . . . . . . . . . . . . . . . . . . 404
If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Logical IMPLIES . . . . . . . . . . . . . . . . . . . . . . . 405
Not Equal or Test for Inequality . . . . . . . . . . . . . . 405
Negative Integer . . . . . . . . . . . . . . . . . . . . . . . 405
Positive Integer . . . . . . . . . . . . . . . . . . . . . . . . 406
Less Than or Equal . . . . . . . . . . . . . . . . . . . . . . 406
Less Than . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Lexically Scoped Variable within an Expression . . . . . . 407
List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Local Variable within an Expression . . . . . . . . . . . . 408
Memb er . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Module Definition . . . . . . . . . . . . . . . . . . . . . . 408
Module Instance . . . . . . . . . . . . . . . . . . . . . . . 410
Identifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Next Statement . . . . . . . . . . . . . . . . . . . . . . . . 411
Logical NOT . . . . . . . . . . . . . . . . . . . . . . . . . 411
Logical OR . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Procedure Parameter within an Expression . . . . . . . . 411
x • Contents
Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Procedure Definition . . . . . . . . . . . . . . . . . . . . . 412
Product, Quotient, Power . . . . . . . . . . . . . . . . . . 414
Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Rational . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Read Statement . . . . . . . . . . . . . . . . . . . . . . . . 415
Return Statement . . . . . . . . . . . . . . . . . . . . . . 415
Rectangular Table . . . . . . . . . . . . . . . . . . . . . . 415
Save Statement . . . . . . . . . . . . . . . . . . . . . . . . 417
Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Statement Sequence . . . . . . . . . . . . . . . . . . . . . 417
Stop Maple . . . . . . . . . . . . . . . . . . . . . . . . . . 418
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Sum, Difference . . . . . . . . . . . . . . . . . . . . . . . . 418
Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Table Reference . . . . . . . . . . . . . . . . . . . . . . . . 419
Try Statement . . . . . . . . . . . . . . . . . . . . . . . . 419
Unevaluated Expression . . . . . . . . . . . . . . . . . . . 420
Use Statement . . . . . . . . . . . . . . . . . . . . . . . . 420
Logical XOR . . . . . . . . . . . . . . . . . . . . . . . . . 421
Polynomials with Integer Coefficients modulo n . . . . . . 421
A.3 The Use of Hashing in Maple . . . . . . . . . . . . . . . . 422
Basic Hash Tables . . . . . . . . . . . . . . . . . . . . . . 422
Dynamic Hash Tables . . . . . . . . . . . . . . . . . . . . 423
The Simplification Table . . . . . . . . . . . . . . . . . . . 423
The Name Table . . . . . . . . . . . . . . . . . . . . . . . 424
Rememb er Tables . . . . . . . . . . . . . . . . . . . . . . . 425
Maple Language Arrays and Tables . . . . . . . . . . . . . 426
Maple Language Rectangular Tables . . . . . . . . . . . . 426
A.4 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Index 429
Ngày đăng: 18/01/2014, 08:20
Xem thêm: Tài liệu 978-1-897310-47-2AdvancedProgramming1 pptx, Tài liệu 978-1-897310-47-2AdvancedProgramming1 pptx