Information Technology Reference
In-Depth Information
/home/me/python/modules . To notify Python about this directory, you can either
add the directory name to sys.path in the application code 19
sys.path = sys.path + ['/home/me/python/modules']
or you can set PYTHONPATH appropriately in your start-up file. If you work in a Unix
Bash environment, the start-up file is normally .bashrc , and in this file you can
write
export PYTHONPATH=$PYTHONPATH:/home/me/python/modules
Maple Libraries
A collection of Maple procedures can be turned into a package , which is the Maple
terminology for what we previously have referred to as library. The procedure for
creating a package is a bit more comprehensive than for the other programming
languages we touch upon here, so we refer to the online help facility in Maple
for complete information. Try first help - topic search - package, structure. A
complete example on creating a package is found under the main chapter Exam-
ple Worksheets in the help menu. Go further to Language and System and then to
examples/binarytree.
Say the name of your package is numint . Here is a typical use of the package:
with(numint); # import functions like Trapezoidal, Simpson, ...
f1 := x -> exp(-x * x) * log(1+x * sin(x)); # func to be integrated
qt := Trapezoidal(0, 2, f1, 1000):
qs := Simpson(0, 2, f1, 1000):
printf("Trapezoidal=%e, Simpson=%e", qt, qs);
6.5.2
Motivation for Object-Oriented Libraries
The function libraries presented in the previous section are easy to apply; a user
just needs to get access to the library and call the desired function. Application
codes often require the user to specify at run time the numerical methods to be used
for, e.g., integration. This is easily accomplished by reading information about the
method and calling the appropriate function in an if-else test:
if method eq. ”Trapezoidal” then
Trapezoidal ( a , b , f , n )
else if method eq. ”Simpson” then
Simpson ( a , b , f , n )
andsoon.
With Python we can build code at run time and execute a string of code with the
exec function. This allows reading both the method and the function as user input,
19 Here sys.path is a list of strings (directory names), and the statement adds two lists.
Search WWH ::




Custom Search