Thread Subject: sparse matrix and precision???

Subject: sparse matrix and precision???

From: Duc

Date: 30 May, 2011 18:03:04

Message: 1 of 5

Please consider the following commands and outputs:
________________________________
K>> K(60884208)

ans =

   0.162500000000000

K>> KK(60884208)

ans =

   0.162500000000000

K>> KK(60884208) - K(60884208)

ans =

    -2.775557561562891e-17
___________________________________

K is a sparse matrix and KK is a full matrix. My question: How to make K uses the same numeric data as KK, since the min function on K and KK produces different results? :((

Subject: sparse matrix and precision???

From: John D'Errico

Date: 30 May, 2011 18:17:04

Message: 2 of 5

"Duc" wrote in message <is0m4o$7pc$1@newscl01ah.mathworks.com>...
> Please consider the following commands and outputs:
> ________________________________
> K>> K(60884208)
>
> ans =
>
> 0.162500000000000
>
> K>> KK(60884208)
>
> ans =
>
> 0.162500000000000
>
> K>> KK(60884208) - K(60884208)
>
> ans =
>
> -2.775557561562891e-17
> ___________________________________
>
> K is a sparse matrix and KK is a full matrix. My question: How to make K uses the same numeric data as KK, since the min function on K and KK produces different results? :((

You have not convinced us, (not even remotely convinced)
that those two numbers were ever identical from the two
sources.

>> format long g
>> K = 0.162500000000000;
>> Ksparse = sparse(K);
>> K - Ksparse
ans =
     0

Just because the two numbers are displayed as the same
to 15 digits, does not make them identical to the full 52
bits of precision afforded by a double.

Even if these two numbers were produced by the same
operations on a full versus sparse array, does not make
them assuredly identical.

Welcome to the world of floating point arithmetic.

John

Subject: sparse matrix and precision???

From: Duc

Date: 30 May, 2011 18:48:04

Message: 3 of 5

Hi John,

So sorry for my deficiencies here. My first approach is the full matrix operations, then I switch to sparse matrix. So, it is assumed that K and KK is the same numeric data, but they're the same only few first iterations.

inside a for loop:
__________________________________________________________________
k = B_s'*C_shear *B_s*gaussWeights(q)*det(Jacob);
K = K + sparse(colMap, rowMap,k,GDof,GDof);
KK(elementDof,elementDof)=KK(elementDof,elementDof)+k;
___________________________________________________________________

After the loop, KK - K is not a zero matrix :(

Subject: sparse matrix and precision???

From: John D'Errico

Date: 30 May, 2011 20:15:04

Message: 4 of 5

"Duc" wrote in message <is0op4$dua$1@newscl01ah.mathworks.com>...
> Hi John,
>
> So sorry for my deficiencies here. My first approach is the full matrix operations, then I switch to sparse matrix. So, it is assumed that K and KK is the same numeric data, but they're the same only few first iterations.
>
> inside a for loop:
> __________________________________________________________________
> k = B_s'*C_shear *B_s*gaussWeights(q)*det(Jacob);
> K = K + sparse(colMap, rowMap,k,GDof,GDof);
> KK(elementDof,elementDof)=KK(elementDof,elementDof)+k;
> ___________________________________________________________________
>
> After the loop, KK - K is not a zero matrix :(

The point is that when you add more than two numbers in
floating point arithmetic, the sequence that you add them
matters.

>> (.3 + .2 + .6) == (.6 + .3 + .2)
ans =
     0

There is NO presumption that the sequence of operations
is identical for a sparse versus floating point operation,
nor should you EVER trust the least significant bits of
such an operation.

As I said, welcome to the floating point world.

John

Subject: sparse matrix and precision???

From: Duc

Date: 30 May, 2011 20:42:04

Message: 5 of 5

Very happy to enter the world of floating point arithmetic :)

I've fixed the problem after two hours of debugging. It was my mistake (K\f instead of K(active,active)\f(active)) leads to the confusion :D

Thank you very much, John. Have a nice time!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
eps Duc 30 May, 2011 14:04:09
sparse Duc 30 May, 2011 14:04:09
rssFeed for this Thread

Contact us at files@mathworks.com