@@ -636,3 +636,51 @@ const char *test_wsum_hess_add_shared_subexpr(void)
636636 free_expr (f );
637637 return 0 ;
638638}
639+
640+ const char * test_wsum_hess_multiply_shared_subexpr (void )
641+ {
642+ /* multiply(log x, exp(log x)) and multiply(exp(log x), log x) with one
643+ * shared log x node: evaluating the side that contains log x re-evaluates
644+ * log x's Hessian with other weights, which must not leak into the other
645+ * side. */
646+ double u_vals [3 ] = {0.5 , 1.0 , 1.5 };
647+ double w [3 ] = {1.0 , 2.0 , 3.0 };
648+
649+ expr * x = new_variable (3 , 1 , 0 , 3 );
650+ expr * log_x = new_log (x );
651+ expr * f = new_elementwise_mult (log_x , new_exp (log_x ));
652+ mu_assert ("check_wsum_hess failed (shared node on the left)" ,
653+ check_wsum_hess (f , u_vals , w , NUMERICAL_DIFF_DEFAULT_H ));
654+ free_expr (f );
655+
656+ x = new_variable (3 , 1 , 0 , 3 );
657+ log_x = new_log (x );
658+ f = new_elementwise_mult (new_exp (log_x ), log_x );
659+ mu_assert ("check_wsum_hess failed (shared node on the right)" ,
660+ check_wsum_hess (f , u_vals , w , NUMERICAL_DIFF_DEFAULT_H ));
661+ free_expr (f );
662+ return 0 ;
663+ }
664+
665+ const char * test_wsum_hess_matmul_shared_subexpr (void )
666+ {
667+ /* log(X) @ exp(log(X)) and exp(log(X)) @ log(X) with one shared log(X)
668+ * node, X is 2x2 */
669+ double u_vals [4 ] = {0.5 , 1.0 , 1.5 , 2.0 };
670+ double w [4 ] = {1.0 , 2.0 , 3.0 , 4.0 };
671+
672+ expr * X = new_variable (2 , 2 , 0 , 4 );
673+ expr * log_X = new_log (X );
674+ expr * Z = new_matmul (log_X , new_exp (log_X ));
675+ mu_assert ("check_wsum_hess failed (shared node on the left)" ,
676+ check_wsum_hess (Z , u_vals , w , NUMERICAL_DIFF_DEFAULT_H ));
677+ free_expr (Z );
678+
679+ X = new_variable (2 , 2 , 0 , 4 );
680+ log_X = new_log (X );
681+ Z = new_matmul (new_exp (log_X ), log_X );
682+ mu_assert ("check_wsum_hess failed (shared node on the right)" ,
683+ check_wsum_hess (Z , u_vals , w , NUMERICAL_DIFF_DEFAULT_H ));
684+ free_expr (Z );
685+ return 0 ;
686+ }
0 commit comments