Resistor mesh: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 187:
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
 
namespace ResistorMesh {
class Node {
public Node(double v, int fixed_) {
V = v;
Fixed = fixed_;
}
 
public double V { get; set; }
public int Fixed { get; set; }
}
 
class Program {
static void SetBoundary(List<List<Node>> m) {
m[1][1].V = 1.0;
m[1][1].Fixed = 1;
 
m[6][7].V = -1.0;
m[6][7].Fixed = -1;
}
 
static double CalcuateDifference(List<List<Node>> m, List<List<Node>> d, int w, int h) {
double total = 0.0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
double v = 0.0;
int 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;
}
 
static double Iter(List<List<Node>> m, int w, int h) {
List<List<Node>> d = new List<List<Node>>(h);
for (int i = 0; i < h; i++) {
List<Node> t = new List<Node>(w);
for (int j = 0; j < w; j++) {
t.Add(new Node(0.0, 0));
}
d.Add(t);
}
 
double[] curr = new double[3];
double diff = 1e10;
 
while (diff > 1e-24) {
SetBoundary(m);
diff = CalcuateDifference(m, d, w, h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
m[i][j].V -= d[i][j].V;
}
}
}
 
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int k = 0;
if (i != 0) k++;
if (j != 0) k++;
if (i < h - 1) k++;
if (j < w - 1) k++;
curr[m[i][j].Fixed + 1] += d[i][j].V * k;
}
}
 
return (curr[2] - curr[0]) / 2.0;
}
 
const int S = 10;
static void Main(string[] args) {
List<List<Node>> mesh = new List<List<Node>>(S);
for (int i = 0; i < S; i++) {
List<Node> t = new List<Node>(S);
for (int j = 0; j < S; j++) {
t.Add(new Node(0.0, 0));
}
mesh.Add(t);
}
 
double r = 2.0 / Iter(mesh, S, S);
Console.WriteLine("R = {0:F15}", r);
}
}
}</lang>
{{out}}
<pre>R = 1.608991241729890</pre>
 
=={{header|C++}}==
Line 317 ⟶ 430:
{{out}}
<pre>R = 1.60899124172989</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
 
namespace ResistorMesh {
class Node {
public Node(double v, int fixed_) {
V = v;
Fixed = fixed_;
}
 
public double V { get; set; }
public int Fixed { get; set; }
}
 
class Program {
static void SetBoundary(List<List<Node>> m) {
m[1][1].V = 1.0;
m[1][1].Fixed = 1;
 
m[6][7].V = -1.0;
m[6][7].Fixed = -1;
}
 
static double CalcuateDifference(List<List<Node>> m, List<List<Node>> d, int w, int h) {
double total = 0.0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
double v = 0.0;
int 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;
}
 
static double Iter(List<List<Node>> m, int w, int h) {
List<List<Node>> d = new List<List<Node>>(h);
for (int i = 0; i < h; i++) {
List<Node> t = new List<Node>(w);
for (int j = 0; j < w; j++) {
t.Add(new Node(0.0, 0));
}
d.Add(t);
}
 
double[] curr = new double[3];
double diff = 1e10;
 
while (diff > 1e-24) {
SetBoundary(m);
diff = CalcuateDifference(m, d, w, h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
m[i][j].V -= d[i][j].V;
}
}
}
 
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int k = 0;
if (i != 0) k++;
if (j != 0) k++;
if (i < h - 1) k++;
if (j < w - 1) k++;
curr[m[i][j].Fixed + 1] += d[i][j].V * k;
}
}
 
return (curr[2] - curr[0]) / 2.0;
}
 
const int S = 10;
static void Main(string[] args) {
List<List<Node>> mesh = new List<List<Node>>(S);
for (int i = 0; i < S; i++) {
List<Node> t = new List<Node>(S);
for (int j = 0; j < S; j++) {
t.Add(new Node(0.0, 0));
}
mesh.Add(t);
}
 
double r = 2.0 / Iter(mesh, S, S);
Console.WriteLine("R = {0:F15}", r);
}
}
}</lang>
{{out}}
<pre>R = 1.608991241729890</pre>
 
