Resistor mesh: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|REXX}}: changed indentations.) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(29 intermediate revisions by 16 users not shown) | |||
Line 8:
;See also:
* (humor, nerd sniping) [http://xkcd.com/356/ xkcd.com cartoon] (you can solve that for extra credits)
* [https://www.paulinternet.nl/?page=resistors An article on how to calculate this and an implementation in Mathematica]
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">-V DIFF_THRESHOLD = 1e-40
T.enum Fixed
FREE
A
B
T Node
Float voltage
Fixed fixed
F (v = 0.0, f = Fixed.FREE)
.voltage = v
.fixed = f
F set_boundary(&m)
m[1][1] = Node( 1.0, Fixed.A)
m[6][7] = Node(-1.0, Fixed.B)
F calc_difference(m, &d)
V h = m.len
V w = m[0].len
V total = 0.0
L(i) 0 .< h
L(j) 0 .< w
V v = 0.0
V n = 0
I i != 0 {v += m[i - 1][j].voltage; n++}
I j != 0 {v += m[i][j - 1].voltage; n++}
I i < h-1 {v += m[i + 1][j].voltage; n++}
I j < w-1 {v += m[i][j + 1].voltage; n++}
v = m[i][j].voltage - v / n
d[i][j].voltage = v
I m[i][j].fixed == FREE
total += v ^ 2
R total
F iter(&m)
V h = m.len
V w = m[0].len
V difference = [[Node()] * w] * h
L
set_boundary(&m)
I calc_difference(m, &difference) < :DIFF_THRESHOLD
L.break
L(di) difference
V i = L.index
L(dij) di
V j = L.index
m[i][j].voltage -= dij.voltage
V cur = [0.0] * 3
L(di) difference
V i = L.index
L(dij) di
V j = L.index
cur[Int(m[i][j].fixed)] += (dij.voltage *
(Int(i != 0) + Int(j != 0) + (i < h - 1) + (j < w - 1)))
R (cur[Int(Fixed.A)] - cur[Int(Fixed.B)]) / 2.0
V w = 10
V h = 10
V mesh = [[Node()] * w] * h
print(‘R = #.16’.format(2 / iter(&mesh)))</syntaxhighlight>
{{out}}
<pre>R = 1.6089912417307285</pre>
=={{header|Ada}}==
{{trans|C}}
<
procedure ResistMesh is
H, W : constant Positive := 10;
Line 81 ⟶ 156:
diff := 4.0 / (curA - curB);
IIO.Put (diff, Exp => 0); New_Line;
end ResistMesh;</
{{out}}<pre> 1.60899124173073</pre>
Line 87 ⟶ 162:
{{trans|Maxima}}
{{works with|BBC BASIC for Windows}}
<
*FLOAT 64
@% = &F0F
Line 117 ⟶ 192:
PROC_invert(A())
B() = A().B()
= B(k%, 0)</
{{out}}
<pre>Resistance = 1.60899124173071 ohms</pre>
=={{header|C}}==
<
#include <stdlib.h>
Line 186 ⟶ 261:
printf("R = %g\n", 2 / iter(mesh, S, S));
return 0;
}</
=={{header
{{trans|Java}}
<
using System.Collections.Generic;
Line 297 ⟶ 372:
}
}
}</
{{out}}
<pre>R = 1.608991241729890</pre>
=={{header|C++}}==
{{trans|C#}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
class Node {
private:
double v;
int fixed;
public:
Node() : v(0.0), fixed(0) {
// empty
}
Node(double v, int fixed) : v(v), fixed(fixed) {
// empty
}
double getV() const {
return v;
}
void setV(double nv) {
v = nv;
}
int getFixed() const {
return fixed;
}
void setFixed(int nf) {
fixed = nf;
}
};
void setBoundary(std::vector<std::vector<Node>>& m) {
m[1][1].setV(1.0);
m[1][1].setFixed(1);
m[6][7].setV(-1.0);
m[6][7].setFixed(-1);
}
double calculateDifference(const std::vector<std::vector<Node>>& m, std::vector<std::vector<Node>>& d, const int w, const 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].getV();
n++;
}
if (j > 0) {
v += m[i][j - 1].getV();
n++;
}
if (i + 1 < h) {
v += m[i + 1][j].getV();
n++;
}
if (j + 1 < w) {
v += m[i][j + 1].getV();
n++;
}
v = m[i][j].getV() - v / n;
d[i][j].setV(v);
if (m[i][j].getFixed() == 0) {
total += v * v;
}
}
}
return total;
}
double iter(std::vector<std::vector<Node>>& m, const int w, const int h) {
using namespace std;
vector<vector<Node>> d;
for (int i = 0; i < h; ++i) {
vector<Node> t(w);
d.push_back(t);
}
double curr[] = { 0.0, 0.0, 0.0 };
double diff = 1e10;
while (diff > 1e-24) {
setBoundary(m);
diff = calculateDifference(m, d, w, h);
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
m[i][j].setV(m[i][j].getV() - d[i][j].getV());
}
}
}
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].getFixed() + 1] += d[i][j].getV()*k;
}
}
return (curr[2] - curr[0]) / 2.0;
}
const int S = 10;
int main() {
using namespace std;
vector<vector<Node>> mesh;
for (int i = 0; i < S; ++i) {
vector<Node> t(S);
mesh.push_back(t);
}
double r = 2.0 / iter(mesh, S, S);
cout << "R = " << setprecision(15) << r << '\n';
return 0;
}</syntaxhighlight>
{{out}}
<pre>R = 1.60899124172989</pre>
=={{header|D}}==
{{trans|C}}
<
enum Node.FP differenceThreshold = 1e-40;
Line 380 ⟶ 585:
writefln("R = %.19f", 2 / mesh.iter);
}</
{{out}}
<pre>R = 1.6089912417307296556</pre>
Line 386 ⟶ 591:
=={{header|ERRE}}==
We'll solve the linear system. We'll write [[wp:Kirchhoff's circuit laws|Kirchhoff's circuit laws]] at each node and search for a voltage distribution that creates a 1A current coming from A exiting in B. The difference of voltage between B and A is then the resistance.
<syntaxhighlight lang="erre">
PROGRAM RESISTENCE_MESH
Line 463 ⟶ 668:
PRINT("Resistence=";ABS(A[A,NN+1]-A[B,NN+1]))
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>Nodes 12 68
Line 472 ⟶ 677:
The functions for this have been implemented in Euler already. Thus the following commands solve this problem.
<syntaxhighlight lang="euler math toolbox">
>load incidence;
>{u,r}=solvePotentialX(makeRectangleX(10,10),12,68); r,
1.60899124173
</syntaxhighlight>
The necessary functions in the file incidence.e are as follows. There are versions with full matrices. But the functions listed here use compressed matrices and the conjugate gradient method.
<syntaxhighlight lang="text">
function makeRectangleX (n:index,m:index)
## Make the incidence matrix of a rectangle grid in compact form.
Line 525 ⟶ 730:
return {u,2/f}
endfunction
</syntaxhighlight>
Here is the code for the conjugate gradient method for compressed, sparse matrices from cpx.e.
<syntaxhighlight lang="text">
function cgX (H:cpx, b:real column, x0:real column=none, f:index=10)
## Conjugate gradient method to solve Hx=b for compressed H.
Line 569 ⟶ 774:
return x;
endfunction
</syntaxhighlight>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 01-07-2018
' compile with: fbc -s console
Line 651 ⟶ 857:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Nodes a: 12 b: 68
Line 659 ⟶ 865:
=={{header|Go}}==
{{trans|C}}
<
import "fmt"
Line 761 ⟶ 967:
fmt.Printf("R = %g\n", 2/iter(mesh, S, S))
}
</syntaxhighlight>
=={{header|Haskell}}==
{{trans|Octave}} All mutations are expressed as monoidal operations.
<
import Numeric.LinearAlgebra (linearSolve, toDense, (!), flatten)
import Data.Monoid ((<>), Sum(..))
Line 795 ⟶ 1,001:
x `when` p = if p then x else mempty
current = toDense [ ((a, 0), -1) , ((b, 0), 1) , ((n^2-1, 0), 0) ]</
{{Out}}
Line 805 ⟶ 1,011:
We represent the mesh as a [[wp:Ybus matrix|Ybus matrix]] with B as the reference node and A as the first node and invert it to find the Z bus (which represents resistance). The first element of the first row of this Z matrix is the resistance between nodes A and B. (It has to be the first element because A was the first node. And we can "ignore" B because we made everything be relative to B.) Most of the work is defining <code>Y</code> which represents the Ybus.
<
nodeA=: 1 1
nodeB=: 6 7
Line 816 ⟶ 1,022:
Yii=: (* =@i.@#) #/.~ {."1 wiring NB. diagonal of Y represents connections to B
Yij=: -1:`(<"1@[)`]}&(+/~ 0*i.1+#ref) wiring NB. off diagonal of Y represents wiring
Y=: _1 _1 }. Yii+Yij</
Here, the result of <code>nodes conn offset</code> represents all pairs of nodes where we can connect the argument nodes to neighboring nodes at the specified offset, and <code>wiring</code> is a list of index pairs representing all connections made by all resistors (note that each connection is represented twice -- node e is connected to node f AND node f is connected to node e). Yii contains the values for the diagonal elements of the Y bus while Yij contains the values for the off diagonal elements of the Y bus.
Line 822 ⟶ 1,028:
So:
<
1.60899</
Or, if we want an exact answer (this is slow), we can assume our resistors are perfect:
<
455859137025721r283319837425200</
(here, the letter 'r' separates the numerator from the denominator)
Line 834 ⟶ 1,040:
To get a better feel for what the <code>conn</code> operation is doing, here is a small illustration:
<
0 0
0 1
Line 861 ⟶ 1,067:
2 1
2 2</
In other words, each coordinate pair is matched up with the coordinate pair that you would get by adding the offset to the first of the pair. In actual use, we use this four times, with four offsets (two horizontal and two vertical) to get our complete mesh.
Line 867 ⟶ 1,073:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.List;
Line 973 ⟶ 1,179:
System.out.printf("R = %.15f", r);
}
}</
{{out}}
<pre>R = 1.608991241729889</pre>
=={{header|JavaScript}}==
Kirchhoff's circuit laws on the resistor mesh are represented as a linear
equation system for the electric potential at each node of the grid.
The linear equation is then solved using the [https://en.wikipedia.org/wiki/Conjugate_gradient_method conjugate gradient method]:
<syntaxhighlight lang=JavaScript>
// Vector addition, scalar multiplication & dot product:
const add = (u, v) => {let i = u.length; while(i--) u[i] += v[i]; return u;};
const sub = (u, v) => {let i = u.length; while(i--) u[i] -= v[i]; return u;};
const mul = (a, u) => {let i = u.length; while(i--) u[i] *= a; return u;};
const dot = (u, v) => {let s = 0, i = u.length; while(i--) s += u[i]*v[i]; return s;};
const W = 10, H = 10, A = 11, B = 67;
function getAdjacent(node){ // Adjacency lists for square grid
let list = [], x = node % W, y = Math.floor(node / W);
if (x > 0) list.push(node - 1);
if (y > 0) list.push(node - W);
if (x < W - 1) list.push(node + 1);
if (y < H - 1) list.push(node + W);
return list;
}
function linOp(u){ // LHS of the linear equation
let v = new Float64Array(W * H);
for(let i = 0; i < v.length; i++){
if ( i === A || i === B ) {
v[i] = u[i];
continue;
}
// For each node other then A, B calculate the net current flow:
for(let j of getAdjacent(i)){
v[i] += (j === A || j === B) ? u[i] : u[i] - u[j];
}
}
return v;
}
function getRHS(phiA = 1, phiB = 0){ // RHS of the linear equation
let b = new Float64Array(W * H);
// Setting boundary conditions (electric potential at A and B):
b[A] = phiA;
b[B] = phiB;
for(let j of getAdjacent(A)) b[j] = phiA;
for(let j of getAdjacent(B)) b[j] = phiB;
return b;
}
function init(phiA = 1, phiB = 0){ // initialize unknown vector
let u = new Float64Array(W * H);
u[A] = phiA;
u[B] = phiB;
return u;
}
function solveLinearSystem(err = 1e-20){ // conjugate gradient solver
let b = getRHS();
let u = init();
let r = sub(linOp(u), b);
let p = r;
let e = dot(r,r);
while(true){
let Ap = linOp(p);
let alpha = e / dot(p, Ap);
u = sub(u, mul(alpha, p.slice()));
r = sub(linOp(u), b);
let e_new = dot(r,r);
let beta = e_new / e;
if(e_new < err) return u;
e = e_new;
p = add(r, mul(beta, p));
}
}
function getResistance(u){
let curr = 0;
for(let j of getAdjacent(A)) curr += u[A] - u[j];
return 1 / curr;
}
let phi = solveLinearSystem();
let res = getResistance(phi);
console.log(`R = ${res} Ohm`);
</syntaxhighlight>
{{out}}
<pre>R = 1.608991241730955 Ohm</pre>
=={{header|jq}}==
'''Works with jq and gojq, that is, the C and Go implementations of jq.'''
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
# Create a $rows * $columns matrix initialized with the input value
def matrix($rows; $columns):
. as $in
| [range(0;$columns)|$in] as $row
| [range(0;$rows)|$row];
def Node($v; $fixed):
{$v, $fixed};
# input: a suitable matrix of Nodes
def setBoundary:
.[1][1].v = 1
| .[1][1].fixed = 1
| .[6][7].v = -1
| .[6][7].fixed = -1 ;
# input: {d, m} where
# .d and .m are matrices (as produced by matrix(h; w)) of Nodes
# output: {d, m, diff} with d updated
def calcDiff($w; $h):
def adjust($cond; action): if $cond then action | .n += 1 else . end;
reduce range(0; $h) as $i (.diff = 0;
reduce range(0; $w) as $j (.;
.v = 0
| .n = 0
| adjust($i > 0; .v += .m[$i-1][$j].v)
| adjust($j > 0; .v += .m[$i][$j-1].v)
| adjust($i + 1 < $h; .v += .m[$i+1][$j].v)
| adjust($j + 1 < $w; .v += .m[$i][$j+1].v)
| .v = .m[$i][$j].v - .v/.n
| .d[$i][$j].v = .v
| if (.m[$i][$j].fixed == 0) then .diff += .v * .v else . end ) ) ;
# input: a mesh of width w and height h, i.e. a matrix as prodcued by matrix(h;w)
def iter:
length as $h
| (.[0]|length) as $w
| { m : .,
d : (Node(0;0) | matrix($h; $w)),
cur: [0,0,0],
diff: 1e10 }
| until (.diff <= 1e-24;
.m |= setBoundary
| calcDiff($w; $h)
| reduce range(0;$h) as $i (.;
reduce range(0;$w) as $j (.;
.m[$i][$j].v += (- .d[$i][$j].v) )) )
| reduce range(0; $h) as $i (.;
reduce range(0; $w) as $j (.;
.k = 0
| if ($i != 0) then .k += 1 else . end
| if ($j != 0) then .k += 1 else . end
| if ($i < $h - 1) then .k += 1 else . end
| if ($j < $w - 1) then .k += 1 else . end
| .cur[.m[$i][$j].fixed + 1] += .d[$i][$j].v * .k ))
| (.cur[2] - .cur[0]) / 2 ;
def task($S):
def mesh: Node(0; 0) | matrix($S; $S);
(2 / (mesh | iter)) as $r
| "R = \($r) ohms";
task(10)
</syntaxhighlight>
{{output}}
<pre>
R = 1.608991241729889 ohms
</pre>
=={{header|Julia}}==
Line 982 ⟶ 1,355:
Because the graph is a rectangular grid, we can in turn write the incidence matrix D in terms of Kronecker products ⊗ (<code>kron</code> in Julia) of "one-dimensional" D<sub>1</sub> matrices (the incidence matrix of a 1d resistor network).
We use Julia's built-in sparse-matrix solvers (based on SuiteSparse) to solve the resulting sparse linear system efficiently
<
D1 = speye(N-1,N) - spdiagm(ones(N-1),1,N-1,N)
D = [ kron(D1, speye(N)); kron(speye(N), D1) ]
Line 988 ⟶ 1,361:
b = zeros(N^2); b[i], b[j] = 1, -1
v = (D' * D) \ b
v[i] - v[j]</
{{out}}
<pre>
Line 998 ⟶ 1,371:
=={{header|Kotlin}}==
{{trans|C}}
<
typealias List2D<T> = List<List<T>>
Line 1,059 ⟶ 1,432:
val r = 2.0 / iter(mesh, S, S)
println("R = $r")
}</
{{out}}
Line 1,065 ⟶ 1,438:
R = 1.608991241729889
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{works with|Mathematica|13.0}}
Use <b>KirchhoffMatrix</b> and <b>DrazinInverse</b> to compute the effective resistance matrix of a graph:
<syntaxhighlight lang="mathematica">ResistanceMatrix[g_Graph] := With[{n = VertexCount[g], km = KirchhoffMatrix[g]},
Table[ ReplacePart[ Diagonal[ DrazinInverse[ ReplacePart[km, k -> UnitVector[n, k]]]], k -> 0],
{k, n}]
]
rm = ResistanceMatrix[GridGraph[{10, 10}]];
N[rm[[12, 68]], 40]</syntaxhighlight>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
{{works with|Mathematica|8.0}}
Use <b>KirchhoffMatrix</b> and <b>PseudoInverse</b> to compute the effective resistance matrix of a graph to the desired precision:
<syntaxhighlight lang="mathematica">ResistanceMatrix[g_, prec_:$MachinePrecision]:= With[{m = PseudoInverse[N[KirchhoffMatrix[g], prec]]},
Outer[Plus, Diagonal[m], Diagonal[m]] - m - Transpose[m]
]
rm = ResistanceMatrix[GridGraph[{10, 10}], 40];
rm[[12, 68]]</syntaxhighlight>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
=={{header|Maxima}}==
<
for the potential at A and B, since I = R (V(B) - V(A)) where I is given and we want R.
Atually, we will compute potential at each node, except A where we assume it's 0.
Without
is known up to a constant. For A we will simply write the equation V(A) = 0, to
keep the program simple.
Line 1,128 ⟶ 1,529:
bfloat(%), fpprec = 40;
3.89226554090400912102670691601064387507b0</
=={{header|Modula-2}}==
<
FROM RConversions IMPORT RealToStringFixed;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,270 ⟶ 1,642:
ReadChar;
END ResistorMesh.</
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">const S = 10
type
NodeKind = enum nodeFree, nodeA, nodeB
Node = object
v: float
fixed: NodeKind
Mesh[H, W: static int] = array[H, array[W, Node]]
func setBoundary(m: var Mesh) =
m[1][1].v = 1.0
m[1][1].fixed = nodeA
m[6][7].v = -1.0
m[6][7].fixed = nodeB
func calcDiff[H, W: static int](m,: Mesh[H, W]; d: var Mesh[H, W]): float =
for i in 0..<H:
for j in 0..<W:
var v = 0.0
var n = 0
if i > 0:
v += m[i - 1][j].v
inc n
if j > 0:
v += m[i][j - 1].v
inc n
if i + 1 < m.H:
v += m[i + 1][j].v
inc n
if j + 1 < m.W:
v += m[i][j + 1].v
inc n
v = m[i][j].v - v / n.toFloat
d[i][j].v = v
if m[i][j].fixed == nodeFree:
result += v * v
func iter[H, W: static int](m: var Mesh[H, W]): float =
var
d: Mesh[H, W]
cur: array[NodeKind, float]
diff = 1e10
while diff > 1e-24:
m.setBoundary()
diff = calcDiff(m, d)
for i in 0..<H:
for j in 0..<W:
m[i][j].v -= d[i][j].v
for i in 0..<H:
for j in 0..<W:
var k = 0
if i != 0: inc k
if j != 0: inc k
if i < m.H - 1: inc k
if j < m.W - 1: inc k
cur[m[i][j].fixed] += d[i][j].v * k.toFloat
result = (cur[nodeA] - cur[nodeB]) / 2
when isMainModule:
var mesh: Mesh[S, S]
let r = 2 / mesh.iter()
echo "R = ", r</syntaxhighlight>
{{out}}
<pre>R = 1.608991241729889</pre>
=={{header|Octave}}==
We'll solve the linear system. We'll write [[wp:Kirchhoff's circuit laws|Kirchhoff's circuit laws]] at each node and search for a voltage distribution that creates a 1A current coming from A exiting in B. The difference of voltage between B and A is then the resistance.
<
NN = N*N;
G = sparse(NN, NN);
Line 1,314 ⟶ 1,765:
VB = voltage( B );
full( abs( VA - VB ) )</
{{out}}
<pre>ans = 1.6090</pre>
Line 1,320 ⟶ 1,771:
=={{header|Perl}}==
{{trans|C}}
<
use warnings;
my ($w, $h) = (9, 9);
Line 1,357 ⟶ 1,809:
sub iter {
my $diff = 1;
while ($diff > 1e-
set_boundary();
$diff = calc_diff();
#print "error^2: $diff\
for my $i (0 .. $h) {
for my $j (0 .. $w) {
Line 1,367 ⟶ 1,819:
}
}
my @current = (0) x 3;
Line 1,379 ⟶ 1,830:
}
{{out}}
<pre>R = 1.
=={{header|Phix}}==
Line 1,448 ⟶ 1,838:
uses inverse() from [[Gauss-Jordan_matrix_inversion#Phix]]
and matrix_mul() from [[Matrix_multiplication#Phix]]
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">resistormesh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ni</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nj</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ai</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">aj</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bi</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bj</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ni</span><span style="color: #0000FF;">*</span><span style="color: #000000;">nj</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">A</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">B</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ni</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nj</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">nj</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">j</span><span style="color: #000080;font-style:italic;">--1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">ai</span> <span style="color: #008080;">and</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">aj</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #000000;">ni</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">nj</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">nj</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;"><</span><span style="color: #000000;">nj</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">bi</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">nj</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">bj</span>
<span style="color: #000000;">B</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">A</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">inverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">A</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">B</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">matrix_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">A</span><span style="color: #0000FF;">,</span><span style="color: #000000;">B</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">B</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Resistance = %.13f ohms\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">resistormesh</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,482 ⟶ 1,874:
=={{header|Python}}==
{{trans|D}}
<
class Fixed:
Line 1,545 ⟶ 1,937:
print "R = %.16f" % (2 / iter(mesh))
main()</
{{out}}
<pre>R = 1.6089912417307286</pre>
Line 1,551 ⟶ 1,943:
{{trans|Maxima}}
<
from fractions import Fraction
Line 1,621 ⟶ 2,013:
# python grid.py 10 10 1 1 7 6
# 455859137025721/283319837425200
# 1.6089912417307297</
=={{header|Racket}}==
Line 1,628 ⟶ 2,020:
This version avoids mutation... possibly a little more costly than C, but more functional.
<
(require racket/flonum)
Line 1,697 ⟶ 2,089:
(module+ main
(printf "R = ~a~%" (mesh-R '((1 1) (6 7)) 10 10)))</
{{out}}
<pre>R = 1.6089912417301238</pre>
=={{header|Raku}}==
(formerly Perl 6)
{{trans|C}}
<syntaxhighlight lang="raku" line>my $*TOLERANCE = 1e-12;
sub set-boundary(@mesh,@p1,@p2) {
@mesh[ @p1[0] ; @p1[1] ] = 1;
@mesh[ @p2[0] ; @p2[1] ] = -1;
}
sub solve(@p1, @p2, Int \w, Int \h) {
my @d = [0 xx w] xx h;
my @V = [0 xx w] xx h;
my @fixed = [0 xx w] xx h;
set-boundary(@fixed,@p1,@p2);
loop {
set-boundary(@V,@p1,@p2);
my $diff = 0;
for (flat ^h X ^w) -> \i, \j {
my @neighbors = (@V[i-1;j], @V[i;j-1], @V[i+1;j], @V[i;j+1]).grep: *.defined;
@d[i;j] = my \v = @V[i;j] - @neighbors.sum / @neighbors;
$diff += v × v unless @fixed[i;j];
}
last if $diff =~= 0;
for (flat ^h X ^w) -> \i, \j {
@V[i;j] -= @d[i;j];
}
}
my @current;
for (flat ^h X ^w) -> \i, \j {
@current[ @fixed[i;j]+1 ] += @d[i;j] × (?i + ?j + (i < h-1) + (j < w-1) );
}
(@current[2] - @current[0]) / 2
}
say 2 / solve (1,1), (6,7), 10, 10;
</syntaxhighlight>
{{out}}
<pre>1.60899124172989</pre>
=={{header|REXX}}==
Line 1,707 ⟶ 2,142:
Dropping the decimal digits precision ('''numeric digits''') to '''10''' makes the execution '''3''' times faster.
<
if 2=='f2'x then ohms = "ohms" /*EBCDIC machine? Then use 'ohms'. */
else ohms = "Ω" /* ASCII " " " Greek Ω.*/
Line 1,719 ⟶ 2,154:
if digs=='' | digs=="," then digs= 20 /* " " " " " " */
numeric digits digs /*use moderate decimal digs (precision)*/
minVal = 1'e-' || (digs*2) /*calculate the threshold
say ' minimum value is ' format(minVal,,,,0) " using " digs ' decimal digits'; say
say ' resistor mesh size is: ' wide "wide, " high 'high' ; say
Line 1,751 ⟶ 2,186:
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,766 ⟶ 2,201:
=={{header|Sidef}}==
{{trans|Perl}}
<
var v = h.of { w.of(0) } # voltage
Line 1,815 ⟶ 2,250:
}
say "R = #{2 / iter()}"</
{{out}}
<pre>
Line 1,823 ⟶ 2,258:
=={{header|Tcl}}==
{{trans|C}}
<
# This code is structured as a class with a little trivial DSL parser
Line 1,917 ⟶ 2,352:
expr {$voltageDifference / [my FindCurrentFixpoint $epsilon]}
}
}</
Setting up and solving this particular problem:
<
size {10 10}
fixed {1 1 1.0}
fixed {6 7 -1.0}
}
puts [format "R = %.12g" [mesh solveForResistance]]</
{{out}}
<pre>
R = 1.60899124173
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="wren">class Node {
construct new(v, fixed) {
_v = v
_fixed = fixed
}
v { _v }
v=(value) { _v = value }
fixed { _fixed }
fixed=(value) { _fixed = value }
}
var setBoundary = Fn.new { |m|
m[1][1].v = 1
m[1][1].fixed = 1
m[6][7].v = -1
m[6][7].fixed = -1
}
var calcDiff = Fn.new { |m, d, w, h|
var total = 0
for (i in 0...h) {
for (j in 0...w) {
var v = 0
var n = 0
if (i > 0) {
v = v + m[i-1][j].v
n = n + 1
}
if (j > 0) {
v = v + m[i][j-1].v
n = n + 1
}
if (i + 1 < h) {
v = v + m[i+1][j].v
n = n + 1
}
if (j + 1 < w) {
v = v + m[i][j+1].v
n = n + 1
}
v = m[i][j].v - v/n
d[i][j].v = v
if (m[i][j].fixed == 0) total = total + v*v
}
}
return total
}
var iter = Fn.new { |m, w, h|
var d = List.filled(h, null)
for (i in 0...h) {
d[i] = List.filled(w, null)
for (j in 0...w) d[i][j] = Node.new(0, 0)
}
var cur = [0] * 3
var diff = 1e10
while (diff > 1e-24) {
setBoundary.call(m)
diff = calcDiff.call(m, d, w, h)
for (i in 0...h) {
for (j in 0...w) m[i][j].v = m[i][j].v - d[i][j].v
}
}
for (i in 0...h) {
for (j in 0...w) {
var k = 0
if (i != 0) k = k + 1
if (j != 0) k = k + 1
if (i < h - 1) k = k + 1
if (j < w - 1) k = k + 1
cur[m[i][j].fixed + 1] = cur[m[i][j].fixed + 1] + d[i][j].v * k
}
}
return (cur[2] - cur[0]) / 2
}
var S = 10
var mesh = List.filled(S, null)
for (i in 0...S) {
mesh[i] = List.filled(S, null)
for (j in 0...S) mesh[i][j] = Node.new(0, 0)
}
var r = 2 / iter.call(mesh, S, S)
System.print("R = %(r)")</syntaxhighlight>
{{out}}
<pre>
R = 1.6089912417299
</pre>
=={{header|XPL0}}==
{{trans|C}}
<
def S = 10;
Line 1,980 ⟶ 2,509:
real MeshV(S,S); int MeshF(S,S);
RlOut(0, 2.0 / Iter(MeshV, MeshF, S, S))</
{{out}}
Line 1,986 ⟶ 2,515:
1.60899
</pre>
=={{header|Yabasic}}==
{{trans|ERRE}}
<syntaxhighlight lang="yabasic"> N=10
NN=N*N
DIM A(NN,NN+1)
NODE=0
FOR ROW=1 TO N
FOR COL=1 TO N
NODE=NODE+1
IF ROW>1 THEN
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE-N)=-1
END IF
IF ROW<N THEN
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE+N)=-1
END IF
IF COL>1 THEN
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE-1)=-1
END IF
IF COL<N THEN
A(NODE,NODE)=A(NODE,NODE)+1
A(NODE,NODE+1)=-1
END IF
NEXT
NEXT
AR=2 : AC=2 : A=AC+N*(AR-1)
BR=7 : BC=8 : B=BC+N*(BR-1)
A(A,NN+1)=-1
A(B,NN+1)=1
PRINT "Nodes ",A,B
// solve linear system
// using Gauss-Seidel method
// with pivoting
R=NN
FOR J=1 TO R
FOR I=J TO R
IF A(I,J)<>0 BREAK
NEXT
IF I=R+1 THEN
PRINT "No solution!"
END
END IF
FOR K=1 TO R+1
T = A(J,K)
A(J,K) = A(I,K)
A(I,K) = T
NEXT
Y=1/A(J,J)
FOR K=1 TO R+1
A(J,K)=Y*A(J,K)
NEXT
FOR I=1 TO R
IF I<>J THEN
Y=-A(I,J)
FOR K=1 TO R+1
A(I,K)=A(I,K)+Y*A(J,K)
NEXT
END IF
NEXT
NEXT
PRINT "Resistence = "; : PRINT ABS(A(A,NN+1)-A(B,NN+1)) USING "%1.13f"</syntaxhighlight>
=={{header|zkl}}==
{{trans|Maxima}}
Uses the GNU Scientific Library.
<
fcn onGrid(i,j,p,q){ ((0<=i<p) and (0<=j<q)) }
fcn gridResistor(p,q, ai,aj, bi,bj){
Line 2,009 ⟶ 2,607:
b[k:=bi*q + bj]=1;
A.AxEQb(b)[k];
}</
<
{{out}}
<pre>
|