Based on the following matlab: 
 
 % Based on Eqn 11.9 in H&Z ("First order geometric error (Sampson distance)")
 syms f4 f5 f6 f7 f8 f9 real
 syms r s real
 syms x y X Y real
 % row 1 is parameterised
 f1 = r*f4 + s*f7;
 f2 = r*f5 + s*f8;
 f3 = r*f6 + 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, [f4 f5 f6 f7 f8 f9 r s]);
 % generate code
 ccode(d, 'file', 'ccode/f23_sampson_value.c')
 ccode(J, 'file', 'ccode/f23_sampson_jac.c')