General.pm (PDL-2.076) | : | General.pm (PDL-2.077) | ||
---|---|---|---|---|

# Copyright (C) 1998 Tuomas J. Lukka. | # Copyright (C) 1998 Tuomas J. Lukka. | |||

# All rights reserved, except redistribution | # All rights reserved, except redistribution | |||

# with PDL under the PDL License permitted. | # with PDL under the PDL License permitted. | |||

package PDL::Demos::General; | package PDL::Demos::General; | |||

use PDL; | ||||

PDL::Demos::Routines->import(); | sub info {('pdl', 'general demo')} | |||

sub comment($); | ||||

sub act($); | ||||

sub output; | ||||

sub run { | my @demo = ( | |||

[comment => q| | ||||

comment q| | ||||

Welcome to a short tour of PDL's capabilities. | Welcome to a short tour of PDL's capabilities. | |||

This tour shows some of the main selling points | This tour shows some of the main selling points | |||

of PDL. However, because we want this script to | of PDL. However, because we want this script to | |||

run everywhere, some modules which require external | run everywhere, some modules which require external | |||

modules for use are explicitly excluded, namely | modules for use are explicitly excluded, namely | |||

- PDL::Graphics::TriD (3D Graphics) [*] | - PDL::Graphics::TriD (3D Graphics) [*] | |||

- PDL::Graphics::PGPLOT (PGPLOT graphics) | - PDL::Graphics::PGPLOT (PGPLOT graphics) | |||

- PDL::IO::FlexRaw (flexible raw input/output) | - PDL::IO::FlexRaw (flexible raw input/output) | |||

[*]: this module has its separate demos in a subdirectory. | [*]: this module has its separate demos in a subdirectory. | |||

skipping to change at line 38 | skipping to change at line 33 | |||

use PDL; | use PDL; | |||

to work properly, so that you can simply say | to work properly, so that you can simply say | |||

perl script.pl | perl script.pl | |||

or you can just try some of the commands illustrated | or you can just try some of the commands illustrated | |||

in the demos by just retyping them at the perldl or pdl | in the demos by just retyping them at the perldl or pdl | |||

'pdl>' command prompt. | 'pdl>' command prompt. | |||

|; | |], | |||

act q| | [act => q| | |||

$x = zeroes 5,5; # 5x5 matrix | $x = zeroes 5,5; # 5x5 matrix | |||

output $x; | output $x; | |||

|; | |], | |||

act q| | [act => q| | |||

# Now, don't think that the number of dimensions is limited | # Now, don't think that the number of dimensions is limited | |||

# to two: | # to two: | |||

$m = zeroes(3,2,2); # 3x2x2 cube | $m = zeroes(3,2,2); # 3x2x2 cube | |||

output $m; | output $m; | |||

|; | |], | |||

act q| | [act => q| | |||

$x ++; # Operators like increment work.. | $x ++; # Operators like increment work.. | |||

output $x; | output $x; | |||

|; | |], | |||

act q| | [act => q| | |||

# xvals and yvals (yes, there is also zvals...) | # xvals and yvals (yes, there is also zvals...) | |||

# give you ndarrays which give the coordinate value. | # give you ndarrays which give the coordinate value. | |||

$y = xvals $x; | $y = xvals $x; | |||

output $y; | output $y; | |||

|; | |], | |||

act q| | [act => q| | |||

# So you can do things like | # So you can do things like | |||

$y = $x + 0.1 * xvals($x) + 0.01 * yvals($x); | $y = $x + 0.1 * xvals($x) + 0.01 * yvals($x); | |||

output $y; | output $y; | |||

|; | |], | |||

act q| | [act => q| | |||

# Arithmetic operations work: | # Arithmetic operations work: | |||

$x = xvals(10) / 5; | $x = xvals(10) / 5; | |||

output $x,"\n"; | output $x,"\n"; | |||

output ((sin $x),"\n"); | output ((sin $x),"\n"); | |||

|; | |], | |||

act q| | [act => q| | |||

# You can also take slices: | # You can also take slices: | |||

output $y; | output $y; | |||

output $y->slice(":,2:3"); # rows 2 and 3 | output $y->slice(":,2:3"); # rows 2 and 3 | |||

|; | |], | |||

act q| | [act => q| | |||

output $y->slice("2:3,:"); # or columns 2 and 3 | output $y->slice("2:3,:"); # or columns 2 and 3 | |||

|; | |], | |||

act q| | [act => q| | |||

output $y; | output $y; | |||

output $y->diagonal(0,1),"\n"; # 0 and 1 are the dimensions | output $y->diagonal(0,1),"\n"; # 0 and 1 are the dimensions | |||

|; | |], | |||

act q| | [act => q| | |||

# One of the really nifty features is that the | # One of the really nifty features is that the | |||

# slices are actually references back to the original | # slices are actually references back to the original | |||

# ndarray: | # ndarray: | |||

$diag = $y->diagonal(0,1); | $diag = $y->diagonal(0,1); | |||

output $y; | output $y; | |||

output $diag,"\n"; | output $diag,"\n"; | |||

$diag+=100; | $diag+=100; | |||

output "AFTER:\n"; | output "AFTER:\n"; | |||

output $diag,"\n"; | output $diag,"\n"; | |||

output "Now, guess what \$y looks like?\n"; | output "Now, guess what \$y looks like?\n"; | |||

|; | |], | |||

act q| | [act => q| | |||

# Yes, it has changed: | # Yes, it has changed: | |||

output $y; | output $y; | |||

|; | |], | |||

act q| | [act => q| | |||

# Another example (we only modify elements 0,2 and 4 of | # Another example (we only modify elements 0,2 and 4 of | |||

# each row): | # each row): | |||

$t = $y->slice("0:4:2"); $t += 50; | $t = $y->slice("0:4:2"); $t += 50; | |||

output $y; | output $y; | |||

|; | |], | |||

act q| | [act => q| | |||

# There are lots of useful functions in e.g. PDL::Primitive | # There are lots of useful functions in e.g. PDL::Primitive | |||

# and PDL::Slices - we can't show you all but here are some | # and PDL::Slices - we can't show you all but here are some | |||

# examples: | # examples: | |||

output $y; | output $y; | |||

output $y->sum, "\n"; | output $y->sum, "\n"; | |||

output $y->sumover,"\n"; # Only over first dim. | output $y->sumover,"\n"; # Only over first dim. | |||

|; | |], | |||

act q| | [act => q| | |||

output $y->transpose; | output $y->transpose; | |||

output $y->minimum,"\n"; # over first dim. | output $y->minimum,"\n"; # over first dim. | |||

output $y->min,"\n"; | output $y->min,"\n"; | |||

|; | |], | |||

act q| | [act => q| | |||

output $y->random; | output $y->random; | |||

|; | |], | |||

act q| | [act => q| | |||

# Here are some more advanced tricks for selecting | # Here are some more advanced tricks for selecting | |||

# parts of 1-D vectors: | # parts of 1-D vectors: | |||

$x = (xvals 12)/3; | $x = (xvals 12)/3; | |||

$i = which(sin($x) > 0.5); # Indices of those sines > 0.5 | $i = which(sin($x) > 0.5); # Indices of those sines > 0.5 | |||

output $x,"\n"; | output $x,"\n"; | |||

output $i,"\n"; | output $i,"\n"; | |||

output $x->index($i),"\n"; | output $x->index($i),"\n"; | |||

# and we can have the effect of the last command in one | # and we can have the effect of the last command in one | |||

# go using 'where' instead of 'which' and 'index' as in | # go using 'where' instead of 'which' and 'index' as in | |||

output $x->where(sin($x) > 0.5),"\n"; | output $x->where(sin($x) > 0.5),"\n"; | |||

# and finally take the sin of these elements | # and finally take the sin of these elements | |||

# (to show that these are indeed the correct ones) | # (to show that these are indeed the correct ones) | |||

output sin($x->index($i)),"\n"; | output sin($x->index($i)),"\n"; | |||

|; | |], | |||

comment q| | [comment => q| | |||

We hope you enjoyed these demos illustrating some | We hope you enjoyed these demos illustrating some | |||

of the basic capabilities of PDL. | of the basic capabilities of PDL. | |||

We encourage you to play with these commands in | We encourage you to play with these commands in | |||

the perldl or pdl2 shell and use its online help support | the perldl or pdl2 shell and use its online help support | |||

to find out more about these and other commands and | to find out more about these and other commands and | |||

features of PDL. | features of PDL. | |||

Just type 'help' to get started. | Just type 'help' to get started. | |||

|], | ||||

); | ||||

|; | sub demo { @demo } | |||

} | ||||

1; | 1; | |||

End of changes. 38 change blocks. | ||||

45 lines changed or deleted | | 40 lines changed or added |