-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
complex.jl
266 lines (240 loc) · 8.23 KB
/
complex.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See https://github.com/jump-dev/JuMP.jl
#############################################################################
module TestComplexNumberSupport
using JuMP
using Test
import LinearAlgebra
import MutableArithmetics
import SparseArrays
const MA = MutableArithmetics
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
function test_complex_aff_expr()
model = Model()
@variable(model, x)
y = (1 + 2im) * x + 1
@test typeof(y) == GenericAffExpr{Complex{Float64},VariableRef}
@test 1im + x == 1im + 1 * x
@test 1im + x == 1im + (1 + 0im) * x
@test 1im - x == -(1 + 0im)x + 1im
@test 1im - 1 * x == -1 * x + 1im
return
end
function test_complex_quad_expr()
model = Model()
@variable(model, x)
y = im * x^2
@test typeof(y) == GenericQuadExpr{Complex{Float64},VariableRef}
@test y == x^2 * im
@test x^2 + y == y + x^2
@test x^2 + y == MA.@rewrite(x^2 + y)
@test x^2 + y == MA.@rewrite(x^2 + 1 * y)
@test x^2 + y == MA.@rewrite(y + x^2)
@test x^2 + y == MA.@rewrite(y + 1 * x^2)
@test x^2 + y == MA.@rewrite(x^2 + im * x^2)
@test x^2 - y == -(y - x^2)
@test x^2 - y == MA.@rewrite(x^2 - y)
@test x^2 - y == MA.@rewrite(x^2 + im * y * im)
@test x^2 - y == MA.@rewrite(-(y - x^2))
z = x^2
@test y + z * im == im * z + y
@test y + z * im == MA.@rewrite(y + z * im)
@test y + z * im == MA.@rewrite(im * z + y)
@test y - x^2 == -x^2 + y
@test y - x^2 == MA.@rewrite(y - x^2)
@test y - x^2 == MA.@rewrite(-x^2 + y)
return
end
function test_complex_plus_variable()
model = Model()
@variable(model, x)
y = x + im
@test typeof(y) == GenericAffExpr{Complex{Float64},VariableRef}
@test y == im + x
return
end
function test_complex_minus_variable()
model = Model()
@variable(model, x)
y = im - x
@test typeof(y) == GenericAffExpr{Complex{Float64},VariableRef}
@test -y == x - im
return
end
function test_complex_aff_expr_convert()
model = Model()
@variable(model, x)
y = (1 + 2im) * x + 1
y_int = convert(GenericAffExpr{Complex{Int},VariableRef}, y)
@test typeof(y_int) == GenericAffExpr{Complex{Int},VariableRef}
@test y_int == y
@test_throws InexactError convert(AffExpr, y)
return
end
function test_complex_add_aff()
model = Model()
@variable(model, x)
real_aff = 3x - 1
complex_aff = (1 + 2im) * x + 1
@test complex_aff == MA.@rewrite((1 + 2im) * x + 1)
@test complex_aff == MA.@rewrite(1 + (1 + 2im) * x)
@test complex_aff == MA.@rewrite(1 + (2im) * x + x)
@test real_aff + complex_aff == complex_aff + real_aff
@test real_aff - complex_aff == -(complex_aff - real_aff)
return
end
function test_complex_vector_constraint()
model = Model()
@variable(model, x)
con_ref = @constraint(model, [(1 + 2im) * x + 1] in MOI.Zeros(1))
@test list_of_constraint_types(model) ==
[(Vector{GenericAffExpr{Complex{Float64},VariableRef}}, MOI.Zeros)]
con_obj = constraint_object(con_ref)
@test jump_function(con_obj) == [(1 + 2im) * x + 1]
@test moi_set(con_obj) == MOI.Zeros(1)
end
function test_complex_vector_constraint()
model = Model()
@variable(model, x)
con_ref = @constraint(model, [(1 + 2im) * x^2 + 1] in MOI.Zeros(1))
@test list_of_constraint_types(model) ==
[(Vector{GenericQuadExpr{Complex{Float64},VariableRef}}, MOI.Zeros)]
con_obj = constraint_object(con_ref)
@test jump_function(con_obj) == [(1 + 2im) * x^2 + 1]
@test moi_set(con_obj) == MOI.Zeros(1)
end
function test_complex_scalar_affine_constraint()
model = Model()
@variable(model, x)
con_ref = @constraint(model, (1 + 2im) * x == 1.0)
@test list_of_constraint_types(model) ==
[(GenericAffExpr{ComplexF64,VariableRef}, MOI.EqualTo{ComplexF64})]
con_obj = constraint_object(con_ref)
@test jump_function(con_obj) == (1 + 2im) * x
@test moi_set(con_obj) == MOI.EqualTo(1.0 + 0.0im)
return
end
function test_complex_scalar_quadratic_constraint()
model = Model()
@variable(model, x)
con_ref = @constraint(model, (1 + 2im) * x^2 == 1.0)
@test list_of_constraint_types(model) ==
[(GenericQuadExpr{ComplexF64,VariableRef}, MOI.EqualTo{ComplexF64})]
con_obj = constraint_object(con_ref)
@test jump_function(con_obj) == (1 + 2im) * x^2
@test moi_set(con_obj) == MOI.EqualTo(1.0 + 0.0im)
return
end
function test_complex_print()
model = Model()
@variable(model, x)
y = (1 + 2im) * x + 1
@test sprint(show, y) == "(1.0 + 2.0im) x + (1.0 + 0.0im)"
y = im * x
@test sprint(show, y) == "(0.0 + 1.0im) x"
return
end
function test_complex_print_zeros()
model = Model()
@variable(model, x in ComplexPlane())
@test sprint(show, real(x)) == "real(x)"
@test sprint(show, imag(x)) == "imag(x)"
return
end
function test_complex_conj()
model = Model()
@variable(model, x)
@test conj(x) === x
@test real(x) === x
@test imag(x) == 0
real_aff = 2 * x + 3
@test conj(real_aff) === real_aff
@test real(real_aff) === real_aff
@test imag(real_aff) == 0
complex_aff = (2 + im) * x + 3 - im
@test conj(complex_aff) == (2 - im) * x + 3 + im
@test real(complex_aff) == 2x + 3
@test imag(complex_aff) == x - 1
real_quad = 4x^2 + 2 * x + 3
@test conj(real_quad) === real_quad
@test real(real_quad) === real_quad
@test imag(real_quad) === real_quad
complex_quad = (4 - 5im) * x^2 + (2 + im) * x + 3 - im
@test conj(complex_quad) == (4 + 5im) * x^2 + (2 - im) * x + 3 + im
@test real(complex_quad) == 4x^2 + 2x + 3
@test imag(complex_quad) == -5x^2 + x - 1
end
function test_complex_abs2()
model = Model()
@variable(model, x)
@test abs2(x) == x^2
@test abs2(x + 2im) == x^2 + 4
@test abs2(x + 2) == x^2 + 4x + 4
@test abs2(x * im + 2) == x^2 + 4
end
function test_hermitian()
model = Model()
@variable(model, x)
A = [3 1im; -1im 2x]
@test A isa Matrix{GenericAffExpr{ComplexF64,VariableRef}}
A = [3x^2 1im; -1im 2x]
@test A isa Matrix{GenericQuadExpr{ComplexF64,VariableRef}}
A = [3x 1im; -1im 2x^2]
@test A isa Matrix{GenericQuadExpr{ComplexF64,VariableRef}}
A = [3x 1im; -1im 2x]
@test A isa Matrix{GenericAffExpr{ComplexF64,VariableRef}}
@test isequal_canonical(A', A)
H = LinearAlgebra.Hermitian(A)
T = GenericAffExpr{ComplexF64,VariableRef}
@test H isa LinearAlgebra.Hermitian{T,Matrix{T}}
@test isequal_canonical(A[1, 2], LinearAlgebra.adjoint(A[2, 1]))
@test isequal_canonical(H[1, 2], LinearAlgebra.adjoint(H[2, 1]))
for i in 1:2, j in 1:2
@test isequal_canonical(A[i, j], H[i, j])
end
return
end
function test_complex_sparse_arrays_dropzeros()
model = Model()
@variable(model, x)
a = 2.0 + 1.0im
for rhs in (0.0 + 0.0im, 0.0 - 0.0im, -0.0 + 0.0im, -0.0 + -0.0im)
# We need to explicitly set the .constant field to avoid a conversion to
# 0.0 + 0.0im
expr = a * x
expr.constant = rhs
@test isequal(SparseArrays.dropzeros(expr), a * x)
expr.constant = 1.0 + rhs
@test isequal(SparseArrays.dropzeros(expr), a * x + 1.0)
end
return
end
function test_complex_hermitian_constraint()
model = Model()
@variable(model, x[1:2, 1:2])
H = LinearAlgebra.Hermitian(x)
@test vectorize(H, HermitianMatrixShape(2)) ==
[x[1, 1], x[1, 2], x[2, 2], 0.0]
@constraint(model, c, H in HermitianPSDCone())
@test constraint_object(c).func == [x[1, 1], x[1, 2], x[2, 2], 0.0]
@test function_string(MIME("text/plain"), constraint_object(c)) ==
"[x[1,1] x[1,2];\n x[1,2] x[2,2]]"
return
end
end
TestComplexNumberSupport.runtests()