-
Notifications
You must be signed in to change notification settings - Fork 98
/
FieldArrays.jl
696 lines (602 loc) · 18.5 KB
/
FieldArrays.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# Make arrays of field behave like Maps
function return_cache(f::AbstractArray{T},x::Point) where T<:Field
S = return_type(testitem(f),x)
cr = CachedArray(zeros(S,size(f)))
if isconcretetype(T)
cf = return_cache(testitem(f),x)
else
cf = nothing
end
cr, cf
end
"""
Implementation of `return_cache` for a array of `Field`.
If the field vector has length `nf` and it is evaluated in one point, it
returns an `nf` vector with the result. If the same array is applied to a
vector of `np` points, it returns a matrix `np` x `nf`.
"""
function evaluate!(c,f::AbstractArray{T},x::Point) where T<:Field
cr, cf = c
setsize!(cr,size(f))
r = cr.array
if isconcretetype(T)
for j in eachindex(f)
@inbounds r[j] = evaluate!(cf,f[j],x)
end
else
for j in eachindex(f)
@inbounds r[j] = evaluate(f[j],x)
end
end
r
end
function return_cache(f::AbstractArray{T},x::AbstractArray{<:Point}) where T<:Field
S = return_type(testitem(f),testitem(x))
cr = CachedArray(zeros(S,(size(x)...,size(f)...)))
if isconcretetype(T)
cf = return_cache(f,testitem(x))
else
cf = nothing
end
cr, cf
end
function evaluate!(c,f::AbstractArray{T},x::AbstractArray{<:Point}) where T<:Field
cr, cf = c
setsize!(cr,(size(x)...,size(f)...))
r = cr.array
if isconcretetype(T)
for i in eachindex(x)
fxi = evaluate!(cf,f,x[i])
for j in CartesianIndices(f)
r[i,j] = fxi[j]
end
end
else
for j in eachindex(f)
for i in eachindex(x)
r[i,j] = evaluate(f[j],x[i])
end
end
end
r
end
function testargs(f::AbstractArray{T},x::Point) where T<:Field
testargs(testitem(f),x)
end
function testargs(f::AbstractArray{T},x::AbstractArray{<:Point}) where T<:Field
testargs(testitem(f),x)
end
function test_field_array(f::AbstractArray{<:Field}, x, v, cmp=(==); grad=nothing, gradgrad=nothing)
test_map(v,f,x;cmp=cmp)
if grad != nothing
test_map(grad,Broadcasting(∇)(f),x;cmp=cmp)
end
if gradgrad != nothing
test_map(gradgrad,Broadcasting(∇∇)(f),x;cmp=cmp)
end
true
end
# Opening the door to optimize arrays of field gradients
"""
A wrapper that represents the broadcast of `gradient` over an array of fields.
Ng is the number of times the gradient is applied
"""
struct FieldGradientArray{Ng,A,T,N} <: AbstractArray{T,N}
fa::A
function FieldGradientArray{Ng}(f::AbstractArray{<:Field}) where Ng
T = typeof(gradient(testitem(f),Val(Ng)))
N = ndims(f)
A = typeof(f)
new{Ng,A,T,N}(f)
end
end
function return_value(k::Broadcasting{typeof(∇)},a::AbstractArray{<:Field})
evaluate(k,a)
end
function return_value(k::Broadcasting{typeof(∇∇)},a::AbstractArray{<:Field})
evaluate(k,a)
end
function evaluate!(cache,k::Broadcasting{typeof(∇)},a::AbstractArray{<:Field})
FieldGradientArray{1}(a)
end
function evaluate!(cache,k::Broadcasting{typeof(∇)},a::FieldGradientArray{N}) where N
FieldGradientArray{N+1}(a.fa)
end
function evaluate!(cache,k::Broadcasting{typeof(∇∇)},a::AbstractArray{<:Field})
FieldGradientArray{2}(a)
end
function gradient(a::AbstractArray{<:Field})
msg =
"""\n
Function gradient (aka ∇) is not defined for arrays of Field objects.
Use Broadcasting(∇) instead.
"""
@unreachable msg
end
function ∇∇(a::AbstractArray{<:Field})
msg =
"""\n
Double gradient application (aka ∇∇) is not defined for arrays of Field objects.
Use Broadcasting(∇∇) instead.
"""
@unreachable msg
end
Base.size(a::FieldGradientArray) = size(a.fa)
Base.axes(a::FieldGradientArray) = axes(a.fa)
Base.getindex(a::FieldGradientArray{Ng},i::Integer) where Ng = gradient(a.fa[i],Val(Ng))
Base.getindex(
a::FieldGradientArray{Ng,A,T,N},i::Vararg{Integer,N}) where {Ng,A,T,N} = gradient(a.fa[i...],Val(Ng))
Base.IndexStyle(::Type{<:FieldGradientArray{Ng,A}}) where {Ng,A} = IndexStyle(A)
# Optimizing linear_combination.
function linear_combination(a::AbstractVector{<:Number},b::AbstractVector{<:Field})
column = 1
LinearCombinationField(a,b,column)
end
struct LinearCombinationField{V,F} <: Field
values::V
fields::F
column::Int
end
for T in (:(Point),:(AbstractVector{<:Point}))
@eval begin
function return_value(a::LinearCombinationField,x::$T)
fx = return_value(a.fields,x)
v = a.values
k = LinearCombinationMap(a.column)
return_value(k,v,fx)
end
function return_cache(a::LinearCombinationField,x::$T)
cf = return_cache(a.fields,x)
fx = return_value(a.fields,x)
v = a.values
k = LinearCombinationMap(a.column)
ck = return_cache(k,v,fx)
cf, ck
end
function evaluate!(cache,a::LinearCombinationField,x::$T)
cf, ck = cache
fx = evaluate!(cf,a.fields,x)
v = a.values
k = LinearCombinationMap(a.column)
evaluate!(ck,k,v,fx)
end
end
end
for op in (:∇,:∇∇)
@eval begin
function $op(a::LinearCombinationField)
fields = Broadcasting($op)(a.fields)
LinearCombinationField(a.values,fields,a.column)
end
end
end
function linear_combination(a::AbstractMatrix{<:Number},b::AbstractVector{<:Field})
#[ LinearCombinationField(a,b,i) for i in 1:size(a,2) ]
LinearCombinationFieldVector(a,b)
end
struct LinearCombinationFieldVector{V,F} <: AbstractVector{LinearCombinationField{V,F}}
values::V
fields::F
function LinearCombinationFieldVector(values::AbstractMatrix{<:Number},fields::AbstractVector{<:Field})
@check size(values,1) == length(fields) """\n
Incompatible sizes for performing the linear combination
linear_combination(values,fields) = transpose(values)*fields
size(values,1) != length(fields)
"""
V = typeof(values)
F = typeof(fields)
new{V,F}(values,fields)
end
end
Base.size(a::LinearCombinationFieldVector) = (size(a.values,2),)
Base.getindex(a::LinearCombinationFieldVector,i::Integer) = LinearCombinationField(a.values,a.fields,i)
Base.IndexStyle(::Type{<:LinearCombinationField}) = IndexLinear()
for T in (:(Point),:(AbstractVector{<:Point}))
@eval begin
function return_value(a::LinearCombinationFieldVector,x::$T)
fx = return_value(a.fields,x)
v = a.values
k = LinearCombinationMap(:)
return_value(k,v,fx)
end
function return_cache(a::LinearCombinationFieldVector,x::$T)
cf = return_cache(a.fields,x)
fx = return_value(a.fields,x)
v = a.values
k = LinearCombinationMap(:)
ck = return_cache(k,v,fx)
cf, ck
end
function evaluate!(cache,a::LinearCombinationFieldVector,x::$T)
cf, ck = cache
fx = evaluate!(cf,a.fields,x)
v = a.values
k = LinearCombinationMap(:)
evaluate!(ck,k,v,fx)
end
end
end
for op in (:∇,:∇∇)
@eval begin
function evaluate!(cache,k::Broadcasting{typeof($op)},a::LinearCombinationFieldVector)
fields = Broadcasting($op)(a.fields)
LinearCombinationFieldVector(a.values,fields)
end
end
end
function get_children(n::TreeNode, a::LinearCombinationFieldVector)
(similar_tree_node(n,a.values),similar_tree_node(n,a.fields))
end
# This is the map that acts on values
struct LinearCombinationMap{T} <: Map
column::T
LinearCombinationMap(column::Integer) = new{typeof(column)}(column)
LinearCombinationMap(column::Colon) = new{typeof(column)}(column)
end
function evaluate!(cache,k::LinearCombinationMap{<:Integer},v::AbstractArray,fx::AbstractVector)
z = zero(return_type(outer,testitem(fx),testitem(v)))
@check length(fx) == size(v,1)
@inbounds for i in eachindex(fx)
# We need to do the product in this way
# so that the gradient also works
z += outer(fx[i],v[i,k.column])
end
z
end
function return_value(k::LinearCombinationMap{<:Integer},v::AbstractArray,fx::AbstractMatrix)
if size(fx,2) == size(v,1)
evaluate(k,v,fx)
else
c = return_cache(k,v,fx)
c.array
end
end
function return_value(k::LinearCombinationMap{<:Integer},v::AbstractVector,fx::AbstractVector)
Ta = eltype(v)
Tb = eltype(fx)
za = zero(Ta)
zb = zero(Tb)
zero( zb⊗za + zb⊗za )
end
function return_cache(k::LinearCombinationMap{<:Integer},v::AbstractArray,fx::AbstractMatrix)
vf = testitem(fx)
vv = testitem(v)
T = typeof( vf⊗vv + vf⊗vv )
r = zeros(T,size(fx,1))
CachedArray(r)
end
function evaluate!(cache,k::LinearCombinationMap{<:Integer},v::AbstractArray,fx::AbstractMatrix)
@check size(fx,2) == size(v,1)
setsize!(cache,(size(fx,1),))
r = cache.array
z = zero(eltype(r))
@inbounds for p in 1:size(fx,1)
rp = z
for i in 1:size(fx,2)
rp += outer(fx[p,i],v[i,k.column])
end
r[p] = rp
end
r
end
function evaluate!(cache,k::LinearCombinationMap{Colon},v::AbstractVector,fx::AbstractVector)
evaluate!(cache,LinearCombinationMap(1),v,fx)
end
function return_value(k::LinearCombinationMap{Colon},v::AbstractVector,fx::AbstractMatrix)
return_value(LinearCombinationMap(1),v,fx)
end
function return_value(k::LinearCombinationMap{Colon},v::AbstractVector,fx::AbstractVector)
return_value(LinearCombinationMap(1),v,fx)
end
function return_cache(k::LinearCombinationMap{Colon},v::AbstractVector,fx::AbstractMatrix)
return_cache(LinearCombinationMap(1),v,fx)
end
function evaluate!(cache,k::LinearCombinationMap{Colon},v::AbstractVector,fx::AbstractMatrix)
evaluate!(cache,LinearCombinationMap(1),v,fx)
end
function return_cache(k::LinearCombinationMap{Colon},v::AbstractMatrix,fx::AbstractVector)
vf = testitem(fx)
vv = testitem(v)
T = typeof( vf⊗vv + vf⊗vv )
r = zeros(T,size(v,2))
CachedArray(r)
end
function evaluate!(cache,k::LinearCombinationMap{Colon},v::AbstractMatrix,fx::AbstractVector)
@check length(fx) == size(v,1)
setsize!(cache,(size(v,2),))
r = cache.array
@inbounds for j in eachindex(r)
rj = zero(eltype(r))
for i in eachindex(fx)
rj += outer(fx[i],v[i,j])
end
r[j] = rj
end
r
end
function return_cache(k::LinearCombinationMap{Colon},v::AbstractMatrix,fx::AbstractMatrix)
vf = testitem(fx)
vv = testitem(v)
T = typeof( vf⊗vv + vf⊗vv )
r = zeros(T,size(fx,1),size(v,2))
CachedArray(r)
end
function evaluate!(cache,k::LinearCombinationMap{Colon},v::AbstractMatrix,fx::AbstractMatrix)
@check size(fx,2) == size(v,1)
setsize!(cache,(size(fx,1),size(v,2)))
r = cache.array
@inbounds for p in 1:size(fx,1)
for j in 1:size(r,2)
rj = zero(eltype(r))
for i in 1:size(fx,2)
rj += outer(fx[p,i],v[i,j])
end
r[p,j] = rj
end
end
r
end
# Optimizing transpose
testitem(a::Transpose{<:Field}) = testitem(a.parent)
evaluate!(cache,k::Broadcasting{typeof(∇)},a::Transpose{<:Field}) = transpose(k(a.parent))
evaluate!(cache,k::Broadcasting{typeof(∇∇)},a::Transpose{<:Field}) = transpose(k(a.parent))
return_cache(k::Transpose{<:Field},x::Point) = return_cache(k.parent,x)
evaluate!(cache,k::Transpose{<:Field},x::Point) = transpose(evaluate!(cache,k.parent,x))
return_cache(k::Transpose{<:Field},x::AbstractVector{<:Point}) = return_cache(k.parent,x)
function evaluate!(cache,k::Transpose{<:Field},x::AbstractVector{<:Point})
TransposeFieldIndices(evaluate!(cache,k.parent,x))
end
struct TransposeMap <: Map end
evaluate!(cache,k::TransposeMap,a::AbstractVector) = transpose(a)
evaluate!(cache,k::TransposeMap,a::AbstractMatrix) = TransposeFieldIndices(a)
"""
Given a matrix `np` x `nf1` x `nf2` result of the evaluation of a field vector
on a vector of points, it returns an array in which the field axes (second and
third axes) are permuted. It is equivalent as `Base.permutedims(A,(1,3,2))`
but more performant, since it does not involve allocations.
"""
struct TransposeFieldIndices{A,T} <: AbstractArray{T,3}
matrix::A
function TransposeFieldIndices(matrix::AbstractMatrix{T}) where T
A = typeof(matrix)
new{A,T}(matrix)
end
end
function TransposeFieldIndices{A,T}(::UndefInitializer,shape::NTuple{3,Integer}) where {A,T}
TransposeFieldIndices(similar(A,(shape[1],shape[3])))
end
Base.size(a::TransposeFieldIndices) = (size(a.matrix,1),1,size(a.matrix,2))
Base.axes(a::TransposeFieldIndices) = (axes(a.matrix,1),Base.OneTo(1),axes(a.matrix,2))
Base.IndexStyle(::Type{<:TransposeFieldIndices{A}}) where A = IndexStyle(A)
Base.getindex(a::TransposeFieldIndices,i::Integer,j::Integer,k::Integer) = a.matrix[i,k]
Base.getindex(a::TransposeFieldIndices,i::Integer) = a.matrix[i]
Base.setindex!(a::TransposeFieldIndices,v,i::Integer,j::Integer,k::Integer) = (a.matrix[i,k] = v)
Base.setindex!(a::TransposeFieldIndices,v,i::Integer) = (a.matrix[i] = v)
# Integration
"""
Integration of a given array of fields in the "physical" space
"""
function integrate(a::AbstractArray{<:Field},x::AbstractVector{<:Point},w::AbstractVector{<:Real})
cache = return_cache(integrate,a,x,w)
evaluate!(cache,integrate,a,x,w)
end
"""
Integration of a given array of fields in the "reference" space
"""
function integrate(a::AbstractArray{<:Field},q::AbstractVector{<:Point},w::AbstractVector{<:Real},j::Field)
cache = return_cache(integrate,a,q,w,j)
evaluate!(cache,integrate,a,q,w,j)
end
# Broadcast operations
function return_value(k::Broadcasting{<:Operation},args::Union{Field,AbstractArray{<:Field}}...)
BroadcastOpFieldArray(k.f.op,args...)
end
function evaluate!(cache,k::Broadcasting{<:Operation},args::Union{Field,AbstractArray{<:Field}}...)
BroadcastOpFieldArray(k.f.op,args...)
end
"""
Type that represents a broadcast operation over a set of `AbstractArray{<:Field}`.
The result is a sub-type of `AbstractArray{<:Field}`
"""
struct BroadcastOpFieldArray{O,T,N,A} <: AbstractArray{T,N}
op::O
args::A
function BroadcastOpFieldArray(op,args::Union{Field,AbstractArray{<:Field}}...)
fs = map(testitem,args)
T = return_type(Operation(op),fs...)
s = map(size,args)
bs = Base.Broadcast.broadcast_shape(s...)
N = length(bs)
A = typeof(args)
O = typeof(op)
new{O,T,N,A}(op,args)
end
end
Base.size(a::BroadcastOpFieldArray) = Base.Broadcast.broadcast_shape(map(size,a.args)...)
Base.axes(a::BroadcastOpFieldArray) = Base.Broadcast.broadcast_shape(map(axes,a.args)...)
Base.IndexStyle(::Type{<:BroadcastOpFieldArray}) = IndexLinear()
Base.getindex(a::BroadcastOpFieldArray,i::Integer) = broadcast(Operation(a.op),a.args...)[i]
function testitem(a::BroadcastOpFieldArray)
fs = map(testitem,a.args)
return_value(Operation(a.op),fs...)
end
for T in (:(Point),:(AbstractArray{<:Point}))
@eval begin
function return_cache(f::BroadcastOpFieldArray,x::$T)
cfs = map(fi -> return_cache(fi,x),f.args)
rs = map(fi -> return_value(fi,x),f.args)
bm = BroadcastingFieldOpMap(f.op)
r = return_cache(bm,rs...)
r, cfs
end
function evaluate!(c,f::BroadcastOpFieldArray,x::$T)
r, cfs = c
rs = map((ci,fi) -> evaluate!(ci,fi,x),cfs,f.args)
bm = BroadcastingFieldOpMap(f.op)
evaluate!(r,bm,rs...)
end
end
end
# With this type we mark that we are doing Broadcasting(op) on the result of evaluating Fields/FieldArrays
# This allow us to do some optimizations for block arrays that are only true in this context, not in a
# general Broadcasting operation.
struct BroadcastingFieldOpMap{F} <: Map
op::F
end
return_value(a::BroadcastingFieldOpMap,args...) = return_value(Broadcasting(a.op),args...)
return_cache(a::BroadcastingFieldOpMap,args...) = return_cache(Broadcasting(a.op),args...)
evaluate!(cache,a::BroadcastingFieldOpMap,args...) = evaluate!(cache,Broadcasting(a.op),args...)
return_value(a::BroadcastingFieldOpMap,args::AbstractArray...) = return_value(Broadcasting(a.op),args...)
return_cache(a::BroadcastingFieldOpMap,args::AbstractArray...) = return_cache(Broadcasting(a.op),args...)
evaluate!(cache,a::BroadcastingFieldOpMap,args::AbstractArray...) = evaluate!(cache,Broadcasting(a.op),args...)
# Follow optimizations are very important to achieve performance
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
a::AbstractArray{T,N},
b::AbstractArray{S,N}) where {T,S,N}
@check size(a) == size(b) || (length(a)==0 && length(b)==0)
setsize!(cache,size(a))
r = cache.array
for i in eachindex(a)
r[i] = f.op(a[i],b[i])
end
r
end
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
a::AbstractMatrix,
b::AbstractArray{S,3} where S)
@check size(a,1) == size(b,1)
@check size(b,2) == 1 || size(b,1) == 0
np, ni = size(a)
nj = size(b,3)
setsize!(cache,(np,ni,nj))
r = cache.array
for j in 1:nj
for p in 1:np
bpj = b[p,1,j]
for i in 1:ni
r[p,i,j] = f.op(a[p,i],bpj)
end
end
end
r
end
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
b::AbstractArray{S,3} where S,
a::AbstractMatrix)
@check size(a,1) == size(b,1)
@check size(b,2) == 1 || size(b,1) == 0
np, ni = size(a)
nj = size(b,3)
setsize!(cache,(np,ni,nj))
r = cache.array
for p in 1:np
for j in 1:nj
bpj = b[p,1,j]
for i in 1:ni
r[p,i,j] = f.op(bpj,a[p,i])
end
end
end
r
end
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
a::AbstractVector,
b::AbstractMatrix)
@check size(a,1) == size(b,1)
np, ni = size(b)
setsize!(cache,(np,ni))
r = cache.array
for p in 1:np
ap = a[p]
for i in 1:ni
r[p,i] = f.op(ap,b[p,i])
end
end
r
end
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
b::AbstractMatrix,
a::AbstractVector)
@check size(a,1) == size(b,1)
np, ni = size(b)
setsize!(cache,(np,ni))
r = cache.array
for p in 1:np
ap = a[p]
for i in 1:ni
r[p,i] = f.op(b[p,i],ap)
end
end
r
end
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
a::AbstractVector,
b::AbstractArray{S,3} where S)
@check size(a,1) == size(b,1)
np, ni, nj = size(b)
setsize!(cache,(np,ni,nj))
r = cache.array
for p in 1:np
ap = a[p]
for j in 1:nj
for i in 1:ni
r[p,i,j] = f.op(ap,b[p,i,j])
end
end
end
r
end
function evaluate!(
cache,
f::BroadcastingFieldOpMap,
b::AbstractArray{S,3} where S,
a::AbstractVector)
@check size(a,1) == size(b,1)
np, ni, nj = size(b)
setsize!(cache,(np,ni,nj))
r = cache.array
for p in 1:np
ap = a[p]
for j in 1:nj
for i in 1:ni
r[p,i,j] = f.op(b[p,i,j],ap)
end
end
end
r
end
# Gradient of the sum
for op in (:+,:-)
@eval begin
function evaluate!(cache,::Broadcasting{typeof(∇)},a::BroadcastOpFieldArray{typeof($op)})
f = a.args
g = map( Broadcasting(∇), f)
Broadcasting(Operation($op))(g...)
end
end
end
# Gradient of the product
for op in (:*,:⋅,:⊙,:⊗)
@eval begin
function evaluate!(cache,::Broadcasting{typeof(∇)},a::BroadcastOpFieldArray{typeof($op)})
f = a.args
@notimplementedif length(f) != 2
f1, f2 = f
g1, g2 = map(Broadcasting(∇), f)
k(F1,F2,G1,G2) = product_rule($op,F1,F2,G1,G2)
Broadcasting(Operation(k))(f1,f2,g1,g2)
end
end
end