Based on the following matlab:
% Based on Eqn 11.9 in H&Z ("First order geometric error (Sampson distance)")
syms f1 f2 f3 f7 f8 f9 real
syms r s real
syms x y X Y real
% row 2 is parameterised
f4 = r*f1 + s*f7;
f5 = r*f2 + s*f8;
f6 = r*f3 + s*f9;
% build F
F = [f1 f2 f3; f4 f5 f6; f7 f8 f9];
% the sampson distance and its analytic jacobian
Fx = F*[x y 1]';
FtX = F'*[X Y 1]';
XFx = [X Y 1] * F * [x y 1]';
d = XFx^2 / (Fx(1)^2 + Fx(2)^2 + FtX(1)^2 + FtX(2)^2);
J = jacobian(d, [f1 f2 f3 f7 f8 f9 r s]);
% generate code
ccode(d, 'file', 'ccode/f13_sampson_value.c')
ccode(J, 'file', 'ccode/f13_sampson_jac.c')