
Task

Given    10×10    grid nodes   (as shown in the image)   interconnected by    1Ω    resistors as shown,

find the resistance between points   A   and   B.



See also

   (humor, nerd sniping)   xkcd.com cartoon


var [const] GSL=Import("zklGSL");	// libGSL (GNU Scientific Library)
fcn onGrid(i,j,p,q){ ((0<=i<p) and (0<=j<q)) }
fcn gridResistor(p,q, ai,aj, bi,bj){
   n,A := p*q, GSL.Matrix(n,n);  // zero filled
   foreach i,j in (p,q){
      k:=i*q + j;
      if(i==ai and j==aj) A[k,k]=1;
      else{
	 c:=0;
	 if(onGrid(i+1,j,   p,q)){ c+=1; A[k, k+q]=-1 }
	 if(onGrid(i-1,j,   p,q)){ c+=1; A[k, k-q]=-1 }
	 if(onGrid(i,  j+1, p,q)){ c+=1; A[k, k+1]=-1 }
	 if(onGrid(i,  j-1, p,q)){ c+=1; A[k, k-1]=-1 }
	 A[k,k]=c;
      }
   }
   b:=GSL.Vector(n);  // zero filled
   b[k:=bi*q + bj]=1;
   A.AxEQb(b)[k];
}