mirror of
https://github.com/gonum/gonum.git
synced 2025-10-14 19:26:30 +08:00
fixed svd for wide matrices by operating on transposed copy
This commit is contained in:
12
mat64/svd.go
12
mat64/svd.go
@@ -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}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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)
|
||||||
|
Reference in New Issue
Block a user