Addition, Subtraction, Multiplication, and Transpose of Matrices using Perl

Standard Deviation Output

This is a Perl programming tutorial for computing addition, subtraction, multiplication, and the transpose of matrices with custom dimension (row × column). In this tutorial, I have used addition(), subtraction(), multiplication(), and transpose() subroutines to perform matrix manipulation by user's choice. Moreover, the input and output of data processing is done through getmatrix() and display() subroutines. The output of the matrix result is formatted in rows and columns mimic to an array.

Matrix Manipulation

In mathematics, a matrix is a rectangular array of numbers/symbols/expressions arranged in rows and columns. The individual items (elements or entries) in an m × n matrix A, often denoted by ai,j, where i and j represents the dimension. The rule for addition (A + B) or subtraction (A − B) of two matrices is that it must have an equal number of rows and columns. However, the rule for matrix multiplication (A × B) is that two matrices can be multiplied only when the number of columns in the first matrix A equals the number of rows in the second matrix B.

Matrices (A ,  B, and AT)
\[\mathbf{A}=\left[ \begin{matrix} {{a}_{11}} & {{a}_{12}} & \cdots & {{a}_{1n}} \\ {{a}_{21}} & {{a}_{22}} & \cdots & {{a}_{2n}} \\ \vdots & \vdots & \ddots & \vdots \\ {{a}_{m1}} & {{a}_{m2}} & \cdots & {{a}_{mn}} \\ \end{matrix} \right]\] \[\mathbf{B}=\left[ \begin{matrix} {{b}_{11}} & {{b}_{12}} & \cdots & {{b}_{1n}} \\ {{b}_{21}} & {{b}_{22}} & \cdots & {{b}_{2n}} \\ \vdots & \vdots & \ddots & \vdots \\ {{b}_{m1}} & {{b}_{m2}} & \cdots & {{b}_{mn}} \\ \end{matrix} \right]\] \[{{\mathbf{A}}^{T}}=\left[ \begin{matrix} {{a}_{11}} & {{a}_{21}} & \cdots & {{a}_{m1}} \\ {{a}_{12}} & {{a}_{22}} & \cdots & {{a}_{m2}} \\ \vdots & \vdots & \ddots & \vdots \\ {{a}_{1n}} & {{a}_{2n}} & \cdots & {{a}_{mn}} \\ \end{matrix} \right]\]
Addition (A + B)
\[\mathbf{A}+\mathbf{B}=\left[ \begin{matrix} {{a}_{11}}+{{b}_{11}} & {{a}_{12}}+{{b}_{12}} & \cdots & {{a}_{1n}}+{{b}_{1n}} \\ {{a}_{21}}+{{b}_{21}} & {{a}_{22}}+{{b}_{22}} & \cdots & {{a}_{2n}}+{{b}_{2n}} \\ \vdots & \vdots & \ddots & \vdots \\ {{a}_{m1}}+{{b}_{m1}} & {{a}_{m2}}+{{b}_{m2}} & \cdots & {{a}_{mn}}+{{b}_{mn}} \\ \end{matrix} \right]\]
Subtraction (A − B)
\[\mathbf{A}-\mathbf{B}=\left[ \begin{matrix} {{a}_{11}}-{{b}_{11}} & {{a}_{12}}-{{b}_{12}} & \cdots & {{a}_{1n}}-{{b}_{1n}} \\ {{a}_{21}}-{{b}_{21}} & {{a}_{22}}-{{b}_{22}} & \cdots & {{a}_{2n}}-{{b}_{2n}} \\ \vdots & \vdots & \ddots & \vdots \\ {{a}_{m1}}-{{b}_{m1}} & {{a}_{m2}}-{{b}_{m2}} & \cdots & {{a}_{mn}}-{{b}_{mn}} \\ \end{matrix} \right]\]
Multiplication (A × B)
\[\left[ \begin{matrix} {{a}_{11}}\times {{b}_{11}}+{{a}_{12}}\times {{b}_{21}}+...+{{a}_{1n}}\times {{b}_{m1}} & \cdots & {{a}_{11}}\times {{b}_{1n}}+{{a}_{12}}\times {{b}_{2n}}+...+{{a}_{1n}}\times {{b}_{mn}} \\ {{a}_{21}}\times {{b}_{11}}+{{a}_{22}}\times {{b}_{21}}+...+{{a}_{2n}}\times {{b}_{m1}} & \cdots & {{a}_{21}}\times {{b}_{1n}}+{{a}_{22}}\times {{b}_{2n}}+...+{{a}_{2n}}\times {{b}_{mn}} \\ \vdots & \ddots & \vdots \\ {{a}_{m1}}\times {{b}_{11}}+{{a}_{m2}}\times {{b}_{21}}+...+{{a}_{mn}}\times {{b}_{m1}} & \cdots & {{a}_{m1}}\times {{b}_{1n}}+{{a}_{m2}}\times {{b}_{2n}}+...+{{a}_{mn}}\times {{b}_{mn}} \\ \end{matrix} \right]\]

Source Code

