forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator.jl
787 lines (730 loc) · 35.8 KB
/
operator.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
using LinearAlgebra, Test
using JuMP
using OffsetArrays
# For "DimensionMismatch when performing vector-matrix multiplication with custom types #988"
import Base: +, *
struct MyType{T}
a::T
end
struct MySumType{T}
a::T
end
Base.copy(t::MyType) = t
Base.one(::Type{MyType{T}}) where {T} = MyType(one(T))
Base.zero(::Type{MySumType{T}}) where {T} = MySumType(zero(T))
Base.zero(::MySumType{T}) where {T} = MySumType(zero(T))
Base.transpose(t::MyType) = MyType(t.a)
Base.transpose(t::MySumType) = MySumType(t.a)
LinearAlgebra.adjoint(t::Union{MyType, MySumType}) = t
+(t1::MyT, t2::MyS) where {MyT<:Union{MyType, MySumType}, MyS<:Union{MyType, MySumType}} = MySumType(t1.a+t2.a)
*(t1::MyType{S}, t2::T) where {S, T} = MyType(t1.a*t2)
*(t1::S, t2::MyType{T}) where {S, T} = MyType(t1*t2.a)
*(t1::MyType{S}, t2::MyType{T}) where {S, T} = MyType(t1.a*t2.a)
function JuMP.isequal_canonical(t::MySumType, s::MySumType)
return JuMP.isequal_canonical(t.a, s.a)
end
function JuMP.isequal_canonical(x::AbstractArray{<:MySumType},
y::AbstractArray{<:MySumType})
return size(x) == size(y) && all(JuMP.isequal_canonical.(x, y))
end
function operators_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Type{<:JuMP.AbstractVariableRef})
AffExprType = JuMP.GenericAffExpr{Float64, VariableRefType}
QuadExprType = JuMP.GenericQuadExpr{Float64, VariableRefType}
@testset "Promotion" begin
m = ModelType()
I = Int
V = VariableRefType
A = AffExprType
Q = QuadExprType
@test promote_type(V, I) == A
@test promote_type(I, V) == A
@test promote_type(A, I) == A
@test promote_type(I, A) == A
@test promote_type(A, V) == A
@test promote_type(V, A) == A
@test promote_type(Q, I) == Q
@test promote_type(I, Q) == Q
@test promote_type(Q, A) == Q
@test promote_type(A, Q) == Q
@test promote_type(Q, V) == Q
@test promote_type(V, Q) == Q
@test promote_type(Q, A) == Q
@test promote_type(A, Q) == Q
end
@testset "Basic operator overloads" begin
model = ModelType()
@variable(model, w)
@variable(model, x)
@variable(model, y)
@variable(model, z)
aff = @inferred 7.1 * x + 2.5
@test_expression_with_string 7.1 * x + 2.5 "7.1 x + 2.5"
aff2 = @inferred 1.2 * y + 1.2
@test_expression_with_string 1.2 * y + 1.2 "1.2 y + 1.2"
q = @inferred 2.5 * y * z + aff
@test_expression_with_string 2.5 * y * z + aff "2.5 y*z + 7.1 x + 2.5"
q2 = @inferred 8 * x * z + aff2
@test_expression_with_string 8 * x * z + aff2 "8 x*z + 1.2 y + 1.2"
@test_expression_with_string 2 * x * x + 1 * y * y + z + 3 "2 x² + y² + z + 3"
@testset "Comparison" begin
@testset "iszero" begin
@test !iszero(x)
@test !iszero(aff)
@test iszero(zero(aff))
@test !iszero(q)
@test iszero(zero(q))
end
@testset "isequal_canonical" begin
@test JuMP.isequal_canonical((@inferred 3w + 2y), @inferred 2y + 3w)
@test !JuMP.isequal_canonical((@inferred 3w + 2y + 1), @inferred 3w + 2y)
@test !JuMP.isequal_canonical((@inferred 3w + 2y), @inferred 3y + 2w)
@test !JuMP.isequal_canonical((@inferred 3w + 2y), @inferred 3w + y)
@test !JuMP.isequal_canonical(aff, aff2)
@test !JuMP.isequal_canonical(aff2, aff)
@test JuMP.isequal_canonical(q, @inferred 2.5z*y + aff)
@test !JuMP.isequal_canonical(q, @inferred 2.5y*z + aff2)
@test !JuMP.isequal_canonical(q, @inferred 2.5x*z + aff)
@test !JuMP.isequal_canonical(q, @inferred 2.5y*x + aff)
@test !JuMP.isequal_canonical(q, @inferred 1.5y*z + aff)
@test JuMP.isequal_canonical(q2, @inferred 8z*x + aff2)
@test !JuMP.isequal_canonical(q2, @inferred 8x*z + aff)
@test !JuMP.isequal_canonical(q2, @inferred 7x*z + aff2)
@test !JuMP.isequal_canonical(q2, @inferred 8x*y + aff2)
@test !JuMP.isequal_canonical(q2, @inferred 8y*z + aff2)
end
end
# Different objects that must all interact:
# 1. Number
# 2. Variable
# 3. AffExpr
# 4. QuadExpr
# 1. Number tests
@testset "Number--???" begin
# 1-1 Number--Number - nope!
# 1-2 Number--Variable
@test_expression_with_string 4.13 + w "w + 4.13"
@test_expression_with_string 3.16 - w "-w + 3.16"
@test_expression_with_string 5.23 * w "5.23 w"
@test_throws ErrorException 2.94 / w
# 1-3 Number--AffExpr
@test_expression_with_string 1.5 + aff "7.1 x + 4"
@test_expression_with_string 1.5 - aff "-7.1 x - 1"
@test_expression_with_string 2 * aff "14.2 x + 5"
@test_throws ErrorException 2 / aff
# 1-4 Number--QuadExpr
@test_expression_with_string 1.5 + q "2.5 y*z + 7.1 x + 4"
@test_expression_with_string 1.5 - q "-2.5 y*z - 7.1 x - 1"
@test_expression_with_string 2 * q "5 y*z + 14.2 x + 5"
@test_throws ErrorException 2 / q
end
# 2. Variable tests
@testset "Variable--???" begin
# 2-0 Variable unary
@test (+x) === x
@test_expression_with_string -x "-x"
# 2-1 Variable--Number
@test_expression_with_string w + 4.13 "w + 4.13"
@test_expression_with_string w - 4.13 "w - 4.13"
@test_expression_with_string w * 4.13 "4.13 w"
@test_expression_with_string w / 2.00 "0.5 w"
@test w == w
@test_expression_with_string x*y - 1 "x*y - 1"
@test_expression_with_string x^2 "x²"
@test_expression_with_string x^1 "x"
@test_expression_with_string x^0 "1"
@test_throws ErrorException x^3
@test_throws ErrorException x^1.5
# 2-2 Variable--Variable
@test_expression_with_string w + x "w + x"
@test_expression_with_string w - x "w - x"
@test_expression_with_string w * x "w*x"
@test_expression_with_string x - x "0 x"
@test_throws ErrorException w / x
@test_expression_with_string y*z - x "y*z - x"
# 2-3 Variable--AffExpr
@test_expression_with_string z + aff "z + 7.1 x + 2.5"
@test_expression_with_string z - aff "z - 7.1 x - 2.5"
@test_expression_with_string z * aff "7.1 z*x + 2.5 z"
@test_throws ErrorException z / aff
@test_throws MethodError z ≤ aff
@test_expression_with_string 7.1 * x - aff "0 x - 2.5"
# 2-4 Variable--QuadExpr
@test_expression_with_string w + q "2.5 y*z + w + 7.1 x + 2.5"
@test_expression_with_string w - q "-2.5 y*z + w - 7.1 x - 2.5"
@test_throws ErrorException w*q
@test_throws ErrorException w/q
end
# 3. AffExpr tests
@testset "AffExpr--???" begin
# 3-0 AffExpr unary
@test_expression_with_string +aff "7.1 x + 2.5"
@test_expression_with_string -aff "-7.1 x - 2.5"
# 3-1 AffExpr--Number
@test_expression_with_string aff + 1.5 "7.1 x + 4"
@test_expression_with_string aff - 1.5 "7.1 x + 1"
@test_expression_with_string aff * 2 "14.2 x + 5"
@test_expression_with_string aff / 2 "3.55 x + 1.25"
@test_throws MethodError aff ≤ 1
@test aff == aff
@test_throws MethodError aff ≥ 1
@test_expression_with_string aff - 1 "7.1 x + 1.5"
@test_expression_with_string aff^2 "50.41 x² + 35.5 x + 6.25"
@test_expression_with_string (7.1*x + 2.5)^2 "50.41 x² + 35.5 x + 6.25"
@test_expression_with_string aff^1 "7.1 x + 2.5"
@test_expression_with_string (7.1*x + 2.5)^1 "7.1 x + 2.5"
@test_expression_with_string aff^0 "1"
@test_expression_with_string (7.1*x + 2.5)^0 "1"
@test_throws ErrorException aff^3
@test_throws ErrorException (7.1*x + 2.5)^3
@test_throws ErrorException aff^1.5
@test_throws ErrorException (7.1*x + 2.5)^1.5
# 3-2 AffExpr--Variable
@test_expression_with_string aff + z "7.1 x + z + 2.5"
@test_expression_with_string aff - z "7.1 x - z + 2.5"
@test_expression_with_string aff * z "7.1 x*z + 2.5 z"
@test_throws ErrorException aff/z
@test_expression_with_string aff - 7.1 * x "0 x + 2.5"
# 3-3 AffExpr--AffExpr
@test_expression_with_string aff + aff2 "7.1 x + 1.2 y + 3.7"
@test_expression_with_string aff - aff2 "7.1 x - 1.2 y + 1.3"
@test_expression_with_string aff * aff2 "8.52 x*y + 3 y + 8.52 x + 3"
@test string((x+x)*(x+3)) == string((x+3)*(x+x)) # Issue #288
@test_throws ErrorException aff/aff2
@test_expression_with_string aff-aff "0 x"
# 4-4 AffExpr--QuadExpr
@test_expression_with_string aff2 + q "2.5 y*z + 1.2 y + 7.1 x + 3.7"
@test_expression_with_string aff2 - q "-2.5 y*z + 1.2 y - 7.1 x - 1.3"
@test_throws ErrorException aff2 * q
@test_throws ErrorException aff2 / q
end
# 4. QuadExpr
# TODO: This test block and others above should be rewritten to be
# self-contained. The definitions of q, w, and aff2 are too far to
# easily check correctness of the tests.
@testset "QuadExpr--???" begin
# 4-0 QuadExpr unary
@test_expression_with_string +q "2.5 y*z + 7.1 x + 2.5"
@test_expression_with_string -q "-2.5 y*z - 7.1 x - 2.5"
# 4-1 QuadExpr--Number
@test_expression_with_string q + 1.5 "2.5 y*z + 7.1 x + 4"
@test_expression_with_string q - 1.5 "2.5 y*z + 7.1 x + 1"
@test_expression_with_string q * 2 "5 y*z + 14.2 x + 5"
@test_expression_with_string q / 2 "1.25 y*z + 3.55 x + 1.25"
@test q == q
@test_expression_with_string aff2 - q "-2.5 y*z + 1.2 y - 7.1 x - 1.3"
# 4-2 QuadExpr--Variable
@test_expression_with_string q + w "2.5 y*z + 7.1 x + w + 2.5"
@test_expression_with_string q - w "2.5 y*z + 7.1 x - w + 2.5"
@test_throws ErrorException q*w
@test_throws ErrorException q/w
# 4-3 QuadExpr--AffExpr
@test_expression_with_string q + aff2 "2.5 y*z + 7.1 x + 1.2 y + 3.7"
@test_expression_with_string q - aff2 "2.5 y*z + 7.1 x - 1.2 y + 1.3"
@test_throws ErrorException q * aff2
@test_throws ErrorException q / aff2
# 4-4 QuadExpr--QuadExpr
@test_expression_with_string q + q2 "2.5 y*z + 8 x*z + 7.1 x + 1.2 y + 3.7"
@test_expression_with_string q - q2 "2.5 y*z - 8 x*z + 7.1 x - 1.2 y + 1.3"
@test_throws ErrorException q * q2
@test_throws ErrorException q / q2
end
end
@testset "Higher-level operators" begin
model = ModelType()
@testset "sum" begin
sum_m = ModelType()
@variable(sum_m, 0 ≤ matrix[1:3,1:3] ≤ 1, start = 1)
@testset "sum(j::DenseAxisArray{Variable})" begin
@test_expression_with_string sum(matrix) "matrix[1,1] + matrix[2,1] + matrix[3,1] + matrix[1,2] + matrix[2,2] + matrix[3,2] + matrix[1,3] + matrix[2,3] + matrix[3,3]"
end
@testset "sum(j::DenseAxisArray{T}) where T<:Real" begin
@test sum(JuMP.start_value.(matrix)) ≈ 9
end
@testset "sum(j::Array{VariableRef})" begin
@test string(sum(matrix[1:3,1:3])) == string(sum(matrix))
end
@testset "sum(affs::Array{AffExpr})" begin
@test_expression_with_string sum([2*matrix[i,j] for i in 1:3, j in 1:3]) "2 matrix[1,1] + 2 matrix[2,1] + 2 matrix[3,1] + 2 matrix[1,2] + 2 matrix[2,2] + 2 matrix[3,2] + 2 matrix[1,3] + 2 matrix[2,3] + 2 matrix[3,3]"
end
@testset "sum(quads::Array{QuadExpr})" begin
@test_expression_with_string sum([2*matrix[i,j]^2 for i in 1:3, j in 1:3]) "2 matrix[1,1]² + 2 matrix[2,1]² + 2 matrix[3,1]² + 2 matrix[1,2]² + 2 matrix[2,2]² + 2 matrix[3,2]² + 2 matrix[1,3]² + 2 matrix[2,3]² + 2 matrix[3,3]²"
end
S = [1,3]
@variable(sum_m, x[S], start=1)
@testset "sum(j::JuMPDict{VariableRef})" begin
@test_expression sum(x)
@test length(string(sum(x))) == 11 # order depends on hashing
@test occursin("x[1]",string(sum(x)))
@test occursin("x[3]",string(sum(x)))
end
@testset "sum(j::JuMPDict{T}) where T<:Real" begin
@test sum(JuMP.start_value.(x)) == 2
end
end
@testset "dot" begin
dot_m = ModelType()
@variable(dot_m, 0 ≤ x[1:3] ≤ 1)
@test_expression_with_string dot(x[1],x[1]) "x[1]²"
@test_expression_with_string dot(2,x[1]) "2 x[1]"
@test_expression_with_string dot(x[1],2) "2 x[1]"
c = vcat(1:3)
@test_expression_with_string dot(c,x) "x[1] + 2 x[2] + 3 x[3]"
@test_expression_with_string dot(x,c) "x[1] + 2 x[2] + 3 x[3]"
A = [1 3 ; 2 4]
@variable(dot_m, 1 ≤ y[1:2,1:2] ≤ 1)
@test_expression_with_string dot(A,y) "y[1,1] + 2 y[2,1] + 3 y[1,2] + 4 y[2,2]"
@test_expression_with_string dot(y,A) "y[1,1] + 2 y[2,1] + 3 y[1,2] + 4 y[2,2]"
B = ones(2,2,2)
@variable(dot_m, 0 ≤ z[1:2,1:2,1:2] ≤ 1)
@test_expression_with_string dot(B,z) "z[1,1,1] + z[2,1,1] + z[1,2,1] + z[2,2,1] + z[1,1,2] + z[2,1,2] + z[1,2,2] + z[2,2,2]"
@test_expression_with_string dot(z,B) "z[1,1,1] + z[2,1,1] + z[1,2,1] + z[2,2,1] + z[1,1,2] + z[2,1,2] + z[1,2,2] + z[2,2,2]"
@objective(dot_m, Max, dot(x, ones(3)) - dot(y, ones(2,2)))
for i in 1:3
JuMP.set_start_value(x[i], 1)
end
for i in 1:2, j in 1:2
JuMP.set_start_value(y[i,j], 1)
end
for i in 1:2, j in 1:2, k in 1:2
JuMP.set_start_value(z[i,j,k], 1)
end
@test dot(c, JuMP.start_value.(x)) ≈ 6
@test dot(A, JuMP.start_value.(y)) ≈ 10
@test dot(B, JuMP.start_value.(z)) ≈ 8
@testset "JuMP issue #656" begin
issue656 = ModelType()
@variable(issue656, x)
floats = Float64[i for i in 1:2]
anys = Array{Any}(undef, 2)
anys[1] = 10
anys[2] = 20 + x
@test dot(floats, anys) == 10 + 40 + 2x
end
if ModelType <: Model
# Only `Model` is guaranteed to have `operator_counter`, so
# only test for that case.
@testset "dot doesn't trigger operator_counter" begin
# Check that dot is not falling back to default, inefficient
# addition (JuMP PR #943).
model = ModelType()
@test model.operator_counter == 0
@variable(model, x[1:100])
JuMP.set_start_value.(x, 1:100)
@expression(model, test_sum, sum(x[i] * i for i in 1:100))
@expression(model, test_dot1, dot(x, 1:100))
@expression(model, test_dot2, dot(1:100, x))
@test model.operator_counter == 0
test_add = test_dot1 + test_dot2
@test model.operator_counter == 1 # Check triggerable.
test_sum_value = JuMP.value(test_sum, JuMP.start_value)
@test test_sum_value ≈ JuMP.value(test_dot1, JuMP.start_value)
@test test_sum_value ≈ JuMP.value(test_dot2, JuMP.start_value)
end
end
end
end
@testset "Vectorized operations" begin
@testset "Transpose" begin
m = ModelType()
@variable(m, x[1:3])
@variable(m, y[1:2,1:3])
@variable(m, z[2:5])
@test JuMP.isequal_canonical(x', [x[1] x[2] x[3]])
@test JuMP.isequal_canonical(copy(transpose(x)), [x[1] x[2] x[3]])
@test JuMP.isequal_canonical(y', [y[1,1] y[2,1]
y[1,2] y[2,2]
y[1,3] y[2,3]])
@test JuMP.isequal_canonical(copy(transpose(y)),
[y[1,1] y[2,1]
y[1,2] y[2,2]
y[1,3] y[2,3]])
@test (z')' == z
@test transpose(transpose(z)) == z
end
@testset "Vectorized arithmetic" begin
model = ModelType()
@variable(model, x[1:3])
A = [2 1 0
1 2 1
0 1 2]
B = sparse(A)
@variable(model, X11)
@variable(model, X23)
X = sparse([1, 2], [1, 3], [X11, X23], 3, 3) # for testing Variable
@test JuMP.isequal_canonical([X11 0. 0.; 0. 0. X23; 0. 0. 0.], @inferred JuMP._densify_with_jump_eltype(X))
@variable(model, Xd[1:3, 1:3])
Y = sparse([1, 2], [1, 3], [2X11, 4X23], 3, 3) # for testing GenericAffExpr
Yd = [2X11 0 0
0 0 4X23
0 0 0]
Z = sparse([1, 2], [1, 3], [X11^2, 2X23^2], 3, 3) # for testing GenericQuadExpr
Zd = [X11^2 0 0
0 0 2X23^2
0 0 0]
v = [4, 5, 6]
@testset "Sum of matrices" begin
@test_expression(Xd + Yd)
@test_expression(Xd + 2Yd)
@test_expression(Xd + Yd * 2)
@test_expression(Yd + Xd)
@test_expression(Yd + 2Xd)
@test_expression(Yd + Xd * 2)
@test_expression(Yd + Zd)
@test_expression(Yd + 2Zd)
@test_expression(Yd + Zd * 2)
@test_expression(Zd + Yd)
@test_expression(Zd + 2Yd)
@test_expression(Zd + Yd * 2)
@test_expression(Zd + Xd)
@test_expression(Zd + 2Xd)
@test_expression(Zd + Xd * 2)
@test_expression(Xd + Zd)
@test_expression(Xd + 2Zd)
@test_expression(Xd + Zd * 2)
end
@test JuMP.isequal_canonical(A*x, [2x[1] + x[2]
2x[2] + x[1] + x[3]
x[2] + 2x[3]])
@test JuMP.isequal_canonical(A*x, B*x)
@test JuMP.isequal_canonical(A*x, JuMP.@_build_expression(B*x))
@test JuMP.isequal_canonical(JuMP.@_build_expression(A*x), JuMP.@_build_expression(B*x))
@test JuMP.isequal_canonical(x'*A, [2x[1]+x[2]; 2x[2]+x[1]+x[3]; x[2]+2x[3]]')
@test JuMP.isequal_canonical(x'*A, x'*B)
@test JuMP.isequal_canonical(x'*A, JuMP.@_build_expression(x'*B))
@test JuMP.isequal_canonical(JuMP.@_build_expression(x'*A), JuMP.@_build_expression(x'*B))
@test JuMP.isequal_canonical(x'*A*x, 2x[1]*x[1] + 2x[1]*x[2] + 2x[2]*x[2] + 2x[2]*x[3] + 2x[3]*x[3])
@test JuMP.isequal_canonical(x'A*x, x'*B*x)
@test JuMP.isequal_canonical(x'*A*x, JuMP.@_build_expression(x'*B*x))
@test JuMP.isequal_canonical(JuMP.@_build_expression(x'*A*x), JuMP.@_build_expression(x'*B*x))
y = A*x
@test JuMP.isequal_canonical(-x, [-x[1], -x[2], -x[3]])
@test JuMP.isequal_canonical(-y, [-2x[1] - x[2]
-x[1] - 2x[2] - x[3]
-x[2] - 2x[3]])
@test JuMP.isequal_canonical(y .+ 1, [2x[1] + x[2] + 1
x[1] + 2x[2] + x[3] + 1
x[2] + 2x[3] + 1])
@test JuMP.isequal_canonical(y .- 1, [2x[1] + x[2] - 1
x[1] + 2x[2] + x[3] - 1
x[2] + 2x[3] - 1])
@test JuMP.isequal_canonical(y .+ 2ones(3), [2x[1] + x[2] + 2
x[1] + 2x[2] + x[3] + 2
x[2] + 2x[3] + 2])
@test JuMP.isequal_canonical(y .- 2ones(3), [2x[1] + x[2] - 2
x[1] + 2x[2] + x[3] - 2
x[2] + 2x[3] - 2])
@test JuMP.isequal_canonical(2ones(3) .+ y, [2x[1] + x[2] + 2
x[1] + 2x[2] + x[3] + 2
x[2] + 2x[3] + 2])
@test JuMP.isequal_canonical(2ones(3) .- y, [-2x[1] - x[2] + 2
-x[1] - 2x[2] - x[3] + 2
-x[2] - 2x[3] + 2])
@test JuMP.isequal_canonical(y .+ x, [3x[1] + x[2]
x[1] + 3x[2] + x[3]
x[2] + 3x[3]])
@test JuMP.isequal_canonical(x .+ y, [3x[1] + x[2]
x[1] + 3x[2] + x[3]
x[2] + 3x[3]])
@test JuMP.isequal_canonical(2y .+ 2x, [6x[1] + 2x[2]
2x[1] + 6x[2] + 2x[3]
2x[2] + 6x[3]])
@test JuMP.isequal_canonical(y .- x, [ x[1] + x[2]
x[1] + x[2] + x[3]
x[2] + x[3]])
@test JuMP.isequal_canonical(x .- y, [-x[1] - x[2]
-x[1] - x[2] - x[3]
-x[2] - x[3]])
@test JuMP.isequal_canonical(y .+ x[:], [3x[1] + x[2]
x[1] + 3x[2] + x[3]
x[2] + 3x[3]])
@test JuMP.isequal_canonical(x[:] .+ y, [3x[1] + x[2]
x[1] + 3x[2] + x[3]
x[2] + 3x[3]])
@test JuMP.isequal_canonical(JuMP.@_build_expression(A*x/2), A*x/2)
@test JuMP.isequal_canonical(X*v, [4X11; 6X23; 0])
@test JuMP.isequal_canonical(v'*X, [4X11 0 5X23])
@test JuMP.isequal_canonical(copy(transpose(v))*X, [4X11 0 5X23])
@test JuMP.isequal_canonical(X'*v, [4X11; 0; 5X23])
@test JuMP.isequal_canonical(copy(transpose(X))*v, [4X11; 0; 5X23])
@test JuMP.isequal_canonical(X*A, [2X11 X11 0
0 X23 2X23
0 0 0 ])
@test JuMP.isequal_canonical(A*X, [2X11 0 X23
X11 0 2X23
0 0 X23])
@test JuMP.isequal_canonical(A*X', [2X11 0 0
X11 X23 0
0 2X23 0])
@test JuMP.isequal_canonical(X'*A, [2X11 X11 0
0 0 0
X23 2X23 X23])
@test JuMP.isequal_canonical(copy(transpose(X))*A, [2X11 X11 0
0 0 0
X23 2X23 X23])
@test JuMP.isequal_canonical(A'*X, [2X11 0 X23
X11 0 2X23
0 0 X23])
@test JuMP.isequal_canonical(copy(transpose(X))*A, X'*A)
@test JuMP.isequal_canonical(copy(transpose(A))*X, A'*X)
@test JuMP.isequal_canonical(X*A, X*B)
@test JuMP.isequal_canonical(Y'*A, copy(transpose(Y))*A)
@test JuMP.isequal_canonical(A*Y', A*copy(transpose(Y)))
@test JuMP.isequal_canonical(Z'*A, copy(transpose(Z))*A)
@test JuMP.isequal_canonical(Xd'*Y, copy(transpose(Xd))*Y)
@test JuMP.isequal_canonical(Y'*Xd, copy(transpose(Y))*Xd)
@test JuMP.isequal_canonical(Xd'*Xd, copy(transpose(Xd))*Xd)
@test JuMP.isequal_canonical(A*X, B*X)
@test JuMP.isequal_canonical(A*X', B*X')
@test JuMP.isequal_canonical(A'*X, B'*X)
end
@testset "Dot-ops" begin
m = ModelType()
@variable(m, x[1:2,1:2])
A = [1 2;
3 4]
B = sparse(A)
y = SparseMatrixCSC(2, 2, copy(B.colptr), copy(B.rowval), vec(x))
@test JuMP.isequal_canonical(A.+x, [1+x[1,1] 2+x[1,2];
3+x[2,1] 4+x[2,2]])
@test JuMP.isequal_canonical(A.+x, B.+x)
@test JuMP.isequal_canonical(A.+x, A.+y)
@test JuMP.isequal_canonical(A.+y, B.+y)
@test JuMP.isequal_canonical(x.+A, [1+x[1,1] 2+x[1,2];
3+x[2,1] 4+x[2,2]])
@test JuMP.isequal_canonical(x.+A, x.+B)
@test JuMP.isequal_canonical(x.+A, y.+A)
@test JuMP.isequal_canonical(x .+ x, [2x[1,1] 2x[1,2]; 2x[2,1] 2x[2,2]])
@test JuMP.isequal_canonical(y.+A, y.+B)
@test JuMP.isequal_canonical(A.-x, [1-x[1,1] 2-x[1,2];
3-x[2,1] 4-x[2,2]])
@test JuMP.isequal_canonical(A.-x, B.-x)
@test JuMP.isequal_canonical(A.-x, A.-y)
@test JuMP.isequal_canonical(x .- x, [zero(AffExprType) for _1 in 1:2, _2 in 1:2])
@test JuMP.isequal_canonical(A.-y, B.-y)
@test JuMP.isequal_canonical(x.-A, [-1+x[1,1] -2+x[1,2];
-3+x[2,1] -4+x[2,2]])
@test JuMP.isequal_canonical(x.-A, x.-B)
@test JuMP.isequal_canonical(x.-A, y.-A)
@test JuMP.isequal_canonical(y.-A, y.-B)
@test JuMP.isequal_canonical(A.*x, [1*x[1,1] 2*x[1,2];
3*x[2,1] 4*x[2,2]])
@test JuMP.isequal_canonical(A.*x, B.*x)
@test JuMP.isequal_canonical(A.*x, A.*y)
@test JuMP.isequal_canonical(A.*y, B.*y)
@test JuMP.isequal_canonical(x.*A, [1*x[1,1] 2*x[1,2];
3*x[2,1] 4*x[2,2]])
@test JuMP.isequal_canonical(x.*A, x.*B)
@test JuMP.isequal_canonical(x.*A, y.*A)
@test JuMP.isequal_canonical(y.*A, y.*B)
@test JuMP.isequal_canonical(x .* x, [x[1,1]^2 x[1,2]^2; x[2,1]^2 x[2,2]^2])
@test_throws ErrorException JuMP.isequal_canonical(A./x, [1*x[1,1] 2*x[1,2];
3*x[2,1] 4*x[2,2]])
@test JuMP.isequal_canonical(x./A, [1/1*x[1,1] 1/2*x[1,2];
1/3*x[2,1] 1/4*x[2,2]])
@test JuMP.isequal_canonical(x./A, x./B)
@test JuMP.isequal_canonical(x./A, y./A)
@test_throws ErrorException A./y
@test_throws ErrorException B./y
# TODO: Refactor to avoid calling the internal JuMP function
# `_densify_with_jump_eltype`.
z = JuMP._densify_with_jump_eltype((2 .* y) ./ 3)
@test JuMP.isequal_canonical((2 .* x) ./ 3, z)
z = JuMP._densify_with_jump_eltype(2 * (y ./ 3))
@test JuMP.isequal_canonical(2 .* (x ./ 3), z)
z = JuMP._densify_with_jump_eltype((x[1,1],) .* B)
@test JuMP.isequal_canonical((x[1,1],) .* A, z)
end
@testset "Vectorized comparisons" begin
m = ModelType()
@variable(m, x[1:3])
A = [1 2 3
0 4 5
6 0 7]
B = sparse(A)
# force vector output
cref1 = @constraint(m, reshape(x, (1, 3)) * A * x .>= 1)
c1 = JuMP.constraint_object.(cref1)
f1 = map(c -> c.func, c1)
@test JuMP.isequal_canonical(f1, [x[1]*x[1] + 2x[1]*x[2] + 4x[2]*x[2] + 9x[1]*x[3] + 5x[2]*x[3] + 7x[3]*x[3]])
@test all(c -> c.set.lower == 1, c1)
cref2 = @constraint(m, x'*A*x >= 1)
c2 = JuMP.constraint_object.(cref2)
@test JuMP.isequal_canonical(f1[1], c2.func)
mat = [ 3x[1] + 12x[3] + 4x[2]
2x[1] + 12x[2] + 10x[3]
15x[1] + 5x[2] + 21x[3]]
cref3 = @constraint(m, (x'A)' + 2A*x .<= 1)
c3 = JuMP.constraint_object.(cref3)
f3 = map(c->c.func, c3)
@test JuMP.isequal_canonical(f3, mat)
@test all(c -> c.set.upper == 1, c3)
@test JuMP.isequal_canonical((x'A)' + 2A*x, (x'A)' + 2B*x)
@test JuMP.isequal_canonical((x'A)' + 2A*x, (x'B)' + 2A*x)
@test JuMP.isequal_canonical((x'A)' + 2A*x, (x'B)' + 2B*x)
@test JuMP.isequal_canonical((x'A)' + 2A*x, JuMP.@_build_expression((x'A)' + 2A*x))
@test JuMP.isequal_canonical((x'A)' + 2A*x, JuMP.@_build_expression((x'B)' + 2A*x))
@test JuMP.isequal_canonical((x'A)' + 2A*x, JuMP.@_build_expression((x'A)' + 2B*x))
@test JuMP.isequal_canonical((x'A)' + 2A*x, JuMP.@_build_expression((x'B)' + 2B*x))
cref4 = @constraint(m, -1 .<= (x'A)' + 2A*x .<= 1)
c4 = JuMP.constraint_object.(cref4)
f4 = map(c->c.func, c4)
@test JuMP.isequal_canonical(f4, mat)
@test all(c -> c.set.lower == -1, c4)
@test all(c -> c.set.upper == 1, c4)
cref5 = @constraint(m, -[1:3;] .<= (x'A)' + 2A*x .<= 1)
c5 = JuMP.constraint_object.(cref5)
f5 = map(c->c.func, c5)
@test JuMP.isequal_canonical(f5, mat)
@test map(c -> c.set.lower, c5) == -[1:3;]
@test all(c -> c.set.upper == 1, c4)
cref6 = @constraint(m, -[1:3;] .<= (x'A)' + 2A*x .<= [3:-1:1;])
c6 = JuMP.constraint_object.(cref6)
f6 = map(c->c.func, c6)
@test JuMP.isequal_canonical(f6, mat)
@test map(c -> c.set.lower, c6) == -[1:3;]
@test map(c -> c.set.upper, c6) == [3:-1:1;]
cref7 = @constraint(m, -[1:3;] .<= (x'A)' + 2A*x .<= 3)
c7 = JuMP.constraint_object.(cref7)
f7 = map(c->c.func, c7)
@test JuMP.isequal_canonical(f7, mat)
@test map(c -> c.set.lower, c7) == -[1:3;]
@test all(c -> c.set.upper == 3, c7)
end
end
@testset "Operators for non-Array AbstractArrays" begin
m = ModelType()
@variable(m, x[1:3])
# This is needed to compare arrays that have nonstandard indexing
elements_equal(A::AbstractArray{T, N}, B::AbstractArray{T, N}) where {T, N} = all(a == b for (a, b) in zip(A, B))
for x2 in (OffsetArray(x, -length(x)), view(x, :), sparse(x))
@test elements_equal(+x, +x2)
@test elements_equal(-x, -x2)
@test elements_equal(x .+ first(x), x2 .+ first(x2))
@test elements_equal(x .- first(x), x2 .- first(x2))
@test elements_equal(first(x) .- x, first(x2) .- x2)
@test elements_equal(first(x) .+ x, first(x2) .+ x2)
@test elements_equal(2 .* x, 2 .* x2)
@test elements_equal(first(x) .+ x2, first(x2) .+ x)
@test sum(x) == sum(x2)
if !JuMP._one_indexed(x2)
@test_throws DimensionMismatch x + x2
end
end
end
@testset "diagm for non-Array AbstractArrays" begin
m = ModelType()
@variable(m, x[1:3])
for x2 in (OffsetArray(x, -length(x)), view(x, :), sparse(x))
if !JuMP._one_indexed(x2)
@test_throws AssertionError diagm(x2)
else
@test diagm(x) == diagm(x2)
end
end
end
@testset "Custom types" begin
ElemT = MySumType{AffExprType}
model = ModelType()
@variable model Q[1:3, 1:3] PSD
@testset "DimensionMismatch when performing vector-matrix multiplication #988" begin
x = [MyType(1), MyType(2), MyType(3)]
y = Q * x
z = x' * Q
@test y isa Vector{ElemT}
@test size(y) == (3,)
@test z isa Adjoint{ElemT, <:Any}
@test size(z) == (1, 3)
for i in 1:3
# Q is symmetric
a = zero(AffExprType)
a += Q[1,i]
a += 2Q[2,i]
a += 3Q[3,i]
# Q[1,i] + 2Q[2,i] + 3Q[3,i] is rearranged as 2 Q[2,3] + Q[1,3] + 3 Q[3,3]
@test z[i].a == y[i].a == a
end
end
@testset "Matrix multiplication #1990" begin
X = MyType.((1:3)' .+ (1:3))
@test_expression Q * X
Y = Q * X
@test_expression X' * Q
Z = X' * Q
@test Y isa Matrix{ElemT}
@test size(Y) == (3, 3)
@test Z isa Matrix{ElemT}
@test size(Z) == (3, 3)
@test JuMP.isequal_canonical(Z', Y)
@test_expression Q * X'
Y = Q * X'
@test_expression X * Q
Z = X * Q
@test Y isa Matrix{ElemT}
@test size(Y) == (3, 3)
@test Z isa Matrix{ElemT}
@test size(Z) == (3, 3)
@test JuMP.isequal_canonical(Z', Y)
end
end
@testset "operator_warn" begin
model = ModelType()
@variable model x[1:51]
# JuMPExtension does not have the `operator_counter` field
if ModelType <: Model
@test model.operator_counter == 0
end
# Triggers the increment of operator_counter since sum(x) has more than 50 terms
@test_expression(sum(x) + 2x[1])
if ModelType <: Model
# The following check verifies that this test covers the code incrementing `operator_counter`
@test model.operator_counter == 1
end
end
@testset "Symmetric Matrix" begin
model = ModelType()
Q = @variable(model, [1:2, 1:2], Symmetric)
@test_expression 2Q
# See https://github.com/JuliaLang/julia/issues/32374
@test_expression -Q
end
@testset "UniformScaling" begin
model = ModelType()
@testset "Scalar" begin
@variable(model, x)
@test_expression_with_string x + 2I "x + 2"
@test_expression_with_string (x + 1) + I "x + 2"
@test_expression_with_string x - 2I "x - 2"
@test_expression_with_string (x - 1) - I "x - 2"
@test_expression_with_string 2I + x "x + 2"
@test_expression_with_string I + (x + 1) "x + 2"
@test_expression_with_string 2I - x "-x + 2"
@test_expression_with_string I - (x - 1) "-x + 2"
@test_expression_with_string I * x "x"
@test_expression_with_string I * (x + 1) "x + 1"
@test_expression_with_string (x + 1) * I "x + 1"
end
@testset "Matrix $(typeof(x))" for x in [@variable(model, [1:2, 1:2]),
@variable(model, [1:2, 1:2], Symmetric)]
@test_expression x + 2I
@test_expression (x .+ 1) + I
@test_expression x - 2I
@test_expression (x .- 1) - I
@test_expression 2I + x
@test_expression I + (x .+ 1)
@test_expression 2I - x
@test_expression I - (x .- 1)
@test_expression I * x
@test_expression I * (x .+ 1)
@test_expression (x .+ 1) * I
@test_expression (x .+ 1) + I * I
@test_expression (x .+ 1) + 2 * I
@test_expression (x .+ 1) + I * 2
end
end
end
@testset "Operators for JuMP.Model" begin
operators_test(Model, VariableRef)
end
@testset "Operators for JuMPExtension.MyModel" begin
operators_test(JuMPExtension.MyModel, JuMPExtension.MyVariableRef)
end