Skip to content

Commit

Permalink
Merge branch 'JuliaSymbolics:master' into adjoint_symbolics
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophHotter authored Aug 29, 2024
2 parents ba291c6 + aab293a commit 1fced24
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
15 changes: 11 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SymbolicUtils"
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
authors = ["Shashi Gowda"]
version = "3.2.0"
version = "3.5.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand All @@ -26,15 +26,21 @@ TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
Unityper = "a7c27f48-0311-42f6-a7f8-2c11e75eb415"

[weakdeps]
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"

[extensions]
SymbolicUtilsLabelledArraysExt = "LabelledArrays"

[compat]
AbstractTrees = "0.4"
Bijections = "0.1.2"
ChainRulesCore = "1"
Combinatorics = "1.0"
ConstructionBase = "1.1"
ConstructionBase = "1.5.7"
DataStructures = "0.18"
DocStringExtensions = "0.8, 0.9"
DynamicPolynomials = "0.5"
DynamicPolynomials = "0.5, 0.6"
IfElse = "0.1"
LabelledArrays = "1.5"
MultivariatePolynomials = "0.5"
Expand All @@ -51,6 +57,7 @@ julia = "1.3"
[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PkgBenchmark = "32113eaa-f34f-5b0d-bd6c-c81e245fc73d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -59,4 +66,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["BenchmarkTools", "Documenter", "Pkg", "PkgBenchmark", "Random", "ReferenceTests", "Test", "Zygote"]
test = ["BenchmarkTools", "Documenter", "LabelledArrays", "Pkg", "PkgBenchmark", "Random", "ReferenceTests", "Test", "Zygote"]
25 changes: 25 additions & 0 deletions ext/SymbolicUtilsLabelledArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module SymbolicUtilsLabelledArraysExt

using LabelledArrays
using LabelledArrays.StaticArrays
using SymbolicUtils

@inline function SymbolicUtils.Code.create_array(A::Type{<:SLArray}, T, nd::Val, d::Val{dims}, elems...) where {dims}
a = SymbolicUtils.Code.create_array(SArray, T, nd, d, elems...)
if nfields(dims) === ndims(A)
similar_type(A, eltype(a), Size(dims))(a)
else
a
end
end

@inline function SymbolicUtils.Code.create_array(A::Type{<:LArray}, T, nd::Val, d::Val{dims}, elems...) where {dims}
data = SymbolicUtils.Code.create_array(Array, T, nd, d, elems...)
if nfields(dims) === ndims(A)
LArray{eltype(data),nfields(dims),typeof(data),LabelledArrays.symnames(A)}(data)
else
data
end
end

end
21 changes: 1 addition & 20 deletions src/code.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Code

using StaticArrays, LabelledArrays, SparseArrays, LinearAlgebra, NaNMath, SpecialFunctions
using StaticArrays, SparseArrays, LinearAlgebra, NaNMath, SpecialFunctions

export toexpr, Assignment, (), Let, Func, DestructuredArgs, LiteralExpr,
SetArray, MakeArray, MakeSparseArray, MakeTuple, AtIndex,
Expand Down Expand Up @@ -578,25 +578,6 @@ end
MArray{Tuple{dims...}, T}(elems...)
end

## LabelledArrays
@inline function create_array(A::Type{<:SLArray}, T, nd::Val, d::Val{dims}, elems...) where {dims}
a = create_array(SArray, T, nd, d, elems...)
if nfields(dims) === ndims(A)
similar_type(A, eltype(a), Size(dims))(a)
else
a
end
end

@inline function create_array(A::Type{<:LArray}, T, nd::Val, d::Val{dims}, elems...) where {dims}
data = create_array(Array, T, nd, d, elems...)
if nfields(dims) === ndims(A)
LArray{eltype(data),nfields(dims),typeof(data),LabelledArrays.symnames(A)}(data)
else
data
end
end

## We use a separate type for Sparse Arrays to sidestep the need for
## iszero to be defined on the expression type
@matchable struct MakeSparseArray{S<:AbstractSparseArray}
Expand Down
5 changes: 3 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ isdiv(x) = isa_SymType(Val(:Div), x)

Base.isequal(::Symbolic, x) = false
Base.isequal(x, ::Symbolic) = false
Base.isequal(::Symbolic, ::Missing) = false
Base.isequal(::Missing, ::Symbolic) = false
Base.isequal(::Symbolic, ::Symbolic) = false
coeff_isequal(a, b) = isequal(a, b) || ((a isa AbstractFloat || b isa AbstractFloat) && (a==b))
function _allarequal(xs, ys)::Bool
Expand Down Expand Up @@ -996,10 +998,9 @@ variable. So, `h(1, g)` will fail and `h(1, f)` will work.
"""
macro syms(xs...)
defs = map(xs) do x
n, t = _name_type(x)
T = esc(t)
nt = _name_type(x)
n, t = nt.name, nt.type
T = esc(t)
:($(esc(n)) = Sym{$T}($(Expr(:quote, n))))
end
Expr(:block, defs...,
Expand Down
3 changes: 3 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ end
@syms a b c
@test isequal(a + b, a + b + 0.01 - 0.01)
@test isequal(a + NaN, a + NaN)

@test !isequal(a, missing)
@test !isequal(missing, b)
end

@testset "subtyping" begin
Expand Down

0 comments on commit 1fced24

Please sign in to comment.