perldl>? inv
####
Module PDL::MatrixOps
inv
Signature: (a(m,m); sv opt )
$a1 = inv($a, {$opt});
Invert a square matrix.
You feed in an NxN matrix in $a, and get back its inverse (if it
exists). The code is inplace-aware, so you can get back the
inverse in $a itself if you want -- though temporary storage is
used either way.
You can cache the LU decomposition in an output option variable.
"inv" uses lu_decomp by default; that is a numerically stable
(pivoting) LU decomposition method. If you ask it to thread then
a numerically unstable (non-pivoting) method is used instead, so
avoid threading over collections of large (say, more than 4x4) or
near-singular matrices unless precision is not important.
OPTIONS:
* s
Boolean value indicating whether to complain if the matrix is
singular. If this is false, singular matrices cause inverse to
barf.
If it is true, then singular matrices cause inverse to return
undef.
In the threading case, no checking for singularity is
performed, if any of the matrices in your threaded collection
are singular, they receive NaN entries.
##
##
perldl>?? inverse
##
##
perldl> ?? inverse
PDL::Transform::unmap ...
Map an image or N-D dataset using the inverse as a
coordinate transform.
erfi The inverse of the error function. Works inplace.
ifft Complex Inverse FFT of the "real" and "imag" arrays
[inplace]
ifftnd N-dimensional inverse FFT
invert Apply an inverse transformation to some input
coordinates.
realifft Inverse of one-dimensional realfft routine [inplace].
t_fits FITS pixel-to-scientific transformation with inverse
t_inverse Return the inverse of a PDL::Transform. This just
reverses the func/inv, idim/odim, itype/otype, and
iunit/ounit pairs. Note that sometimes you end up
with a transform that cannot be applied or mapped,
because either the mathematical inverse doesn't exist
or the inverse func isn't implemented.
t_quadratic Quadratic scaling -- cylindrical pincushion
(n-d; with inverse)
t_radial Convert Cartesian to radial/cylindrical coordinates.
(2-D/3-D; with inverse)
t_spherical Convert Cartesian to spherical coordinates.
(3-D; with inverse)
##
##
perldl>?? invert
##
##
inv Invert a square matrix.
invert Apply an inverse transformation to some
input coordinates.
##
##
perldl>demo
##
##
perldl>demo PDL
##
##
----
Welcome to a short tour of PDL's capabilities.
This tour shows some of the main selling points
of PDL. However, because we want this script to
run everywhere, some modules which require external
modules for use are explicitly excluded, namely
- PDL::Graphics::TriD (3D Graphics) [*]
- PDL::Graphics::PGPLOT (PGPLOT graphics)
- PDL::IO::FlexRaw (flexible raw input/output)
[*]: this module has its separate demos in a subdirectory.
Note that your own scripts must start with
use PDL;
to work properly, so that you can simply say
perl script.pl
or you can just try some of the commands illustrated
in the demos by just retyping them at the perldl
command prompt.
---- (press enter)
##
##
perldl> $scalar_piddle = pdl 42
perldl> $one_dimensional_piddle = pdl(1,2,3)
perldl> $two_dimensional_piddle = pdl([1,2,3],[4,5,6])
##
##
perldl> ? vars
##
##
PDL variables in package main::
Name Type Dimension Flow State Mem
----------------------------------------------------------------
$scalar_piddle Double D [] P 0.01Kb
$one_dimensional_piddle Double D [3] P 0.02Kb
$two_dimensional_piddle Double D [3,2] P 0.05Kb
##
##
perldl> $pdl_of_zeroes = zeroes(10,2)
perldl> p $pdl_of_zeroes
[
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
]
perldl> $pdl_of_ones = ones(5,3)
perldl> p $pdl_of_ones
[
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
]
perldl> $pdl_identity = identity(4,4)
perldl> p $pdl_identity
[
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
]
##
##
perldl>$new_pdl_of_zeroes = zeroes(byte, 10, 2)
##
##
perldl> ? vars
PDL variables in package main::
Name Type Dimension Flow State Mem
----------------------------------------------------------------
$pdl_of_zeroes Double D [10,2] P 0.16Kb
$new_pdl_of_zeroes Byte D [10,2] P 0.02Kb
##
##
+ - * / ** > <
>= <= == != << >> &
| ^ += -= *= /= %=
**= >>= <<= &= |= ^=
<=> ! % ++ --
##
##
abs acos acosh asin asinh
atan atan2 atanh cos cosh
sin sinh tan tanh sqrt
ceil floor rint exp log log1
##
##
perldl>$pdl_of_zeroes = null;
##
##
perldl>$n = nelem($new_pdl_of_zeroes);
##
##
perldl>$n = $new_pdl_of_zeroes->nelem();
##
##
perldl>@dims = dims($new_pdl_of_zeroes);
##
##
perldl>@dims = $new_pdl_of_zeroes->dims;
##
##
perldl>$info = $new_pdl_of_zeroes->info;
##
##
T Type
D Formatted Dimensions
F Dataflow status
S Some internal flags (P=physical,V=Vaffine,C=changed)
C Class of this piddle, i.e. "ref $pdl"
A Address of the piddle struct as a unique identifier
M Calculated memory consumption of this piddle's data area
##
##
perldl> p $new_pdl_of_zeroes->info("Mem : %M");
##
##
Mem : 0.02Kb
##
##
perldl> p $piddle = sequence(10,12);
##
##
[
[ 0 1 2 3 4 5 6 7 8 9]
[ 10 11 12 13 14 15 16 17 18 19]
[ 20 21 22 23 24 25 26 27 28 29]
[ 30 31 32 33 34 35 36 37 38 39]
[ 40 41 42 43 44 45 46 47 48 49]
[ 50 51 52 53 54 55 56 57 58 59]
[ 60 61 62 63 64 65 66 67 68 69]
[ 70 71 72 73 74 75 76 77 78 79]
[ 80 81 82 83 84 85 86 87 88 89]
[ 90 91 92 93 94 95 96 97 98 99]
[100 101 102 103 104 105 106 107 108 109]
[110 111 112 113 114 115 116 117 118 119]
]
##
##
perldl> p at($piddle, 5,3);
##
##
perldl> p $piddle->at(5,3);
##
##
perldl> p $slice_1a = slice($piddle, "0:2,-1");
##
##
[
[110 111 112]
]
##
##
perldl> p $slice_1b = $piddle->slice("0:2,-1");
##
##
[
[110 111 112]
]
##
##
perldl> p $slice_2a = slice($piddle,"-1:-2,:");
##
##
[
[ 9 8]
[ 19 18]
[ 29 28]
[ 39 38]
[ 49 48]
[ 59 58]
[ 69 68]
[ 79 78]
[ 89 88]
[ 99 98]
[109 108]
[119 118]
]
##
##
perldl> p $slice_2b = $piddle->slice("-1:-2,:");
##
##
[
[ 9 8]
[ 19 18]
[ 29 28]
[ 39 38]
[ 49 48]
[ 59 58]
[ 69 68]
[ 79 78]
[ 89 88]
[ 99 98]
[109 108]
[119 118]
]
##
##
perldl> p $second_column = $piddle(1,:);
##
##
[
[ 1]
[ 11]
[ 21]
[ 31]
[ 41]
[ 51]
[ 61]
[ 71]
[ 81]
[ 91]
[101]
[111]
]
##
##
perldl> p $second_row = $piddle(:,1);
##
##
[
[10 11 12 13 14 15 16 17 18 19]
]
##
##
perldl> p $first_to_third_columns = $piddle(0:2,:);
##
##
[
[ 0 1 2]
[ 10 11 12]
[ 20 21 22]
[ 30 31 32]
[ 40 41 42]
[ 50 51 52]
[ 60 61 62]
[ 70 71 72]
[ 80 81 82]
[ 90 91 92]
[100 101 102]
[110 111 112]
]
##
##
perldl> p $even_columns = $piddle(0:-1:2,:);
##
##
[
[ 0 2 4 6 8]
[ 10 12 14 16 18]
[ 20 22 24 26 28]
[ 30 32 34 36 38]
[ 40 42 44 46 48]
[ 50 52 54 56 58]
[ 60 62 64 66 68]
[ 70 72 74 76 78]
[ 80 82 84 86 88]
[ 90 92 94 96 98]
[100 102 104 106 108]
[110 112 114 116 118]
]
##
##
perldl> p $odd_columns = $piddle(1:-1:2,:);
##
##
[
[ 1 3 5 7 9]
[ 11 13 15 17 19]
[ 21 23 25 27 29]
[ 31 33 35 37 39]
[ 41 43 45 47 49]
[ 51 53 55 57 59]
[ 61 63 65 67 69]
[ 71 73 75 77 79]
[ 81 83 85 87 89]
[ 91 93 95 97 99]
[101 103 105 107 109]
[111 113 115 117 119]
]
##
##
perldl> p $even_rows = $piddle(:,0:-1:2);
##
##
[
[ 0 1 2 3 4 5 6 7 8 9]
[ 20 21 22 23 24 25 26 27 28 29]
[ 40 41 42 43 44 45 46 47 48 49]
[ 60 61 62 63 64 65 66 67 68 69]
[ 80 81 82 83 84 85 86 87 88 89]
[100 101 102 103 104 105 106 107 108 109]
]
##
##
perldl> p $odd_rows = $piddle(:,1:-1:2);
##
##
[
[ 10 11 12 13 14 15 16 17 18 19]
[ 30 31 32 33 34 35 36 37 38 39]
[ 50 51 52 53 54 55 56 57 58 59]
[ 70 71 72 73 74 75 76 77 78 79]
[ 90 91 92 93 94 95 96 97 98 99]
[110 111 112 113 114 115 116 117 118 119]
]
##
##
perldl> p $one_element_slice = $piddle(5,5);
##
##
[
[55]
]
##
##
perldl> p $dice1 = dice($piddle, [1,3],[0,7]);
##
##
[
[ 1 3]
[71 73]
]
##
##
perldl> p $dice2 = $piddle->dice(X,[0,1,8]);
##
##
[
[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[80 81 82 83 84 85 86 87 88 89]
]
##
##
perldl> p $dice2 .= -99;
##
##
[
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
]
##
##
perldl> p $piddle
[
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[ 20 21 22 23 24 25 26 27 28 29]
[ 30 31 32 33 34 35 36 37 38 39]
[ 40 41 42 43 44 45 46 47 48 49]
[ 50 51 52 53 54 55 56 57 58 59]
[ 60 61 62 63 64 65 66 67 68 69]
[ 70 71 72 73 74 75 76 77 78 79]
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[ 90 91 92 93 94 95 96 97 98 99]
[100 101 102 103 104 105 106 107 108 109]
[110 111 112 113 114 115 116 117 118 119]
]
##
##
perldl> p $indx = intersect( which($piddle>43), which($piddle<72) );
##
##
[44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71]
##
##
p $piddle->flat->index( $indx ) .= 255; #assigning the new values
##
##
[255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255]
##
##
perldl> p $piddle
[
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[ 20 21 22 23 24 25 26 27 28 29]
[ 30 31 32 33 34 35 36 37 38 39]
[ 40 41 42 43 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255]
[255 255 72 73 74 75 76 77 78 79]
[-99 -99 -99 -99 -99 -99 -99 -99 -99 -99]
[ 90 91 92 93 94 95 96 97 98 99]
[100 101 102 103 104 105 106 107 108 109]
[110 111 112 113 114 115 116 117 118 119]
]
##
##
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use PDL;
use PDL::Graphics::PGPLOT;
# getting a brain image from the ITK (http://itk.org) CVS website
my $img_url = "http://www.itk.org/cgi-bin/viewcvs.cgi/*checkout*/Examples/Data/BrainProtonDensitySliceBorder20.png?rev=1.2&root=Insight";
my $savefilename = "brain.png";
my $status = getstore($img_url,$savefilename);
print $status."\n" if is_success($status);
# reading the image and creating a copy of it
$brain = rim( “brain.png” );
$brain2 = $brain->copy;
#plotting the two images in two different windows
$dev = '/XSERVE';
$win = PDL::Graphics::PGPLOT::Window->new( { Dev => $dev } );
$win->imag( $brain );
$win2 = PDL::Graphics::PGPLOT::Window->new( { Dev => $dev } );
$win2->imag( $brain2 );
# The pixel values in the images are in the range [0, 255]
# (with zero representing black and 255 representing white).
#
# Let's change all the pixel values greater than 220 to be
# equal to zero. This will create some black spots in the
# image
$indx = which( $brain > 220 );
$brain2->flat->index( $indx ) .= 0;
# Let's plot the new image
$win2->imag( $brain2 );
# Now, we free up the windows so that you can close them.
$win->close();
$win2->close();