inner.go 2.6 KB

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