###############################################################################
#This maple script shows that the integral (9) has the closed fom solution (10)

restart;
with(LinearAlgebra):

assume(r,real,r>0);

# vector field in first order approximation
uf := u0 + u_x*x + u_y*y + u_z*z;
vf := v0 + v_x*x + v_y*y + v_z*z;
wf := w0 + w_x*x + w_y*y + w_z*z;

# we consider the line segment S = (x0,y0,z0)->(x1,y1,z1)
x0 := 0;
y0 := 0;
z0 := 0;

x1 := x0 + r*rx;
y1 := y0 + r*ry;
z1 := z0 + r*rz;

# we integrate S in the vector field and get the line segment SS = (xx0,yy0,zz0)->(xx1,yy1,zz1)
xx0 := x0 + al*eval(uf,{x=x0,y=y0,z=z0});
yy0 := y0 + al*eval(vf,{x=x0,y=y0,z=z0});
zz0 := z0 + al*eval(wf,{x=x0,y=y0,z=z0});

xx1 := x1 + al*eval(uf,{x=x1,y=y1,z=z1});
yy1 := y1 + al*eval(vf,{x=x1,y=y1,z=z1});
zz1 := z1 + al*eval(wf,{x=x1,y=y1,z=z1});

# we compare the lenth of S and SS: 
hh := factor(sqrt((xx1-xx0)^2 + (yy1-yy0)^2 + (zz1-zz0)^2) - sqrt((x1-x0)^2 + (y1-y0)^2 + (z1-z0)^2));
hhh := simplify(limit(limit(hh/(al*r),al=0),r=0));

# the error is the integration over all directions of S
# err is the integral (10)
err := factor(int(int(cos(be)*eval(hhh,{rx=cos(al)*cos(be) , ry=sin(al)*cos(be) , rz=sin(be)})^2, be=-Pi/2..Pi/2),al=0..2*Pi)/(4*Pi));

# Frobenius norm of J + J^T
frobeq := simplify(
+ (u_x+u_x)^2
+ (u_y+v_x)^2
+ (u_z+w_x)^2

+ (v_x+u_y)^2
+ (v_y+v_y)^2
+ (v_z+w_y)^2

+ (w_x+u_z)^2
+ (w_y+v_z)^2
+ (w_z+w_z)^2
);

# trace of J
trac := u_x + v_y + w_z;

# the constant c = 1/30 in this case
eerr := (1/30)*(frobeq + 2*trac^2);

#we have to show that err=eerr:
factor(err-eerr);
