File: concat.t

package info (click to toggle)
libmath-matrixreal-perl 2.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,120 kB
  • sloc: perl: 2,837; makefile: 8
file content (71 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use Test::Simple tests =>7;
use File::Spec;
use lib File::Spec->catfile("..","lib");
use Math::MatrixReal;

my $eye = Math::MatrixReal->new_diag([ 1,1,1] );
my $full = Math::MatrixReal->new_from_string(<<MATRIX);
[ 3 4 1 ]
[ 4 3 5 ]
[ 1 2 3 ]
MATRIX

my $fulleye = Math::MatrixReal->new_from_string(<<MATRIX);
[ 3 4 1 1 0 0 ]
[ 4 3 5 0 1 0 ]
[ 1 2 3 0 0 1 ]
MATRIX

my $eyefull = Math::MatrixReal->new_from_string(<<MATRIX);
[ 1 0 0 3 4 1 ]
[ 0 1 0 4 3 5 ]
[ 0 0 1 1 2 3 ]
MATRIX


my $eps = 1/1000;
my $concat = $eye . $full;
my $concat2= $full. $eye;

ok( ref $concat eq "Math::MatrixReal" , 'Concatenation returns the correct object');

my ($rows,$cols) = $concat->dim(); 
ok( $rows == 3, 'Concatenation preserves number of rows');
ok( $cols == 6, 'Concatenation does the right thing for cols');

my $res = $eyefull - $concat;
my $res2= $fulleye - $concat2;

ok(abs($res) < $eps ,'Left Concatenation of matrices with the same number of rows works' );
ok(abs($res2) < $eps,'Right Concatenation of matrices with the same number of rows works' );

my $a = Math::MatrixReal->new_diag([1, 2]);
my $b = Math::MatrixReal->new_diag([1, 2, 3]);
my $c;

eval { $c = $a . $b };
if ($@){
	ok(1, 'Concatenation of matrices with same number of rows only');
} else {
	ok(0, 'Concatenation of matrices with same number of rows only');
}

$c = Math::MatrixReal->new_from_string(<<MATRIX);
[ 3 4 1 9 ]
[ 4 3 5 9 ]
[ 1 2 3 0 ]
MATRIX
my $d = Math::MatrixReal->new_from_string(<<MATRIX);
[ 77 ]
[ 69 ]
[ 42 ]
MATRIX
my $dc = Math::MatrixReal->new_from_string(<<MATRIX);
[ 77 3 4 1 9 ]
[ 69 4 3 5 9 ]
[ 42 1 2 3 0 ]
MATRIX
$eps = 1e-8;
ok( abs( $dc - ($d.$c) ) < $eps, 'Concatenation of matrices with different number of columns works');