239
239
# simplify(::Val{:*}, f::MOI.ScalarNonlinearFunction)
240
240
function test_simplify_ScalarNonlinearFunction_multiplication ()
241
241
x, y, z = MOI. VariableIndex .(1 : 3 )
242
+ sinx = op (:sin , x)
242
243
# *(x, *(y, z)) -> *(x, y, z)
243
244
@test ≈ (SymbolicAD. simplify (op (:* , x, op (:* , y, z))), op (:* , x, y, z))
244
245
# *(x, *(y, z, *(x, 2))) -> *(x, y, z, x, 2)
@@ -248,11 +249,11 @@ function test_simplify_ScalarNonlinearFunction_multiplication()
248
249
op (:* , x, y, z, x, 2 ),
249
250
)
250
251
# *(x, 3, 2) -> *(x, 6)
251
- ret = op (:* , x, 3 , 2 )
252
- @test ≈ (SymbolicAD. simplify (ret) , op (:* , x , 6 ))
252
+ @test ≈ (SymbolicAD . simplify ( op (:* , x, 3 , 2 )), 6.0 * x )
253
+ @test ≈ (SymbolicAD. simplify (op (: * , sinx, 3 , 2 )) , op (:* , sinx , 6 ))
253
254
# *(3, x, 2) -> *(6, x)
254
- ret = op (:* , 3 , x, 2 )
255
- @test ≈ (SymbolicAD. simplify (ret) , op (:* , 6 , x ))
255
+ @test ≈ (SymbolicAD . simplify ( op (:* , 3 , x, 2 )), 6.0 * x )
256
+ @test ≈ (SymbolicAD. simplify (op (: * , 3 , sinx, 2 )) , op (:* , 6 , sinx ))
256
257
# *(x, 1) -> x
257
258
ret = op (:* , x, 1 )
258
259
@test ≈ (SymbolicAD. simplify (ret), x)
@@ -272,55 +273,56 @@ end
272
273
# simplify(::Val{:+}, f::MOI.ScalarNonlinearFunction)
273
274
function test_simplify_ScalarNonlinearFunction_addition ()
274
275
x, y, z = MOI. VariableIndex .(1 : 3 )
275
- # (+(x, +(y, z)))=>(+(x, y, z)),
276
- @test ≈ (SymbolicAD. simplify (op (:+ , x, op (:+ , y, z))), op (:+ , x, y, z))
277
- # +(sin(x), -cos(x))=>sin(x)-cos(x),
278
276
sinx = op (:sin , x)
279
277
cosx = op (:cos , x)
278
+ # (+(x, +(y, z)))=>(+(x, y, z)),
279
+ @test ≈ (SymbolicAD. simplify (op (:+ , sinx, op (:+ , y, z))), op (:+ , sinx, y, z))
280
+ @test ≈ (
281
+ SymbolicAD. simplify (op (:+ , x, op (:+ , y, z))),
282
+ 1.0 * x + 1.0 * y + 1.0 * z,
283
+ )
284
+ # +(sin(x), -cos(x))=>sin(x)-cos(x),
280
285
@test ≈ (SymbolicAD. simplify (op (:+ , sinx, op (:- , cosx))), op (:- , sinx, cosx))
281
286
# (+(x, 1, 2))=>(+(x, 3)),
282
- ret = op (:+ , x, 1 , 2 )
283
- @test ≈ (SymbolicAD. simplify (ret) , op (:+ , x , 3 ))
284
- # (+(1, x, 2))=>(+(3, x)),
285
- ret = op (:+ , 1 , x, 2 )
286
- @test ≈ (SymbolicAD. simplify (ret) , op (:+ , 3 , x ))
287
+ @test ≈ (SymbolicAD . simplify ( op (:+ , x, 1 , 2 )), x + 3.0 )
288
+ @test ≈ (SymbolicAD. simplify (op (: + , sinx, 1 , 2 )) , op (:+ , sinx , 3 ))
289
+ # (+(1, x, 2))=>(+(3, x)),ret =
290
+ @test ≈ (SymbolicAD . simplify ( op (:+ , 1 , x, 2 )), x + 3.0 )
291
+ @test ≈ (SymbolicAD. simplify (op (: + , 1 , sinx, 2 )) , op (:+ , 3 , sinx ))
287
292
# +(x, 0) -> x
288
- ret = op (:+ , x, 0 )
289
- @test SymbolicAD. simplify (ret) ≈ x
293
+ @test SymbolicAD. simplify (op (:+ , x, 0 )) ≈ x
290
294
# +(0, x) -> x
291
- ret = op (:+ , 0 , x)
292
- @test SymbolicAD. simplify (ret) ≈ x
295
+ @test SymbolicAD. simplify (op (:+ , 0 , x)) ≈ x
293
296
# +(-(x, x), 0) -> 0
294
- f = op (:+ , op (:- , x, x), 0 )
295
- @test SymbolicAD. simplify (f) === false
297
+ @test SymbolicAD. simplify (op (:+ , op (:- , x, x), 0 )) === false
296
298
return
297
299
end
298
300
299
301
# simplify(::Val{:-}, f::MOI.ScalarNonlinearFunction)
300
302
function test_simplify_ScalarNonlinearFunction_subtraction ()
301
303
x, y = MOI. VariableIndex (1 ), MOI. VariableIndex (2 )
304
+ sinx = op (:sin , x)
302
305
f = op (:- , x)
303
306
# -x -> -x
304
- @test SymbolicAD. simplify (f) ≈ f
307
+ @test SymbolicAD. simplify (op (:- , x)) ≈ - 1.0 * x
308
+ @test SymbolicAD. simplify (op (:- , sinx)) ≈ op (:- , sinx)
305
309
# -(-(x)) -> x
306
310
ret = op (:- , f)
307
311
@test SymbolicAD. simplify (ret) ≈ x
308
312
# -(x, 0) -> x
309
313
ret = op (:- , x, 0 )
310
314
@test SymbolicAD. simplify (ret) ≈ x
311
315
# -(0, x) -> -x
312
- ret = op (:- , 0 , x)
313
- @test SymbolicAD. simplify (ret) ≈ f
316
+ @test SymbolicAD. simplify (op (:- , 0 , sinx)) ≈ op (:- , sinx)
314
317
# -(x, x) -> 0
315
318
ret = op (:- , x, x)
316
319
@test SymbolicAD. simplify (ret) ≈ 0
317
320
# -(x, -y) -> +(x, y)
318
- f = op (:- , x, op (:- , y))
319
- target = op (:+ , x, y)
320
- @test SymbolicAD. simplify (f) ≈ target
321
+ @test SymbolicAD. simplify (op (:- , x, op (:- , y))) ≈ 1.0 * x + 1.0 * y
322
+ @test SymbolicAD. simplify (op (:- , sinx, op (:- , y))) ≈ op (:+ , sinx, y)
321
323
# -(x, y) -> -(x, y)
322
- f = op (:- , x , y)
323
- @test SymbolicAD. simplify (f) ≈ f
324
+ @test SymbolicAD . simplify ( op (: - , sinx, y)) ≈ op (:- , sinx , y)
325
+ @test SymbolicAD. simplify (op (: - , x, y)) ≈ 1.0 * x - 1.0 * y
324
326
return
325
327
end
326
328
@@ -412,7 +414,8 @@ function test_simplify_deep()
412
414
g = op (:^ , x[i], 1 )
413
415
f = op (:+ , f, g)
414
416
end
415
- @test ≈ (SymbolicAD. simplify (f), op (:+ , convert (Vector{Any}, x)))
417
+ ret = MOI. ScalarAffineFunction (MOI. ScalarAffineTerm .(1.0 , x), 0.0 )
418
+ @test ≈ (SymbolicAD. simplify (f), ret)
416
419
return
417
420
end
418
421
@@ -700,6 +703,39 @@ function test_SymbolicAD_univariate_registered()
700
703
return
701
704
end
702
705
706
+ function test_simplify_if_affine ()
707
+ x = MOI. VariableIndex (1 )
708
+ for (f, ret) in Any[
709
+ op (:* , 2 )=> 2 ,
710
+ op (:* , 2 // 3 )=> 2 / 3 ,
711
+ op (:* , 2 , 3 )=> 6 ,
712
+ op (:* , 2 , x, 3 )=> 6 * x,
713
+ op (:+ , 2 , 3 )=> 5 ,
714
+ op (:- , 2 )=> - 2 ,
715
+ op (:- , 2 , 3 )=> - 1 ,
716
+ op (:- , x)=> - 1 * x,
717
+ op (:- , x, 2 )=> 1.0 * x- 2.0 ,
718
+ op (:- , 2 , x)=> 2.0 +- 1.0 * x,
719
+ op (:+ , 2 , x)=> 2 + x,
720
+ op (:+ , x, x)=> 2.0 * x,
721
+ op (:+ , x, 2 , x)=> 2.0 * x+ 2.0 ,
722
+ op (:+ , x, 2 , op (:+ , x))=> 2.0 * x+ 2.0 ,
723
+ # Early termination because not affine
724
+ op (:+ , op (:sin , x))=> nothing ,
725
+ op (:- , op (:sin , x))=> nothing ,
726
+ op (:- , op (:sin , x), 1 )=> nothing ,
727
+ op (:- , x, op (:sin , x))=> nothing ,
728
+ op (:* , 2 , x, 3 , x)=> nothing ,
729
+ op (:* , 2 , 3 , op (:sin , x))=> nothing ,
730
+ op (:log , x)=> nothing ,
731
+ op (:+ , big (1 ) * x, big (2 ))=> nothing ,
732
+ op (:+ , x, big (2 ))=> nothing ,
733
+ ]
734
+ @test SymbolicAD. _simplify_if_affine! (f) ≈ something (ret, f)
735
+ end
736
+ return
737
+ end
738
+
703
739
end # module
704
740
705
741
TestMathOptSymbolicAD. runtests ()
0 commit comments