1. Perl
  2. Module
  3. PDL

Handling of "matrix" in PDL

I will explain the handling of "matrix" in PDL.

Creating a matrix

Use the pdl function to create a matrix.

use PDL;

my $mat = mpdl [
  [1, 2],
  [3, 4]
];;

The following matrix is created.

[
 [1 2]
 [3 4]
]

Matrix addition

Use the + operator to add matrices.

use PDL;

my $mat1 = pdl [
  [1, 2],
  [3, 4]
];;
my $mat2 = pdl [
  [5, 6],
  [7, 8]
];;

my $mat_sum = $mat1 + $mat2;

Each element is added and the result is as follows.

[
 [6 8]
 [10 12]
]

Matrix subtraction

Use the - operator to add matrices.

use PDL;

my $mat1 = pdl [
  [1, 2],
  [3, 4]
];;

my $mat2 = pdl [
  [5, 6],
  [7, 8]
];;

my $mat_sub = $mat2- $mat1;

Each element is subtracted and the result is as follows.

[
 [4 4]
 [4 4]
]

Matrix product

Use the x operator to find the matrix product.

use PDL;

my $mat1 = pdl [
  [1, 2],
  [3, 4]
];;
my $mat2 = pdl [
  [5, 6],
  [7, 8]
];;

my $mat_multi = $mat1 x $mat2;

The calculated result is as follows.

[
 [19 22]
 [43 50]
]

Get matrix elements

Use the at method to get the elements of the matrix. Note that the row and column order is different from a regular matrix, and the subscripts start at 0.

$matrix->at(column, row)

The following is an example to get the value (3) of the 2nd row and 1st column of the 2x2 matrix.

use PDL;

my $mat = pdl [
  [1, 2],
  [3, 4]
];;
my $val = $mat->at(0, 1);

Set matrix elements

To set the elements of a matrix, use the notation nice slice . You need to load the PDL::NiceSlice module to use nice slice notation.

use PDL;
use PDL::NiceSlice;

$matrix (column, row). = Value

This notation is a bit special, with parentheses after the variable. Also, the . = string concatenation and equal combination is overloaded for value assignment.

Below is an example of a 2x2 matrix with the value in the 2nd row and 1st column set to 5. Note that the subscripts start at 0.

use PDL;
use PDL::NiceSlice;

my $mat = pdl [
  [1, 2],
  [3, 4]
];;
$mat (0, 1). = 5;

The output of this variable is as follows.

[
 [1 2]
 [5 4]
]

Create an identity matrix

Let's create an identity matrix. Let's make a 3x3 identity matrix. The procedure is to create a 3x3 square matrix with all zeros in the zeroes function of PDL::Core . Then slice the diagonal with the diagonal method and substitute 1. The argument (0, 1) means 1D and 2D. It means to get the diagonal of 1D and 2D.

use PDL;

my $mat = PDL::Core::zeroes (3, 3);
$mat->diagonal(0, 1). = 1;

The output result is as follows.

[
 [1 0 0]
 [0 1 0]
 [0 0 1]
]

Find the determinant

Use the det method to find the determinant.

use PDL;

my $mat = pdl [
  [1, 2],
  [3, 4]
];;

my $det = $mat->det;

The value of the determinant is as follows.

-2

Find the inverse matrix

Use the inv function to find the inverse matrix.

use PDL;

my $mat = pdl [1, 2], [3, 4];

my $mat_inv = inv $mat;

The calculated result is as follows.

[
 [  -twenty one]
 [1.5 -0.5]
]

Create a vector (column vector)

To create a vector (column vector):

use PDL;

my $vec = pdl [
  [1],
  [2]
];;

The output result of the data is as follows.

[
 [1]
 [2]
]

Calculate the linear transformation

Let's calculate the linear transformation. It is an operation of square matrix × column vector.

use PDL;

my $mat = pdl [
  [1, 2],
  [3, 4]
];;

my $vec = pdl [
  [Five],
  [6]
];;

my $result = $mat x $vec;

The output result is as follows.

[
 [17]
 [39]
]

t [5, 6] was converted to t [17, 39] by the 2 × 2 matrix [[1, 2], [3, 4]]. (t means transpose)

Related Informatrion