📜  Perl中的矩阵乘法

📅  最后修改于: 2022-05-13 01:55:43.184000             🧑  作者: Mango

Perl中的矩阵乘法

矩阵是以行和列的形式存储数据的多维数组。 Perl 允许对这些矩阵执行各种操作,例如加法、减法、除法和乘法。 Perl 在语法上与 C 非常相似,对于具有 C、C++ 知识的用户来说很容易。

给定两个矩阵,任务是将它们相乘。矩阵可以是正方形或矩形。

例子:

Input : mat1[][] = {{2, 4}, 
                    {3, 4}}
        mat2[][] = {{1, 2}, 
                    {1, 3}}       
Output : {{6, 16}, 
          {7, 18}}

首先,用户必须在的帮助下提供两个矩阵的维度。

Perl
print "Enter the Dimensions of matrices: ";
my $a = , my $b = ,
my $c = , my $d = ;


Perl
if($b != $c)
{
    print "Matrix multiplication can't happen\n";
}


Perl
#!/usr/bin/perl
 
else
{
    my @matrix1, my @matrix2, my @matrix3;
     
    # Filling up matrix 1 with user input
    print "Enter values for Matrix 1: ";
    for(my $i = 0; $i < $a; $i++)
    {
        for(my $j = 0; $j < $b; $j++)
        {
            # Taking user input
            $matrix1[$i][$j] = ;
        }
    }
     
    # Filling up matrix 2 with user input
    print "Enter values for Matrix 2: ";
    for(my $i = 0; $i < $c; $i++)
    {
        for(my $j = 0; $j < $d; $j++)
        {
            # Taking user input
            $matrix2[$i][$j] = ;
        }
    }
}


Perl
#!/usr/bin/perl
 
# Nested loops to multiply matrices
 
# Initializing temporary variable
my $temp = 0;
 
for(my $i = 0; $i < $a; $i++)
{
    for(my $j = 0; $j < $d; $j++)
    {
        for(my $g = 0; $g < $b; $g++)
        {
            # Multiplying the matrix values and
            # adding them to temp
            $temp += $matrix1[$i][$g] *
                     $matrix2[$g][$j];
        }
         
        $matrix3[$i][$j] = $temp;
        chomp $matrix3[$i][$j];
         
        # Printing resultant matrix
        print $matrix3[$i][$j];
        print "\t";
        $temp = 0;
    }
    print "\n";
}
}


Perl
# Perl program for matrix multiplication
use strict;
use warnings;
 
# Taking Matrix dimensions from the user
print "Enter the Dimensions of matrices: ";
my $a = , my $b = ,
my $c = , my $d = ;
 
# Checking if the matrices
# are multiplicative
if($b != $c)
{
    print "Matrix multiplication can't happen\n";
}
 
# Getting values for the matrix
else
{
    my @matrix1, my @matrix2, my @matrix3;
     
    # Taking values for the First matrix
    print "Enter the values for Matrix 1: ";
    for(my $i = 0; $i < $a; $i++)
    {
        for(my $j = 0; $j < $b; $j++)
        {
            # Taking user input
            $matrix1[$i][$j] = ;
        }
    }
 
    # Taking values for the Second matrix
    print "Enter the values for Matrix 2: ";
    for(my $i = 0; $i < $c; $i++)
    {
        for(my $j = 0; $j < $d; $j++)
        {
            # Taking user input
            $matrix2[$i][$j] = ;
        }
    }
     
    # Initializing temporary variable
    my $temp = 0;
     
    print "\nResultant matrix:\n";
     
    # Nested-loops for multiplication process
    for(my $i = 0; $i < $a; $i++)
    {
        for(my $j = 0; $j < $d; $j++)
        {
            for(my $g = 0; $g < $b; $g++)
            {
                 
                # Multiplying the matrix values and
                # adding them to temp
                $temp += $matrix1[$i][$g] *
                         $matrix2[$g][$j];
            }
             
            # Assigning the value of temp
            # into the matrix 3
            $matrix3[$i][$j] = $temp;
            chomp $matrix3[$i][$j];
             
            # printing matrix values
            print $matrix3[$i][$j];
            print "\t";
             
            # Setting temporary variable
            # back to zero
            $temp = 0;
        }
        print "\n";
    }
}


为了将两个矩阵相乘,第一个矩阵中的列数必须等于第二个矩阵中的行数。要检查矩阵是否为乘法if-else条件循环,请在代码中使用。

Perl

if($b != $c)
{
    print "Matrix multiplication can't happen\n";
}

