@@ -407,3 +407,116 @@ let
407407 @test isequal (expand_derivatives (D (Symbolics. scbrt (1 + x ^ 2 ))), simplify ((2 x) / (3 Symbolics. scbrt (1 + x^ 2 )^ 2 )))
408408 @test isequal (expand_derivatives (D (Symbolics. slog (1 + x ^ 2 ))), simplify ((2 x) / (1 + x ^ 2 )))
409409end
410+
411+ # Hessian sparsity involving unknown functions
412+ let
413+ @variables x₁ x₂ p q[1 : 1 ]
414+ expr = 3 x₁^ 2 + 4 x₁ * x₂
415+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
416+
417+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + p
418+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
419+
420+ # issue 643: example test2_num
421+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + q[1 ]
422+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
423+
424+ # Custom function: By default assumed to be non-linear
425+ myexp (x) = exp (x)
426+ @register_symbolic myexp (x)
427+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + myexp (p)
428+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
429+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + myexp (x₂)
430+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true true ]
431+
432+ mylogaddexp (x, y) = log (exp (x) + exp (y))
433+ @register_symbolic mylogaddexp (x, y)
434+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mylogaddexp (p, 2 )
435+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
436+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mylogaddexp (3 , p)
437+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
438+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mylogaddexp (p, 2 )
439+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
440+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mylogaddexp (p, q[1 ])
441+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
442+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mylogaddexp (p, x₂)
443+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true true ]
444+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mylogaddexp (x₂, 4 )
445+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true true ]
446+
447+ # Custom linear function: Possible to extend `Symbolics.linearity_1`/`Symbolics.linearity_2`
448+ myidentity (x) = x
449+ @register_symbolic myidentity (x)
450+ Symbolics. linearity_1 (:: typeof (myidentity)) = true
451+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + myidentity (p)
452+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
453+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + myidentity (q[1 ])
454+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
455+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + myidentity (x₂)
456+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
457+
458+ mymul1plog (x, y) = x * (1 + log (y))
459+ @register_symbolic mymul1plog (x, y)
460+ Symbolics. linearity_2 (:: typeof (mymul1plog)) = (true , false , false )
461+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mymul1plog (p, q[1 ])
462+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
463+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mymul1plog (x₂, q[1 ])
464+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true false ]
465+ expr = 3 x₁^ 2 + 4 x₁ * x₂ + mymul1plog (q[1 ], x₂)
466+ @test Matrix (Symbolics. hessian_sparsity (expr, [x₁, x₂])) == [true true ; true true ]
467+ end
468+
469+ # issue #555
470+ let
471+ # first example
472+ @variables p[1 : 1 ] x[1 : 1 ]
473+ p = collect (p)
474+ x = collect (x)
475+ @test collect (Symbolics. sparsehessian (p[1 ] * x[1 ], x)) == [0 ;;]
476+ @test isequal (collect (Symbolics. sparsehessian (p[1 ] * x[1 ]^ 2 , x)), [2 p[1 ];;])
477+
478+ # second example
479+ @variables a[1 : 2 ]
480+ a = collect (a)
481+ ex = (a[1 ]+ a[2 ])^ 2
482+ @test Symbolics. hessian (ex, [a[1 ]]) == [2 ;;]
483+ @test collect (Symbolics. sparsehessian (ex, [a[1 ]])) == [2 ;;]
484+ @test collect (Symbolics. sparsehessian (ex, a)) == fill (2 , 2 , 2 )
485+ end
486+
487+ # issue #847
488+ let
489+ @variables x[1 : 2 ] y[1 : 2 ]
490+ x = Symbolics. scalarize (x)
491+ y = Symbolics. scalarize (y)
492+
493+ z = (x[1 ] + x[2 ]) * (y[1 ] + y[2 ])
494+ @test Symbolics. islinear (z, x)
495+ @test Symbolics. isaffine (z, x)
496+
497+ z = (x[1 ] + x[2 ])
498+ @test Symbolics. islinear (z, x)
499+ @test Symbolics. isaffine (z, x)
500+ end
501+
502+ # issue #790
503+ let
504+ c (x) = [sum (x) - 1 ]
505+ @variables xs[1 : 2 ] ys[1 : 1 ]
506+ w = Symbolics. scalarize (xs)
507+ v = Symbolics. scalarize (ys)
508+ expr = dot (v, c (w))
509+ @test ! Symbolics. islinear (expr, w)
510+ @test Symbolics. isaffine (expr, w)
511+ @test collect (Symbolics. hessian_sparsity (expr, w)) == fill (false , 2 , 2 )
512+ end
513+
514+ # issue #749
515+ let
516+ @variables x y
517+ @register_symbolic Base. FastMath. exp_fast (x, y)
518+ expr = Base. FastMath. exp_fast (x, y)
519+ @test ! Symbolics. islinear (expr, [x, y])
520+ @test ! Symbolics. isaffine (expr, [x, y])
521+ @test collect (Symbolics. hessian_sparsity (expr, [x, y])) == fill (true , 2 , 2 )
522+ end
0 commit comments