-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathapi_minmax.go
155 lines (146 loc) · 4.81 KB
/
api_minmax.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package tensor
import "github.com/pkg/errors"
func MinBetween(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var minbetweener MinBetweener
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor addition
if oe != nil {
return oe.MinBetween(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.MinBetween(at, bt, opts...)
}
if minbetweener, ok = at.Engine().(MinBetweener); ok {
return minbetweener.MinBetween(at, bt, opts...)
}
if minbetweener, ok = bt.Engine().(MinBetweener); ok {
return minbetweener.MinBetween(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support MinBetween")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.MinBetweenScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.MinBetweenScalar(at, bt, leftTensor, opts...)
}
if minbetweener, ok = at.Engine().(MinBetweener); ok {
return minbetweener.MinBetweenScalar(at, bt, leftTensor, opts...)
}
if minbetweener, ok = bt.Engine().(MinBetweener); ok {
return minbetweener.MinBetweenScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support MinBetween")
}
default:
if oe != nil {
return oe.MinBetweenScalar(at, bt, true, opts...)
}
if minbetweener, ok = at.Engine().(MinBetweener); ok {
return minbetweener.MinBetweenScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support MinBetween")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.MinBetweenScalar(bt, at, false, opts...)
}
if minbetweener, ok = bt.Engine().(MinBetweener); ok {
return minbetweener.MinBetweenScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support MinBetween")
default:
return nil, errors.Errorf("Cannot perform MinBetween of %T and %T", a, b)
}
}
panic("Unreachable")
}
func MaxBetween(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var maxbetweener MaxBetweener
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor addition
if oe != nil {
return oe.MaxBetween(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.MaxBetween(at, bt, opts...)
}
if maxbetweener, ok = at.Engine().(MaxBetweener); ok {
return maxbetweener.MaxBetween(at, bt, opts...)
}
if maxbetweener, ok = bt.Engine().(MaxBetweener); ok {
return maxbetweener.MaxBetween(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support MaxBetween")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.MaxBetweenScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.MaxBetweenScalar(at, bt, leftTensor, opts...)
}
if maxbetweener, ok = at.Engine().(MaxBetweener); ok {
return maxbetweener.MaxBetweenScalar(at, bt, leftTensor, opts...)
}
if maxbetweener, ok = bt.Engine().(MaxBetweener); ok {
return maxbetweener.MaxBetweenScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support MaxBetween")
}
default:
if oe != nil {
return oe.MaxBetweenScalar(at, bt, true, opts...)
}
if maxbetweener, ok = at.Engine().(MaxBetweener); ok {
return maxbetweener.MaxBetweenScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support MaxBetween")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.MaxBetweenScalar(bt, at, false, opts...)
}
if maxbetweener, ok = bt.Engine().(MaxBetweener); ok {
return maxbetweener.MaxBetweenScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support MaxBetween")
default:
return nil, errors.Errorf("Cannot perform MaxBetween of %T and %T", a, b)
}
}
panic("Unreachable")
}