cmatrix.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright ©2013 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. "math"
  7. "math/cmplx"
  8. "gonum.org/v1/gonum/blas/cblas128"
  9. "gonum.org/v1/gonum/floats/scalar"
  10. )
  11. // CMatrix is the basic matrix interface type for complex matrices.
  12. type CMatrix interface {
  13. // Dims returns the dimensions of a Matrix.
  14. Dims() (r, c int)
  15. // At returns the value of a matrix element at row i, column j.
  16. // It will panic if i or j are out of bounds for the matrix.
  17. At(i, j int) complex128
  18. // H returns the conjugate transpose of the Matrix. Whether H
  19. // returns a copy of the underlying data is implementation dependent.
  20. // This method may be implemented using the Conjugate type, which
  21. // provides an implicit matrix conjugate transpose.
  22. H() CMatrix
  23. }
  24. // A RawCMatrixer can return a cblas128.General representation of the receiver. Changes to the cblas128.General.Data
  25. // slice will be reflected in the original matrix, changes to the Rows, Cols and Stride fields will not.
  26. type RawCMatrixer interface {
  27. RawCMatrix() cblas128.General
  28. }
  29. var (
  30. _ CMatrix = Conjugate{}
  31. _ Unconjugator = Conjugate{}
  32. )
  33. // Conjugate is a type for performing an implicit matrix conjugate transpose.
  34. // It implements the Matrix interface, returning values from the conjugate
  35. // transpose of the matrix within.
  36. type Conjugate struct {
  37. CMatrix CMatrix
  38. }
  39. // At returns the value of the element at row i and column j of the conjugate
  40. // transposed matrix, that is, row j and column i of the Matrix field.
  41. func (t Conjugate) At(i, j int) complex128 {
  42. z := t.CMatrix.At(j, i)
  43. return cmplx.Conj(z)
  44. }
  45. // Dims returns the dimensions of the transposed matrix. The number of rows returned
  46. // is the number of columns in the Matrix field, and the number of columns is
  47. // the number of rows in the Matrix field.
  48. func (t Conjugate) Dims() (r, c int) {
  49. c, r = t.CMatrix.Dims()
  50. return r, c
  51. }
  52. // H performs an implicit conjugate transpose by returning the Matrix field.
  53. func (t Conjugate) H() CMatrix {
  54. return t.CMatrix
  55. }
  56. // Unconjugate returns the Matrix field.
  57. func (t Conjugate) Unconjugate() CMatrix {
  58. return t.CMatrix
  59. }
  60. // Unconjugator is a type that can undo an implicit conjugate transpose.
  61. type Unconjugator interface {
  62. // Note: This interface is needed to unify all of the Conjugate types. In
  63. // the cmat128 methods, we need to test if the Matrix has been implicitly
  64. // transposed. If this is checked by testing for the specific Conjugate type
  65. // then the behavior will be different if the user uses H() or HTri() for a
  66. // triangular matrix.
  67. // Unconjugate returns the underlying Matrix stored for the implicit
  68. // conjugate transpose.
  69. Unconjugate() CMatrix
  70. }
  71. // useC returns a complex128 slice with l elements, using c if it
  72. // has the necessary capacity, otherwise creating a new slice.
  73. func useC(c []complex128, l int) []complex128 {
  74. if l <= cap(c) {
  75. return c[:l]
  76. }
  77. return make([]complex128, l)
  78. }
  79. // useZeroedC returns a complex128 slice with l elements, using c if it
  80. // has the necessary capacity, otherwise creating a new slice. The
  81. // elements of the returned slice are guaranteed to be zero.
  82. func useZeroedC(c []complex128, l int) []complex128 {
  83. if l <= cap(c) {
  84. c = c[:l]
  85. zeroC(c)
  86. return c
  87. }
  88. return make([]complex128, l)
  89. }
  90. // zeroC zeros the given slice's elements.
  91. func zeroC(c []complex128) {
  92. for i := range c {
  93. c[i] = 0
  94. }
  95. }
  96. // unconjugate unconjugates a matrix if applicable. If a is an Unconjugator, then
  97. // unconjugate returns the underlying matrix and true. If it is not, then it returns
  98. // the input matrix and false.
  99. func unconjugate(a CMatrix) (CMatrix, bool) {
  100. if ut, ok := a.(Unconjugator); ok {
  101. return ut.Unconjugate(), true
  102. }
  103. return a, false
  104. }
  105. // CEqual returns whether the matrices a and b have the same size
  106. // and are element-wise equal.
  107. func CEqual(a, b CMatrix) bool {
  108. ar, ac := a.Dims()
  109. br, bc := b.Dims()
  110. if ar != br || ac != bc {
  111. return false
  112. }
  113. // TODO(btracey): Add in fast-paths.
  114. for i := 0; i < ar; i++ {
  115. for j := 0; j < ac; j++ {
  116. if a.At(i, j) != b.At(i, j) {
  117. return false
  118. }
  119. }
  120. }
  121. return true
  122. }
  123. // CEqualApprox returns whether the matrices a and b have the same size and contain all equal
  124. // elements with tolerance for element-wise equality specified by epsilon. Matrices
  125. // with non-equal shapes are not equal.
  126. func CEqualApprox(a, b CMatrix, epsilon float64) bool {
  127. // TODO(btracey):
  128. ar, ac := a.Dims()
  129. br, bc := b.Dims()
  130. if ar != br || ac != bc {
  131. return false
  132. }
  133. for i := 0; i < ar; i++ {
  134. for j := 0; j < ac; j++ {
  135. if !cEqualWithinAbsOrRel(a.At(i, j), b.At(i, j), epsilon, epsilon) {
  136. return false
  137. }
  138. }
  139. }
  140. return true
  141. }
  142. // TODO(btracey): Move these into a cmplxs if/when we have one.
  143. func cEqualWithinAbsOrRel(a, b complex128, absTol, relTol float64) bool {
  144. if cEqualWithinAbs(a, b, absTol) {
  145. return true
  146. }
  147. return cEqualWithinRel(a, b, relTol)
  148. }
  149. // cEqualWithinAbs returns true if a and b have an absolute
  150. // difference of less than tol.
  151. func cEqualWithinAbs(a, b complex128, tol float64) bool {
  152. return a == b || cmplx.Abs(a-b) <= tol
  153. }
  154. const minNormalFloat64 = 2.2250738585072014e-308
  155. // cEqualWithinRel returns true if the difference between a and b
  156. // is not greater than tol times the greater value.
  157. func cEqualWithinRel(a, b complex128, tol float64) bool {
  158. if a == b {
  159. return true
  160. }
  161. if cmplx.IsNaN(a) || cmplx.IsNaN(b) {
  162. return false
  163. }
  164. // Cannot play the same trick as in floats/scalar because there are multiple
  165. // possible infinities.
  166. if cmplx.IsInf(a) {
  167. if !cmplx.IsInf(b) {
  168. return false
  169. }
  170. ra := real(a)
  171. if math.IsInf(ra, 0) {
  172. if ra == real(b) {
  173. return scalar.EqualWithinRel(imag(a), imag(b), tol)
  174. }
  175. return false
  176. }
  177. if imag(a) == imag(b) {
  178. return scalar.EqualWithinRel(ra, real(b), tol)
  179. }
  180. return false
  181. }
  182. if cmplx.IsInf(b) {
  183. return false
  184. }
  185. delta := cmplx.Abs(a - b)
  186. if delta <= minNormalFloat64 {
  187. return delta <= tol*minNormalFloat64
  188. }
  189. return delta/math.Max(cmplx.Abs(a), cmplx.Abs(b)) <= tol
  190. }