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