RosettaCodeData/Task/Resistor-mesh/Phix/resistor-mesh.phix
2026-02-01 16:33:20 -08:00

27 lines
822 B
Text

function resistormesh(integer ni, nj, ai, aj, bi, bj)
integer n = ni*nj, k, c
sequence A = repeat(repeat(0,n),n),
B = repeat({0},n)
for i=1 to ni do
for j=1 to nj do
k = (i-1)*nj + j--1
if i=ai and j=aj then
A[k,k] = 1
else
c = 0
if i<ni then c += 1; A[k,k+nj] = -1 end if
if i>1 then c += 1; A[k,k-nj] = -1 end if
if j<nj then c += 1; A[k,k+1] = -1 end if
if j>1 then c += 1; A[k,k-1] = -1 end if
A[k,k] = c
end if
end for
end for
k = (bi-1)*nj +bj
B[k,1] = 1
A = inverse(A)
B = matrix_mul(A,B)
return B[k,1]
end function
printf(1,"Resistance = %.13f ohms\n",resistormesh(10, 10, 2, 2, 8, 7))