-1

Does anyone know how they would find the n-th power of the adjacency matrix?

Here is the matrix I am trying to write the code for

0 0 1 0    
0 0 1 0                                                                 
1 1 0 1                                                                      
0 0 1 0                                                                   

and the 2nd power adjacency matrix is:

 1 1 0 1                                                                    
 1 1 0 1                                                                    
 0 0 3 0                                                                    
 1 1 0 1                                                                    

I am not sure how I can calculate it with a perl code I only have a code for reading in my file

sub matrix_read_file {
    my ($filename) = @_;
    my @matrix;
    open (my $F, '<', $filename) or die "Could not open $filename: $!";
    while (my $line =<$F> ) {
        chomp $line;
        next if $line =~ /^\s*$/; # skip blank lines
        my @row = split /\s+/, $line;
        push @matrix, \@row;
    }
    close $F;
    print  "$matrix[2][1]\n";  #test to see if individual elements print
    print "$matrix[1][2]\n";
    return \@matrix;
}
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
Johnny
  • 17
  • 2
  • Including at least some code would be very useful. How are you representing your matrices, for example. – Shawn Nov 14 '21 at 01:34
  • @Shawn I don't have any code written, but I have the initial matrix typed in exactly as above as a text file that I read in. I am trying to get the 2nd power adjacency matrix as my output – Johnny Nov 14 '21 at 02:32
  • You might look into [Math::Matrix](https://metacpan.org/pod/Math::Matrix). – Shawn Nov 14 '21 at 02:43
  • [Here](https://pastebin.com/nD6KVrYf) is an example using [PDL](https://metacpan.org/pod/PDL) – Håkon Hægland Nov 14 '21 at 09:15

1 Answers1

1

Here is an example:

use v5.22.0;  # signatures requires perl >= 5.22
use feature qw(say);
use strict;
use warnings;
use experimental qw(signatures);
use Data::Dumper;
{
    my $m = [[0,0,1,0],[0,0,1,0],[1,1,0,1],[0,0,1,0]];
    my $m2 = matmul( $m, $m );
    print Dumper($m2);
}

# Assume $m1 has size [pxq] and $m2 has size [qxr]  :
#  p = number of rows in $m1
#  q = number of columns in $m1  ==  number of rows in $m2
#  r = number of columns in $m2
# result = [pxq] * [qxr] => [p x r]
sub matmul( $m1, $m2 ) {

    my $p = $#$m1;
    my $q = $#{$m1->[0]};
    my $r = $#{$m2->[0]};
    my @res;
    for my $i (0..$p) {
        for my $j (0..$r) {
            my $sum = 0;
            for my $k (0..$q) {
                $sum += $m1->[$i][$k] * $m2->[$k][$j];
            }
            $res[$i][$j] = $sum;
        }
    }
    return \@res;
}

Or alternatively, using the PDL module:

use feature qw(say);
use strict;
use warnings;
use PDL;
my $m = pdl [[0,0,1,0],[0,0,1,0],[1,1,0,1],[0,0,1,0]];
my $m2 = $m x $m;
print $m2;
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174