Skip to content

Commit

Permalink
Merge pull request #443 from DhairyaLGandhi/dg/convert
Browse files Browse the repository at this point in the history
chore: add `has_trivial_array_constructor`
  • Loading branch information
ChrisRackauckas authored Jun 5, 2024
2 parents 87b6460 + 193993c commit 93a7908
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand All @@ -66,4 +67,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

[targets]
test = ["SafeTestsets", "Pkg", "Test", "Aqua", "Random", "SparseArrays", "SuiteSparse", "BandedMatrices", "BlockBandedMatrices", "GPUArraysCore", "StaticArrays", "StaticArraysCore", "Tracker", "ReverseDiff", "ChainRules", "FillArrays"]
test = ["SafeTestsets", "Pkg", "Test", "Aqua", "Random", "SparseArrays", "SuiteSparse", "BandedMatrices", "BlockBandedMatrices", "GPUArraysCore", "StaticArrays", "StaticArraysCore", "Tracker", "ReverseDiff", "ChainRules", "FillArrays", "ComponentArrays"]
3 changes: 2 additions & 1 deletion docs/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ ArrayInterface.aos_to_soa
ArrayInterface.promote_eltype
ArrayInterface.restructure
ArrayInterface.safevec
```
ArrayInterface.has_trivial_array_constructor
```
24 changes: 24 additions & 0 deletions src/ArrayInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,28 @@ ensures_sorted(@nospecialize( T::Type{<:AbstractRange})) = true
ensures_sorted(T::Type) = is_forwarding_wrapper(T) ? ensures_sorted(parent_type(T)) : false
ensures_sorted(@nospecialize(x)) = ensures_sorted(typeof(x))

"""
has_trivial_array_constructor(T::Type, args...) -> Bool
Returns `true` if an object of type `T` can be constructed using the collection of `args`
Note: This checks if a compatible `convert` methood exists between `T` and `args`
# Examples:
```julia
julia> ca = ComponentVector((x = rand(3), y = rand(4),))
ComponentVector{Float64}(x = [0.6549137106381634, 0.37555505280294565, 0.8521039568665254], y = [0.40314196291239024, 0.35484725607638834, 0.6580528978034597, 0.10055508457632167])
julia> ArrayInterface.has_trivial_array_constructor(typeof(ca), ones(6))
true
julia> ArrayInterface.has_trivial_array_constructor(typeof(cv), (x = rand(6),))
false
```
"""
function has_trivial_array_constructor(::Type{T}, args...) where T
applicable(convert, T, args...)
end

end # module
8 changes: 7 additions & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using ArrayInterface
using ArrayInterface: zeromatrix, undefmatrix
import ArrayInterface: has_sparsestruct, findstructralnz, fast_scalar_indexing, lu_instance,
parent_type, zeromatrix
using ComponentArrays
using LinearAlgebra
using Random
using SparseArrays
Expand Down Expand Up @@ -289,4 +290,9 @@ end
end
@test ArrayInterface.ldlt_instance(SymTridiagonal(A' * A)) isa typeof(ldlt(SymTridiagonal(A' * A)))
end
end
end

@testset "Array conversion" begin
cv = ComponentVector((x = rand(3), y = rand(3)))
@test ArrayInterface.has_trivial_array_constructor(typeof(cv), rand(6))
end

0 comments on commit 93a7908

Please sign in to comment.