-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathreal_operate.jl
341 lines (306 loc) · 7.89 KB
/
real_operate.jl
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright (c) 2014: Madeleine Udell and contributors
#
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file or at https://opensource.org/license/bsd-2-clause
# Here we cover both real -> real and complex -> real
# First we cover real -> real, then complex -> real
# Only two types allowed here for real -> real
const AllAllowedReal{T} = Union{SparseTape{T},SPARSE_VECTOR{T}}
## Vararg
# `+`
# (Vector, Vector)
function real_operate(
::typeof(+),
::Type{T},
v1::SPARSE_VECTOR{T},
v2::SPARSE_VECTOR{T},
) where {T<:Real}
return v1 + v2
end
# (Vector, SparseTape)
function real_operate(
::typeof(+),
::Type{T},
v::SPARSE_VECTOR{T},
tape::SparseTape{T},
) where {T<:Real}
return real_operate(+, T, tape, v)
end
# (SparseTape, Vector)
function real_operate(
::typeof(+),
::Type{T},
tape::SparseTape{T},
v::SPARSE_VECTOR{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
return SparseTape(
SparseAffineOperation(op.matrix, op.vector + v),
tape.variables,
)
end
# (SparseTape, SparseTape)
function real_operate(
::typeof(+),
::Type{T},
tape1::SparseTape{T},
tape2::SparseTape{T},
) where {T<:Real}
@assert MOI.output_dimension(tape1) == MOI.output_dimension(tape2)
op1 = SparseAffineOperation(tape1)
op2 = SparseAffineOperation(tape2)
if tape1.variables == tape2.variables
op = SparseAffineOperation(
op1.matrix + op2.matrix,
op1.vector + op2.vector,
)
return SparseTape(op, tape1.variables)
else
mat = hcat(op1.matrix, op2.matrix)
vec = op1.vector + op2.vector
op = SparseAffineOperation(mat, vec)
return SparseTape(op, vcat(tape1.variables, tape2.variables))
end
end
# Reduce to 2-arg
function real_operate(
::typeof(+),
::Type{T},
arg1::AllAllowedReal{T},
arg2::AllAllowedReal{T},
arg3::AllAllowedReal{T},
args::AllAllowedReal{T}...,
) where {T<:Real}
all_args = (arg1, arg2, arg3, args...)
vec_args = (a for a in all_args if a isa Vector)
tape_args = (a for a in all_args if a isa SparseTape)
if isempty(tape_args)
return sum(vec_args)
else
tape = foldl((a, b) -> real_operate(+, T, a, b), tape_args)
end
if isempty(vec_args)
return tape
else
v = sum(vec_args)
return real_operate(+, T, tape, v)
end
end
# `-`
# Unary `-`
function real_operate(
::typeof(-),
::Type{T},
tape::SparseTape{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
return SparseTape(
SparseAffineOperation(-op.matrix, -op.vector),
tape.variables,
)
end
function real_operate(
::typeof(-),
::Type{T},
v::SPARSE_VECTOR{T},
) where {T<:Real}
return -v
end
# 2+ args: reduce to unary - and +
function real_operate(
::typeof(-),
::Type{T},
x::AllAllowedReal{T},
ys::AllAllowedReal{T}...,
) where {T<:Real}
mys = (real_operate(-, T, y) for y in ys)
return real_operate(+, T, x, mys...)
end
# `vcat`
# 1-arg does nothing
function real_operate(
::typeof(vcat),
::Type{T},
v::AllAllowedReal{T},
) where {T<:Real}
return v
end
# we do all pairs of `SparseTape` and `Vector`, and then do 3+ arguments by iterating
function real_operate(
::typeof(vcat),
::Type{T},
tape1::SparseTape{T},
tape2::SparseTape{T},
) where {T<:Real}
op1 = SparseAffineOperation(tape1)
op2 = SparseAffineOperation(tape2)
A = SparseArrays.blockdiag(op1.matrix, op2.matrix)
b = vcat(op1.vector, op2.vector)
x = vcat(tape1.variables, tape2.variables)
return SparseTape(SparseAffineOperation(A, b), x)
end
function real_operate(
::typeof(vcat),
::Type{T},
tape::SparseTape{T},
v::SPARSE_VECTOR{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
n = length(v)
m = size(op.matrix, 2) # bad for uniformscaling
b = vcat(op.vector, v)
# Workaround SparseSuiteGraphBLAS bug with vcat
# where vcatting two (1,1) GBMatrix yields GBVector
# if op.matrix isa GBMatrix
# A = GBMatrix{T,T}(size(op.matrix, 1) + n, m)
# A[1:size(op.matrix, 1), :] = op.matrix
# else
Z = spzeros(T, n, m)
A = vcat(op.matrix, Z)
# end
return SparseTape(SparseAffineOperation(A, b), tape.variables)
end
function real_operate(
::typeof(vcat),
::Type{T},
v::SPARSE_VECTOR{T},
tape::SparseTape{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
n = length(v)
m = size(op.matrix, 2) # bad for uniformscaling
b = vcat(v, op.vector)
# Workaround SparseSuiteGraphBLAS bug with vcat
# where vcatting two (1,1) GBMatrix yields GBVector
# if op.matrix isa GBMatrix
# A = GBMatrix{T,T}(n + size(op.matrix, 1), m)
# A[(n+1):end, :] = op.matrix
# else
Z = spzeros(T, n, m)
A = vcat(Z, op.matrix)
# end
return SparseTape(SparseAffineOperation(A, b), tape.variables)
end
function real_operate(
::typeof(vcat),
::Type{T},
v1::SPARSE_VECTOR{T},
v2::SPARSE_VECTOR{T},
) where {T<:Real}
return vcat(v1, v2)
end
function real_operate(
::typeof(vcat),
::Type{T},
arg1::AllAllowedReal{T},
arg2::AllAllowedReal{T},
arg3::AllAllowedReal{T},
args::AllAllowedReal{T}...,
) where {T<:Real}
all_args = (arg1, arg2, arg3, args...)
return foldl((a, b) -> real_operate(vcat, T, a, b), all_args)
end
function real_operate(
::typeof(vcat),
::Type{T},
arg1::SparseTape{T},
arg2::SparseTape{T},
arg3::SparseTape{T},
args::SparseTape{T}...,
) where {T<:Real}
all_args = (arg1, arg2, arg3, args...)
ops = SparseAffineOperation.(all_args)
A = SparseArrays.blockdiag((op.matrix for op in ops)...)
b = vcat((op.vector for op in ops)...)
x = vcat((arg.variables for arg in all_args)...)
return SparseTape(SparseAffineOperation(A, b), x)
end
## Unary
# `sum`
function real_operate(
::typeof(sum),
::Type{T},
tape::SparseTape{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
mat = sum(op.matrix; dims = 1)
vec = [sum(op.vector)]
return SparseTape{T}(SparseAffineOperation(mat, vec), tape.variables)
end
function real_operate(
::typeof(sum),
::Type{T},
v::SPARSE_VECTOR{T},
) where {T<:Real}
return [sum(v)]
end
## Binary
# `add_operation`
# Here the left-side argument may be a `SparseMatrixCSC{T}` or a `T`
# and the right-argument is either a `SparseTape{T}` or `SPARSE_VECTOR{T}`
function real_operate(
::typeof(add_operation),
::Type{T},
A::AbstractMatrix,
tape::SparseTape{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
return SparseTape(
SparseAffineOperation(A * op.matrix, A * op.vector),
tape.variables,
)
# return add_operation(tape, SparseAffineOperation(A, spzeros(T, size(A, 1))))
end
function real_operate(
::typeof(add_operation),
::Type{T},
A::AbstractMatrix,
v::SPARSE_VECTOR{T},
) where {T<:Real}
return SPARSE_VECTOR{T}(A * v)
end
function real_operate(
::typeof(add_operation),
::Type{T},
x::Real,
tape::SparseTape{T},
) where {T<:Real}
op = SparseAffineOperation(tape)
return SparseTape(
SparseAffineOperation(x * op.matrix, x * op.vector),
tape.variables,
)
end
function real_operate(
::typeof(add_operation),
::Type{T},
x::Real,
v::SPARSE_VECTOR{T},
) where {T<:Real}
return SPARSE_VECTOR{T}(real_convert(T, x) * v)
end
# Here we have our two complex -> real functions
# These are allowed these inputs:
const ComplexToRealInputs{T} = Union{
ComplexTape{T},
SparseTape{T},
ComplexStructOfVec{T},
SPARSE_VECTOR{T},
SPARSE_VECTOR{Complex{T}},
}
# `real`
function real_operate(
::typeof(real),
::Type{T},
c::ComplexToRealInputs{T},
) where {T}
return real(c)
end
# `imag`
function real_operate(
::typeof(imag),
::Type{T},
c::ComplexToRealInputs{T},
) where {T}
return imag(c)
end