Skip to content

Commit 9371e2a

Browse files
authored
Merge pull request #235 from JuliaSymbolics/s/create_vec
create_array fixes
2 parents 491ad80 + 1f6f032 commit 9371e2a

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

src/code.jl

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,12 @@ MakeArray(elems, similarto) = MakeArray(elems, similarto, nothing)
369369
function toexpr(a::MakeArray, st)
370370
similarto = toexpr(a.similarto, st)
371371
T = similarto isa Type ? similarto : :(typeof($similarto))
372+
ndim = ndims(a.elems)
372373
elT = a.output_eltype
373374
quote
374375
$create_array($T,
375376
$elT,
377+
Val{$ndim}(),
376378
Val{$(size(a.elems))}(),
377379
$(map(x->toexpr(x, st), a.elems)...),)
378380
end
@@ -388,62 +390,92 @@ end
388390
arr
389391
end
390392

391-
@inline function create_array(A::Type{<:Array}, T, d::Val, elems...)
393+
@inline function create_array(A::Type{<:Array}, T, ::Val, d::Val, elems...)
392394
_create_array(A, T, d, elems...)
393395
end
394396

395-
@inline function create_array(A::Type{<:Array}, ::Nothing, d::Val{dims}, elems...) where dims
397+
@inline function create_array(A::Type{<:Array}, ::Nothing, ::Val, d::Val{dims}, elems...) where dims
396398
T = promote_type(map(typeof, elems)...)
397399
_create_array(A, T, d, elems...)
398400
end
399401

400-
@inline function create_array(A::Type{<:SubArray{T,N,P,I,L}}, S, d::Val, elems...) where {T,N,P,I,L}
401-
create_array(P, S, d, elems...)
402+
## Vector
403+
#
404+
@inline function create_array(::Type{<:Array}, ::Nothing, ::Val{1}, ::Val{dims}, elems...) where dims
405+
[elems...]
402406
end
403407

404-
@inline function create_array(A::Type{<:PermutedDimsArray{T,N,perm,iperm,P}}, S, d::Val, elems...) where {T,N,perm,iperm,P}
405-
create_array(P, S, d, elems...)
408+
@inline function create_array(::Type{<:Array}, T, ::Val{1}, ::Val{dims}, elems...) where dims
409+
T[elems...]
406410
end
407411

408412
## Matrix
409413

410-
@inline function create_array(::Type{<:Matrix}, ::Nothing, ::Val{dims}, elems...) where dims
411-
Base.hvcat(dims, elems...)
414+
@inline function create_array(::Type{<:Array}, ::Nothing, ::Val{2}, ::Val{dims}, elems...) where dims
415+
vhcat(dims, elems...)
412416
end
413417

414-
@inline function create_array(::Type{<:Matrix}, T, ::Val{dims}, elems...) where dims
415-
Base.typed_hvcat(T, dims, elems...)
418+
@inline function create_array(::Type{<:Array}, T, ::Val{2}, ::Val{dims}, elems...) where dims
419+
typed_vhcat(T, dims, elems...)
416420
end
417421

418-
@inline function create_array(A::Type{<:Transpose{T,P}}, S, d::Val, elems...) where {T,P}
419-
create_array(P, S, d, elems...)
422+
423+
vhcat(sz::Tuple{Int,Int}, xs::T...) where {T} = typed_vhcat(T, sz, xs...)
424+
vhcat(sz::Tuple{Int,Int}, xs::Number...) = typed_vhcat(Base.promote_typeof(xs...), sz, xs...)
425+
vhcat(sz::Tuple{Int,Int}, xs...) = typed_vhcat(Base.promote_eltypeof(xs...), sz, xs...)
426+
427+
function typed_vhcat(::Type{T}, sz::Tuple{Int, Int}, xs...) where T
428+
nr,nc = sz
429+
a = Matrix{T}(undef, nr, nc)
430+
k = 1
431+
for j=1:nc
432+
@inbounds for i=1:nr
433+
a[i, j] = xs[k]
434+
k += 1
435+
end
436+
end
437+
a
438+
end
439+
440+
## Arrays of the special kind
441+
@inline function create_array(A::Type{<:SubArray{T,N,P,I,L}}, S, nd::Val, d::Val, elems...) where {T,N,P,I,L}
442+
create_array(P, S, nd, d, elems...)
443+
end
444+
445+
@inline function create_array(A::Type{<:PermutedDimsArray{T,N,perm,iperm,P}}, S, nd::Val, d::Val, elems...) where {T,N,perm,iperm,P}
446+
create_array(P, S, nd, d, elems...)
447+
end
448+
449+
450+
@inline function create_array(A::Type{<:Transpose{T,P}}, S, nd::Val, d::Val, elems...) where {T,P}
451+
create_array(P, S, nd, d, elems...)
420452
end
421453

422-
@inline function create_array(A::Type{<:UpperTriangular{T,P}}, S, d::Val, elems...) where {T,P}
423-
create_array(P, S, d, elems...)
454+
@inline function create_array(A::Type{<:UpperTriangular{T,P}}, S, nd::Val, d::Val, elems...) where {T,P}
455+
create_array(P, S, nd, d, elems...)
424456
end
425457

426458
## SArray
427-
@inline function create_array(::Type{<:SArray}, ::Nothing, ::Val{dims}, elems...) where dims
459+
@inline function create_array(::Type{<:SArray}, ::Nothing, nd::Val, ::Val{dims}, elems...) where dims
428460
SArray{Tuple{dims...}}(elems...)
429461
end
430462

431-
@inline function create_array(::Type{<:SArray}, T, ::Val{dims}, elems...) where dims
463+
@inline function create_array(::Type{<:SArray}, T, nd::Val, ::Val{dims}, elems...) where dims
432464
SArray{Tuple{dims...}, T}(elems...)
433465
end
434466

435467
## LabelledArrays
436-
@inline function create_array(A::Type{<:SLArray}, T, d::Val{dims}, elems...) where {dims}
437-
a = create_array(SArray, T, d, elems...)
468+
@inline function create_array(A::Type{<:SLArray}, T, nd::Val, d::Val{dims}, elems...) where {dims}
469+
a = create_array(SArray, T, nd, d, elems...)
438470
if nfields(dims) === ndims(A)
439471
similar_type(A, eltype(a), Size(dims))(a)
440472
else
441473
a
442474
end
443475
end
444476

445-
@inline function create_array(A::Type{<:LArray}, T, d::Val{dims}, elems...) where {dims}
446-
data = create_array(Array, T, d, elems...)
477+
@inline function create_array(A::Type{<:LArray}, T, nd::Val, d::Val{dims}, elems...) where {dims}
478+
data = create_array(Array, T, nd, d, elems...)
447479
if nfields(dims) === ndims(A)
448480
LArray{eltype(data),nfields(dims),typeof(data),LabelledArrays.symnames(A)}(data)
449481
else

test/code.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
6969

7070
test_repr(toexpr(MakeArray([a,b,a+b], :arr)),
7171
quote
72-
$(SymbolicUtils.Code.create_array)(typeof(arr), nothing, Val{(3,)}(), a, b, $(+)(a, b))
72+
$(SymbolicUtils.Code.create_array)(typeof(arr), nothing, Val{1}(), Val{(3,)}(), a, b, $(+)(a, b))
7373
end)
7474

7575
toexpr(Let([a 1, b 2, :arr [1,2]],

0 commit comments

Comments
 (0)