|
90 | 90 | end
|
91 | 91 | end
|
92 | 92 |
|
| 93 | +@generated function reduce{D}(op, a::StaticArray, ::Type{Val{D}}) |
| 94 | + S = size(a) |
| 95 | + if S[D] == 1 |
| 96 | + return :(return a) |
| 97 | + else |
| 98 | + N = ndims(a) |
| 99 | + Snew = ([n==D ? 1:S[n] for n = 1:N]...) |
| 100 | + newtype = similar_type(a, Snew) |
| 101 | + |
| 102 | + exprs = Array{Expr}(Snew) |
| 103 | + itr = [1:n for n = Snew] |
| 104 | + for i = Base.product(itr...) |
| 105 | + ik = copy([i...]) |
| 106 | + ik[D] = 2 |
| 107 | + expr = :(op(a[$(i...)], a[$(ik...)])) |
| 108 | + for k = 3:S[D] |
| 109 | + ik[D] = k |
| 110 | + expr = :(op($expr, a[$(ik...)])) |
| 111 | + end |
| 112 | + |
| 113 | + exprs[i...] = expr |
| 114 | + end |
| 115 | + |
| 116 | + return quote |
| 117 | + $(Expr(:meta,:inline)) |
| 118 | + @inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...))) |
| 119 | + end |
| 120 | + end |
| 121 | +end |
| 122 | + |
93 | 123 | # These are all similar in Base but not @inline'd
|
94 | 124 | @inline sum{T}(a::StaticArray{T}) = reduce(+, zero(T), a)
|
95 | 125 | @inline prod{T}(a::StaticArray{T}) = reduce(*, one(T), a)
|
|
101 | 131 | @inline sumabs2{T}(a::StaticArray{T}) = mapreduce(abs2, +, zero(T), a)
|
102 | 132 | @inline minimum(a::StaticArray) = reduce(min, a) # base has mapreduce(idenity, scalarmin, a)
|
103 | 133 | @inline maximum(a::StaticArray) = reduce(max, a) # base has mapreduce(idenity, scalarmax, a)
|
| 134 | +@inline minimum{D}(a::StaticArray, dim::Type{Val{D}}) = reduce(min, a, dim) |
| 135 | +@inline maximum{D}(a::StaticArray, dim::Type{Val{D}}) = reduce(max, a, dim) |
| 136 | + |
| 137 | +@generated function diff{D}(a::StaticArray, ::Type{Val{D}}=Val{1}) |
| 138 | + S = size(a) |
| 139 | + N = ndims(a) |
| 140 | + Snew = ([n==D ? S[n]-1:S[n] for n = 1:N]...) |
| 141 | + newtype = similar_type(a, Snew) |
| 142 | + |
| 143 | + exprs = Array{Expr}(Snew) |
| 144 | + itr = [1:n for n = Snew] |
| 145 | + |
| 146 | + for i1 = Base.product(itr...) |
| 147 | + i2 = copy([i1...]) |
| 148 | + i2[D] = i1[D] + 1 |
| 149 | + exprs[i1...] = :(a[$(i2...)] - a[$(i1...)]) |
| 150 | + end |
104 | 151 |
|
105 |
| - |
| 152 | + return quote |
| 153 | + $(Expr(:meta,:inline)) |
| 154 | + @inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...))) |
| 155 | + end |
| 156 | +end |
106 | 157 |
|
107 | 158 | ###############
|
108 | 159 | ## mapreduce ##
|
|
0 commit comments