现在,如果不满足上述条件,那么我们知道我们必须在else条件中实现矩阵乘法,为此,我们将首先在嵌套 for循环的帮助下获取两个矩阵内的值。第一个 for 循环将用于更改行,第二个将用于访问该特定行中的所有列。

Perl

#!/usr/bin/perl
 
else
{
    my @matrix1, my @matrix2, my @matrix3;
     
    # Filling up matrix 1 with user input
    print "Enter values for Matrix 1: ";
    for(my $i = 0; $i < $a; $i++)
    {
        for(my $j = 0; $j < $b; $j++)
        {
            # Taking user input
            $matrix1[$i][$j] = ;
        }
    }
     
    # Filling up matrix 2 with user input
    print "Enter values for Matrix 2: ";
    for(my $i = 0; $i < $c; $i++)
    {
        for(my $j = 0; $j < $d; $j++)
        {
            # Taking user input
            $matrix2[$i][$j] = ;
        }
    }
}

之后,进行乘法过程,其中第一个矩阵的第 i列与第二个矩阵的第 j行相乘,然后将各自的乘积相加,以找出乘积矩阵的[i][j]位置处的值。为了执行此操作,代码中使用了三个嵌套循环,如下所示:

Perl

#!/usr/bin/perl
 
# Nested loops to multiply matrices
 
# Initializing temporary variable
my $temp = 0;
 
for(my $i = 0; $i < $a; $i++)
{
    for(my $j = 0; $j < $d; $j++)
    {
        for(my $g = 0; $g < $b; $g++)
        {
            # Multiplying the matrix values and
            # adding them to temp
            $temp += $matrix1[$i][$g] *
                     $matrix2[$g][$j];
        }
         
        $matrix3[$i][$j] = $temp;
        chomp $matrix3[$i][$j];
         
        # Printing resultant matrix
        print $matrix3[$i][$j];
        print "\t";
        $temp = 0;
    }
    print "\n";
}
}

在上面编写的代码中,临时变量(temp)初始化为0。在第一个矩阵的第i列和第二个矩阵的第j行相乘之后,乘积被添加到temp中,这样我们就可以通过一次将两个数字相乘而找到的所有产品相加。这里变量 $g 的 for 循环帮助我们访问之前定义的两个矩阵的所有位置。要理解逻辑,请考虑对于矩阵 1,我们使用i固定列,然后使用 ' g ' 移动到各个行,在矩阵 2 中,我们使用j固定行,然后使用g更改列。我们知道$b = $c$g是从0 到 $b还是从0 到 $c都没有关系。最后,我们将 temp 中的值放到其各自的位置,然后将 temp 再次设置为 0,然后在其他位置找到值。这将导致形成所需的产品矩阵。

以下是执行矩阵乘法过程的最终代码:

Perl

# Perl program for matrix multiplication
use strict;
use warnings;
 
# Taking Matrix dimensions from the user
print "Enter the Dimensions of matrices: ";
my $a = , my $b = ,
my $c = , my $d = ;
 
# Checking if the matrices
# are multiplicative
if($b != $c)
{
    print "Matrix multiplication can't happen\n";
}
 
# Getting values for the matrix
else
{
    my @matrix1, my @matrix2, my @matrix3;
     
    # Taking values for the First matrix
    print "Enter the values for Matrix 1: ";
    for(my $i = 0; $i < $a; $i++)
    {
        for(my $j = 0; $j < $b; $j++)
        {
            # Taking user input
            $matrix1[$i][$j] = ;
        }
    }
 
    # Taking values for the Second matrix
    print "Enter the values for Matrix 2: ";
    for(my $i = 0; $i < $c; $i++)
    {
        for(my $j = 0; $j < $d; $j++)
        {
            # Taking user input
            $matrix2[$i][$j] = ;
        }
    }
     
    # Initializing temporary variable
    my $temp = 0;
     
    print "\nResultant matrix:\n";
     
    # Nested-loops for multiplication process
    for(my $i = 0; $i < $a; $i++)
    {
        for(my $j = 0; $j < $d; $j++)
        {
            for(my $g = 0; $g < $b; $g++)
            {
                 
                # Multiplying the matrix values and
                # adding them to temp
                $temp += $matrix1[$i][$g] *
                         $matrix2[$g][$j];
            }
             
            # Assigning the value of temp
            # into the matrix 3
            $matrix3[$i][$j] = $temp;
            chomp $matrix3[$i][$j];
             
            # printing matrix values
            print $matrix3[$i][$j];
            print "\t";
             
            # Setting temporary variable
            # back to zero
            $temp = 0;
        }
        print "\n";
    }
}

输出:

矩阵乘法