Please try the following function on B1=rand(3); B2=rand(3);
function phi=gradtest2(B1,B2)
d=size(B1,1);
M = stiefelfactory(d, d,2);
problem.M = M;
% Define the problem cost function
problem.cost = @cost;
function f = cost(X)
f=0;
f=f+(1/2)*norm(X(:,:,1)'*X(:,:,2)-B1,'fro')^2;
f=f+(1/2)*norm(X(:,:,2)'*X(:,:,1)-B2,'fro')^2;
end
% The gradient
problem.grad = @(X) problem.M.egrad2rgrad(X, egrad(X));
function g = egrad(X)
C=(B1'+B2)/2;
g(:,:,1)=2*X(:,:,2)*(X(:,:,2)'*X(:,:,1)-C);
C=(B2'+B1)/2;
g(:,:,2)=2*X(:,:,1)*(X(:,:,1)'*X(:,:,2)-C ) ;
end
% The Hessian
problem.hess = @(X, eta) problem.M.ehess2rhess(X, egrad(X), ehess(X, eta), eta);
function Hess = ehess(X, eta)
C=(B1'+B2)/2;
Hess(:,:,1)=2*X(:,:,2)*(eta(:,:,2)'*X(:,:,1)+X(:,:,2)'*eta(:,:,1))...
+2*eta(:,:,2)'*(X(:,:,2)'*X(:,:,1)-C);
C=(B2'+B1)/2;
Hess(:,:,2)=2*X(:,:,1)*(eta(:,:,1)'*X(:,:,2)+X(:,:,1)'*eta(:,:,2))...
+2*eta(:,:,1)'*(X(:,:,1)'*X(:,:,2)-C);
end
checkgradient(problem);
drawnow;
pause;
checkhessian(problem);
drawnow;
pause;
end
Out of bad experience, I usually do not trust my code ... so please check!
Considering the amount of time which I (and you!) have invested in this calculation again brings me back to my point of automatic differentiation - if only for problems with Frobenius norms and polynomial matrix functions. But - I understand well that this would be a major tasks, and I suppose other items are more urgent.
Thanks again for your wonderful program which deserves wide propagation!
Considering the amount of time which I (and you!) have invested in this calculation again brings me back to my point of automatic differentiation - if only for problems with Frobenius norms and polynomial matrix functions.