Skip to content

add an undef initializer for LArray #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LabelledArrays"
uuid = "2ee39098-c373-598a-b85f-a56591580800"
authors = ["Chris Rackauckas <[email protected]>"]
version = "1.12.0"
version = "1.12.1"

[deps]
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"
Expand Down
6 changes: 5 additions & 1 deletion src/LabelledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ end
Base.NamedTuple(x::Union{LArray, SLArray}) = NamedTuple{symnames(typeof(x))}(x.__x)
@inline Base.reshape(a::SLArray, s::Size) = StaticArrays.similar_type(a, s)(Tuple(a))

function ArrayInterfaceCore.ismutable(::Type{<:LArray{T, N, Syms}}) where {T, N, Syms}
function ArrayInterfaceCore.ismutable(::Type{<:LArray{T, N, D, Syms}}) where {T, N, D, Syms}
ArrayInterfaceCore.ismutable(T)
end
ArrayInterfaceCore.can_setindex(::Type{<:SLArray}) = false

function ArrayInterfaceCore.undefmatrix(x::LArray{T, N, D, Syms}) where {T, N, D, Syms}
similar(x.__x, length(Syms), length(Syms))
end

function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache,
u::LArray{T, N, D, Syms}) where {T <: ForwardDiff.Dual,
N, D, Syms}
Expand Down
13 changes: 9 additions & 4 deletions src/larray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ struct LArray{T, N, D <: AbstractArray{T, N}, Syms} <: DenseArray{T, N}
LArray{T, N, D, Syms}(__x) where {T, N, D, Syms} = new{T, N, D, Syms}(__x)
end

function LArray{T, N, D, Syms}(::UndefInitializer, n::Int64) where {T, N, D, Syms}
@assert length(Syms) == n
LArray{T, N, D, Syms}(similar(D, n))
end

#####################################
# NamedTuple compatibility
#####################################
Expand Down Expand Up @@ -199,8 +204,8 @@ A.a == 1
```

Users can also generate a labelled array with undefined values by instead giving
the dimensions. This approach is useful if the user intends to pre-allocate an
array for some later input.
the dimensions. This approach is useful if the user intends to pre-allocate an
array for some later input.

```julia
A = @LArray Float64 (2,2) (:a,:b,:c,:d)
Expand All @@ -226,7 +231,7 @@ julia> z.a
4
```

The labels of LArray and SLArray can be accessed
The labels of LArray and SLArray can be accessed
by function `symbols`, which returns a tuple of symbols.
"""
macro LArray(vals, syms)
Expand Down Expand Up @@ -260,7 +265,7 @@ A = @LVector Float64 (:a,:b,:c,:d)
A .= rand(4)
```

On the other hand, users can also initialize the vector and set its values at the
On the other hand, users can also initialize the vector and set its values at the
same time:

```julia
Expand Down
1 change: 1 addition & 0 deletions test/larrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using LabelledArrays, Test, InteractiveUtils
mat = rand(4, 3)
@test mat * vals ≈ mat * [1.0, 2.0, 3.0]
syms = (:a, :b, :c)
@test typeof(typeof(x)(undef, 3)) == typeof(x)

for (i, s) in enumerate(syms)
@show i, s
Expand Down