Skip to content

Commit 31e3f6f

Browse files
Merge pull request #1368 from AayushSabharwal/as/complex-getname
feat: support `getname` for symbolic complex numbers
2 parents c31c3fd + 06957b4 commit 31e3f6f

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/complex.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,20 @@ Base.iszero(x::Complex{<:Num}) = iszero(real(x)) && iszero(imag(x))
8181
Base.isone(x::Complex{<:Num}) = isone(real(x)) && iszero(imag(x))
8282
_iszero(x::Complex{<:Num}) = _iszero(unwrap(x))
8383
_isone(x::Complex{<:Num}) = _isone(unwrap(x))
84+
85+
function SymbolicIndexingInterface.hasname(x::ComplexTerm)
86+
a = arguments(unwrap(x.im))[1]
87+
b = arguments(unwrap(x.re))[1]
88+
return isequal(a, b) && hasname(a)
89+
end
90+
91+
function _getname(x::ComplexTerm, val)
92+
a = arguments(unwrap(x.im))[1]
93+
b = arguments(unwrap(x.re))[1]
94+
if isequal(a, b)
95+
return _getname(a, val)
96+
end
97+
if val == _fail
98+
throw(ArgumentError("Variable $x doesn't have a name."))
99+
end
100+
end

src/variable.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,18 @@ function SymbolicIndexingInterface.symbolic_type(::Type{T}) where {S <: Abstract
503503
ArraySymbolic()
504504
end
505505

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

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

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

514519
function SymbolicIndexingInterface.symbolic_evaluate(ex::Union{Num, Arr, Symbolic, Equation, Inequality}, d::Dict; kwargs...)
515520
val = fixpoint_sub(ex, d; kwargs...)

test/complex.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Symbolics, Test
22
using SymbolicUtils: metadata
33
using Symbolics: unwrap
4+
using SymbolicIndexingInterface: getname, hasname
45

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

@@ -37,3 +38,15 @@ end
3738
z2 = 1.0 + z*im
3839
@test isnothing(metadata(unwrap(z1.re)))
3940
end
41+
42+
@testset "getname" begin
43+
@variables t a b x::Complex y(t)::Complex z(a, b)::Complex
44+
@test hasname(x)
45+
@test getname(x) == :x
46+
@test hasname(y)
47+
@test getname(y) == :y
48+
@test hasname(z)
49+
@test getname(z) == :z
50+
@test !hasname(2x)
51+
@test !hasname(x + y)
52+
end

0 commit comments

Comments
 (0)