1. Perl
  2. Module
  3. PDL

Learn the basics of PDL

I would like to write an introduction to PDL so that people who are doing statistical analysis in R language can perform statistical analysis work using PDL in Perl. If PDL, Perl's statistical analysis library, becomes available, it will be possible to perform statistical processing of multi-column data as in Excel.

First, let's create the simplest one-dimensional data structure. Take a look at the following code. You can use the pdl function to create a one-dimensional data structure.

use strict;
use warnings;

use PDL;

# Create data
my $nums = pdl [2, 4, 7];

# Data output
print $nums;

When I run this example, I get the following output on the screen:

[2 4 7]

Creating PDL variable

You can first create a PDL variable using the pdl function. First, load the PDL to use the minimum functions of the PDL.

use PDL;

After loading the PDL, you can use the pdl function. Use the pdl function to create a PDL variable.

my $nums = pdl [2, 4, 7];

Data acquisition and modification

The data created by the pdl function is called a PDL variable. Let's get the elements of a one-dimensional PDL variable. Use the at method to get the data.

my $first = $nums->at(0);
my $second = $nums->at(1);

In the above example, we got the value of the element contained in the PDL variable, but we can also get it as a PDL variable.

There are several ways to get some data as PDL variable, but it's easier to write in nice slice notation. Read the PDL::NiceSlice module for writing nice slice notation. This is done at the beginning of the script.

use PDL::NiceSlice;

To get an element as a PDL variable, use the notation $variable name (index) .

# Get element as PDL variable
my $pdl_first = $nums (0);
my $pdl_second = $nums (1);

These are PDL variable, so if you want to get the element value itself, you can retrieve it with the at method.

my $first = $pdl_first->at(0);
my $second = $pdl_second->at(0);

If you want to change the value of an element, use nice slice notation as well. Use the . = operator. . = Is overridden for assignment, not string concatenation.

# Change the value of the element
$nums (0). = 5;

PDL also allows you to extract multiple parts of a PDL variable as a new PDL variable. Let's extract the second and third elements. You can extract a part of the PDL variable by using the notation $variable name (n: m) as follows.

my $pdl_parts = $nums (1: 2);

You can also assign values to all the elements of the extracted PDL variable.

$nums (1: 2). = 8;

The feature of PDL is that the same operation can be performed on the set of parts extract in this way.

Let's get the length of one-dimensional data. Use the dims method to get the length of the data.

my $length = $nums->dims;

Example

This is an example that can be executed.

use strict;
use warnings;

use PDL;
use PDL::NiceSlice;

# Create data
my $nums = pdl [2, 4, 7];

# Element output
print $nums->at(0) . "\n";
print $nums->at(1) . "\n";

# Retrieving PDL variable
my $pdl_first = $nums (0);
my $pdl_second = $nums (1);

# Substitution
$nums (0). = 5;

# Getting multi-element PDL variable
my $pdl_parts = $nums (1: 2);

# Assignment to multiple elements retrieved as PDL variable
$nums (1: 2). = 8;

print $nums . "\n";

Four arithmetic operations of constants

Use the + operator to add the same value to all values of a PDL variable .

use PDL;
my $pdl = pdl [1, 1, 2, 2, 2, 3];
my $pdl_add = $pdl + 3;

Do the same for subtraction , multiplication , division , quotient , and exponentiation . can do.

# Subtraction
my $pdl_sub = $pdl - 3;

# Multiplication
my $pdl_product = $pdl * 3;

# Division
my $pdl_div = $pdl/3;

# Quotient
my $pdl_quotient = $pdl % 3;

# Exponentiation
my $pdl_pow = $pdl + 3;

Multiplication and division correspond to constant multiples of the vector. Keep in mind that you are doing operations between PDL variable and ordinary values, not operations between PDL variable.

Extract unique values

Use the uniq method to retrieve the unique value .

my $pdl_uniq = $pdl->uniq;

Four arithmetic operations between PDL variable

To add PDL variable , use the + operator for PDL variable. Each element is added.

# Addition of PDL variable
my $pdl1 = pdl [1, 2, 3];
my $pdl2 = pdl [4, 5, 6];
my $pdl_add = $pdl1 + $pdl2;

Do the same for subtraction , multiplication , division , quotient , and exponentiation . can do. Each element is calculated.

# Subtraction
my $pdl_sub = $pdl1-$pdl2;

# Multiplication
my $pdl_product = $pdl1 * $pdl2;

# Division
my $pdl_div = $pdl1/$pdl2;

# Quotient
my $pdl_quotient = $pdl1 %$pdl2;

# Exponentiation
my $pdl_pow = $pdl1 + $pdl2;

Addition and subtraction corresponds to addition and subtraction of vectors.

Dot product

There is no special way to calculate the dot product. To calculate the inner product , multiply the PDL variable and then find the sum.

# Dot product
my $inner_product = ($pdl1 * $pdl2)->sum;

The result of the dot product is just a value, not a PDL variable.

Example

This is an example that can be executed.

use strict;
use warnings;

use PDL;

# Addition
my $pdl = pdl [1, 1, 2, 2, 2, 3];
my $pdl_add = $pdl + 3;

# Subtraction
my $pdl_sub = $pdl - 3;

# Multiplication
my $pdl_product = $pdl * 3;

# Division
my $pdl_div = $pdl/3;

# Quotient
my $pdl_quotient = $pdl % 3;

# Get unique value
my $pdl_uniq = $pdl->uniq;

# Calculation result
print "$pdl_add\n";
print "$pdl_sub\n";
print "$pdl_product\n";
print "$pdl_div\n";
print "$pdl_quotient\n";
print "$pdl_uniq\n";
use strict;
use warnings;

use PDL;

# Addition of PDL variable
my $pdl1 = pdl [1, 2, 3];
my $pdl2 = pdl [4, 5, 6];
my $pdl_add = $pdl1 + $pdl2;

# Subtraction
my $pdl_sub = $pdl1-$pdl2;

# multiplication
my $pdl_product = $pdl1 * $pdl2;

# Division
my $pdl_div = $pdl1/$pdl2;

# Quotient
my $pdl_quotient = $pdl1 %$pdl2;

# Exponentiation
my $pdl_pow = $pdl1 + $pdl2;

# Dot product
my $inner_product = ($pdl1 * $pdl2)->sum;

# Calculation result
print "$pdl_add\n";
print "$pdl_sub\n";
print "$pdl_product\n";
print "$pdl_div\n";
print "$pdl_quotient\n";
print "$inner_product\n";

Related Informatrion