#!C:\Perl64\bin\perl.exe -w
system("cls");
print "\n +-----------------------------------+";
print "\n |   Matrix Manipulation Functions   |";
print "\n +-----------------------------------+";
print "\n 1. Matrix Addition";
print "\n 2. Matrix Subtraction";
print "\n 3. Matrix Multiplication";
print "\n 4. Transpose of Matrix";
print "\n\n Enter Your Choice: ";
$c = <>;
chomp($c);

if ($c == 1) { addition(); }
elsif ($c == 2) { subtraction(); }
elsif ($c == 3) { multiplication(); }
elsif ($c == 4) { transpose(); }
else { print "\n Invalid Choice!"; }

sub getmatrix {
  my $xl = $_[0];
  print "\n Enter Size of Row and Column in " . $xl . ":\n\n";
  print " Size of Row = ";
  $m = <>;
  chomp($m);
  print " Size of Column = ";
  $n = <>;
  chomp($n);
  my @a;
  print "\n Enter Elements of Matrix " . $xl . ":\n\n";
  for ($i = 0; $i < $m; $i++) {
    for ($j = 0; $j < $n; $j++) {
      $p = $i+1;
      $q = $j+1;
      print " $xl\[$p,$q\] = ";
      $a[$i][$j] = <>;
      chomp($a[$i][$j]);
    }
  }
  return ($m, $n, @a);
}

sub addition {
  my ($ma1, $na1, @xa) = getmatrix("A");
  my ($ma2, $na2, @ya) = getmatrix("B");
  if (($ma1 == $ma2) && ($na1 == $na2)) {
    for ($i = 0; $i < $ma1; $i++) {
      for ($j = 0; $j <$na1; $j++) {
        $za[$i][$j] = $xa[$i][$j] + $ya[$i][$j];
      }
    }
    system("cls");
    print "\n Matrix Addition (A+B):\n\n";
    print "\n\tMatrix A:\n\n";
    display(\@xa, $ma1, $na1);
    print "\n\tMatrix B:\n\n";
    display(\@ya, $ma2, $na2);
    print "\n\tMatrix (A+B):\n\n";
    display(\@za, $ma1, $na2);
  } else {
    print "\n Warning: Addition is not possible.";
    print "\n You have entered improper rows and columns.\n";
  }
}

sub subtraction {
  my ($ms1, $ns1, @xs) = getmatrix("A");
  my ($ms2, $ns2, @ys) = getmatrix("B");
  if (($ms1 == $ms2) && ($ns1 == $ns2)) {
    for ($i = 0; $i < $ms1; $i++) {
      for ($j = 0; $j <$ns1; $j++) {
        $zs[$i][$j] = $xs[$i][$j] - $ys[$i][$j];
      }
    }
    system("cls");
    print "\n Matrix Subtraction (A-B):\n\n";
    print "\n\tMatrix A:\n\n";
    display(\@xs, $ms1, $ns1);
    print "\n\tMatrix B:\n\n";
    display(\@ys, $ms2, $ns2);
    print "\n\tMatrix (A+B):\n\n";
    display(\@zs, $ms1, $ns2);
  } else {
    print "\n Warning: Subtraction is not possible.";
    print "\n You have entered improper rows and columns.\n";
  }
}

sub multiplication {
  my ($mm1, $nm1, @xm) = getmatrix("A");
  my ($mm2, $nm2, @ym) = getmatrix("B");
  if ($nm1 == $mm2) {
    for ($i = 0; $i < $mm1; $i++) {
      for ($j = 0; $j < $nm2; $j++) {
        $zm[$i][$j] = 0;
        for ($k = 0; $k < $nm1; $k++) {
          $zm[$i][$j] += ($xm[$i][$k] * $ym[$k][$j]);
        }
      }
    }
    system("cls");
    print "\n Matrix Multiplication (AxB):\n\n";
    print "\n\tMatrix A:\n\n";
    display(\@xm, $mm1, $nm1);
    print "\n\tMatrix B:\n\n";
    display(\@ym, $mm2, $nm2);
    print "\n\tMatrix (AxB):\n\n";
    display(\@zm, $mm1, $nm2);
  } else {
    print "\n Warning: Multiplication is not possible.";
    print "\n You have entered improper rows and columns.\n";
  }
}

sub transpose {
  my ($mt1, $nt1, @xt) = getmatrix("A");
  for ($i = 0; $i < $nt1; $i++) {
    for ($j = 0; $j <$mt1; $j++) {
      $zt[$i][$j] = $xt[$j][$i];
    }
  }
  system("cls");
  print "\n Matrix transpose (A^T):\n\n";
  print "\n\tMatrix A:\n\n";
  display(\@xt, $mt1, $nt1);
  print "\n\tMatrix (A^T):\n\n";
  display(\@zt, $nt1, $mt1);
}

sub display {
  my @x = @{$_[0]};
  my $m = $_[1];
  my $n = $_[2];
  print "\t\t";
  for ($i = 0; $i < $m; $i++) {
    for ($j = 0; $j < $n; $j++) {
      printf("%8s", $x[$i][$j]);
    }
    print "\n\n\n\t\t";
  }
}

<>;

Output

Standard Deviation Input

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions