inner.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright ©2014 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. "gonum.org/v1/gonum/blas"
  7. "gonum.org/v1/gonum/blas/blas64"
  8. "gonum.org/v1/gonum/internal/asm/f64"
  9. )
  10. // Inner computes the generalized inner product
  11. // xᵀ A y
  12. // between the vectors x and y with matrix A, where x and y are treated as
  13. // column vectors.
  14. //
  15. // This is only a true inner product if A is symmetric positive definite, though
  16. // the operation works for any matrix A.
  17. //
  18. // Inner panics if x.Len != m or y.Len != n when A is an m x n matrix.
  19. func Inner(x Vector, a Matrix, y Vector) float64 {
  20. m, n := a.Dims()
  21. if x.Len() != m {
  22. panic(ErrShape)
  23. }
  24. if y.Len() != n {
  25. panic(ErrShape)
  26. }
  27. if m == 0 || n == 0 {
  28. return 0
  29. }
  30. var sum float64
  31. switch a := a.(type) {
  32. case RawSymmetricer:
  33. amat := a.RawSymmetric()
  34. if amat.Uplo != blas.Upper {
  35. // Panic as a string not a mat.Error.
  36. panic(badSymTriangle)
  37. }
  38. var xmat, ymat blas64.Vector
  39. if xrv, ok := x.(RawVectorer); ok {
  40. xmat = xrv.RawVector()
  41. } else {
  42. break
  43. }
  44. if yrv, ok := y.(RawVectorer); ok {
  45. ymat = yrv.RawVector()
  46. } else {
  47. break
  48. }
  49. for i := 0; i < x.Len(); i++ {
  50. xi := x.AtVec(i)
  51. if xi != 0 {
  52. if ymat.Inc == 1 {
  53. sum += xi * f64.DotUnitary(
  54. amat.Data[i*amat.Stride+i:i*amat.Stride+n],
  55. ymat.Data[i:],
  56. )
  57. } else {
  58. sum += xi * f64.DotInc(
  59. amat.Data[i*amat.Stride+i:i*amat.Stride+n],
  60. ymat.Data[i*ymat.Inc:], uintptr(n-i),
  61. 1, uintptr(ymat.Inc),
  62. 0, 0,
  63. )
  64. }
  65. }
  66. yi := y.AtVec(i)
  67. if i != n-1 && yi != 0 {
  68. if xmat.Inc == 1 {
  69. sum += yi * f64.DotUnitary(
  70. amat.Data[i*amat.Stride+i+1:i*amat.Stride+n],
  71. xmat.Data[i+1:],
  72. )
  73. } else {
  74. sum += yi * f64.DotInc(
  75. amat.Data[i*amat.Stride+i+1:i*amat.Stride+n],
  76. xmat.Data[(i+1)*xmat.Inc:], uintptr(n-i-1),
  77. 1, uintptr(xmat.Inc),
  78. 0, 0,
  79. )
  80. }
  81. }
  82. }
  83. return sum
  84. case RawMatrixer:
  85. amat := a.RawMatrix()
  86. var ymat blas64.Vector
  87. if yrv, ok := y.(RawVectorer); ok {
  88. ymat = yrv.RawVector()
  89. } else {
  90. break
  91. }
  92. for i := 0; i < x.Len(); i++ {
  93. xi := x.AtVec(i)
  94. if xi != 0 {
  95. if ymat.Inc == 1 {
  96. sum += xi * f64.DotUnitary(
  97. amat.Data[i*amat.Stride:i*amat.Stride+n],
  98. ymat.Data,
  99. )
  100. } else {
  101. sum += xi * f64.DotInc(
  102. amat.Data[i*amat.Stride:i*amat.Stride+n],
  103. ymat.Data, uintptr(n),
  104. 1, uintptr(ymat.Inc),
  105. 0, 0,
  106. )
  107. }
  108. }
  109. }
  110. return sum
  111. }
  112. for i := 0; i < x.Len(); i++ {
  113. xi := x.AtVec(i)
  114. for j := 0; j < y.Len(); j++ {
  115. sum += xi * a.At(i, j) * y.AtVec(j)
  116. }
  117. }
  118. return sum
  119. }