=={{header|D}}==
Line 700:
endfunction
</lang>
 
=={{Header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 01-07-2018
' compile with: fbc -s console
Line 1,195 ⟶ 1,196:
R = 1.608991241729889
</pre>
 
=={{header|Mathematica}}==
{{trans|Maxima}}
<lang mathematica>gridresistor[p_, q_, ai_, aj_, bi_, bj_] :=
Block[{A, B, k, c, V}, A = ConstantArray[0, {p*q, p*q}];
Do[k = (i - 1) q + j;
If[{i, j} == {ai, aj}, A[[k, k]] = 1, c = 0;
If[1 <= i + 1 <= p && 1 <= j <= q, c++; A[[k, k + q]] = -1];
If[1 <= i - 1 <= p && 1 <= j <= q, c++; A[[k, k - q]] = -1];
If[1 <= i <= p && 1 <= j + 1 <= q, c++; A[[k, k + 1]] = -1];
If[1 <= i <= p && 1 <= j - 1 <= q, c++; A[[k, k - 1]] = -1];
A[[k, k]] = c], {i, p}, {j, q}];
B = SparseArray[(k = (bi - 1) q + bj) -> 1, p*q];
LinearSolve[A, B][[k]]];
N[gridresistor[10, 10, 2, 2, 8, 7], 40]</lang>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
 
 
{{works with|Mathematica|9.0}}
<lang mathematica>graphresistor[g_, a_, b_] :=
LinearSolve[
SparseArray[{{a, a} -> 1, {i_, i_} :> Length@AdjacencyList[g, i],
Alternatives @@ Join[#, Reverse /@ #] &[
List @@@ EdgeList[VertexDelete[g, a]]] -> -1}, {VertexCount[
g], VertexCount[g]}], SparseArray[b -> 1, VertexCount[g]]][[b]];
N[graphresistor[GridGraph[{10, 10}], 12, 77], 40]</lang>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
 
=={{header|Maxima}}==
Line 1,259 ⟶ 1,289:
bfloat(%), fpprec = 40;
3.89226554090400912102670691601064387507b0</lang>
 
=={{header|Mathematica}}==
{{trans|Maxima}}
<lang mathematica>gridresistor[p_, q_, ai_, aj_, bi_, bj_] :=
Block[{A, B, k, c, V}, A = ConstantArray[0, {p*q, p*q}];
Do[k = (i - 1) q + j;
If[{i, j} == {ai, aj}, A[[k, k]] = 1, c = 0;
If[1 <= i + 1 <= p && 1 <= j <= q, c++; A[[k, k + q]] = -1];
If[1 <= i - 1 <= p && 1 <= j <= q, c++; A[[k, k - q]] = -1];
If[1 <= i <= p && 1 <= j + 1 <= q, c++; A[[k, k + 1]] = -1];
If[1 <= i <= p && 1 <= j - 1 <= q, c++; A[[k, k - 1]] = -1];
A[[k, k]] = c], {i, p}, {j, q}];
B = SparseArray[(k = (bi - 1) q + bj) -> 1, p*q];
LinearSolve[A, B][[k]]];
N[gridresistor[10, 10, 2, 2, 8, 7], 40]</lang>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
 
 
{{works with|Mathematica|9.0}}
<lang mathematica>graphresistor[g_, a_, b_] :=
LinearSolve[
SparseArray[{{a, a} -> 1, {i_, i_} :> Length@AdjacencyList[g, i],
Alternatives @@ Join[#, Reverse /@ #] &[
List @@@ EdgeList[VertexDelete[g, a]]] -> -1}, {VertexCount[
g], VertexCount[g]}], SparseArray[b -> 1, VertexCount[g]]][[b]];
N[graphresistor[GridGraph[{10, 10}], 12, 77], 40]</lang>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
 
=={{header|Modula-2}}==
Line 1,512 ⟶ 1,513:
{{out}}
<pre>R = 1.608991</pre>
 
=={{header|Perl 6}}==
{{trans|c}}
<lang perl6>my $S = 10;
 
my @fixed;
 
sub allocmesh ($w, $h) {
gather for ^$h {
take [0 xx $w];
}
}
 
sub force-fixed(@f) {
@f[1][1] = 1;
@f[6][7] = -1;
}
 
sub force-v(@v) {
@v[1][1] = 1;
@v[6][7] = -1;
}
sub calc_diff(@v, @d, Int $w, Int $h) {
my $total = 0;
for (flat ^$h X ^$w) -> $i, $j {
my @neighbors = grep *.defined, @v[$i-1][$j], @v[$i][$j-1], @v[$i+1][$j], @v[$i][$j+1];
my $v = [+] @neighbors;
@d[$i][$j] = $v = @v[$i][$j] - $v / +@neighbors;
$total += $v * $v unless @fixed[$i][$j];
}
return $total;
}
sub iter(@v, Int $w, Int $h) {
my @d = allocmesh($w, $h);
my $diff = 1e10;
my @cur = 0, 0, 0;
 
while $diff > 1e-24 {
force-v(@v);
$diff = calc_diff(@v, @d, $w, $h);
for (flat ^$h X ^$w) -> $i, $j {
@v[$i][$j] -= @d[$i][$j];
}
}
 
for (flat ^$h X ^$w) -> $i, $j {
@cur[ @fixed[$i][$j] + 1 ]
+= @d[$i][$j] * (?$i + ?$j + ($i < $h - 1) + ($j < $w - 1));
}
 
return (@cur[2] - @cur[0]) / 2;
}
my @mesh = allocmesh($S, $S);
 
@fixed = allocmesh($S, $S);
force-fixed(@fixed);
 
say 2 / iter(@mesh, $S, $S);</lang>
{{out}}
<pre>1.60899124172989</pre>
 
=={{header|Phix}}==
Line 1,833 ⟶ 1,771:
{{out}}
<pre>R = 1.6089912417301238</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|c}}
<lang perl6>my $S = 10;
 
