This project has moved and is read-only. For the latest updates, please go here.

Error on SingularValueDecomposition for A[m,n] with m<n

Nov 15, 2013 at 12:20 PM
Hi,

I'm using SVD to solve an AX=B linear system where:
1) rank(A)==rank(x) -> determined system 1 solution
2) rank(A)>rank(x) -> overdetermined system 1 best fit solution
3) rank(A)<rank(x) -> underdetermined system family of solutions

as a reference I'm finding the parameters (a,b,c,d) of a sphere in space, given M number of (X,Y,Z) points, where M>4,M==4,M<4:
'x² + y² + z² + ax + by + cz + d = 0
A-> [Xi,Yi,Zi,1] X->[a,b,c,d] B->[- (xi² + yi² + zi²)] con i=1,..M

I've got a problem when in case rank(A)<rank(x) -> for M=3 only 3(XYZ) points in :
ASVD = A.SingularValueDecomposition
see code below .
But according to SVD theory it should work for this case also (one of the singular value shall be==0). An SVD library in excel I'm using to cross chek the calculations works in all the three situations mentioned above :
Dim Asfera(,) As Double = _
             New Double(,) {{-418.746, 310.726, 313.969, 1}, _
                            {-418.746, 337.451, 229.786, 1}, _
                            {-305.253, 321.895, 304.895, 1}}

Dim Bsfera() As Double = _
             New Double() {-370475.3926, -342022.9957, _
                           -289756.7461}

Dim SVsfera() As Double = nothing
Dim Xsfera() As Double = NRvb.LLSS(Asfera, Bsfera, SVsfera)
......
......
......

Public Shared Function LLSS(ByVal Ain(,) As Double, _
                          ByVal Bin() As Double, _
                          ByRef SingularValues() As Double, _
                          Optional ByVal RoundZero As Double = 1.0E-20) As Double()
'Linear Least Square Solution A•X = B
Dim TmpMat(,) As Double
Dim A, U As RectangularMatrix
Dim D, D0inv, V As SquareMatrix
Dim B As ColumnVector
Dim ASVD As SingularValueDecomposition
'----------------------------------------------------

LLSS = Nothing
If Ain.Equals(Nothing) Then GoTo ExitLLSS
If Bin.Equals(Nothing) Then GoTo ExitLLSS

'B = A•X
A = New RectangularMatrix(Ain)
B = New ColumnVector(Bin)

ASVD = A.SingularValueDecomposition
.......................
.......................
.......................
ExitLLSS:
Return LLSS
End Function

while if I add a redundant row (a copy of one set of data) to the Asfera and Bsfera matrices it works ! and it returns the last singular value as==0
Dim Asfera(,) As Double = _
             New Double(,) {{-418.746, 310.726, 313.969, 1}, _
                            {-418.746, 337.451, 229.786, 1}, _
                            {-418.746, 337.451, 229.786, 1}, _
                            {-305.253, 321.895, 304.895, 1}}

Dim Bsfera() As Double = _
             New Double() {-370475.3926, -342022.9957, _
                           -342022.9957, -289756.7461}







Nov 25, 2013 at 7:56 PM
I have filed a bug (https://metanumerics.codeplex.com/workitem/7686) on this issue, which is definitely a defect in our code. Thanks for the repro!
Nov 26, 2013 at 10:45 AM
Edited Nov 26, 2013 at 10:46 AM
For your info the workaround I'm using (and it works) is that when the input matrix A[m,n] has m(rows)<n(columns) you can redimensioning the A matrix to A[n,n] by adding k=n-m rows that are identical to row[m], the resulting SVD so far results are always correct
Jan 4, 2014 at 9:19 AM
This bug has been fixed in the latest check-in. Thanks for the report!