|
NAMEPDL::MATLAB - A guide for MATLAB users.INTRODUCTIONIf 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 as possible.This document is not a tutorial. For that, go to PDL::QuickStart. This document complements the Quick Start guide, as it highlights the key differences between MATLAB and PDL. PerlThe key difference between MATLAB and PDL is Perl.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 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 result of its Perl foundation. You do not have to learn much Perl to be effective with PDL. But if you wish to learn Perl, there is excellent documentation available on-line (<http://perldoc.perl.org>) or through the command "perldoc perl". There is also a beginner's portal (<http://perl-begin.org>). Perl's module repository is called CPAN (<http://www.cpan.org>) and it has a vast array of modules. Run "perldoc cpan" for more information. TERMINOLOGY: PIDDLEMATLAB typically refers to vectors, matrices, and arrays. Perl already has arrays, and the terms "vector" and "matrix" typically refer to one- and two-dimensional collections of data. Having no good term to describe their object, PDL developers coined the term "piddle" to give a name to their data type.A piddle consists of a series of numbers organized as an N-dimensional data set. Piddles provide efficient storage and fast computation of large N-dimensional matrices. They are highly optimized for numerical work. For more information, see "Piddles vs Perl Arrays" later in this document. COMMAND WINDOW AND IDEUnlike MATLAB, PDL does not come with a dedicated IDE. It does however come with an interactive shell and you can use a Perl IDE to develop PDL programs.PDL interactive shellTo start the interactive shell, open a terminal and run "perldl" or "pdl2". As in MATLAB, the interactive shell is the best way to learn the language. To exit the shell, type "exit", just like MATLAB.Writing PDL programsOne popular IDE for Perl is called Padre (<http://padre.perlide.org>). It is cross platform and easy to use.Whenever you write a stand-alone PDL program (i.e. outside the "perldl" or "pdl2" shell) you must start the program with "use PDL;". This command imports the PDL module into Perl. Here is a sample PDL program: use PDL; # Import main PDL module. use PDL::NiceSlice; # Import additional PDL module. use PDL::AutoLoader; # Import additional PDL module. $b = pdl [2,3,4]; # Statements end in semicolon. $A = pdl [ [1,2,3],[4,5,6] ]; # 2-dimensional matrix. print $A x $b->transpose; Save this file as "myprogram.pl" and run it with: perl myprogram.pl New: Flexible syntaxIn current versions of PDL (version 2.4.7 or later) there is a flexible matrix syntax that can look extremely similar to MATLAB:1) Use a ';' to delimit rows: $b = pdl q[ 2,3,4 ]; $A = pdl q[ 1,2,3 ; 4,5,6 ]; 2) Use spaces to separate elements: $b = pdl q[ 2 3 4 ]; $A = pdl q[ 1 2 3 ; 4 5 6 ]; Basically, as long as you put a "q" in front of the opening bracket, PDL should "do what you mean". So you can write in a syntax that is more comfortable for you. MODULES FOR MATLAB USERSThere are two modules that MATLAB users will want to use:
BASIC FEATURESThis section explains how PDL's syntax differs from MATLAB. Most MATLAB users will want to start here.General "gotchas"
Creating Piddles
Matrix Operations
Functions that aggregate dataSome functions (like "sum", "max" and "min") aggregate data for an N-dimensional data set. This is a place where MATLAB and PDL take a different approach:
Higher dimensional data setsA related issue is how MATLAB and PDL understand data sets of higher dimension. MATLAB was designed for 1D vectors and 2D matrices. Higher dimensional objects ("N-D arrays") were added on top. In contrast, PDL was designed for N-dimensional piddles from the start. This leads to a few surprises in MATLAB that don't occur in PDL:
Loop StructuresPerl has many loop structures, but we will only show the one that is most familiar to MATLAB users:MATLAB PerlDL ------ ------ for i = 1:10 for $i (1..10) { disp(i) print $i endfor }
Piddles vs Perl ArraysIt is important to note the difference between a Piddle and a Perl array. Perl has a general-purpose array object that can hold any type of element:@perl_array = 1..10; @perl_array = ( 12, "Hello" ); @perl_array = ( 1, 2, 3, \@another_perl_array, sequence(5) ); Perl arrays allow you to create powerful data structures (see Data structures below), but they are not designed for numerical work. For that, use piddles: $pdl = pdl [ 1, 2, 3, 4 ]; $pdl = sequence 10_000_000; $pdl = ones 600, 600; For example: $points = pdl 1..10_000_000 # 4.7 seconds $points = sequence 10_000_000 # milliseconds TIP: You can use underscores in numbers ("10_000_000" reads better than 10000000). ConditionalsPerl has many conditionals, but we will only show the one that is most familiar to MATLAB users:MATLAB PerlDL ------ ------ if value > MAX if ($value > $MAX) { disp("Too large") print "Too large\n"; elseif value < MIN } elsif ($value < $MIN) { disp("Too small") print "Too small\n"; else } else { disp("Perfect!") print "Perfect!\n"; end }
TIMTOWDI (There Is More Than One Way To Do It)One of the most interesting differences between PDL and other tools is the expressiveness of the Perl language. TIMTOWDI, or "There Is More Than One Way To Do It", is Perl's motto.Perl was written by a linguist, and one of its defining properties is that statements can be formulated in different ways to give the language a more natural feel. For example, you are unlikely to say to a friend: "While I am not finished, I will keep working." Human language is more flexible than that. Instead, you are more likely to say: "I will keep working until I am finished." Owing to its linguistic roots, Perl is the only programming language with this sort of flexibility. For example, Perl has traditional while-loops and if-statements: while ( ! finished() ) { keep_working(); } if ( ! wife_angry() ) { kiss_wife(); } But it also offers the alternative until and unless statements: until ( finished() ) { keep_working(); } unless ( wife_angry() ) { kiss_wife(); } And Perl allows you to write loops and conditionals in "postfix" form: keep_working() until finished(); kiss_wife() unless wife_angry(); In this way, Perl often allows you to write more natural, easy to understand code than is possible in more restrictive programming languages. FunctionsPDL's syntax for declaring functions differs significantly from MATLAB's.MATLAB PerlDL ------ ------ function retval = foo(x,y) sub foo { retval = x.**2 + x.*y my ($x, $y) = @_; endfunction return $x**2 + $x*$y; } Don't be intimidated by all the new syntax. Here is a quick run through a function declaration in PDL: 1) "sub" stands for "subroutine". 2) "my" declares variables to be local to the function. 3) "@_" is a special Perl array that holds all the function parameters. This might seem like a strange way to do functions, but it allows you to make functions that take a variable number of parameters. For example, the following function takes any number of parameters and adds them together: sub mysum { my ($i, $total) = (0, 0); for $i (@_) { $total += $i; } return $total; } 4) You can assign values to several variables at once using the syntax: ($a, $b, $c) = (1, 2, 3); So, in the previous examples: # This declares two local variables and initializes them to 0. my ($i, $total) = (0, 0); # This takes the first two elements of @_ and puts them in $x and $y. my ($x, $y) = @_; 5) The "return" statement gives the return value of the function, if any. ADDITIONAL FEATURESASCII File IOTo read data files containing whitespace separated columns of numbers (as would be read using the MATLAB load command) one uses the PDL rcols in PDL::IO::Misc. For a general review of the IO functionality available in PDL, see the documentation for PDL::IO, e.g., "help PDL::IO" in the pdl2 shell or " pdldoc PDL::IO " from the shell command line.Data structuresTo create complex data structures, MATLAB uses "cell arrays" and "structure arrays". Perl's arrays and hashes offer similar functionality but are more powerful and flexible. This section is only a quick overview of what Perl has to offer. To learn more about this, please go to <http://perldoc.perl.org/perldata.html> or run the command "perldoc perldata".
PerformancePDL has powerful performance features, some of which are not normally available in numerical computation tools. The following pages will guide you through these features:
PlottingPDL has full-featured plotting abilities. Unlike MATLAB, PDL relies more on third-party libraries (pgplot and PLplot) for its 2D plotting features. Its 3D plotting and graphics uses OpenGL for performance and portability. PDL has three main plotting modules:
Writing GUIsThrough Perl, PDL has access to all the major toolkits for creating a cross platform graphical user interface. One popular option is wxPerl (<http://wxperl.sourceforge.net>). These are the Perl bindings for wxWidgets, a powerful GUI toolkit for writing cross-platform applications.wxWidgets is designed to make your application look and feel like a native application in every platform. For example, the Perl IDE Padre is written with wxPerl. SimulinkSimulink is a graphical dynamical system modeler and simulator. It can be purchased separately as an add-on to MATLAB. PDL and Perl do not have a direct equivalent to MATLAB's Simulink. If this feature is important to you, then take a look at Scilab:<http://www.scilab.org> Scilab is another numerical analysis software. Like PDL, it is free and open source. It doesn't have PDL's unique features, but it is very similar to MATLAB. Scilab comes with Xcos (previously Scicos), a graphical system modeler and simulator similar to Simulink. COPYRIGHTCopyright 2010 Daniel Carrera (dcarrera@gmail.com). You can distribute and/or modify this document under the same terms as the current Perl license.See: http://dev.perl.org/licenses/
Visit the GSP FreeBSD Man Page Interface. |