Skip to content

[Enhancement] make Complex{Symbolics.Num} a ScalarSymbolic() similar to Symbolics.Num #1950

Description

@OliverAh

In src/variable.jl:331

SymbolicIndexingInterface.symbolic_type(::Type{Symbolics.Num}) = ScalarSymbolic()

the symbolic_type of a Symbolics.Num is inferred to be a ScalarSymbolic.
However, when working with complex-valued symbols, they are currently inferred to be a NotSymbolic. This causes some issues in substitution of matrices of symbols, where they are inserted as SymbolicUtils.Const instead of SymbolicUtils.array_literal. This in return prevents get_variables to find the symbols in the matrix.

Solution

Would it be possible to add also the following line to the file?

SymbolicIndexingInterface.symbolic_type(::Type{Complex{Symbolics.Num}}) = ScalarSymbolic()

Demonstrator

import Symbolics
import SymbolicUtils

# Uncomment next line to see new behaviour.
# SymbolicUtils.SymbolicIndexingInterface.symbolic_type(::Type{Complex{Symbolics.Num}}) = SymbolicUtils.ScalarSymbolic()

θ, λ = Symbolics.@variables θ::Real λ::Real

real_f =    Symbolics.variable(:real_f;    T=Symbolics.FnType{Tuple{Vararg{Number}}, Number, Nothing})(θ)
complex_f = Symbolics.variable(:complex_f; T=Symbolics.FnType{Tuple{Vararg{Number}}, Number, Nothing})(λ)

real_mat =    Matrix([exp(    θ) 0.0; 0.0 0.0])
complex_mat = Matrix([exp(1im*λ) 0.0; 0.0 0.0])

expr = real_f * complex_f

real_subs =    Symbolics.substitute(expr, Dict(real_f => real_mat))  # substitutes a SymbolicUtils.array_literal
complex_subs = Symbolics.substitute(expr, Dict(complex_f => complex_mat)) # substitutes a SymbolicUtils.Const

real_vars =    Symbolics.get_variables(real_subs)
complex_vars = Symbolics.get_variables(complex_subs) # does not find λ

The added line would make also the matrix in complex_subs to be a array_literal (same as in real_subs), and get_variables would then be able to find λ.

Tested with
SymbolicUtils v4.45.0
Symbolics v7.36.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions