fixed svd for wide matrices by operating on transposed copy

This commit is contained in:
dane
2014-01-13 17:57:45 +01:00
parent b0e0bdf3de
commit f9544b6951
2 changed files with 13 additions and 1 deletions

View File

@@ -36,6 +36,15 @@ func SVD(a *Dense, epsilon, small float64, wantu, wantv bool) SVDFactors {
// panic(ErrShape) // panic(ErrShape)
// } // }
trans := false
if m < n {
at := new(Dense)
at.TCopy(a)
a = at
m, n = a.Dims()
trans = true
}
sigma := make([]float64, min(m+1, n)) sigma := make([]float64, min(m+1, n))
nu := min(m, n) nu := min(m, n)
var u, v *Dense var u, v *Dense
@@ -425,6 +434,9 @@ func SVD(a *Dense, epsilon, small float64, wantu, wantv bool) SVDFactors {
} }
} }
if trans {
return SVDFactors{v, sigma, u}
}
return SVDFactors{u, sigma, v} return SVDFactors{u, sigma, v}
} }

View File

@@ -105,7 +105,7 @@ func (s *S) TestSVD(c *check.C) {
small: math.Pow(2, -966.0), small: math.Pow(2, -966.0),
// FIXME(kortschak) sigma is one element longer than it should be. // FIXME(kortschak) sigma is one element longer than it should be.
sigma: []float64{21.25950088109745, 1.5415021616856577, 1.2873979074613637, 0}, sigma: []float64{21.259500881097434, 1.5415021616856566, 1.2873979074613628},
}, },
} { } {
svd := SVD(DenseCopyOf(t.a), t.epsilon, t.small, t.wantu, t.wantv) svd := SVD(DenseCopyOf(t.a), t.epsilon, t.small, t.wantu, t.wantv)