Resistor mesh: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, added wording to the REXX section header about precision and execution times.)
m (→‎{{header|REXX}}: changed wording of the output, allowed specification of decimal digits used; adding verbiage to the REXX section header, optimized the inner DO loop.)
Line 1,245: Line 1,245:
=={{header|REXX}}==
=={{header|REXX}}==
{{trans|Ada}}
{{trans|Ada}}
This version allows specification of the   grid size   and the location of the   '''A'''   and   '''B'''   points.
This version allows specification of the grid size,   the locations of the   '''A'''   and   '''B'''   points,   and the number of decimal digits precision.


Dropping the   '''numeric digits'''   to   '''10'''   will make the execution   3   times faster.
Dropping the decimal digits precision   ('''numeric digits''')   to   '''10'''   will make the execution   '''3'''   times faster.
<lang rexx>/*REXX program calculates the resistance between any two points on a resister grid.*/
<lang rexx>/*REXX program calculates the resistance between any two points on a resister grid.*/
numeric digits 20 /*use moderate decimal digs (precision)*/
minVal = (1'e-' || (digits()*2)) / 1 /*calculate the threshold minimul value*/
if 2=='f2'x then ohms = "ohms" /*EBCDIC machine? Then use 'ohms'. */
if 2=='f2'x then ohms = "ohms" /*EBCDIC machine? Then use 'ohms'. */
else ohms = "Ω" /* ASCII " " " Greek Ω.*/
else ohms = "Ω" /* ASCII " " " Greek Ω.*/
parse arg high wide Arow Acol Brow Bcol . /*obtain optional arguments from the CL*/
parse arg high wide Arow Acol Brow Bcol digs . /*obtain optional arguments from the CL*/
if high=='' | high=="," then high=10 /*Not specified? Then use the default.*/
if high=='' | high=="," then high=10 /*Not specified? Then use the default.*/
if wide=='' | wide=="," then wide=10 /* " " " " " " */
if wide=='' | wide=="," then wide=10 /* " " " " " " */
Line 1,260: Line 1,258:
if Brow=='' | Brow=="," then Brow= 7 /* " " " " " " */
if Brow=='' | Brow=="," then Brow= 7 /* " " " " " " */
if Bcol=='' | Bcol=="," then Bcol= 8 /* " " " " " " */
if Bcol=='' | Bcol=="," then Bcol= 8 /* " " " " " " */
say ' minimum value = ' translate(format(minVal, , , , 0), "e", 'E'); say
if digs=='' | digs=="," then digs=20 /* " " " " " " */
say ' resistor mesh is of size: ' wide "wide, " high 'high' ; say
numeric digits digs /*use moderate decimal digs (precision)*/
minVal = 1'e-' || (digs*2) /*calculate the threshold minimul value*/
say ' minimum value is ' format(minVal,,,,0) " using " digs ' decimal digits'; say
say ' resistor mesh size is: ' wide "wide, " high 'high' ; say
say ' point A is at (row,col): ' Arow"," Acol
say ' point A is at (row,col): ' Arow"," Acol
say ' point B is at (row,col): ' Brow"," Bcol
say ' point B is at (row,col): ' Brow"," Bcol
@.=0; cell.=1
@.=0; cell.=1
do until $ <= minVal; v = 0
do until $<=minVal; v=0
@.Arow.Acol = 1 ; cell.Arow.Acol = 0
@.Arow.Acol= 1 ; cell.Arow.Acol=0
@.Brow.Bcol = '-1' ; cell.Brow.Bcol = 0
@.Brow.Bcol= '-1' ; cell.Brow.Bcol=0
$=0
$=0
do i=1 for high; im=i - 1; ip=i + 1
do i=1 for high; im=i-1; ip=i+1
do j=1 for wide; jm=j - 1; jp=j + 1; n=0; v=0
do j=1 for wide; n=0; v=0
if i\==1 then do; v=v + @.im.j; n=n + 1; end
if i\==1 then do; v=v + @.im.j; n=n+1; end
if j\==1 then do; v=v + @.i.jm; n=n + 1; end
if j\==1 then do; jm=j-1; v=v + @.i.jm; n=n+1; end
if i<high then do; v=v + @.ip.j; n=n + 1; end
if i<high then do; v=v + @.ip.j; n=n+1; end
if j<wide then do; v=v + @.i.jp; n=n + 1; end
if j<wide then do; jp=j+1; v=v + @.i.jp; n=n+1; end
v=@.i.j - v/n; #.i.j=v; if cell.i.j then $=$ + v*v
v=@.i.j - v/n; #.i.j=v; if cell.i.j then $=$ + v*v
end /*j*/
end /*j*/
end /*i*/
end /*i*/
do r=1 for High
do r=1 for High
do c=1 for Wide; @.r.c = @.r.c - #.r.c
do c=1 for Wide; @.r.c= @.r.c - #.r.c
end /*c*/
end /*c*/
end /*r*/
end /*r*/
end /*until*/
end /*until*/
say
say
Acur = #.Arow.Acol * sides(Arow, Acol)
Acur= #.Arow.Acol * sides(Arow, Acol)
Bcur = #.Brow.Bcol * sides(Brow, Bcol)
Bcur= #.Brow.Bcol * sides(Brow, Bcol)
say ' resistance between point A and point B is: ' 4/(Acur-Bcur) ohms
say ' resistance between point A and point B is: ' 4 / (Acur - Bcur) ohms
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sides: parse arg row,col; z=0; if row\==1 & row\==high then z=z + 2; else z=z + 1
sides: parse arg row,col; z=0; if row\==1 & row\==high then z=z+2; else z=z+1
if col\==1 & col\==wide then z=z + 2; else z=z + 1</lang>
if col\==1 & col\==wide then z=z+2; else z=z+1
return z</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
minimum value = 1e-40
minimum value is 1E-40 using 20 decimal digits


resistor mesh is of size: 10 wide, 10 high
resistor mesh size is: 10 wide, 10 high


point A is at (row,col): 2, 2
point A is at (row,col): 2, 2