Resistor mesh: Difference between revisions
Content added Content deleted
(Added Kotlin) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations, added wording to the REXX section header about precision and execution times.) |
||
Line 1,246: | Line 1,246: | ||
{{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 and the location of the '''A''' and '''B''' points. |
||
Dropping the '''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)*/ |
numeric digits 20 /*use moderate decimal digs (precision)*/ |
||
minVal = (1'e-' || (digits()*2)) / 1 /*calculate the threshold minimul value*/ |
minVal = (1'e-' || (digits()*2)) / 1 /*calculate the threshold minimul value*/ |
||
if 2=='f2'x then ohms = |
if 2=='f2'x then ohms = "ohms" /*EBCDIC machine? Then use 'ohms'. */ |
||
else ohms = |
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 . /*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.*/ |
||
Line 1,263: | Line 1,265: | ||
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 = |
@.Arow.Acol = 1 ; cell.Arow.Acol = 0 |
||
@.Brow.Bcol = -1 ; |
@.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; jm=j - 1; jp=j + 1; 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; 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; 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*/ |
||
Line 1,287: | Line 1,289: | ||
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; |
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= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |