Obtain the shape function and its derivative at each element

Hello,

I would like to get the shape function (or characteristic function) together with its derivative at each element. Is there a quick way in freefem to do so? Many thanks!

Note: I understand that I can use varf to get the shape function of each node in an integral sense. However, what I need is the shape function of each element separately, without summing up the contribution from surrounding elements. For example, for the following mesh, I would like to get the shape function N1 (and its derivative) of element 0 on node 0, and the shape function N1 (and its derivative) of element 1 on node 0, instead of the sum of two N1 (and their derivatives) from two elements on node 0.

I would also like to calculate the element length in FreeFem++ using the following expression: h_e=(\sum_{i=1}^{n_{e n}}\|\nabla N_i\|)^{-1}, where N_i is the shape function associated with node i. Does anyone know how to compute this in FreeFem++? Thanks.

Here is a try. I don’t know if your norm \|\nabla N_i\| means L^2 norm.

mesh Th=square(2,2);

fespace Vh(Th,P1);
fespace Ph(Th,P0);

Vh u;
Ph derxu,deryu;

real he=0.;
for (int i=0;i<Vh.ndof;i++){
 u[]=0.;
 u[][i]=1.;//basis function i
 derxu=dx(u);deryu=dy(u);
 real xx=Th(0).x,yy=Th(0).y;
 cout << "i=" << i << endl;
 cout << "u(xx,yy) " << u(xx,yy) << endl;
 cout << "dxu,dyu triangle 0: " << derxu[][0] << " " << deryu[][0] << endl;
 cout << "dxu,dyu triangle 1: " << derxu[][1] << " " << deryu[][1] << endl;

 real normi=sqrt(int2d(Th)(derxu^2+deryu^2));
 he=he+normi;
}
he=1./he;
cout << "he = " << he << endl;

Thank you so much for the help!