function p = v3_getPdf_VMF(v, mu, kappa)
%V3_GETPDF_VMF Probability in von Mises-Fisher distribution (2-sphere).
%   P = V3_GETPDF_VMF(V, MU, KAPPA) returns the probability density of a
%   unit 3-vector (1x3 vector) in a von Mises-Fisher distribution of
%   parameters mu (1x3 vector) and kappa (scalar).
%
%   V can contain several vectors (Nx3), P is then of dimension 1xN.
%
%   V and MU must be of unit-length.

%   Author: Damien Teney

% Normalize input vectors
%mu = v3_getUnit(mu);
%v = v3_getUnit(v);

% Make sure kappa is an integer
kappa = round(kappa);

% See R. Detry's thesis, formula 1.20 p.14

% Get the normalization constant
%normalizationConstant = 1 / (exp(1)^kappa - exp(1)^(-kappa)); % Approximation
%normalizationConstant = kappa / (2 * pi * (exp(kappa) - exp(-kappa))); % Correct (?) (< R. Detry's thesis)
%p = 2; normalizationConstant = (1/(((2*pi)^(p/2))*besseli((p/2)-1, kappa)))*(kappa^((p/2)-1)); % Correct value
% Precomputed values (1:512)
normalizationConstants = [1.840655e-001, 3.242487e-001, 4.786513e-001, 6.368334e-001, 7.958108e-001, 9.549355e-001, 1.114086e+000, 1.273240e+000, 1.432395e+000, 1.591549e+000, 1.750704e+000, 1.909859e+000, 2.069014e+000, 2.228169e+000, 2.387324e+000, 2.546479e+000, 2.705634e+000, 2.864789e+000, 3.023944e+000, 3.183099e+000, 3.342254e+000, 3.501409e+000, 3.660564e+000, 3.819719e+000, 3.978874e+000, 4.138029e+000, 4.297183e+000, 4.456338e+000, 4.615493e+000, 4.774648e+000, 4.933803e+000, 5.092958e+000, 5.252113e+000, 5.411268e+000, 5.570423e+000, 5.729578e+000, 5.888733e+000, 6.047888e+000, 6.207043e+000, 6.366198e+000, 6.525353e+000, 6.684508e+000, 6.843663e+000, 7.002817e+000, 7.161972e+000, 7.321127e+000, 7.480282e+000, 7.639437e+000, 7.798592e+000, 7.957747e+000, 8.116902e+000, 8.276057e+000, 8.435212e+000, 8.594367e+000, 8.753522e+000, 8.912677e+000, 9.071832e+000, 9.230987e+000, 9.390142e+000, 9.549297e+000, 9.708452e+000, 9.867606e+000, 1.002676e+001, 1.018592e+001, 1.034507e+001, 1.050423e+001, 1.066338e+001, 1.082254e+001, 1.098169e+001, 1.114085e+001, 1.130000e+001, 1.145916e+001, 1.161831e+001, 1.177747e+001, 1.193662e+001, 1.209578e+001, 1.225493e+001, 1.241409e+001, 1.257324e+001, 1.273240e+001, 1.289155e+001, 1.305071e+001, 1.320986e+001, 1.336902e+001, 1.352817e+001, 1.368733e+001, 1.384648e+001, 1.400563e+001, 1.416479e+001, 1.432394e+001, 1.448310e+001, 1.464225e+001, 1.480141e+001, 1.496056e+001, 1.511972e+001, 1.527887e+001, 1.543803e+001, 1.559718e+001, 1.575634e+001, 1.591549e+001, 1.607465e+001, 1.623380e+001, 1.639296e+001, 1.655211e+001, 1.671127e+001, 1.687042e+001, 1.702958e+001, 1.718873e+001, 1.734789e+001, 1.750704e+001, 1.766620e+001, 1.782535e+001, 1.798451e+001, 1.814366e+001, 1.830282e+001, 1.846197e+001, 1.862113e+001, 1.878028e+001, 1.893944e+001, 1.909859e+001, 1.925775e+001, 1.941690e+001, 1.957606e+001, 1.973521e+001, 1.989437e+001, 2.005352e+001, 2.021268e+001, 2.037183e+001, 2.053099e+001, 2.069014e+001, 2.084930e+001, 2.100845e+001, 2.116761e+001, 2.132676e+001, 2.148592e+001, 2.164507e+001, 2.180423e+001, 2.196338e+001, 2.212254e+001, 2.228169e+001, 2.244085e+001, 2.260000e+001, 2.275916e+001, 2.291831e+001, 2.307747e+001, 2.323662e+001, 2.339578e+001, 2.355493e+001, 2.371409e+001, 2.387324e+001, 2.403240e+001, 2.419155e+001, 2.435071e+001, 2.450986e+001, 2.466902e+001, 2.482817e+001, 2.498733e+001, 2.514648e+001, 2.530564e+001, 2.546479e+001, 2.562395e+001, 2.578310e+001, 2.594226e+001, 2.610141e+001, 2.626057e+001, 2.641972e+001, 2.657888e+001, 2.673803e+001, 2.689719e+001, 2.705634e+001, 2.721550e+001, 2.737465e+001, 2.753381e+001, 2.769296e+001, 2.785212e+001, 2.801127e+001, 2.817042e+001, 2.832958e+001, 2.848873e+001, 2.864789e+001, 2.880704e+001, 2.896620e+001, 2.912535e+001, 2.928451e+001, 2.944366e+001, 2.960282e+001, 2.976197e+001, 2.992113e+001, 3.008028e+001, 3.023944e+001, 3.039859e+001, 3.055775e+001, 3.071690e+001, 3.087606e+001, 3.103521e+001, 3.119437e+001, 3.135352e+001, 3.151268e+001, 3.167183e+001, 3.183099e+001, 3.199014e+001, 3.214930e+001, 3.230845e+001, 3.246761e+001, 3.262676e+001, 3.278592e+001, 3.294507e+001, 3.310423e+001, 3.326338e+001, 3.342254e+001, 3.358169e+001, 3.374085e+001, 3.390000e+001, 3.405916e+001, 3.421831e+001, 3.437747e+001, 3.453662e+001, 3.469578e+001, 3.485493e+001, 3.501409e+001, 3.517324e+001, 3.533240e+001, 3.549155e+001, 3.565071e+001, 3.580986e+001, 3.596902e+001, 3.612817e+001, 3.628733e+001, 3.644648e+001, 3.660564e+001, 3.676479e+001, 3.692395e+001, 3.708310e+001, 3.724226e+001, 3.740141e+001, 3.756057e+001, 3.771972e+001, 3.787888e+001, 3.803803e+001, 3.819719e+001, 3.835634e+001, 3.851550e+001, 3.867465e+001, 3.883381e+001, 3.899296e+001, 3.915212e+001, 3.931127e+001, 3.947043e+001, 3.962958e+001, 3.978874e+001, 3.994789e+001, 4.010705e+001, 4.026620e+001, 4.042536e+001, 4.058451e+001, 4.074367e+001, 4.090282e+001, 4.106198e+001, 4.122113e+001, 4.138029e+001, 4.153944e+001, 4.169860e+001, 4.185775e+001, 4.201690e+001, 4.217606e+001, 4.233521e+001, 4.249437e+001, 4.265352e+001, 4.281268e+001, 4.297183e+001, 4.313099e+001, 4.329014e+001, 4.344930e+001, 4.360845e+001, 4.376761e+001, 4.392676e+001, 4.408592e+001, 4.424507e+001, 4.440423e+001, 4.456338e+001, 4.472254e+001, 4.488169e+001, 4.504085e+001, 4.520000e+001, 4.535916e+001, 4.551831e+001, 4.567747e+001, 4.583662e+001, 4.599578e+001, 4.615493e+001, 4.631409e+001, 4.647324e+001, 4.663240e+001, 4.679155e+001, 4.695071e+001, 4.710986e+001, 4.726902e+001, 4.742817e+001, 4.758733e+001, 4.774648e+001, 4.790564e+001, 4.806479e+001, 4.822395e+001, 4.838310e+001, 4.854226e+001, 4.870141e+001, 4.886057e+001, 4.901972e+001, 4.917888e+001, 4.933803e+001, 4.949719e+001, 4.965634e+001, 4.981550e+001, 4.997465e+001, 5.013381e+001, 5.029296e+001, 5.045212e+001, 5.061127e+001, 5.077043e+001, 5.092958e+001, 5.108874e+001, 5.124789e+001, 5.140705e+001, 5.156620e+001, 5.172536e+001, 5.188451e+001, 5.204367e+001, 5.220282e+001, 5.236198e+001, 5.252113e+001, 5.268029e+001, 5.283944e+001, 5.299860e+001, 5.315775e+001, 5.331691e+001, 5.347606e+001, 5.363522e+001, 5.379437e+001, 5.395353e+001, 5.411268e+001, 5.427184e+001, 5.443099e+001, 5.459015e+001, 5.474930e+001, 5.490846e+001, 5.506761e+001, 5.522677e+001, 5.538592e+001, 5.554508e+001, 5.570423e+001, 5.586339e+001, 5.602254e+001, 5.618169e+001, 5.634085e+001, 5.650000e+001, 5.665916e+001, 5.681831e+001, 5.697747e+001, 5.713662e+001, 5.729578e+001, 5.745493e+001, 5.761409e+001, 5.777324e+001, 5.793240e+001, 5.809155e+001, 5.825071e+001, 5.840986e+001, 5.856902e+001, 5.872817e+001, 5.888733e+001, 5.904648e+001, 5.920564e+001, 5.936479e+001, 5.952395e+001, 5.968310e+001, 5.984226e+001, 6.000141e+001, 6.016057e+001, 6.031972e+001, 6.047888e+001, 6.063803e+001, 6.079719e+001, 6.095634e+001, 6.111550e+001, 6.127465e+001, 6.143381e+001, 6.159296e+001, 6.175212e+001, 6.191127e+001, 6.207043e+001, 6.222958e+001, 6.238874e+001, 6.254789e+001, 6.270705e+001, 6.286620e+001, 6.302536e+001, 6.318451e+001, 6.334367e+001, 6.350282e+001, 6.366198e+001, 6.382113e+001, 6.398029e+001, 6.413944e+001, 6.429860e+001, 6.445775e+001, 6.461691e+001, 6.477606e+001, 6.493522e+001, 6.509437e+001, 6.525353e+001, 6.541268e+001, 6.557184e+001, 6.573099e+001, 6.589015e+001, 6.604930e+001, 6.620846e+001, 6.636761e+001, 6.652677e+001, 6.668592e+001, 6.684508e+001, 6.700423e+001, 6.716339e+001, 6.732254e+001, 6.748170e+001, 6.764085e+001, 6.780001e+001, 6.795916e+001, 6.811832e+001, 6.827747e+001, 6.843663e+001, 6.859578e+001, 6.875494e+001, 6.891409e+001, 6.907325e+001, 6.923240e+001, 6.939156e+001, 6.955071e+001, 6.970987e+001, 6.986902e+001, 7.002817e+001, 7.018733e+001, 7.034648e+001, 7.050564e+001, 7.066479e+001, 7.082395e+001, 7.098310e+001, 7.114226e+001, 7.130141e+001, 7.146057e+001, 7.161972e+001, 7.177888e+001, 7.193803e+001, 7.209719e+001, 7.225634e+001, 7.241550e+001, 7.257465e+001, 7.273381e+001, 7.289296e+001, 7.305212e+001, 7.321127e+001, 7.337043e+001, 7.352958e+001, 7.368874e+001, 7.384789e+001, 7.400705e+001, 7.416620e+001, 7.432536e+001, 7.448451e+001, 7.464367e+001, 7.480282e+001, 7.496198e+001, 7.512113e+001, 7.528029e+001, 7.543944e+001, 7.559860e+001, 7.575775e+001, 7.591691e+001, 7.607606e+001, 7.623522e+001, 7.639437e+001, 7.655353e+001, 7.671268e+001, 7.687184e+001, 7.703099e+001, 7.719015e+001, 7.734930e+001, 7.750846e+001, 7.766761e+001, 7.782677e+001, 7.798592e+001, 7.814508e+001, 7.830423e+001, 7.846339e+001, 7.862254e+001, 7.878170e+001, 7.894085e+001, 7.910001e+001, 7.925916e+001, 7.941832e+001, 7.957747e+001, 7.973663e+001, 7.989578e+001, 8.005494e+001, 8.021409e+001, 8.037325e+001, 8.053240e+001, 8.069156e+001, 8.085071e+001, 8.100987e+001, 8.116902e+001, 8.132818e+001, 8.148733e+001];
normalizationConstant = normalizationConstants(kappa);

% Get the probability
p = normalizationConstant * exp(-kappa * (1 - mu*v'));
