Skip to content

Commit a6d7953

Browse files
authored
Minor ArraySpace optimizations (#314)
* Minor ArraySpace optimizations * Further changes * equality for arrayspace * no anon fun in tensorspace identity fun
1 parent e3a5834 commit a6d7953

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/Multivariate/TensorSpace.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,4 +733,4 @@ end
733733

734734

735735

736-
Fun(::typeof(identity), S::TensorSpace) = Fun(xyz->collect(xyz),S)
736+
Fun(::typeof(identity), S::TensorSpace) = Fun(collect, S)

src/Spaces/ArraySpace.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ ArraySpace(sp::AbstractArray{SS,N}, f = first(sp)) where {SS<:Space,N} =
2323
ArraySpace(S::Space,::Val{n}) where {n} = ArraySpace(@SArray fill(S,n...))
2424
ArraySpace(S::Space,n::NTuple{N,Int}) where {N} = ArraySpace(fill(S,n))
2525
ArraySpace(S::Space,n::Integer) = ArraySpace(S,(n,))
26-
ArraySpace(S::Space,n,m) = ArraySpace(fill(S,(n,m)))
26+
ArraySpace(S::Space,n,m) = ArraySpace(S,(n,m))
2727
ArraySpace(d::Domain,n...) = ArraySpace(Space(d),n...)
2828

2929
Space(sp::AbstractArray{<:Space}) = ArraySpace(sp)
30-
convert(::Type{A}, sp::ArraySpace) where {A<:Array} = A(sp.spaces)::A
31-
Array(sp::ArraySpace) = convert(Array, sp.spaces)
32-
Vector(sp::VectorSpace) = convert(Vector, sp.spaces)
33-
Matrix(sp::MatrixSpace) = convert(Matrix, sp.spaces)
30+
convert(::Type{A}, sp::ArraySpace) where {A<:Array} = convert(A, sp.spaces)::A
31+
(::Type{A})(sp::ArraySpace) where {A<:Array} = A(sp.spaces)
3432

3533

3634
BlockInterlacer(sp::ArraySpace) = BlockInterlacer(map(blocklengths, Tuple(sp.spaces)))
@@ -170,21 +168,21 @@ end
170168
## routines
171169

172170
spacescompatible(AS::ArraySpace,BS::ArraySpace) =
173-
size(AS) == size(BS) && all(spacescompatible.(AS.spaces,BS.spaces))
171+
size(AS) == size(BS) && all(((x,y),) -> spacescompatible(x,y), zip(AS.spaces,BS.spaces))
174172
canonicalspace(AS::ArraySpace) = ArraySpace(canonicalspace.(AS.spaces))
175173
evaluate(f::AbstractVector,S::ArraySpace,x) = map(g->g(x),Fun(S,f))
176174

175+
==(A::ArraySpace, B::ArraySpace) = size(A) == size(B) && all(((x,y),) -> x==y, zip(A.spaces, B.spaces))
177176

178177
## choosedomainspace
179178

180-
function choosedomainspace(A::InterlaceOperator{T,1},sp::ArraySpace) where T
181-
# this ensures correct dispatch for unino
182-
sps = Vector{Space}(
183-
filter(x->!isambiguous(x),map(choosedomainspace,A.ops,sp.spaces)))
179+
function choosedomainspace(A::VectorInterlaceOperator, sp::ArraySpace)
180+
# this ensures correct dispatch for union
181+
sps = filter(!isambiguous, map(choosedomainspace,A.ops,sp.spaces))
184182
if isempty(sps)
185183
UnsetSpace()
186184
else
187-
union(sps...)
185+
reduce(union, sps)
188186
end
189187
end
190188

@@ -223,12 +221,12 @@ Fun(f::AbstractMatrix{FF},d::MatrixSpace) where {FF<:Fun} = Fun(d,coefficients(f
223221
# columns are coefficients
224222
function Fun(M::AbstractMatrix{<:Number},sp::MatrixSpace)
225223
if size(M) size(sp)
226-
throw(DimensionMismatch())
224+
throw(DimensionMismatch("size of array $(size(M)) doesn't match that of the space $(size(sp))"))
227225
end
228-
Fun(map((f,s)->Fun(f,s),M,sp.spaces))
226+
Fun(map(Fun, M, sp.spaces))
229227
end
230228

231-
Fun(M::UniformScaling,sp::MatrixSpace) = Fun(M.λ*Matrix(I,size(sp)...),sp)
229+
Fun(M::UniformScaling,sp::MatrixSpace) = Fun(Matrix(M,size(sp)),sp)
232230

233231

234232

test/SpacesTest.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ using LinearAlgebra
312312
a = ApproxFunBase.ArraySpace(PointSpace(1:4), 2)
313313
b = @inferred ApproxFunBase.ArraySpace(PointSpace(1:4), Val(2))
314314
@test a == b
315+
c = ApproxFunBase.ArraySpace(PointSpace(1:4), 3)
316+
@test a != c
317+
d = ApproxFunBase.ArraySpace(PointSpace(2:5), 2)
318+
@test a != d
319+
315320
a = ApproxFunBase.ArraySpace(PointSpace(1:4), 2, 2)
316321
b = @inferred ApproxFunBase.ArraySpace(PointSpace(1:4), Val((2,2)))
317322
@test a == b

0 commit comments

Comments
 (0)