go ahead... be a heretic PerlMonks

### Convert matrix formats

 on Oct 30, 2010 at 19:15 UTC Need Help??
naturalsciences has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for a script to convert lower-triangular matrix to square matrix. There are probably some modules for matrix manipulation? Or is it something that could be easily done without the use of modules? Suggestions - names for good modules - or maybe it is just a few lines of script. ( My own mind is quite clouded at a moment)

Replies are listed 'Best First'.
Re: Convert matrix formats
by BrowserUk (Pope) on Oct 30, 2010 at 21:45 UTC
Or is it something that could be easily done without the use of modules?

I'm deeply suspicious of questions that seem too easy, but ...

It kinda depends what form you have or obtain the lower-triangle matrix in to start.

But assuming you've loaded the LT matrix into a 2d array, then "converting to a square matrix" consists of padding the upper triangle with zeros, which is one line of perl:

```#! perl -slw
use strict;
use Data::Dump qw[ pp ];

sub fillLTM {
my @lt;
my \$n = 1;
push @lt, [ splice @_, 0, \$n++ ] while @_;
return @lt;
}

while( my \$rawData = <DATA> ) {
## Populate LT matrix
my @lt = fillLTM( split ' ', \$rawData );
pp \@lt;;

push @{ \$lt[\$_] }, (0)x( @{ \$lt[-1] } - @{ \$lt[\$_] } ) for 0 .. \$#
+lt -1;;
pp \@lt;;
}

__DATA__
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Produces:

```C:\test>junk
[
[1],
[2, 3],
[4, 5, 6],
[7, 8, 9, 10]
]
[
[1, 0, 0, 0],
[2, 3, 0, 0],
[4, 5, 6, 0],
[7, 8, 9, 10]
]

[
[1],
[2, 3],
[4, 5, 6],
[7, 8, 9, 10],
[11, 12, 13, 14, 15]
]

[
[1, 0, 0, 0, 0],
[2, 3, 0, 0, 0],
[4, 5, 6, 0, 0],
[7, 8, 9, 10, 0],
[11, 12, 13, 14, 15]
]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
No :D no padding with zeroes. I want to make the upper half a symmetrical one to the lower.
I want to make the upper half a symmetrical one to the lower.

Ah! Thought it was too easy. That'll take 2 lines :)

```#! perl -slw
use strict;
use Data::Dump qw[ pp ];

sub fillLTM {
my @lt;
my \$n = 1;
push @lt, [ splice @_, 0, \$n++ ] while @_;
return @lt;
}

while( my \$rawData = <DATA> ) {
## Populate LT matrix
my @lt = fillLTM( split ' ', \$rawData );
pp \@lt;;

for my \$y ( 0 .. \$#lt -1 ) {
push @{ \$lt[\$y] }, \$lt[\$_][\$y] for \$y+1 .. \$#{ \$lt[-1] };
}
pp \@lt;;
}

__DATA__
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Produces:

```C:\test>junk
[
[1],
[2, 3],
[4, 5, 6],
[7, 8, 9, 10]
]
[
[1, 2, 4,  7],
[2, 3, 5,  8],
[4, 5, 6,  9],
[7, 8, 9, 10]
]

[
[ 1],
[ 2,  3],
[ 4,  5,  6],
[ 7,  8,  9, 10],
[11, 12, 13, 14, 15]
]
[
[ 1,  2,  4,  7, 11],
[ 2,  3,  5,  8, 12],
[ 4,  5,  6,  9, 13],
[ 7,  8,  9, 10, 14],
[11, 12, 13, 14, 15]
]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Convert matrix formats
by lyklev (Pilgrim) on Oct 30, 2010 at 20:54 UTC
You might be looking for the PDL modules, in particular the 2D matrix manipulation modules.
Re: Convert matrix formats
by zentara (Archbishop) on Nov 02, 2010 at 17:13 UTC
As a mental exercise, I wanted to see if I could use PDL's matrix functions to do this. My idea was to transpose the matrix, add it to itself, then subtract the original diagonal. This would extend it's usefullness to huge arrays. I got as far as adding the transpose to itself, and getting the original diagonal, which would be then be zero padded, and subtracted from the result of the matrix addition.

This might be a way to go, if you want to use a module.

```#!/usr/bin/perl
use warnings;
use strict;
use PDL::LiteF;

my \$a = pdl([
[1,   ,     ,     ],
[4,  -5,    ,     ],
[7,  -8,   -9,    ],
[17, -18, -90,  114],
]);

print \$a,"\n";

my \$b = transpose(\$a);
print "\$b\n";

my \$c = \$a+ \$b;
print "\$c\n";

my \$d = \$a->diagonal(0,1);
print "\$d\nNeeds to be padded with zeros and subtracted from  \\$c\n";

print "I'll leave that to you. :-)\n";

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Create A New User
Node Status?
node history
Node Type: perlquestion [id://868501]
Front-paged by Arunbear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-05-20 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (150 votes). Check out past polls.

Notices?