SVD Output Error


DimpleB
12-04-2012, 03:39 PM
Hi i tried to modified the svdcmp.c from float to double version and matrices indexes starting from 0 to 1 a[0][0]...a[m][n]....

I am using the code which is provided in the book version 2.8

I don't get the matrices right all the time...and there is some inconistency between rows and columns could you please help me with it....thank you

I have attached my C code and the SVD outputs from c and matlab...If someone has made the SVD to work could you please email me the whole of your code....thanks a lot

DimpleB
12-04-2012, 04:52 PM
hi i tried A = U*W*VT method....And I don't always get the correct A matrices (even rounding it off doesn't give correct results)....so I am sure there should be something wrong with the code...could you please review it or post your working code...thank you

davekw7x
12-05-2012, 11:26 AM
...could you please review...

I ran your two examples using NR2 svdcmp.cpp (unchanged from the distribution).

Here are the results:

Decomposition matrices:
Matrix u
-0.214837 -0.887231 0.408248
-0.520587 -0.249644 -0.816497
-0.826338 0.387943 0.408248
Diagonal of matrix w
16.848103 1.068370 0.000000
Matrix v-transpose
-0.479671 -0.572368 -0.665064
0.776691 0.075686 -0.625318
0.408248 -0.816497 0.408248

Check product against original matrix:
Original matrix:
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000
Product u*w*(v-transpose):
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000


And


Decomposition matrices:
Matrix u
-0.707107 0.707107
0.707107 0.707107
Diagonal of matrix w
0.000000 7.615773
Matrix v-transpose
-0.928477 0.371391
0.371391 0.928477

Check product against original matrix:
Original matrix:
2.000000 5.000000
2.000000 5.000000
Product u*w*(v-transpose):
2.000000 5.000000
2.000000 5.000000


Results look OK to me. As I mentioned in your other thread, the decomposition is not unique (rearranging rows and columns and changing signs can make U and V look different).

However...

Bottom lines:

The Singular Values are the same as the results from the other programs that you posted.


The product U times W times V-Transpose of the results from the NR code gives the original matrices for your examples.


If you have examples for which the NR code does not give satisfactory results, please post them.



...post your working code...

I used unmodified Numerical Recipes Version 2.11 distribution code. The code is copyrighted and posting it on a forum is not authorized.


Regards,

Dave

DimpleB
12-05-2012, 01:34 PM
hey thanks for replying..you were right I made a calculation error when calculating back the matrices...

However
It works correctly for all square matrices and all m > n....

yet when m <n it gives error shown in the figures

davekw7x
12-05-2012, 02:59 PM
...made a calculation error when calculating back the matrices...

Did it again, I'm thinking.

My results from NR2 svdcmp.cpp:

Decomposition matrices:
Matrix u
-0.578640 0.000000 -0.815583
-0.815583 0.000000 0.578640
Diagonal of matrix w
8.676660 0.000000 3.422801
Matrix v-transpose
-0.603365 -0.521441 -0.603365
0.707107 0.000000 -0.707107
0.368715 -0.853287 0.368715

Check product against original matrix:
Original matrix:
2.000000 5.000000 2.000000
5.000000 2.000000 5.000000
Product u*w*(v-transpose):
2.000000 5.000000 2.000000
5.000000 2.000000 5.000000


I mean, I just put the check multiplication in my program so that I can re-run it with whatever example I want and I visually verify the consistency.

For any particular case, you can go back to Matlab and enter the following with results from your NR program (or paste it into an m-file and execute).

Here's mine:

clear all
u = [
[-0.578640, 0.000000, -0.815583],
[-0.815583, 0.000000, 0.578640],
]

w = [
[ 8.676660, 0.000000, 0.000000],
[ 0.000000, 0.000000, 0.000000],
[ 0.000000, 0.000000, 3.422801]
]

vt = [
[-0.603365, -0.521441, -0.603365],
[ 0.707107, 0.000000, -0.707107],
[ 0.368715, -0.853287, 0.368715],
]

x = u*w*vt

printf('Taa-daa!\n')


Output from GNU Octave 3.2.3 on my Centos 5.8 Linux workstation:

u =

-0.57864 0.00000 -0.81558
-0.81558 0.00000 0.57864

w =

8.67666 0.00000 0.00000
0.00000 0.00000 0.00000
0.00000 0.00000 3.42280

vt =

-0.60337 -0.52144 -0.60337
0.70711 0.00000 -0.70711
0.36872 -0.85329 0.36872

x =

2.0000 5.0000 2.0000
5.0000 2.0000 5.0000

Taa-daa!



Now, maybe you can go back and see what you did wrong in your matrix multiplication. Once you get that right, maybe you are all set...



Regards,

Dave

DimpleB
12-06-2012, 10:27 AM
Hey thanks a lot...again a multiplication error...I am definitely going to review my math....any thanks alot I can finally check SVD off my list...thank you