-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmul.go
141 lines (121 loc) · 2.68 KB
/
mul.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 Jonathan J Lawlor. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package matrixexp
import (
"github.com/gonum/blas"
"github.com/gonum/blas/blas64"
)
// Mul represents matrix multiplication.
type Mul struct {
Left MatrixExp
Right MatrixExp
}
// String implements the Stringer interface.
func (m1 *Mul) String() string {
return m1.Left.String() + ".Mul(" + m1.Right.String() + ")"
}
// Dims returns the matrix dimensions.
func (m1 *Mul) Dims() (r, c int) {
r, _ = m1.Left.Dims()
_, c = m1.Right.Dims()
return
}
// At returns the value at a given row, column index.
func (m1 *Mul) At(r, c int) float64 {
var v float64
_, n := m1.Left.Dims()
for i := 0; i < n; i++ {
v += m1.Left.At(r, i) * m1.Right.At(i, c)
}
return v
}
// Eval returns a matrix literal.
func (m1 *Mul) Eval() MatrixLiteral {
// This should be replaced with a call to Eval on each side, and then a type
// switch to handle the various matrix literals.
lm := m1.Left.Eval()
rm := m1.Right.Eval()
left := lm.AsGeneral()
right := rm.AsGeneral()
r, c := m1.Dims()
m := blas64.General{
Rows: r,
Cols: c,
Stride: c,
Data: make([]float64, r*c),
}
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, left, right, 0, m)
return &General{m}
}
// Copy creates a (deep) copy of the Matrix Expression.
func (m1 *Mul) Copy() MatrixExp {
return &Mul{
Left: m1.Left.Copy(),
Right: m1.Right.Copy(),
}
}
// Err returns the first error encountered while constructing the matrix expression.
func (m1 *Mul) Err() error {
if err := m1.Left.Err(); err != nil {
return err
}
if err := m1.Right.Err(); err != nil {
return err
}
_, c := m1.Left.Dims()
r, _ := m1.Right.Dims()
if c != r {
return ErrInnerDimMismatch{
R: r,
C: c,
}
}
return nil
}
// T transposes a matrix.
func (m1 *Mul) T() MatrixExp {
return &T{m1}
}
// Add two matrices together.
func (m1 *Mul) Add(m2 MatrixExp) MatrixExp {
return &Add{
Left: m1,
Right: m2,
}
}
// Sub subtracts the right matrix from the left matrix.
func (m1 *Mul) Sub(m2 MatrixExp) MatrixExp {
return &Sub{
Left: m1,
Right: m2,
}
}
// Scale performs scalar multiplication.
func (m1 *Mul) Scale(c float64) MatrixExp {
return &Scale{
C: c,
M: m1,
}
}
// Mul performs matrix multiplication.
func (m1 *Mul) Mul(m2 MatrixExp) MatrixExp {
return &Mul{
Left: m1,
Right: m2,
}
}
// MulElem performs element-wise multiplication.
func (m1 *Mul) MulElem(m2 MatrixExp) MatrixExp {
return &MulElem{
Left: m1,
Right: m2,
}
}
// DivElem performs element-wise division.
func (m1 *Mul) DivElem(m2 MatrixExp) MatrixExp {
return &DivElem{
Left: m1,
Right: m2,
}
}