MATLAB.pod (PDL-2.074) | : | MATLAB.pod (PDL-2.075) | ||
---|---|---|---|---|
skipping to change at line 17 | skipping to change at line 17 | |||
If you are a MATLAB user, this page is for you. It explains the key | If you are a MATLAB user, this page is for you. It explains the key | |||
differences between MATLAB and PDL to help you get going as quickly | differences between MATLAB and PDL to help you get going as quickly | |||
as possible. | as possible. | |||
B<This document is not a tutorial>. For that, go to L<PDL::QuickStart| | B<This document is not a tutorial>. For that, go to L<PDL::QuickStart| | |||
PDL::QuickStart>. This document B<complements> the Quick Start guide, as | PDL::QuickStart>. This document B<complements> the Quick Start guide, as | |||
it highlights the key differences between MATLAB and PDL. | it highlights the key differences between MATLAB and PDL. | |||
=head1 Perl | =head1 Perl | |||
The key differences between MATLAB and PDL are threading, and B<Perl>. | The key differences between MATLAB and PDL are broadcasting, and B<Perl>. | |||
Threading means you can get a reference to just a part of your data, | Broadcasting means you can get a reference to just a part of your data, | |||
and operate on it in a way that makes sense for your application. Those | and operate on it in a way that makes sense for your application. Those | |||
operations will be reflected in the original data. | operations will be reflected in the original data. | |||
Perl is a general purpose programming language with thousands of modules | Perl is a general purpose programming language with thousands of modules | |||
freely available on the web. PDL is an extension of Perl. This gives PDL | freely available on the web. PDL is an extension of Perl. This gives PDL | |||
programs access to more features than most numerical tools can dream of. | programs access to more features than most numerical tools can dream of. | |||
At the same time, most syntax differences between MATLAB and PDL are a | At the same time, most syntax differences between MATLAB and PDL are a | |||
result of its Perl foundation. | result of its Perl foundation. | |||
B<You do not have to learn much Perl to be effective with PDL>. But | B<You do not have to learn much Perl to be effective with PDL>. But | |||
skipping to change at line 493 | skipping to change at line 493 | |||
endfor } | endfor } | |||
=over 5 | =over 5 | |||
=item B<Note> | =item B<Note> | |||
Never use for-loops for numerical work. Perl's for-loops are faster | Never use for-loops for numerical work. Perl's for-loops are faster | |||
than MATLAB's, but they both pale against a "vectorized" operation. | than MATLAB's, but they both pale against a "vectorized" operation. | |||
PDL has many tools that facilitate writing vectorized programs. | PDL has many tools that facilitate writing vectorized programs. | |||
These are beyond the scope of this guide. To learn more, see: | These are beyond the scope of this guide. To learn more, see: | |||
L<PDL::Indexing>, L<PDL::Threading>, | L<PDL::Indexing>, L<PDL::Broadcasting>, | |||
and L<PDL::PP>. | and L<PDL::PP>. | |||
Likewise, never use C<1..10> for numerical work, even outside a for-loop. | Likewise, never use C<1..10> for numerical work, even outside a for-loop. | |||
C<1..10> is a Perl array. Perl arrays are designed for flexibility, not | C<1..10> is a Perl array. Perl arrays are designed for flexibility, not | |||
speed. Use I<ndarrays> instead. To learn more, see the next section. | speed. Use I<ndarrays> instead. To learn more, see the next section. | |||
=back | =back | |||
=head2 ndarrays vs Perl Arrays | =head2 ndarrays vs Perl Arrays | |||
skipping to change at line 750 | skipping to change at line 750 | |||
=over 5 | =over 5 | |||
=item L<PDL::Indexing> | =item L<PDL::Indexing> | |||
B<Level>: Beginner | B<Level>: Beginner | |||
This beginner tutorial covers the standard "vectorization" feature that | This beginner tutorial covers the standard "vectorization" feature that | |||
you already know from MATLAB. Use this page to learn how to avoid for-loops | you already know from MATLAB. Use this page to learn how to avoid for-loops | |||
to make your program more efficient. | to make your program more efficient. | |||
=item L<PDL::Threading> | =item L<PDL::Broadcasting> | |||
B<Level>: Intermediate | B<Level>: Intermediate | |||
PDL's "vectorization" feature goes beyond what most numerical software | PDL's "vectorization" feature goes beyond what most numerical software | |||
can do. In this tutorial you'll learn how to "thread" over higher dimensions, | can do. In this tutorial you'll learn how to "broadcast" over higher dimensions, | |||
allowing you to vectorize your program further than is possible in MATLAB. | allowing you to vectorize your program further than is possible in MATLAB. | |||
=item Benchmarks | =item Benchmarks | |||
B<Level>: Intermediate | B<Level>: Intermediate | |||
Perl comes with an easy to use benchmarks module to help you find how | Perl comes with an easy to use benchmarks module to help you find how | |||
long it takes to execute different parts of your code. It is a great | long it takes to execute different parts of your code. It is a great | |||
tool to help you focus your optimization efforts. You can read about it | tool to help you focus your optimization efforts. You can read about it | |||
online (L<http://perldoc.perl.org/Benchmark.html>) or through the | online (L<http://perldoc.perl.org/Benchmark.html>) or through the | |||
skipping to change at line 978 | skipping to change at line 978 | |||
for v = 1:n; P(v, v) = v; end | for v = 1:n; P(v, v) = v; end | |||
for k = 1:n | for k = 1:n | |||
prevD = D; | prevD = D; | |||
D = min(D,D(:,k) + D(k,:)); | D = min(D,D(:,k) + D(k,:)); | |||
coords = find(D<prevD); | coords = find(D<prevD); | |||
from_coords = n * (k-1) + mod(coords-1, n) + 1; % change col to k in 1-bas ed | from_coords = n * (k-1) + mod(coords-1, n) + 1; % change col to k in 1-bas ed | |||
P(coords) = P(from_coords); | P(coords) = P(from_coords); | |||
end | end | |||
end | end | |||
By comparison, the lack of "threading" means that to update the diagonal | By comparison, the lack of "broadcasting" means that to update the diagonal | |||
requires a for-loop, which in the sphere of vectorised calculations is | requires a for-loop, which in the sphere of vectorised calculations is | |||
a bad thing. The calculations of coordinates are complicated by the | a bad thing. The calculations of coordinates are complicated by the | |||
1-based counting. | 1-based counting. | |||
=head1 COPYRIGHT | =head1 COPYRIGHT | |||
Copyright 2010 Daniel Carrera (dcarrera@gmail.com). You can distribute and/or | Copyright 2010 Daniel Carrera (dcarrera@gmail.com). You can distribute and/or | |||
modify this document under the same terms as the current Perl license. | modify this document under the same terms as the current Perl license. | |||
See: L<http://dev.perl.org/licenses/> | See: L<http://dev.perl.org/licenses/> | |||
End of changes. 6 change blocks. | ||||
6 lines changed or deleted | 6 lines changed or added |