MATLAB functions for exponential and logarithmic maps on a unit hypersphere

Please cite the following paper if you use this code:

A. Patel and W.A.P. Smith, Exploring the Identity Manifold: Constrained Operations in Face Space. In Proc. ECCV, pages 112-125, 2010.

EXP Map

function x = exp_map( b,p )
% EXP_MAP The exponential map for n-spheres
% b is the base point (vector in R^n), norm(b)=1
% p is a point on the tangent plane to the hypersphere at b (also a vector in R^n)

if (b==p)
x=b;
else
theta = norm(b-p);
dminusbx = sqrt(2-2.*cos(pi-theta));
l = 2.*sin(theta/2);
alpha = acos( (4+dminusbx.^2-l.^2)./(4*dminusbx) );
dpb = 2.*tan(alpha);
v = b + ((p-b)./norm(p-b)).*dpb;
x = ((v+b)./norm(v+b)).*dminusbx-b;
end

LOG Map

function p = log_map( b,x )
% LOG_MAP The log map for n-spheres
% b is the base point (vector in R^n), norm(b)=1
% x is a point on the hypersphere (also a vector in R^n), norm(x)=1

if (x==b)
p = b;
else
theta = acos(dot(b,x));
alpha = acos((4+norm(b+x).^2-norm(x-b).^2)./(2.*2.*norm(b+x)));
p2 = (2.*(b+x))./(norm(b+x).*cos(alpha)) - b;
p = b+((p2-b)./norm(p2-b)).*theta;
end