Symbolics and Convex

Hi, I’d like to consider a symbolic variable as a constant from Convex.
For example,

julia> using Symbolics

julia> using Convex

julia> @variables x
1-element Vector{Num}:
 x

julia> u = Convex.Variable(1)
Variable
size: (1, 1)
sign: real
vexity: affine
id: 120…439

julia> x + u
ERROR: TypeError: non-boolean (Num) used in boolean context
Stacktrace:
 [1] _sign
   @ ~/.julia/packages/Convex/ukggP/src/constant.jl:17 [inlined]
 [2] Constant (repeats 2 times)
   @ ~/.julia/packages/Convex/ukggP/src/constant.jl:37 [inlined]
 [3] +(x::Num, y::Convex.Variable)
   @ Convex ~/.julia/packages/Convex/ukggP/src/atoms/affine/add_subtract.jl:117
 [4] top-level scope
   @ REPL[4]:1
 [5] top-level scope
   @ ~/.julia/packages/Infiltrator/r3Hf5/src/Infiltrator.jl:710

julia> Convex.Constant(x)
ERROR: TypeError: non-boolean (Num) used in boolean context
Stacktrace:
 [1] _sign
   @ ~/.julia/packages/Convex/ukggP/src/constant.jl:17 [inlined]
 [2] Constant (repeats 2 times)
   @ ~/.julia/packages/Convex/ukggP/src/constant.jl:37 [inlined]
 [3] top-level scope
   @ REPL[5]:1
 [4] top-level scope
   @ ~/.julia/packages/Infiltrator/r3Hf5/src/Infiltrator.jl:710

Is there any way to do this?

The stack trace is informative. Convex.Constant needs the _sign of its argument. x >= 0 is a symbolic expression instead of a boolean.
If your x is positive, you could define

function Convex._sign(v::Num)
    if isequal(v, x)
        return Convex.Positive()
    end
end

What if it cannot determine the sign of x in advance, what should I do?

No. There is no way to integrate Convex.jl with Symbolics.jl

1 Like