my @fixed;
 
sub allocmesh ($w, $h) {
gather for ^$h {
take [0 xx $w];
}
}
 
sub force-fixed(@f) {
@f[1][1] = 1;
@f[6][7] = -1;
}
 
sub force-v(@v) {
@v[1][1] = 1;
@v[6][7] = -1;
}
sub calc_diff(@v, @d, Int $w, Int $h) {
my $total = 0;
for (flat ^$h X ^$w) -> $i, $j {
my @neighbors = grep *.defined, @v[$i-1][$j], @v[$i][$j-1], @v[$i+1][$j], @v[$i][$j+1];
my $v = [+] @neighbors;
@d[$i][$j] = $v = @v[$i][$j] - $v / +@neighbors;
$total += $v * $v unless @fixed[$i][$j];
}
return $total;
}
sub iter(@v, Int $w, Int $h) {
my @d = allocmesh($w, $h);
my $diff = 1e10;
my @cur = 0, 0, 0;
 
while $diff > 1e-24 {
force-v(@v);
$diff = calc_diff(@v, @d, $w, $h);
for (flat ^$h X ^$w) -> $i, $j {
@v[$i][$j] -= @d[$i][$j];
}
}
 
for (flat ^$h X ^$w) -> $i, $j {
@cur[ @fixed[$i][$j] + 1 ]
+= @d[$i][$j] * (?$i + ?$j + ($i < $h - 1) + ($j < $w - 1));
}
 
return (@cur[2] - @cur[0]) / 2;
}
my @mesh = allocmesh($S, $S);
 
@fixed = allocmesh($S, $S);
force-fixed(@fixed);
 
say 2 / iter(@mesh, $S, $S);</lang>
{{out}}
<pre>1.60899124172989</pre>
 
=={{header|REXX}}==
10,327

edits