Question concerning Math::MatrixReal / Floating Point
Author Message Question concerning Math::MatrixReal / Floating Point

The code

#!/bin/perl -w

use Math::MatrixReal;

\$P = Math::MatrixReal->new_from_string(<<'MATRIX');
[ 0.444 0.145 0.000 ]
[ 0.145 0.250 0.000 ]
[ 0.000 0.000 0.160 ]
MATRIX

\$A = Math::MatrixReal->new(3,3);
\$A = \$P -> inverse();

\$true = \$A -> is_symmetric();
print "\$true \n";
print "\$A";

produces the output

0
[  2.778549597110E+00 -1.611558766324E+00  0.000000000000E+00 ]
[ -1.611558766324E+00  4.934704084468E+00  0.000000000000E+00 ]
[  0.000000000000E+00  0.000000000000E+00  6.250000000000E+00 ]

where \$true should be 1.

What do I have to change to have this working correctly ??

Sat, 20 Nov 2004 11:51:51 GMT  Question concerning Math::MatrixReal / Floating Point

Quote:

> The code

> #!/bin/perl -w

> use Math::MatrixReal;

> \$P = Math::MatrixReal->new_from_string(<<'MATRIX');
> [ 0.444 0.145 0.000 ]
> [ 0.145 0.250 0.000 ]
> [ 0.000 0.000 0.160 ]
> MATRIX

> \$A = Math::MatrixReal->new(3,3);
> \$A = \$P -> inverse();

> \$true = \$A -> is_symmetric();
> print "\$true \n";
> print "\$A";

> produces the output

> 0
> [  2.778549597110E+00 -1.611558766324E+00  0.000000000000E+00 ]
> [ -1.611558766324E+00  4.934704084468E+00  0.000000000000E+00 ]
> [  0.000000000000E+00  0.000000000000E+00  6.250000000000E+00 ]

> where \$true should be 1.

> What do I have to change to have this working correctly ??

Make your matrix symmetric:  A = 0.5 * (A + A')

Peter

--
Where do bit streams end?  In bit rivers?

Sat, 20 Nov 2004 13:00:39 GMT  Question concerning Math::MatrixReal / Floating Point

Quote:
> The code

> #!/bin/perl -w

> use Math::MatrixReal;

> \$P = Math::MatrixReal->new_from_string(<<'MATRIX');
> [ 0.444 0.145 0.000 ]
> [ 0.145 0.250 0.000 ]
> [ 0.000 0.000 0.160 ]
> MATRIX

> \$A = Math::MatrixReal->new(3,3);
> \$A = \$P -> inverse();

> \$true = \$A -> is_symmetric();
> print "\$true \n";
> print "\$A";

> produces the output

> 0
> [  2.778549597110E+00 -1.611558766324E+00  0.000000000000E+00 ]
> [ -1.611558766324E+00  4.934704084468E+00  0.000000000000E+00 ]
> [  0.000000000000E+00  0.000000000000E+00  6.250000000000E+00 ]

> where \$true should be 1.

> What do I have to change to have this working correctly ??

Round the matrix elements to a reasonable precision.  While Perl
prints the real values of A[ i, k] and A[ k, i] alike, this is a
result rounded to 12 decimal places.  The actual values in these
elements may be slightly different, and the is_symmetric routine
notices this.  Try:

\$A = \$A->each( sub { sprintf "%f.12", shift } ); # round to 12 places

The resulting matrix is symmetric.

BTW, avoid upper-case variable names.  While it's only a convention,
it just isn't done in Perl programming.  On the other hand, variables
\$a (and \$b) unfortunately have a predefined meaning in Perl, so don't
use these either.  And, while I'm at it, you should use lexical variables
(declared with my()) if you don't actually need a package variable.
You're not running under strict, are you?

Anno

Sat, 20 Nov 2004 13:19:34 GMT  Question concerning Math::MatrixReal / Floating Point

Quote:

> > The code

> > #!/bin/perl -w

> > use Math::MatrixReal;

> > \$P = Math::MatrixReal->new_from_string(<<'MATRIX');
> > [ 0.444 0.145 0.000 ]
> > [ 0.145 0.250 0.000 ]
> > [ 0.000 0.000 0.160 ]
> > MATRIX

> > \$A = Math::MatrixReal->new(3,3);
> > \$A = \$P -> inverse();

> > \$true = \$A -> is_symmetric();
> > print "\$true \n";
> > print "\$A";

> > produces the output

> > 0
> > [  2.778549597110E+00 -1.611558766324E+00  0.000000000000E+00 ]
> > [ -1.611558766324E+00  4.934704084468E+00  0.000000000000E+00 ]
> > [  0.000000000000E+00  0.000000000000E+00  6.250000000000E+00 ]

> > where \$true should be 1.

> > What do I have to change to have this working correctly ??

> Round the matrix elements to a reasonable precision.  While Perl
> prints the real values of A[ i, k] and A[ k, i] alike, this is a
> result rounded to 12 decimal places.  The actual values in these
> elements may be slightly different, and the is_symmetric routine
> notices this.  Try:

>     \$A = \$A->each( sub { sprintf "%f.12", shift } ); # round to 12 places

I tried this, but I think there is a small error here "%.12f" instead of "%f.12"

Quote:

> The resulting matrix is symmetric.

> BTW, avoid upper-case variable names.  While it's only a convention,
> it just isn't done in Perl programming.  On the other hand, variables
> \$a (and \$b) unfortunately have a predefined meaning in Perl, so don't
> use these either.  And, while I'm at it, you should use lexical variables
> (declared with my()) if you don't actually need a package variable.
> You're not running under strict, are you?

This was only a "quick and dirty" test. Thanks for your helpful advices !!

- Show quoted text -

Quote:

> Anno

Sun, 21 Nov 2004 09:08:03 GMT  Question concerning Math::MatrixReal / Floating Point

[...]

Quote:
> >     \$A = \$A->each( sub { sprintf "%f.12", shift } ); # round to 12 places

> I tried this, but I think there is a small error here "%.12f" instead of "%f.12"

You are right, it should have been "%.12f".  Insufficient attention and
testing on my part.

Anno

Sun, 21 Nov 2004 09:50:01 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages