-
Notifications
You must be signed in to change notification settings - Fork 147
/
arrays.jl
1158 lines (970 loc) · 33.4 KB
/
arrays.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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using SymbolicUtils
using SymbolicUtils: @capture
using StaticArraysCore
import Base: eltype, length, ndims, size, axes, eachindex
export @arrayop, ArrayMaker, @makearray, @setview, @setview!
### Store Shape as a metadata in Term{<:AbstractArray} objects
struct ArrayShapeCtx end
#=
There are 2 types of array terms:
`ArrayOp{T<:AbstractArray}` and `Term{<:AbstractArray}`
- ArrayOp represents a Einstein-notation-inspired array operation.
it contains a field `term` which is a `Term` that represents the
operation that resulted in the `ArrayOp`.
I.e. will be `A*b` for the operation `(i,) => A[i,j] * b[j]` for example.
It can be `nothing` if not known.
- calling `shape` on an `ArrayOp` will return the shape of the array or `Unknown()`
- do not rely on the `symtype` or `shape` information of the `.term` when looking at an `ArrayOp`.
call `shape`, `symtype` and `ndims` directly on the `ArrayOp`.
- `Term{<:AbstractArray}`
- calling `shape` on it will return the shape of the array or `Unknown()`, and uses
the `ArrayShapeCtx` metadata context to store this.
The Array type parameter must contain the dimension.
=#
#### ArrayOp ####
"""
ArrayOp(output_idx, expr, reduce)
A tensor expression where `output_idx` are the output indices
`expr`, is the tensor expression and `reduce` is the function
used to reduce over contracted dimensions.
"""
struct ArrayOp{T<:AbstractArray} <: Symbolic{T}
output_idx # output indices
expr # Used in pattern matching
# Useful to infer eltype
reduce
term
shape
ranges::Dict{BasicSymbolic, AbstractRange} # index range each index symbol can take,
# optional for each symbol
metadata
end
function ArrayOp(T::Type, output_idx, expr, reduce, term, ranges=Dict(); metadata=nothing)
sh = make_shape(output_idx, unwrap(expr), ranges)
ArrayOp{T}(output_idx, unwrap(expr), reduce, term, sh, ranges, metadata)
end
function ArrayOp(a::AbstractArray)
i = makesubscripts(ndims(a))
# TODO: formalize symtype(::Type) then!
ArrayOp(symtype(a), (i...,), a[i...], +, a)
end
ConstructionBase.constructorof(s::Type{<:ArrayOp{T}}) where {T} = ArrayOp{T}
function SymbolicUtils.maketerm(::Type{<:ArrayOp}, f, args, _symtype, m)
t = f(args...)
t isa Symbolic && !isnothing(m) ?
metadata(t, m) : t
end
shape(aop::ArrayOp) = aop.shape
const show_arrayop = Ref{Bool}(false)
function Base.show(io::IO, aop::ArrayOp)
if iscall(aop.term) && !show_arrayop[]
show(io, aop.term)
else
print(io, "@arrayop")
print(io, "(_[$(join(string.(aop.output_idx), ","))] := $(aop.expr))")
if aop.reduce != +
print(io, " ($(aop.reduce))")
end
if !isempty(aop.ranges)
print(io, " ", join(["$k in $v" for (k, v) in aop.ranges], ", "))
end
end
end
Base.summary(io::IO, aop::ArrayOp) = Base.array_summary(io, aop, shape(aop))
function Base.showarg(io::IO, aop::ArrayOp, toplevel)
show(io, aop)
toplevel && print(io, "::", typeof(aop))
return nothing
end
symtype(a::ArrayOp{T}) where {T} = T
iscall(a::ArrayOp) = true
function operation(a::ArrayOp)
isnothing(a.term) ? typeof(a) : operation(a.term)
end
function arguments(a::ArrayOp)
isnothing(a.term) ? [a.output_idx, a.expr, a.reduce,
a.term, a.shape, a.ranges, metadata(a)] :
arguments(a.term)
end
function Base.isequal(a::ArrayOp, b::ArrayOp)
a === b && return true
isequal(a.shape, b.shape) &&
isequal(a.ranges, b.ranges) &&
isequal(a.output_idx, b.output_idx) &&
isequal(a.reduce, b.reduce) &&
isequal(operation(a), operation(b)) &&
isequal(a.expr, b.expr)
end
function Base.hash(a::ArrayOp, u::UInt)
hash(a.shape, hash(a.ranges, hash(a.expr, hash(a.output_idx, hash(operation(a), u)))))
end
macro arrayop(output_idx, expr, options...)
rs = []
reduce = +
call = nothing
extra = []
isexpr = MacroTools.isexpr
for o in options
if isexpr(o, :call) && o.args[1] == :in
push!(rs, :($(o.args[2]) => $(o.args[3])))
elseif isexpr(o, :(=)) && o.args[1] == :reduce
reduce = o.args[2]
elseif isexpr(o, :(=)) && o.args[1] == :term
call = o.args[2]
else
push!(extra, o)
end
end
if length(extra) == 1
@warn("@arrayop <call> <idx> <expr> is deprecated, use @arrayop <idx> <expr> term=<call> instead")
call = output_idx
output_idx = expr
expr = extra[1]
end
@assert output_idx.head == :tuple
oidxs = filter(x->x isa Symbol, output_idx.args)
iidxs = find_indices(expr)
idxs = union(oidxs, iidxs)
fbody = call2term(deepcopy(expr))
oftype(x,T) = :($x::$T)
aop = gensym("aop")
quote
let
@syms $(map(x->oftype(x, Int), idxs)...)
expr = $fbody
#TODO: proper Atype
$ArrayOp(Array{$symtype(expr),
$(length(output_idx.args))},
$output_idx,
expr,
$reduce,
$(call2term(call)),
Dict($(rs...)))
end
end |> esc
end
const SymArray = Union{ArrayOp, Symbolic{<:AbstractArray}}
const SymMat = Union{ArrayOp{<:AbstractMatrix}, Symbolic{<:AbstractMatrix}}
const SymVec = Union{ArrayOp{<:AbstractVector}, Symbolic{<:AbstractVector}}
### Propagate ###
#
## Shape ##
function axis_in(a, b)
first(a) >= first(b) && last(a) <= last(b)
end
function make_shape(output_idx, expr, ranges=Dict())
matches = idx_to_axes(expr)
for (sym, ms) in matches
to_check = filter(m->!(shape(m.A) isa Unknown), ms)
# Only check known dimensions. It may be "known symbolically"
isempty(to_check) && continue
restricted = false
if haskey(ranges, sym)
ref_axis = ranges[sym]
restricted = true
else
ref_axis = axes(first(to_check).A, first(to_check).dim)
end
reference = ref_axis
for i in (restricted ? 1 : 2):length(ms)
m = ms[i]
s=shape(m.A)
if s !== Unknown()
if restricted
if !axis_in(ref_axis, axes(m.A, m.dim))
throw(DimensionMismatch("expected $(ref_axis) to be within axes($(m.A), $(m.dim))"))
end
elseif !isequal(axes(m.A, m.dim), reference)
throw(DimensionMismatch("expected axes($(m.A), $(m.dim)) = $(reference)"))
end
end
end
end
sz = map(output_idx) do i
if issym(i)
if haskey(ranges, i)
return axes(ranges[i], 1)
end
if !haskey(matches, i)
error("There was an error processing arrayop expression $expr.\n" *
"Dimension of output index $i in $output_idx could not be inferred")
end
mi = matches[i]
@assert !isempty(mi)
ext = get_extents(mi)
ext isa Unknown && return Unknown()
return Base.OneTo(length(ext))
elseif i isa Integer
return Base.OneTo(1)
end
end
# TODO: maybe we can remove this restriction?
if any(x->x isa Unknown, sz)
Unknown()
else
sz
end
end
function ranges(a::ArrayOp)
rs = Dict{BasicSymbolic, Any}()
ax = idx_to_axes(a.expr)
for i in keys(ax)
if haskey(a.ranges, i)
rs[i] = a.ranges[i]
else
rs[i] = ax[i] #get_extents(ax[i])
end
end
return rs
end
## Eltype ##
eltype(aop::ArrayOp) = symtype(aop.expr)
## Ndims ##
function ndims(aop::ArrayOp)
length(aop.output_idx)
end
### Utils ###
# turn `f(x...)` into `term(f, x...)`
#
function call2term(expr, arrs=[])
!(expr isa Expr) && return :($unwrap($expr))
if expr.head == :call
if expr.args[1] == :(:)
return expr
end
return Expr(:call, term, map(call2term, expr.args)...)
elseif expr.head == :ref
return Expr(:ref, call2term(expr.args[1]), expr.args[2:end]...)
elseif expr.head == Symbol("'")
return Expr(:call, term, adjoint, map(call2term, expr.args)...)
end
return Expr(expr.head, map(call2term, expr.args)...)
end
# Find all symbolic indices in expr
function find_indices(expr, idxs=[])
!(expr isa Expr) && return idxs
if expr.head == :ref
return append!(idxs, filter(x->x isa Symbol, expr.args[2:end]))
elseif expr.head == :call && expr.args[1] == :getindex || expr.args[1] == getindex
return append!(idxs, filter(x->x isa Symbol, expr.args[3:end]))
else
foreach(x->find_indices(x, idxs), expr.args)
return idxs
end
end
struct AxisOf
A
dim
boundary
end
function Base.get(a::AxisOf)
@oops shape(a.A)
axes(a.A, a.dim)
end
function get_extents(xs)
boundaries = map(x->x.boundary, xs)
if all(iszero∘wrap, boundaries)
get(first(xs))
else
ii = findfirst(x->issym(x) || iscall(x), boundaries)
if !isnothing(ii)
error("Could not find the boundary from symbolic index $(xs[ii]). Please manually specify the range of indices.")
end
extent = get(first(xs))
start_offset = -reduce(min, filter(x->x<0, boundaries), init=0)
end_offset = reduce(max, filter(x->x>0, boundaries), init=0)
(first(extent) + start_offset):(last(extent) - end_offset)
end
end
get_extents(x::AbstractRange) = x
## Walk expr looking for symbols used in getindex expressions
# Returns a dictionary of Sym to a vector of AxisOf objects.
# The vector has as many elements as the number of times the symbol
# appears in the expr. AxisOf has three fields:
# A: the array whose indexing it appears in
# dim: The dimension of the array indexed
# boundary: how much padding is this indexing requiring, for example
# boundary is 2 for x[i + 2], and boundary = -2 for x[i - 2]
function idx_to_axes(expr, dict=Dict{Any, Vector}(), ranges=Dict())
if iscall(expr)
if operation(expr) === (getindex)
args = arguments(expr)
for (axis, idx_expr) in enumerate(@views args[2:end])
if issym(idx_expr) || iscall(idx_expr)
vs = get_variables(idx_expr)
isempty(vs) && continue
sym = only(get_variables(idx_expr))
axesvec = Base.get!(() -> [], dict, sym)
push!(axesvec, AxisOf(first(args), axis, idx_expr - sym))
end
end
else
idx_to_axes(operation(expr), dict)
foreach(ex->idx_to_axes(ex, dict), arguments(expr))
end
end
dict
end
#### Term{<:AbstractArray}
#
"""
array_term(f, args...;
container_type = propagate_atype(f, args...),
eltype = propagate_eltype(f, args...),
size = map(length, propagate_shape(f, args...)),
ndims = propagate_ndims(f, args...))
Create a term of `Term{<: AbstractArray}` which
is the representation of `f(args...)`.
Default arguments:
- `container_type=propagate_atype(f, args...)` - the container type,
i.e. `Array` or `StaticArray` etc.
- `eltype=propagate_eltype(f, args...)` - the output element type.
- `size=map(length, propagate_shape(f, args...))` - the
output array size. `propagate_shape` returns a tuple of index ranges.
- `ndims=propagate_ndims(f, args...)` the output dimension.
`propagate_shape`, `propagate_atype`, `propagate_eltype` may
return `Unknown()` to say that the output cannot be determined
"""
function array_term(f, args...;
container_type = propagate_atype(f, args...),
eltype = propagate_eltype(f, args...),
size = Unknown(),
ndims = size !== Unknown() ? length(size) : propagate_ndims(f, args...),
shape = size !== Unknown() ? Tuple(map(x->1:x, size)) : propagate_shape(f, args...))
if container_type == Unknown()
# There's always a fallback for this
container_type = propagate_atype(f, args...)
end
if eltype == Unknown()
eltype = Base.propagate_eltype(container_type)
end
if ndims == Unknown()
ndims = if shape == Unknown()
Any
else
length(shape)
end
end
S = container_type{eltype, ndims}
setmetadata(Term{S}(f, Any[args...]), ArrayShapeCtx, shape)
end
"""
shape(s::Any)
Returns `axes(s)` or Unknown().
"""
shape(s) = axes(s)
"""
shape(s::SymArray)
Extract the shape metadata from a SymArray.
If not known, returns `Unknown()`
"""
function shape(s::Symbolic{<:AbstractArray})
if hasmetadata(s, ArrayShapeCtx)
getmetadata(s, ArrayShapeCtx)
else
Unknown()
end
end
## `propagate_` interface:
# used in the `array_term` construction.
atype(::Type{<:Array}) = Array
atype(::Type{<:SArray}) = SArray
atype(::Type) = AbstractArray
_propagate_atype(::Type{T}, ::Type{T}) where {T} = T
_propagate_atype(::Type{<:Array}, ::Type{<:SArray}) = Array
_propagate_atype(::Type{<:SArray}, ::Type{<:Array}) = Array
_propagate_atype(::Any, ::Any) = AbstractArray
_propagate_atype(T) = T
_propagate_atype() = AbstractArray
function propagate_atype(f, args...)
As = [atype(symtype(T))
for T in Iterators.filter(x->x <: Symbolic{<:AbstractArray}, typeof.(args))]
if length(As) <= 1
_propagate_atype(As...)
else
foldl(_propagate_atype, As)
end
end
function propagate_eltype(f, args...)
As = [eltype(symtype(T))
for T in Iterators.filter(x->symtype(x) <: AbstractArray, args)]
promote_type(As...)
end
function propagate_ndims(f, args...)
if propagate_shape(f, args...) == Unknown()
error("Could not determine the output dimension of $f$args")
else
length(propagate_shape(f, args...))
end
end
function propagate_shape(f, args...)
error("Don't know how to propagate shape for $f$args")
end
### Wrapper type for dispatch
@symbolic_wrap struct Arr{T,N} <: AbstractArray{T, N}
value
end
Base.hash(x::Arr, u::UInt) = hash(unwrap(x), u)
Base.isequal(a::Arr, b::Arr) = isequal(unwrap(a), unwrap(b))
Base.isequal(a::Arr, b::Symbolic) = isequal(unwrap(a), b)
Base.isequal(a::Symbolic, b::Arr) = isequal(b, a)
ArrayOp(x::Arr) = unwrap(x)
function Arr(x)
A = symtype(x)
@assert A <: AbstractArray
Arr{maybewrap(eltype(A)), ndims(A)}(x)
end
const ArrayLike{T,N} = Union{
ArrayOp{AbstractArray{T,N}},
Symbolic{AbstractArray{T,N}},
Arr{T,N},
SymbolicUtils.Term{AbstractArray{T, N}}
} # Like SymArray but includes Arr and Term{Arr}
unwrap(x::Arr) = x.value
maybewrap(T) = has_symwrapper(T) ? wrapper_type(T) : T
# These methods allow @wrapped methods to be more specific and not overwrite
# each other when defined both for matrix and vector
wrapper_type(::Type{<:AbstractMatrix}) = Arr{<:Any, 2}
wrapper_type(::Type{<:AbstractMatrix{T}}) where {T} = Arr{maybewrap(T), 2}
wrapper_type(::Type{<:AbstractVector}) = Arr{<:Any, 1}
wrapper_type(::Type{<:AbstractVector{T}}) where {T} = Arr{maybewrap(T), 1}
function Base.show(io::IO, arr::Arr)
x = unwrap(arr)
iscall(x) && print(io, "(")
print(io, unwrap(arr))
iscall(x) && print(io, ")")
if !(shape(x) isa Unknown)
print(io, "[", join(string.(axes(arr)), ","), "]")
end
end
Base.show(io::IO, ::MIME"text/plain", arr::Arr) = show(io, arr)
################# Base array functions
#
# basic
# these methods are not symbolic but work if we know this info.
geteltype(s::SymArray) = geteltype(symtype(s))
geteltype(::Type{<:AbstractArray{T}}) where {T} = T
geteltype(::Type{<:AbstractArray}) = Unknown()
ndims(s::SymArray) = ndims(symtype(s))
ndims(::Type{<:Arr{<:Any, N}}) where N = N
function eltype(A::Union{Arr, SymArray})
T = geteltype(unwrap(A))
T === Unknown() && error("eltype of $A not known")
return T
end
function length(A::Union{Arr, SymArray})
s = shape(unwrap(A))
s === Unknown() && error("length of $A not known")
return prod(length, s)
end
function size(A::Union{Arr, SymArray})
s = shape(unwrap(A))
s === Unknown() && error("size of $A not known")
return length.(s)
end
function size(A::SymArray, i::Integer)
@assert(i > 0)
i > ndims(A) ? 1 : size(A)[i]
end
function axes(A::Union{Arr, SymArray})
s = shape(unwrap(A))
s === Unknown() && error("axes of $A not known")
return s
end
function axes(A::SymArray, i)
s = shape(A)
s === Unknown() && error("axes of $A not known")
return i <= length(s) ? s[i] : Base.OneTo(1)
end
function eachindex(A::Union{Arr, SymArray})
s = shape(unwrap(A))
s === Unknown() && error("eachindex of $A not known")
return CartesianIndices(s)
end
function get_variables!(vars, e::Arr, varlist=nothing)
foreach(x -> get_variables!(vars, x, varlist), collect(e))
vars
end
### Scalarize
scalarize(a::Array) = map(scalarize, a)
scalarize(term::Symbolic{<:AbstractArray}, idx) = term[idx...]
val2num(::Val{n}) where n = n
function replace_by_scalarizing(ex, dict)
rule = @rule(getindex(~x, ~~i) =>
scalarize(~x, (map(j->substitute(j, dict), ~~i)...,)))
function rewrite_operation(x)
if iscall(x) && iscall(operation(x))
f = operation(x)
ff = replace_by_scalarizing(f, dict)
if metadata(x) !== nothing
maketerm(typeof(x), ff, arguments(x), symtype(x), metadata(x))
else
ff(arguments(x)...)
end
else
nothing
end
end
prewalk_if(x->!(x isa ArrayOp || x isa ArrayMaker),
Rewriters.PassThrough(Chain([rewrite_operation, rule])),
ex)
end
function prewalk_if(cond, f, t)
t′ = cond(t) ? f(t) : return t
if iscall(t′)
if metadata(t′) !== nothing
return maketerm(typeof(t′), TermInterface.head(t′),
map(x->prewalk_if(cond, f, x), children(t′)), symtype(t′), metadata(t′))
else
TermInterface.head(t′)(map(x->prewalk_if(cond, f, x), children(t′))...)
end
else
return t′
end
end
function scalarize(arr::AbstractArray, idx)
arr[idx...]
end
function scalarize(arr, idx)
if iscall(arr)
scalarize_op(operation(arr), arr, idx)
else
error("scalarize is not defined for $arr at idx=$idx")
end
end
scalarize_op(f, arr) = arr
struct ScalarizeCache end
function scalarize_op(f, arr, idx)
if hasmetadata(arr, ScalarizeCache) && getmetadata(arr, ScalarizeCache)[] !== nothing
getmetadata(arr, ScalarizeCache)[][idx...]
else
# wrap and unwrap to call generic methods
thing = unwrap(f(scalarize.(map(wrap, arguments(arr)))...))
if metadata(arr) != nothing
# forward any metadata
try
thing = metadata(thing, metadata(arr))
catch err
@warn "could not attach metadata of subexpression $arr to the scalarized form at idx"
end
end
if !hasmetadata(arr, ScalarizeCache)
arr = setmetadata(arr, ScalarizeCache, Ref{Any}(nothing))
end
getmetadata(arr, ScalarizeCache)[] = thing
thing[idx...]
end
end
@wrapped function Base.:(\)(A::AbstractMatrix, b::AbstractVecOrMat)
t = array_term(\, A, b)
setmetadata(t, ScalarizeCache, Ref{Any}(nothing))
end
@wrapped function Base.inv(A::AbstractMatrix)
t = array_term(inv, A)
setmetadata(t, ScalarizeCache, Ref{Any}(nothing))
end
_det(x, lp) = det(x, laplace=lp)
function scalarize_op(f::typeof(_det), arr)
unwrap(det(map(wrap, collect(arguments(arr)[1])), laplace=arguments(arr)[2]))
end
@wrapped function LinearAlgebra.det(x::AbstractMatrix; laplace=true)
Term{eltype(x)}(_det, [x, laplace])
end
# A * x = b
# A ∈ R^(m x n) x ∈ R^(n, k) = b ∈ R^(m, k)
propagate_ndims(::typeof(\), A, b) = ndims(b)
propagate_ndims(::typeof(inv), A) = ndims(A)
# A(m,k) * B(k,n) = C(m,n)
# A(m,k) \ C(m,n) = B(k,n)
function propagate_shape(::typeof(\), A, b)
if ndims(b) == 1
(axes(A,2),)
else
(axes(A,2), axes(b, 2))
end
end
function propagate_shape(::typeof(inv), A)
@oops shp = shape(A)
@assert ndims(A) == 2 && reverse(shp) == shp "Inv called on a non-square matrix"
shp
end
function scalarize(arr::ArrayOp, idx)
@assert length(arr.output_idx) == length(idx)
axs = ranges(arr)
iidx = collect(keys(axs))
contracted = setdiff(iidx, arr.output_idx)
axes = [get_extents(axs[c]) for c in contracted]
summed = if isempty(contracted)
arr.expr
else
mapreduce(arr.reduce, Iterators.product(axes...)) do idx
replace_by_scalarizing(arr.expr, Dict(contracted .=> idx))
end
end
dict = Dict(oi => (unwrap(i) isa Symbolic ? unwrap(i) : get_extents(axs[oi])[i])
for (oi, i) in zip(arr.output_idx, idx) if unwrap(oi) isa Symbolic)
replace_by_scalarizing(summed, dict)
end
scalarize(arr::Arr, idx) = wrap(scalarize(unwrap(arr),
unwrap.(idx)))
eval_array_term(op, arr) = arr
eval_array_term(op::typeof(inv), arr) = inv(scalarize(wrap(arguments(arr)[1]))) #issue 653
eval_array_term(op::Arr) = wrap(eval_array_term(unwrap(op)))
eval_array_term(op) = eval_array_term(operation(op), op)
function scalarize(arr)
if arr isa Arr || arr isa Symbolic{<:AbstractArray}
if iscall(arr)
arr = eval_array_term(arr)
end
map(Iterators.product(axes(arr)...)) do i
scalarize(arr[i...]) # Use arr[i...] here to trigger any getindex hooks
end
elseif iscall(arr) && operation(arr) == getindex
args = arguments(arr)
scalarize(args[1], (args[2:end]...,))
elseif arr isa Num
wrap(scalarize(unwrap(arr)))
elseif iscall(arr) && symtype(arr) <: Number
t = maketerm(typeof(arr), operation(arr), map(scalarize, arguments(arr)), symtype(arr), metadata(arr))
iscall(t) ? scalarize_op(operation(t), t) : t
else
arr
end
end
@wrapped Base.isempty(x::AbstractArray) = shape(unwrap(x)) !== Unknown() && _iszero(length(x))
Base.collect(x::Arr) = scalarize(x)
Base.collect(x::SymArray) = scalarize(x)
isarraysymbolic(x) = unwrap(x) isa Symbolic && SymbolicUtils.symtype(unwrap(x)) <: AbstractArray
Base.convert(::Type{<:Array{<:Any, N}}, arr::Arr{<:Any, N}) where {N} = scalarize(arr)
### Stencils
struct ArrayMaker{T, AT<:AbstractArray} <: Symbolic{AT}
shape
sequence
metadata
end
function ArrayMaker(a::ArrayLike; eltype=eltype(a))
ArrayMaker{eltype}(size(a), Any[axes(a) => a])
end
function arraymaker(T, shape, views, seq...)
ArrayMaker{T}(shape, [(views .=> seq)...], nothing)
end
iscall(x::ArrayMaker) = true
operation(x::ArrayMaker) = arraymaker
arguments(x::ArrayMaker) = [eltype(x), shape(x), map(first, x.sequence), map(last, x.sequence)...]
shape(am::ArrayMaker) = am.shape
function ArrayMaker{T}(sz::NTuple{N, Integer}, seq::Array=[]; atype=Array, metadata=nothing) where {N,T}
ArrayMaker{T, atype{T, N}}(map(x->1:x, sz), seq, metadata)
end
(::Type{ArrayMaker{T}})(i::Int...; atype=Array) where {T} = ArrayMaker{T}(i, atype=atype)
function Base.show(io::IO, ac::ArrayMaker)
print(io, Expr(:call, :ArrayMaker, ac.shape,
Expr(:block, ac.sequence...)))
end
function get_indexers(ex)
@assert ex.head == :ref
arr = ex.args[1]
args = map(((i,x),)->x == Symbol(":") ? :(1:lastindex($arr, $i)) : x, enumerate(ex.args[2:end]))
replace_ends(arr, args)
end
function search_and_replace(expr, key, val)
isequal(expr, key) && return val
expr isa Expr ?
Expr(expr.head, map(x->search_and_replace(x, key,val), expr.args)...) :
expr
end
function replace_ends(arr, idx)
[search_and_replace(ix, :end, :(lastindex($arr, $i)))
for (i, ix) in enumerate(idx)]
end
macro setview!(definition, arrayop)
setview(definition, arrayop, true)
end
macro setview(definition, arrayop)
setview(definition, arrayop, false)
end
output_index_ranges(c::CartesianIndices) = c.indices
output_index_ranges(ix...) = ix
function setview(definition, arrayop, inplace)
output_view = get_indexers(definition)
output_ref = definition.args[1]
function check_assignment(vw, op)
try Base.Broadcast.broadcast_shape(map(length, vw), size(op))
catch err
if err isa DimensionMismatch
throw(DimensionMismatch("setview did not work while assigning indices " *
"$vw to $op. LHS has size $(map(length, vw)) "*
"and RHS has size $(size(op)) " *
"-- they need to be broadcastable."))
else
rethrow(err)
end
end
end
function push(inplace)
if inplace
function (am, vw, op)
check_assignment(vw, op)
# assert proper size match
push!(am.sequence, vw => op)
am
end
else
function (am, vw, op)
check_assignment(vw, op)
if am isa ArrayMaker
typeof(am)(am.shape, vcat(am.sequence, vw => op))
else
am = ArrayMaker(am)
push!(am.sequence, vw => op)
am
end
end
end
end
quote
$(push(inplace))($output_ref,
$output_index_ranges($(output_view...)), $unwrap($arrayop))
end |> esc
end
macro makearray(definition, sequence)
output_shape = get_indexers(definition)
output_name = definition.args[1]
seq = map(filter(x->!(x isa LineNumberNode), sequence.args)) do pair
@assert pair.head == :call && pair.args[1] == :(=>)
# TODO: make sure the same symbol is used for the lhs array
:(@setview! $(pair.args[2]) $(pair.args[3]))
end
quote
$output_name = $ArrayMaker{Real}(map(length, ($(output_shape...),)))
$(seq...)
$output_name = $wrap($output_name)
end |> esc
end
function best_order(output_idx, ks, rs)
unique!(filter(issym, vcat(reverse(output_idx)..., collect(ks))))
end
function _cat(x, xs...; dims)
arrays = (x, xs...)
if dims isa Integer
sz = Base.cat_size_shape(Base.dims2cat(dims), arrays...)
T = reduce(promote_type, eltype.(xs), init=eltype(x))
newdim = cumsum(map(a->size(a, dims), arrays))
start = 1
A = ArrayMaker{T}(sz...)
for (dim, array) in zip(newdim, arrays)
idx = CartesianIndices(ntuple(n -> n==dims ?
(start:dim) : (1:sz[n]), length(sz)))
start = dim + 1
@setview! A[idx] array
end
return A
else
error("Block diagonal concatenation not supported")
end
end
# Base.cat(x::Arr, xs...; dims) = _cat(x, xs...; dims)
# Base.cat(x::AbstractArray, y::Arr, xs...; dims) = _cat(x, y, xs...; dims)
# vv uncomment these for a major release
# Base.vcat(x::Arr, xs::AbstractVecOrMat...) = cat(x, xs..., dims=1)
# Base.hcat(x::Arr, xs::AbstractVecOrMat...) = cat(x, xs..., dims=2)
# Base.vcat(x::AbstractVecOrMat, y::Arr, xs::AbstractVecOrMat...) = _cat(x, y, xs..., dims=1)
# Base.hcat(x::AbstractVecOrMat, y::Arr, xs::AbstractVecOrMat...) = _cat(x, y, xs..., dims=2)
# Base.vcat(x::Arr, y::Arr) = _cat(x, y, dims=1) # plug ambiguity
# Base.hcat(x::Arr, y::Arr) = _cat(x, y, dims=2)
function scalarize(x::ArrayMaker)
T = eltype(x)
A = Array{wrapper_type(T)}(undef, size(x))
for (vw, arr) in x.sequence
if any(x->x isa AbstractArray, vw)
A[vw...] .= scalarize(arr)
else
A[vw...] = scalarize(arr)
end
end
A
end
function scalarize(x::ArrayMaker, idx)
for (vw, arr) in reverse(x.sequence) # last one wins
if any(x->issym(x) || iscall(x), idx)
return term(getindex, x, idx...)
end
if all(in.(idx, vw))
if symtype(arr) <: AbstractArray
# Filter out non-array indices because the RHS will be one dim less
el = [searchsortedfirst(v, i)
for (v, i) in zip(vw, idx) if v isa AbstractArray]
return scalarize(arr[el...])
else
return arr
end
end
end
if !any(x->issym(x) || iscall(x), idx) && all(in.(idx, axes(x)))
throw(UndefRefError())
end
throw(BoundsError(x, idx))
end
### Codegen
function SymbolicUtils.Code.toexpr(x::ArrayOp, st)
haskey(st.symbolify, x) && return st.symbolify[x]
if iscall(x.term)
toexpr(x.term, st)
else
_array_toexpr(x, st)
end
end
function SymbolicUtils.Code.toexpr(x::Arr, st)
toexpr(unwrap(x), st)
end
function SymbolicUtils.Code.toexpr(x::ArrayMaker, st)
_array_toexpr(x, st)
end
function _array_toexpr(x, st)
outsym = Symbol("_out")
N = length(shape(x))
ex = :(let $outsym = zeros(Float64, map(length, ($(shape(x)...),)))
$(inplace_expr(x, outsym))
$outsym
end) |> LiteralExpr
toexpr(ex, st)
end
function inplace_expr(x, out_array, dict=nothing)
x = unwrap(x)
if symtype(x) <: Number
:($out_array .= $x)
else
:($copy!($out_array, $x))
end