hogsvd.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright ©2017 The Gonum Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mat
  5. import (
  6. "errors"
  7. "gonum.org/v1/gonum/blas/blas64"
  8. )
  9. // HOGSVD is a type for creating and using the Higher Order Generalized Singular Value
  10. // Decomposition (HOGSVD) of a set of matrices.
  11. //
  12. // The factorization is a linear transformation of the data sets from the given
  13. // variable×sample spaces to reduced and diagonalized "eigenvariable"×"eigensample"
  14. // spaces.
  15. type HOGSVD struct {
  16. n int
  17. v *Dense
  18. b []Dense
  19. err error
  20. }
  21. // succFact returns whether the receiver contains a successful factorization.
  22. func (gsvd *HOGSVD) succFact() bool {
  23. return gsvd.n != 0
  24. }
  25. // Factorize computes the higher order generalized singular value decomposition (HOGSVD)
  26. // of the n input r_i×c column tall matrices in m. HOGSV extends the GSVD case from 2 to n
  27. // input matrices.
  28. //
  29. // M_0 = U_0 * Σ_0 * V^T
  30. // M_1 = U_1 * Σ_1 * V^T
  31. // .
  32. // .
  33. // .
  34. // M_{n-1} = U_{n-1} * Σ_{n-1} * V^T
  35. //
  36. // where U_i are r_i×c matrices of singular vectors, Σ are c×c matrices singular values, and V
  37. // is a c×c matrix of singular vectors.
  38. //
  39. // Factorize returns whether the decomposition succeeded. If the decomposition
  40. // failed, routines that require a successful factorization will panic.
  41. func (gsvd *HOGSVD) Factorize(m ...Matrix) (ok bool) {
  42. // Factorize performs the HOGSVD factorisation
  43. // essentially as described by Ponnapalli et al.
  44. // https://doi.org/10.1371/journal.pone.0028072
  45. if len(m) < 2 {
  46. panic("hogsvd: too few matrices")
  47. }
  48. gsvd.n = 0
  49. r, c := m[0].Dims()
  50. a := make([]Cholesky, len(m))
  51. var ts SymDense
  52. for i, d := range m {
  53. rd, cd := d.Dims()
  54. if rd < cd {
  55. gsvd.err = ErrShape
  56. return false
  57. }
  58. if rd > r {
  59. r = rd
  60. }
  61. if cd != c {
  62. panic(ErrShape)
  63. }
  64. ts.Reset()
  65. ts.SymOuterK(1, d.T())
  66. ok = a[i].Factorize(&ts)
  67. if !ok {
  68. gsvd.err = errors.New("hogsvd: cholesky decomposition failed")
  69. return false
  70. }
  71. }
  72. s := getWorkspace(c, c, true)
  73. defer putWorkspace(s)
  74. sij := getWorkspace(c, c, false)
  75. defer putWorkspace(sij)
  76. for i, ai := range a {
  77. for _, aj := range a[i+1:] {
  78. gsvd.err = ai.SolveCholTo(sij, &aj)
  79. if gsvd.err != nil {
  80. return false
  81. }
  82. s.Add(s, sij)
  83. gsvd.err = aj.SolveCholTo(sij, &ai)
  84. if gsvd.err != nil {
  85. return false
  86. }
  87. s.Add(s, sij)
  88. }
  89. }
  90. s.Scale(1/float64(len(m)*(len(m)-1)), s)
  91. var eig Eigen
  92. ok = eig.Factorize(s.T(), EigenRight)
  93. if !ok {
  94. gsvd.err = errors.New("hogsvd: eigen decomposition failed")
  95. return false
  96. }
  97. vc := eig.VectorsTo(nil)
  98. // vc is guaranteed to have real eigenvalues.
  99. rc, cc := vc.Dims()
  100. v := NewDense(rc, cc, nil)
  101. for i := 0; i < rc; i++ {
  102. for j := 0; j < cc; j++ {
  103. a := vc.At(i, j)
  104. v.set(i, j, real(a))
  105. }
  106. }
  107. // Rescale the columns of v by their Frobenius norms.
  108. // Work done in cv is reflected in v.
  109. var cv VecDense
  110. for j := 0; j < c; j++ {
  111. cv.ColViewOf(v, j)
  112. cv.ScaleVec(1/blas64.Nrm2(cv.mat), &cv)
  113. }
  114. b := make([]Dense, len(m))
  115. biT := getWorkspace(c, r, false)
  116. defer putWorkspace(biT)
  117. for i, d := range m {
  118. // All calls to reset will leave a zeroed
  119. // matrix with capacity to store the result
  120. // without additional allocation.
  121. biT.Reset()
  122. gsvd.err = biT.Solve(v, d.T())
  123. if gsvd.err != nil {
  124. return false
  125. }
  126. b[i].Clone(biT.T())
  127. }
  128. gsvd.n = len(m)
  129. gsvd.v = v
  130. gsvd.b = b
  131. return true
  132. }
  133. // Err returns the reason for a factorization failure.
  134. func (gsvd *HOGSVD) Err() error {
  135. return gsvd.err
  136. }
  137. // Len returns the number of matrices that have been factorized. If Len returns
  138. // zero, the factorization was not successful.
  139. func (gsvd *HOGSVD) Len() int {
  140. return gsvd.n
  141. }
  142. // UTo extracts the matrix U_n from the singular value decomposition, storing
  143. // the result in-place into dst. U_n is size r×c.
  144. // If dst is nil, a new matrix is allocated. The resulting U matrix is returned.
  145. //
  146. // UTo will panic if the receiver does not contain a successful factorization.
  147. func (gsvd *HOGSVD) UTo(dst *Dense, n int) *Dense {
  148. if !gsvd.succFact() {
  149. panic(badFact)
  150. }
  151. if n < 0 || gsvd.n <= n {
  152. panic("hogsvd: invalid index")
  153. }
  154. if dst == nil {
  155. r, c := gsvd.b[n].Dims()
  156. dst = NewDense(r, c, nil)
  157. } else {
  158. dst.reuseAs(gsvd.b[n].Dims())
  159. }
  160. dst.Copy(&gsvd.b[n])
  161. var v VecDense
  162. for j, f := range gsvd.Values(nil, n) {
  163. v.ColViewOf(dst, j)
  164. v.ScaleVec(1/f, &v)
  165. }
  166. return dst
  167. }
  168. // Values returns the nth set of singular values of the factorized system.
  169. // If the input slice is non-nil, the values will be stored in-place into the slice.
  170. // In this case, the slice must have length c, and Values will panic with
  171. // matrix.ErrSliceLengthMismatch otherwise. If the input slice is nil,
  172. // a new slice of the appropriate length will be allocated and returned.
  173. //
  174. // Values will panic if the receiver does not contain a successful factorization.
  175. func (gsvd *HOGSVD) Values(s []float64, n int) []float64 {
  176. if !gsvd.succFact() {
  177. panic(badFact)
  178. }
  179. if n < 0 || gsvd.n <= n {
  180. panic("hogsvd: invalid index")
  181. }
  182. _, c := gsvd.b[n].Dims()
  183. if s == nil {
  184. s = make([]float64, c)
  185. } else if len(s) != c {
  186. panic(ErrSliceLengthMismatch)
  187. }
  188. var v VecDense
  189. for j := 0; j < c; j++ {
  190. v.ColViewOf(&gsvd.b[n], j)
  191. s[j] = blas64.Nrm2(v.mat)
  192. }
  193. return s
  194. }
  195. // VTo extracts the matrix V from the singular value decomposition, storing
  196. // the result in-place into dst. V is size c×c.
  197. // If dst is nil, a new matrix is allocated. The resulting V matrix is returned.
  198. //
  199. // VTo will panic if the receiver does not contain a successful factorization.
  200. func (gsvd *HOGSVD) VTo(dst *Dense) *Dense {
  201. if !gsvd.succFact() {
  202. panic(badFact)
  203. }
  204. if dst == nil {
  205. r, c := gsvd.v.Dims()
  206. dst = NewDense(r, c, nil)
  207. } else {
  208. dst.reuseAs(gsvd.v.Dims())
  209. }
  210. dst.Copy(gsvd.v)
  211. return dst
  212. }