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