forked from pa-m/mat32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inner.go
70 lines (65 loc) · 1.47 KB
/
inner.go
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
// 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 mat32
import (
"github.com/arjunsk/mat32/internal/asm/f32"
"gonum.org/v1/gonum/blas/blas32"
)
// Inner computes the generalized inner product
//
// x^T A y
//
// between column vectors x and y with matrix A. 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) float32 {
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 float32
switch a := a.(type) {
case RawMatrixer:
amat := a.RawMatrix()
var ymat blas32.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 * f32.DotUnitary(
amat.Data[i*amat.Stride:i*amat.Stride+n],
ymat.Data,
)
} else {
sum += xi * f32.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
}