| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# Copyright (C) 1998 Tuomas J. Lukka. |
|
2
|
|
|
|
|
|
|
# All rights reserved, except redistribution |
|
3
|
|
|
|
|
|
|
# with PDL under the PDL License permitted. |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package PDL::Demos::General; |
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
0
|
7
|
sub info {('pdl', 'Introduction -- a tour of PDL')} |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my @demo = ( |
|
10
|
|
|
|
|
|
|
[comment => q| |
|
11
|
|
|
|
|
|
|
Welcome to a short tour of PDL's capabilities. |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
This tour shows some of the main selling points |
|
14
|
|
|
|
|
|
|
of PDL. However, because we want this script to |
|
15
|
|
|
|
|
|
|
run everywhere, it doesn't show off modules which |
|
16
|
|
|
|
|
|
|
require external modules for use, including those |
|
17
|
|
|
|
|
|
|
supporting 3D graphics. You can list all the demos |
|
18
|
|
|
|
|
|
|
available on your system by doing "demo" at the |
|
19
|
|
|
|
|
|
|
'pdl>' prompt. |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Note that your own scripts must start with |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use PDL; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
to work properly, so that you can simply say |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
perl script.pl |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
or you can just try some of the commands illustrated |
|
30
|
|
|
|
|
|
|
in the demos by just retyping them at the perldl or pdl |
|
31
|
|
|
|
|
|
|
'pdl>' command prompt. |
|
32
|
|
|
|
|
|
|
|], |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
[act => q| |
|
35
|
|
|
|
|
|
|
$x = zeroes 5,5; # 5x5 matrix |
|
36
|
|
|
|
|
|
|
print $x; |
|
37
|
|
|
|
|
|
|
|], |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
[act => q| |
|
40
|
|
|
|
|
|
|
# Now, don't think that the number of dimensions is limited |
|
41
|
|
|
|
|
|
|
# to two: |
|
42
|
|
|
|
|
|
|
$m = zeroes(3,2,2); # 3x2x2 cube |
|
43
|
|
|
|
|
|
|
print $m; |
|
44
|
|
|
|
|
|
|
|], |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
[act => q| |
|
47
|
|
|
|
|
|
|
$x ++; # Operators like increment work.. |
|
48
|
|
|
|
|
|
|
print $x; |
|
49
|
|
|
|
|
|
|
|], |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
[act => q| |
|
52
|
|
|
|
|
|
|
# xvals and yvals (yes, there is also zvals...) |
|
53
|
|
|
|
|
|
|
# give you ndarrays which give the coordinate value. |
|
54
|
|
|
|
|
|
|
$y = xvals $x; |
|
55
|
|
|
|
|
|
|
print $y; |
|
56
|
|
|
|
|
|
|
|], |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
[act => q| |
|
59
|
|
|
|
|
|
|
# So you can do things like |
|
60
|
|
|
|
|
|
|
$y = $x + 0.1 * xvals($x) + 0.01 * yvals($x); |
|
61
|
|
|
|
|
|
|
print $y; |
|
62
|
|
|
|
|
|
|
|], |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
[act => q| |
|
65
|
|
|
|
|
|
|
# Arithmetic operations work: |
|
66
|
|
|
|
|
|
|
$x = xvals(10) / 5; |
|
67
|
|
|
|
|
|
|
print $x,"\n"; |
|
68
|
|
|
|
|
|
|
print ((sin $x),"\n"); |
|
69
|
|
|
|
|
|
|
|], |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
[act => q| |
|
72
|
|
|
|
|
|
|
# You can also take slices: |
|
73
|
|
|
|
|
|
|
print $y; |
|
74
|
|
|
|
|
|
|
print $y->slice(":,2:3"); # rows 2 and 3 |
|
75
|
|
|
|
|
|
|
|], |
|
76
|
|
|
|
|
|
|
[act => q| |
|
77
|
|
|
|
|
|
|
print $y->slice("2:3,:"); # or columns 2 and 3 |
|
78
|
|
|
|
|
|
|
|], |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
[act => q| |
|
81
|
|
|
|
|
|
|
print $y; |
|
82
|
|
|
|
|
|
|
print $y->diagonal(0,1),"\n"; # 0 and 1 are the dimensions |
|
83
|
|
|
|
|
|
|
|], |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
[act => q| |
|
86
|
|
|
|
|
|
|
# One of the really nifty features is that the |
|
87
|
|
|
|
|
|
|
# slices are actually references back to the original |
|
88
|
|
|
|
|
|
|
# ndarray: |
|
89
|
|
|
|
|
|
|
$diag = $y->diagonal(0,1); |
|
90
|
|
|
|
|
|
|
print $y; |
|
91
|
|
|
|
|
|
|
print $diag,"\n"; |
|
92
|
|
|
|
|
|
|
$diag+=100; |
|
93
|
|
|
|
|
|
|
print "AFTER:\n"; |
|
94
|
|
|
|
|
|
|
print $diag,"\n"; |
|
95
|
|
|
|
|
|
|
print "Now, guess what \$y looks like?\n"; |
|
96
|
|
|
|
|
|
|
|], |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
[act => q| |
|
99
|
|
|
|
|
|
|
# Yes, it has changed: |
|
100
|
|
|
|
|
|
|
print $y; |
|
101
|
|
|
|
|
|
|
|], |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
[act => q| |
|
104
|
|
|
|
|
|
|
# Another example (we only modify elements 0,2 and 4 of |
|
105
|
|
|
|
|
|
|
# each row): |
|
106
|
|
|
|
|
|
|
$t = $y->slice("0:4:2"); $t += 50; |
|
107
|
|
|
|
|
|
|
print $y; |
|
108
|
|
|
|
|
|
|
|], |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
[act => q| |
|
111
|
|
|
|
|
|
|
# There are lots of useful functions in e.g. PDL::Primitive |
|
112
|
|
|
|
|
|
|
# and PDL::Slices - we can't show you all but here are some |
|
113
|
|
|
|
|
|
|
# examples: |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
print $y; |
|
116
|
|
|
|
|
|
|
print $y->sum, "\n"; |
|
117
|
|
|
|
|
|
|
print $y->sumover,"\n"; # Only over first dim. |
|
118
|
|
|
|
|
|
|
|], |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
[act => q| |
|
121
|
|
|
|
|
|
|
print $y->transpose; |
|
122
|
|
|
|
|
|
|
print $y->minimum,"\n"; # over first dim. |
|
123
|
|
|
|
|
|
|
print $y->min,"\n"; |
|
124
|
|
|
|
|
|
|
|], |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
[act => q| |
|
127
|
|
|
|
|
|
|
srandom(5); |
|
128
|
|
|
|
|
|
|
print $y->random; |
|
129
|
|
|
|
|
|
|
|], |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
[act => q| |
|
132
|
|
|
|
|
|
|
# Here are some more advanced tricks for selecting |
|
133
|
|
|
|
|
|
|
# parts of 1-D vectors: |
|
134
|
|
|
|
|
|
|
$x = (xvals 12)/3; |
|
135
|
|
|
|
|
|
|
$i = which(sin($x) > 0.5); # Indices of those sines > 0.5 |
|
136
|
|
|
|
|
|
|
print $x,"\n"; |
|
137
|
|
|
|
|
|
|
print $i,"\n"; |
|
138
|
|
|
|
|
|
|
print $x->index($i),"\n"; |
|
139
|
|
|
|
|
|
|
# and we can have the effect of the last command in one |
|
140
|
|
|
|
|
|
|
# go using 'where' instead of 'which' and 'index' as in |
|
141
|
|
|
|
|
|
|
print $x->where(sin($x) > 0.5),"\n"; |
|
142
|
|
|
|
|
|
|
# and finally take the sin of these elements |
|
143
|
|
|
|
|
|
|
# (to show that these are indeed the correct ones) |
|
144
|
|
|
|
|
|
|
print sin($x->index($i)),"\n"; |
|
145
|
|
|
|
|
|
|
|], |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
[comment => q| |
|
148
|
|
|
|
|
|
|
We hope you enjoyed these demos illustrating some |
|
149
|
|
|
|
|
|
|
of the basic capabilities of PDL. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
We encourage you to play with these commands in |
|
152
|
|
|
|
|
|
|
the perldl shell and use its online help support |
|
153
|
|
|
|
|
|
|
to find out more about these and other commands and |
|
154
|
|
|
|
|
|
|
features of PDL. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Just type 'help' to get started. |
|
157
|
|
|
|
|
|
|
|], |
|
158
|
|
|
|
|
|
|
); |
|
159
|
|
|
|
|
|
|
|
|
160
|
1
|
|
|
1
|
0
|
7
|
sub demo { @demo } |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |