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

feat: support getname for symbolic complex numbers #1368

Merged
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
17 changes: 17 additions & 0 deletions src/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,20 @@ Base.iszero(x::Complex{<:Num}) = iszero(real(x)) && iszero(imag(x))
Base.isone(x::Complex{<:Num}) = isone(real(x)) && iszero(imag(x))
_iszero(x::Complex{<:Num}) = _iszero(unwrap(x))
_isone(x::Complex{<:Num}) = _isone(unwrap(x))

function SymbolicIndexingInterface.hasname(x::ComplexTerm)
a = arguments(unwrap(x.im))[1]
b = arguments(unwrap(x.re))[1]
return isequal(a, b) && hasname(a)
end

function _getname(x::ComplexTerm, val)
a = arguments(unwrap(x.im))[1]
b = arguments(unwrap(x.re))[1]
if isequal(a, b)
return _getname(a, val)
end
if val == _fail
throw(ArgumentError("Variable $x doesn't have a name."))
end
end
9 changes: 7 additions & 2 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,18 @@ function SymbolicIndexingInterface.symbolic_type(::Type{T}) where {S <: Abstract
ArraySymbolic()
end

SymbolicIndexingInterface.hasname(x::Union{Num,Arr}) = hasname(unwrap(x))
SymbolicIndexingInterface.hasname(x::Union{Num,Arr,Complex{Num}}) = hasname(unwrap(x))

function SymbolicIndexingInterface.hasname(x::Symbolic)
issym(x) || !iscall(x) || iscall(x) && (issym(operation(x)) || operation(x) == getindex)
end

SymbolicIndexingInterface.getname(x, val=_fail) = _getname(unwrap(x), val)
# This is type piracy, but changing it breaks precompilation for MTK because it relies on this falling back to
# `_getname` which calls `nameof` which returns the name of the system, when `x::AbstractSystem`.
# FIXME: In a breaking release
function SymbolicIndexingInterface.getname(x, val = _fail)
_getname(unwrap(x), val)
end

function SymbolicIndexingInterface.symbolic_evaluate(ex::Union{Num, Arr, Symbolic, Equation, Inequality}, d::Dict; kwargs...)
val = fixpoint_sub(ex, d; kwargs...)
Expand Down
13 changes: 13 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Symbolics, Test
using SymbolicUtils: metadata
using Symbolics: unwrap
using SymbolicIndexingInterface: getname, hasname

@variables a b::Real z::Complex (Z::Complex)[1:10]

Expand Down Expand Up @@ -37,3 +38,15 @@ end
z2 = 1.0 + z*im
@test isnothing(metadata(unwrap(z1.re)))
end

@testset "getname" begin
@variables t a b x::Complex y(t)::Complex z(a, b)::Complex
@test hasname(x)
@test getname(x) == :x
@test hasname(y)
@test getname(y) == :y
@test hasname(z)
@test getname(z) == :z
@test !hasname(2x)
@test !hasname(x + y)
end
Loading