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  
 
 [ 5 post ] 

 Relevant Pages 

1. Question about floating point math w/perl

2. Floating Point Issues with Math::BigFloat

3. Perl's brain-dead floating point math

4. Floating point math errors

5. Floating point question

6. simple floating point question

7. ANNOUNCE: Math::MatrixReal 1.1

8. ANNOUNCE: Math-MatrixReal 1.0

9. ANNOUNCE: Math::MatrixReal::Ext1

10. Math::BigInt/Float version question?

11. ANNOUNCE: Math::MatrixReal::Aug

12. ANNOUNCE: Math::MatrixReal::Ext1 0.04

 

 
Powered by phpBB® Forum Software