dlabrd.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright ©2015 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 gonum
  5. import (
  6. "gonum.org/v1/gonum/blas"
  7. "gonum.org/v1/gonum/blas/blas64"
  8. )
  9. // Dlabrd reduces the first NB rows and columns of a real general m×n matrix
  10. // A to upper or lower bidiagonal form by an orthogonal transformation
  11. // Q**T * A * P
  12. // If m >= n, A is reduced to upper bidiagonal form and upon exit the elements
  13. // on and below the diagonal in the first nb columns represent the elementary
  14. // reflectors, and the elements above the diagonal in the first nb rows represent
  15. // the matrix P. If m < n, A is reduced to lower bidiagonal form and the elements
  16. // P is instead stored above the diagonal.
  17. //
  18. // The reduction to bidiagonal form is stored in d and e, where d are the diagonal
  19. // elements, and e are the off-diagonal elements.
  20. //
  21. // The matrices Q and P are products of elementary reflectors
  22. // Q = H_0 * H_1 * ... * H_{nb-1}
  23. // P = G_0 * G_1 * ... * G_{nb-1}
  24. // where
  25. // H_i = I - tauQ[i] * v_i * v_iᵀ
  26. // G_i = I - tauP[i] * u_i * u_iᵀ
  27. //
  28. // As an example, on exit the entries of A when m = 6, n = 5, and nb = 2
  29. // [ 1 1 u1 u1 u1]
  30. // [v1 1 1 u2 u2]
  31. // [v1 v2 a a a]
  32. // [v1 v2 a a a]
  33. // [v1 v2 a a a]
  34. // [v1 v2 a a a]
  35. // and when m = 5, n = 6, and nb = 2
  36. // [ 1 u1 u1 u1 u1 u1]
  37. // [ 1 1 u2 u2 u2 u2]
  38. // [v1 1 a a a a]
  39. // [v1 v2 a a a a]
  40. // [v1 v2 a a a a]
  41. //
  42. // Dlabrd also returns the matrices X and Y which are used with U and V to
  43. // apply the transformation to the unreduced part of the matrix
  44. // A := A - V*Yᵀ - X*Uᵀ
  45. // and returns the matrices X and Y which are needed to apply the
  46. // transformation to the unreduced part of A.
  47. //
  48. // X is an m×nb matrix, Y is an n×nb matrix. d, e, taup, and tauq must all have
  49. // length at least nb. Dlabrd will panic if these size constraints are violated.
  50. //
  51. // Dlabrd is an internal routine. It is exported for testing purposes.
  52. func (impl Implementation) Dlabrd(m, n, nb int, a []float64, lda int, d, e, tauQ, tauP, x []float64, ldx int, y []float64, ldy int) {
  53. switch {
  54. case m < 0:
  55. panic(mLT0)
  56. case n < 0:
  57. panic(nLT0)
  58. case nb < 0:
  59. panic(nbLT0)
  60. case nb > n:
  61. panic(nbGTN)
  62. case nb > m:
  63. panic(nbGTM)
  64. case lda < max(1, n):
  65. panic(badLdA)
  66. case ldx < max(1, nb):
  67. panic(badLdX)
  68. case ldy < max(1, nb):
  69. panic(badLdY)
  70. }
  71. if m == 0 || n == 0 || nb == 0 {
  72. return
  73. }
  74. switch {
  75. case len(a) < (m-1)*lda+n:
  76. panic(shortA)
  77. case len(d) < nb:
  78. panic(shortD)
  79. case len(e) < nb:
  80. panic(shortE)
  81. case len(tauQ) < nb:
  82. panic(shortTauQ)
  83. case len(tauP) < nb:
  84. panic(shortTauP)
  85. case len(x) < (m-1)*ldx+nb:
  86. panic(shortX)
  87. case len(y) < (n-1)*ldy+nb:
  88. panic(shortY)
  89. }
  90. bi := blas64.Implementation()
  91. if m >= n {
  92. // Reduce to upper bidiagonal form.
  93. for i := 0; i < nb; i++ {
  94. bi.Dgemv(blas.NoTrans, m-i, i, -1, a[i*lda:], lda, y[i*ldy:], 1, 1, a[i*lda+i:], lda)
  95. bi.Dgemv(blas.NoTrans, m-i, i, -1, x[i*ldx:], ldx, a[i:], lda, 1, a[i*lda+i:], lda)
  96. a[i*lda+i], tauQ[i] = impl.Dlarfg(m-i, a[i*lda+i], a[min(i+1, m-1)*lda+i:], lda)
  97. d[i] = a[i*lda+i]
  98. if i < n-1 {
  99. // Compute Y[i+1:n, i].
  100. a[i*lda+i] = 1
  101. bi.Dgemv(blas.Trans, m-i, n-i-1, 1, a[i*lda+i+1:], lda, a[i*lda+i:], lda, 0, y[(i+1)*ldy+i:], ldy)
  102. bi.Dgemv(blas.Trans, m-i, i, 1, a[i*lda:], lda, a[i*lda+i:], lda, 0, y[i:], ldy)
  103. bi.Dgemv(blas.NoTrans, n-i-1, i, -1, y[(i+1)*ldy:], ldy, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
  104. bi.Dgemv(blas.Trans, m-i, i, 1, x[i*ldx:], ldx, a[i*lda+i:], lda, 0, y[i:], ldy)
  105. bi.Dgemv(blas.Trans, i, n-i-1, -1, a[i+1:], lda, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
  106. bi.Dscal(n-i-1, tauQ[i], y[(i+1)*ldy+i:], ldy)
  107. // Update A[i, i+1:n].
  108. bi.Dgemv(blas.NoTrans, n-i-1, i+1, -1, y[(i+1)*ldy:], ldy, a[i*lda:], 1, 1, a[i*lda+i+1:], 1)
  109. bi.Dgemv(blas.Trans, i, n-i-1, -1, a[i+1:], lda, x[i*ldx:], 1, 1, a[i*lda+i+1:], 1)
  110. // Generate reflection P[i] to annihilate A[i, i+2:n].
  111. a[i*lda+i+1], tauP[i] = impl.Dlarfg(n-i-1, a[i*lda+i+1], a[i*lda+min(i+2, n-1):], 1)
  112. e[i] = a[i*lda+i+1]
  113. a[i*lda+i+1] = 1
  114. // Compute X[i+1:m, i].
  115. bi.Dgemv(blas.NoTrans, m-i-1, n-i-1, 1, a[(i+1)*lda+i+1:], lda, a[i*lda+i+1:], 1, 0, x[(i+1)*ldx+i:], ldx)
  116. bi.Dgemv(blas.Trans, n-i-1, i+1, 1, y[(i+1)*ldy:], ldy, a[i*lda+i+1:], 1, 0, x[i:], ldx)
  117. bi.Dgemv(blas.NoTrans, m-i-1, i+1, -1, a[(i+1)*lda:], lda, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
  118. bi.Dgemv(blas.NoTrans, i, n-i-1, 1, a[i+1:], lda, a[i*lda+i+1:], 1, 0, x[i:], ldx)
  119. bi.Dgemv(blas.NoTrans, m-i-1, i, -1, x[(i+1)*ldx:], ldx, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
  120. bi.Dscal(m-i-1, tauP[i], x[(i+1)*ldx+i:], ldx)
  121. }
  122. }
  123. return
  124. }
  125. // Reduce to lower bidiagonal form.
  126. for i := 0; i < nb; i++ {
  127. // Update A[i,i:n]
  128. bi.Dgemv(blas.NoTrans, n-i, i, -1, y[i*ldy:], ldy, a[i*lda:], 1, 1, a[i*lda+i:], 1)
  129. bi.Dgemv(blas.Trans, i, n-i, -1, a[i:], lda, x[i*ldx:], 1, 1, a[i*lda+i:], 1)
  130. // Generate reflection P[i] to annihilate A[i, i+1:n]
  131. a[i*lda+i], tauP[i] = impl.Dlarfg(n-i, a[i*lda+i], a[i*lda+min(i+1, n-1):], 1)
  132. d[i] = a[i*lda+i]
  133. if i < m-1 {
  134. a[i*lda+i] = 1
  135. // Compute X[i+1:m, i].
  136. bi.Dgemv(blas.NoTrans, m-i-1, n-i, 1, a[(i+1)*lda+i:], lda, a[i*lda+i:], 1, 0, x[(i+1)*ldx+i:], ldx)
  137. bi.Dgemv(blas.Trans, n-i, i, 1, y[i*ldy:], ldy, a[i*lda+i:], 1, 0, x[i:], ldx)
  138. bi.Dgemv(blas.NoTrans, m-i-1, i, -1, a[(i+1)*lda:], lda, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
  139. bi.Dgemv(blas.NoTrans, i, n-i, 1, a[i:], lda, a[i*lda+i:], 1, 0, x[i:], ldx)
  140. bi.Dgemv(blas.NoTrans, m-i-1, i, -1, x[(i+1)*ldx:], ldx, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
  141. bi.Dscal(m-i-1, tauP[i], x[(i+1)*ldx+i:], ldx)
  142. // Update A[i+1:m, i].
  143. bi.Dgemv(blas.NoTrans, m-i-1, i, -1, a[(i+1)*lda:], lda, y[i*ldy:], 1, 1, a[(i+1)*lda+i:], lda)
  144. bi.Dgemv(blas.NoTrans, m-i-1, i+1, -1, x[(i+1)*ldx:], ldx, a[i:], lda, 1, a[(i+1)*lda+i:], lda)
  145. // Generate reflection Q[i] to annihilate A[i+2:m, i].
  146. a[(i+1)*lda+i], tauQ[i] = impl.Dlarfg(m-i-1, a[(i+1)*lda+i], a[min(i+2, m-1)*lda+i:], lda)
  147. e[i] = a[(i+1)*lda+i]
  148. a[(i+1)*lda+i] = 1
  149. // Compute Y[i+1:n, i].
  150. bi.Dgemv(blas.Trans, m-i-1, n-i-1, 1, a[(i+1)*lda+i+1:], lda, a[(i+1)*lda+i:], lda, 0, y[(i+1)*ldy+i:], ldy)
  151. bi.Dgemv(blas.Trans, m-i-1, i, 1, a[(i+1)*lda:], lda, a[(i+1)*lda+i:], lda, 0, y[i:], ldy)
  152. bi.Dgemv(blas.NoTrans, n-i-1, i, -1, y[(i+1)*ldy:], ldy, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
  153. bi.Dgemv(blas.Trans, m-i-1, i+1, 1, x[(i+1)*ldx:], ldx, a[(i+1)*lda+i:], lda, 0, y[i:], ldy)
  154. bi.Dgemv(blas.Trans, i+1, n-i-1, -1, a[i+1:], lda, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
  155. bi.Dscal(n-i-1, tauQ[i], y[(i+1)*ldy+i:], ldy)
  156. }
  157. }
  158. }