# svd - Singular Value Decomposition - ERROR

Hi, I've noticed a problem when trying to use the svd operation with the GMatrix class.

I'm wanting to take some functions that have been written in Matlab and move them into Java. One of the fuctions use a SVD operation which is why I would like to use Java3d. However here is my problem:

In Matlab if I have:

a =

1 2 3

4 5 6

7 8 9

Than [u v w] = svd(a) gives:

u =

-0.2148 0.8872 0.4082

-0.5206 0.2496 -0.8165

-0.8263 -0.3879 0.4082

w =

16.8481 0 0

0 1.0684 0

0 0 0.0000

v =

-0.4797 -0.7767 -0.4082

-0.5724 -0.0757 0.8165

-0.6651 0.6253 -0.4082

But when I try to use GMatrix to do this operation I get totally incorrect values...

--------------

double [][] a = {

{ 1, 2, 3},

{ 4, 5, 6},

{ 7, 8, 9}

};

GMatrix gm = new GMatrix(a.length, a[0].length);

for (int i = 0; i < a.length; i++)

gm.setRow(i, a[i]);

GMatrix u,w,v; u=w=v= new GMatrix(a.length,a[0].length);

gm.SVD(u,w,v);

---------------

This gives me the same value for all 3 matricies (u,w,v):

0.9505863757867169 -0.3104602102825332 7.021949197211334E-22

-0.3104602102825333 -0.9505863757867171 -1.7007545127476131E-18

3.240882705018261E-17 -7.123264695860438E-17 1.0

Does anyone know what I am doing wrong...or is this a bug in the svd operation?

Thanks in advance,

Adam

Thanks, I ended up using the JAMA package and it works perfectly.

I'm not familiar with the GMatrix class, but at a glance, the line

GMatrix u,w,v; u=w=v= new GMatrix(a.length,a[0].length);

looks suspicious to me.

try replacing it with these lines and see what happens:

GMatrix u = new GMatrix(a.length,a[0].length);

GMatrix w = new GMatrix(a.length,a[0].length);

GMatrix v = new GMatrix(a.length,a[0].length);

Best,

Matt

At 05:01 PM 10/19/2004, java3d-interest@javadesktop.org wrote:

>Hi, I've noticed a problem when trying to use the svd operation with the

>GMatrix class.

>

>I'm wanting to take some functions that have been written in Matlab and

>move them into Java. One of the fuctions use a SVD operation which is why

>I would like to use Java3d. However here is my problem:

>In Matlab if I have:

>a =

> 1 2 3

> 4 5 6

> 7 8 9

>Than [u v w] = svd(a) gives:

>u =

> -0.2148 0.8872 0.4082

> -0.5206 0.2496 -0.8165

> -0.8263 -0.3879 0.4082

>w =

> 16.8481 0 0

> 0 1.0684 0

> 0 0 0.0000

>v =

> -0.4797 -0.7767 -0.4082

> -0.5724 -0.0757 0.8165

> -0.6651 0.6253 -0.4082

>

>But when I try to use GMatrix to do this operation I get totally incorrect

>values...

>--------------

> double [][] a = {

> { 1, 2, 3},

> { 4, 5, 6},

> { 7, 8, 9}

> };

>

> GMatrix gm = new GMatrix(a.length, a[0].length);

> for (int i = 0; i < a.length; i++)

> gm.setRow(i, a[i]);

> GMatrix u,w,v; u=w=v= new GMatrix(a.length,a[0].length);

> gm.SVD(u,w,v);

>---------------

>This gives me the same value for all 3 matricies (u,w,v):

>0.9505863757867169 -0.3104602102825332 7.021949197211334E-22

>-0.3104602102825333 -0.9505863757867171 -1.7007545127476131E-18

>3.240882705018261E-17 -7.123264695860438E-17 1.0

>

>

>Does anyone know what I am doing wrong...or is this a bug in the svd

>operation?

>

>Thanks in advance,

>

>Adam

>---

>[Message sent by forum member 'aneal' (Adam Neal)]

>

>http://www.javadesktop.org/forums/thread.jspa?messageID=34125蕍

>

>---------------------------------------------------------------------

>To unsubscribe, e-mail: interest-unsubscribe@java3d.dev.java.net

>For additional commands, e-mail: interest-help@java3d.dev.java.net

---------------------------------------------------------------------

To unsubscribe, e-mail: interest-unsubscribe@java3d.dev.java.net

For additional commands, e-mail: interest-help@java3d.dev.java.net

Ooops...

please note that where I used:

gm.setRow(i, a);

was actually gm.setRow(i, a[ i ]);

but the [ ] around the 'i' made an italics tag which carried through to the end....

If you are serious about the n-Dimensional matrix operations (n > 4)

I would recommend JAMA developed by NIST and MathWorks.

http://math.nist.gov/javanumerics/jama/

-- Scott