Skip to content
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

simplify issymbollike #287

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
[weakdeps]
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[extensions]
RecursiveArrayToolsMeasurementsExt = "Measurements"
RecursiveArrayToolsMonteCarloMeasurementsExt = "MonteCarloMeasurements"
RecursiveArrayToolsTrackerExt = "Tracker"
RecursiveArrayToolsSymbolicsExt = "Symbolics"
RecursiveArrayToolsZygoteExt = "Zygote"

[compat]
Expand Down
8 changes: 8 additions & 0 deletions ext/RecursiveArrayToolsSymbolicsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module RecursiveArrayToolsSymbolicsExt

import RecursiveArrayTools
isdefined(Base, :get_extension) ? (import Symbolics) : (import ..Symbolics)

RecursiveArrayTools.issymbollike(::Union{Symbolics.BasicSymbolic,Symbolics.Num}) = true

end
1 change: 1 addition & 0 deletions src/RecursiveArrayTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Requires
@static if !isdefined(Base, :get_extension)
function __init__()
Requires.@require Measurements="eff96d63-e80a-5855-80a2-b1b0885c5ab7" begin include("../ext/RecursiveArrayToolsMeasurementsExt.jl") end
Requires.@require Symbolics="e88e6eb3-aa80-5325-afca-941959d7151f" begin include("../ext/RecursiveArrayToolsSymbolicsExt.jl") end
Requires.@require Tracker="9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" begin include("../ext/RecursiveArrayToolsTrackerExt.jl") end
Requires.@require Zygote="e88e6eb3-aa80-5325-afca-941959d7151f" begin include("../ext/RecursiveArrayToolsZygoteExt.jl") end
end
Expand Down
15 changes: 4 additions & 11 deletions src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,9 @@ end
struct AllObserved
end

Base.@pure __parameterless_type(T) = Base.typename(T).wrapper
oscardssmith marked this conversation as resolved.
Show resolved Hide resolved

@generated function issymbollike(x)
x <: Union{Symbol, AllObserved} && return quote true end
ss = ["Operation", "Variable", "Sym", "Num", "Term"]
s = string(Symbol(__parameterless_type(x)))
bool = any(x -> occursin(x, s), ss)
quote
$bool
end
end
# extended by Symbolcs
issymbollike(::Any) = false
issymbollike(::Union{Symbol, AllObserved}) = true

function Base.Array(VA::AbstractVectorOfArray{T, N, A}) where {T, N,
A <: AbstractVector{
Expand Down Expand Up @@ -196,6 +188,7 @@ Base.@propagate_inbounds function Base.getindex(A::AbstractDiffEqArray{T, N},
RecursiveArrayTools.VectorOfArray(A.u)[I...]
end

__parameterless_type(T) = Base.typename(T).wrapper
Base.@propagate_inbounds function Base.getindex(A::AbstractVectorOfArray{T, N},
I::Colon...) where {T, N}
@assert length(I) == ndims(A.u[1]) + 1
Expand Down
6 changes: 4 additions & 2 deletions test/downstream/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
ModelingToolkit = "8.33"
MonteCarloMeasurements = "1.1"
OrdinaryDiffEq = "6.31"
Tracker = "0.2"
Unitful = "1.17"
Tracker = "0.2"
4 changes: 4 additions & 0 deletions test/downstream/SymbolicsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using RecursiveArrayTools, Symbolics, Test

@variables x;
@test RecursiveArrayTools.issymbollike(x)