September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
;Task:
|
||||
Given <big> 10×10 </big> grid nodes (as shown in the image) interconnected by <big> 1Ω </big> resistors as shown,
|
||||
<br>find the resistance between point '''A''' and '''B'''.
|
||||
<br>find the resistance between points '''A''' and '''B'''.
|
||||
|
||||
|
||||
;See also:
|
||||
|
|
|
|||
62
Task/Resistor-mesh/Kotlin/resistor-mesh.kotlin
Normal file
62
Task/Resistor-mesh/Kotlin/resistor-mesh.kotlin
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
// version 1.1.4-3
|
||||
|
||||
typealias List2D<T> = List<List<T>>
|
||||
|
||||
const val S = 10
|
||||
|
||||
class Node(var v: Double, var fixed: Int)
|
||||
|
||||
fun setBoundary(m: List2D<Node>) {
|
||||
m[1][1].v = 1.0; m[1][1].fixed = 1
|
||||
m[6][7].v = -1.0; m[6][7].fixed = -1
|
||||
}
|
||||
|
||||
fun calcDiff(m: List2D<Node>, d: List2D<Node>, w: Int, h: Int): Double {
|
||||
var total = 0.0
|
||||
for (i in 0 until h) {
|
||||
for (j in 0 until w) {
|
||||
var v = 0.0
|
||||
var n = 0
|
||||
if (i > 0) { v += m[i - 1][j].v; n++ }
|
||||
if (j > 0) { v += m[i][j - 1].v; n++ }
|
||||
if (i + 1 < h) { v += m[i + 1][j].v; n++ }
|
||||
if (j + 1 < w) { v += m[i][j + 1].v; n++ }
|
||||
v = m[i][j].v - v / n
|
||||
d[i][j].v = v
|
||||
if (m[i][j].fixed == 0) total += v * v
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
fun iter(m: List2D<Node>, w: Int, h: Int): Double {
|
||||
val d = List(h) { List(w) { Node(0.0, 0) } }
|
||||
val cur = DoubleArray(3)
|
||||
var diff = 1e10
|
||||
|
||||
while (diff > 1e-24) {
|
||||
setBoundary(m)
|
||||
diff = calcDiff(m, d, w, h)
|
||||
for (i in 0 until h) {
|
||||
for (j in 0 until w) m[i][j].v -= d[i][j].v
|
||||
}
|
||||
}
|
||||
|
||||
for (i in 0 until h) {
|
||||
for (j in 0 until w) {
|
||||
var k = 0
|
||||
if (i != 0) k++
|
||||
if (j != 0) k++
|
||||
if (i < h - 1) k++
|
||||
if (j < w - 1) k++
|
||||
cur[m[i][j].fixed + 1] += d[i][j].v * k
|
||||
}
|
||||
}
|
||||
return (cur[2] - cur[0]) / 2.0
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val mesh = List(S) { List(S) { Node(0.0, 0) } }
|
||||
val r = 2.0 / iter(mesh, S, S)
|
||||
println("R = $r")
|
||||
}
|
||||
|
|
@ -1,46 +1,45 @@
|
|||
/*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 1=='f1'x then ohms = 'ohms' /*EBCDIC machine? Then use 'ohms'. */
|
||||
else ohms = 'ea'x /* ASCII " " " Greek Ω.*/
|
||||
parse arg high wide Arow Acol Brow Bcol . /*obtain optional arguments from the CL*/
|
||||
if 2=='f2'x then ohms = "ohms" /*EBCDIC machine? Then use 'ohms'. */
|
||||
else ohms = "Ω" /* ASCII " " " Greek Ω.*/
|
||||
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 wide=='' | wide=="," then wide=10 /* " " " " " " */
|
||||
if Arow=='' | Arow=="," then Arow= 2 /* " " " " " " */
|
||||
if Acol=='' | Acol=="," then Acol= 2 /* " " " " " " */
|
||||
if Brow=='' | Brow=="," then Brow= 7 /* " " " " " " */
|
||||
if Bcol=='' | Bcol=="," then Bcol= 8 /* " " " " " " */
|
||||
say ' minimum value = ' translate(format(minVal, , , , 0), "e", 'E'); say
|
||||
say ' resistor mesh is of size: ' wide "wide, " high 'high' ; say
|
||||
if digs=='' | digs=="," then digs=20 /* " " " " " " */
|
||||
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 B is at (row,col): ' Brow"," Bcol
|
||||
@.=0; cell.=1
|
||||
do until $ <= minVal; v = 0
|
||||
@.Arow.Acol = +1 ; cell.Arow.Acol = 0
|
||||
@.Brow.Bcol = -1 ; cell.Brow.Bcol = 0
|
||||
$=0
|
||||
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
|
||||
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 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
|
||||
v=@.i.j - v/n; #.i.j=v; if cell.i.j then $=$ + v*v
|
||||
end /*j*/
|
||||
end /*i*/
|
||||
do r=1 for High
|
||||
do c=1 for Wide; @.r.c = @.r.c - #.r.c
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
end /*until*/
|
||||
@.=0; cell.=1
|
||||
do until $<=minVal; v=0
|
||||
@.Arow.Acol= 1 ; cell.Arow.Acol=0
|
||||
@.Brow.Bcol= '-1' ; cell.Brow.Bcol=0
|
||||
$=0
|
||||
do i=1 for high; im=i-1; ip=i+1
|
||||
do j=1 for wide; n=0; v=0
|
||||
if i\==1 then do; v=v + @.im.j; 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 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
|
||||
end /*j*/
|
||||
end /*i*/
|
||||
do r=1 for High
|
||||
do c=1 for Wide; @.r.c= @.r.c - #.r.c
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
end /*until*/
|
||||
say
|
||||
Acur = #.Arow.Acol * sides(Arow,Acol)
|
||||
Bcur = #.Brow.Bcol * sides(Brow,Bcol)
|
||||
say ' resistance between point A and point B is: ' 4/(Acur-Bcur) ohms
|
||||
Acur= #.Arow.Acol * sides(Arow, Acol)
|
||||
Bcur= #.Brow.Bcol * sides(Brow, Bcol)
|
||||
say ' resistance between point A and point B is: ' 4 / (Acur - Bcur) ohms
|
||||
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
|
||||
if col\==1 & col\==wide 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
|
||||
return z
|
||||
|
|
|
|||
|
|
@ -23,10 +23,8 @@ func set_boundary {
|
|||
func calc_diff {
|
||||
var total_diff = 0
|
||||
for i,j in (^h ~X ^w) {
|
||||
var p = n[i][j]
|
||||
var w = 0
|
||||
p.each { |a| w += v.dig(a...) }
|
||||
d[i][j] = (w = (v[i][j] - w/p.len))
|
||||
var w = n[i][j].map { |a| v.dig(a...) }.sum
|
||||
d[i][j] = (w = (v[i][j] - w/n[i][j].len))
|
||||
f[i][j] || (total_diff += w*w)
|
||||
}
|
||||
total_diff
|
||||
|
|
|
|||
20
Task/Resistor-mesh/Zkl/resistor-mesh-1.zkl
Normal file
20
Task/Resistor-mesh/Zkl/resistor-mesh-1.zkl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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];
|
||||
}
|
||||
1
Task/Resistor-mesh/Zkl/resistor-mesh-2.zkl
Normal file
1
Task/Resistor-mesh/Zkl/resistor-mesh-2.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
gridResistor(10,10, 1,1, 7,6).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue