Resistance network calculator: Difference between revisions

m
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 62:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F gauss(&m)
V (n, p) = (m.len, m[0].len)
L(i) 0 .< n
Line 102:
assert(is_equal(13/7, network(4*4, 0, 4*4-1, ‘0 1 1|1 2 1|2 3 1|4 5 1|5 6 1|6 7 1|8 9 1|9 10 1|10 11 1|12 13 1|13 14 1|14 15 1|0 4 1|4 8 1|8 12 1|1 5 1|5 9 1|9 13 1|2 6 1|6 10 1|10 14 1|3 7 1|7 11 1|11 15 1’)))
assert(is_equal(180 , network(4, 0, 3, ‘0 1 150|0 2 50|1 3 300|2 3 250’)))
print(‘OK’)</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 190:
fmt.Printf("%.6g\n", f)
}
}</langsyntaxhighlight>
 
{{out}}
Line 202:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">function gauss(m)
n, p = length(m), length(m[1])
for i in 1:n
Line 241:
@assert(13//7 == network(4*4,0,4*4-1,"0 1 1|1 2 1|2 3 1|4 5 1|5 6 1|6 7 1|8 9 1|9 10 1|10 11 1|12 13 1|13 14 1|14 15 1|0 4 1|4 8 1|8 12 1|1 5 1|5 9 1|9 13 1|2 6 1|6 10 1|10 14 1|3 7 1|7 11 1|11 15 1"))
@assert(180 == network(4,0,3,"0 1 150|0 2 50|1 3 300|2 3 250"))
</syntaxhighlight>
</lang>
No assertion errors.
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import rationals, sequtils, strscans, strutils, sugar
 
type Fraction = Rational[int]
Line 302:
assert 3 // 2 == network(3*3, 0, 3*3-1, "0 1 1|1 2 1|3 4 1|4 5 1|6 7 1|7 8 1|0 3 1|3 6 1|1 4 1|4 7 1|2 5 1|5 8 1")
assert 13 // 7 == network(4*4, 0, 4*4-1, "0 1 1|1 2 1|2 3 1|4 5 1|5 6 1|6 7 1|8 9 1|9 10 1|10 11 1|12 13 1|13 14 1|14 15 1|0 4 1|4 8 1|8 12 1|1 5 1|5 9 1|9 13 1|2 6 1|6 10 1|10 14 1|3 7 1|7 11 1|11 15 1")
assert 180 // 1 == network(4, 0, 3, "0 1 150|0 2 50|1 3 300|2 3 250")</langsyntaxhighlight>
 
{{out}}
Line 308:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 360:
printf "%10.3f\n", network(@$_);
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 10.000
Line 369:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">argmax</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
Line 418:
<span style="color: #008000;">"0 4 1|4 8 1|8 12 1|1 5 1|5 9 1|9 13 1|2 6 1|6 10 1|10 14 1|3 7 1|7 11 1|11 15 1"</span><span style="color: #0000FF;">))</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;">"%.6g\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">network</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0 1 150|0 2 50|1 3 300|2 3 250"</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 428:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from fractions import Fraction
 
def gauss(m):
Line 461:
assert 3/2 == network(3*3,0,3*3-1,"0 1 1|1 2 1|3 4 1|4 5 1|6 7 1|7 8 1|0 3 1|3 6 1|1 4 1|4 7 1|2 5 1|5 8 1")
assert Fraction(13,7) == network(4*4,0,4*4-1,"0 1 1|1 2 1|2 3 1|4 5 1|5 6 1|6 7 1|8 9 1|9 10 1|10 11 1|12 13 1|13 14 1|14 15 1|0 4 1|4 8 1|8 12 1|1 5 1|5 9 1|9 13 1|2 6 1|6 10 1|10 14 1|3 7 1|7 11 1|11 15 1")
assert 180 == network(4,0,3,"0 1 150|0 2 50|1 3 300|2 3 250")</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Python}}
<syntaxhighlight lang="raku" perl6line>sub gauss ( @m is copy ) {
for @m.keys -> \i {
my \k = max |(i .. @m.end), :by({ @m[$_][i].abs });
Line 508:
;
plan +@tests;
is .[0], network( |.[1..4] ), .[4].substr(0,10)~'…' for @tests;</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var argmax = Fn.new { |m, i|
Line 590:
network.call(4, 0, 3, "0 1 150|0 2 50|1 3 300|2 3 250")
]
for (f in fa) Fmt.print("$.5g", f)</langsyntaxhighlight>
 
{{out}}
Line 603:
{{libheader|GSL}} GNU Scientific Library
This a tweak of [[Resistor_mesh#zkl]]
<langsyntaxhighlight lang="zkl">var [const] GSL=Import.lib("zklGSL"); // libGSL (GNU Scientific Library)
 
fcn network(n,k0,k1,mesh){
Line 619:
b[k1]=1;
A.AxEQb(b)[k1];
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">network(7,0,1,"0 2 6|2 3 4|3 4 10|4 5 2|5 6 8|6 1 4|3 5 6|3 6 6|3 1 8|2 1 8")
.println();
 
Line 630:
 
network(4,0,3,"0 1 150|0 2 50|1 3 300|2 3 250")
.println();</langsyntaxhighlight>
{{out}}
<pre>
9,483

edits