From d031be93081120e36dcf17131623f0e44cb5efd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 29 Feb 2024 21:44:45 +0100 Subject: [PATCH] Fix complex variables of custom coefficient type (#3691) --- src/variables.jl | 8 ++++---- test/test_complex.jl | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index 4687c6989c7..54b71b4d224 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -2104,10 +2104,10 @@ function add_variable( real_part = add_variable(model, _real(var), _real(name)) imag_part = add_variable(model, _imag(var), _imag(name)) # Efficiently build `real_part + imag_part * im` - return GenericAffExpr{ComplexF64,GenericVariableRef{T}}( - zero(ComplexF64), - real_part => one(ComplexF64), - imag_part => convert(ComplexF64, im), + return GenericAffExpr{Complex{T},GenericVariableRef{T}}( + zero(Complex{T}), + real_part => one(Complex{T}), + imag_part => convert(Complex{T}, im), ) end diff --git a/test/test_complex.jl b/test/test_complex.jl index 892649baa51..9adafd116f7 100644 --- a/test/test_complex.jl +++ b/test/test_complex.jl @@ -292,4 +292,22 @@ function test_HermitianPSDCone_general_matrix_error() return end +function test_complex_generic_number_type() + for T in (Float32, Float64, Rational{BigInt}, BigFloat) + model = GenericModel{T}() + @variable(model, x in ComplexPlane()) + @test x isa GenericAffExpr{Complex{T},GenericVariableRef{T}} + end + return end + +function test_hermitian_generic_number_type() + for T in (Float32, Float64, Rational{BigInt}, BigFloat) + model = GenericModel{T}() + @variable(model, x[1:2, 1:2], Hermitian) + @test x[1, 1] isa GenericAffExpr{Complex{T},GenericVariableRef{T}} + end + return +end + +end # TestComplexNumberSupport