Walsh matrix: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added FreeBASIC)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(10 intermediate revisions by 4 users not shown)
Line 1:
{{draft task|Matrices}}
{{Wikipedia|Walsh matrix}}
 
Line 789:
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{Works with|jq}}
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
 
This entry uses a non-recursive method for creating Walsh matrices, but the `kprod` definition
at [[Kronecker_product#jq]] could also be used as follows:
<syntaxhighlight lang="jq">
## Generate a Walsh matrix of size 2^$n for $n >= 1
def walsh:
. as $n
| [[1, 1], [1, -1]] as $w2
| if $n < 2 then $w2 else kprod($w2; $n - 1 | walsh) end;
</syntaxhighlight>
<syntaxhighlight lang="jq">
## Generic matrix functions
 
# Create an m x n matrix
def matrix(m; n; init):
if m == 0 then []
elif m == 1 then [range(0;n) | init]
elif m > 0 then
matrix(1;n;init) as $row
| [range(0;m) | $row ]
else error("matrix\(m);_;_) invalid")
end;
 
# Input: a numeric array
def signChanges:
def s: if . > 0 then 1 elif . < 0 then -1 else 0 end;
. as $row
| reduce range(1;length) as $i (0;
if ($row[$i-1]|s) == -($row[$i]|s) then . + 1 else . end );
 
# Print a matrix of integers
# $width is the minimum width to use per cell
def mprint($width):
def max(s): reduce s as $x (null; if . == null or $x > . then $x else . end);
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
(max($width, (.[][] | tostring | length) + 1)) as $w
| . as $in
| range(0; length) as $i
| reduce range(0; .[$i]|length) as $j ("|"; . + ($in[$i][$j]|lpad($w)))
| . + " |" ;
 
def cprint:
. as $in
| range(0; length) as $i
| reduce range(0; .[$i]|length) as $j (""; . + ($in[$i][$j]));
 
def color: if . == 1 then "🟥" else "🟩" end;
</syntaxhighlight>
'''Walsh matrices'''
<syntaxhighlight lang="jq">
def walshMatrix:
. as $n
| { walsh: matrix($n; $n; 0) }
| .walsh[0][0] = 1
| .k = 1
| until (.k >= $n;
.k as $k
| reduce range (0; $k) as $i (.;
reduce range (0; $k) as $j (.;
.walsh[$i][$j] as $wij
| .walsh[$i+$k][$j] = $wij
| .walsh[$i][$j+$k] = $wij
| .walsh[$i+$k][$j+$k] = -$wij ))
| .k += .k )
| .walsh ;
 
## The tasks
def task1:
(2, 4, 5) as $order
| pow(2; $order)
| "Walsh matrix - order \($order) (\(.) x \(.)), natural order:",
(walshMatrix | mprint(2)),
"";
 
def task2:
(2, 4, 5) as $order
| pow(2; $order)
| "Walsh matrix - order \($order) (\(.) x \(.)), sequency order:",
(walshMatrix | sort_by( signChanges ) | mprint(2)),
"";
 
def task3:
5 as $order
| pow(2; $order)
| "Walsh matrix - order \($order) (\(.) x \(.)), natural order:",
(walshMatrix | map(map(color)) | cprint),
"";
 
def task4:
5 as $order
| pow(2; $order)
| "Walsh matrix - order \($order) (\(.) x \(.)), sequency order:",
(walshMatrix | sort_by( signChanges ) | map(map(color)) | cprint),
"";
 
task1, task2, task3, task4
</syntaxhighlight>
{{output}}
The output for the first two tasks is essentially as for [[#Wren|Wren]].
The output for the last two tasks is as follows:
<pre>
Walsh matrix - order 5 (32 x 32), natural order:
🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥
🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩
🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩
🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥
🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩
🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥
🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥
🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩
🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩
🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥
🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥
🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩
🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥
🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩
🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩
🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥
🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩
🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥
🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥
🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩
🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥
🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩
🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩
🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥
🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥
🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩
🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩
🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥
🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩
🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥
🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥
🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩
 
Walsh matrix - order 5 (32 x 32), sequency order:
🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥
🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩
🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥
🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩
🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥
🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟥🟥🟥🟥🟩🟩🟩🟩
🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥
🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩
🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥
🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩
🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥
🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩
🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥
🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟥🟥🟩🟩🟥🟥🟩🟩
🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥
🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩
🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥
🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩
🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥
🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩
🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥
🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟥🟩🟩🟥🟩🟥🟥🟩
🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥
🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩
🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥
🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩
🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥
🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩
🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥
🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟥🟩🟥🟩🟥🟩🟥🟩
🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥
🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩🟥🟩
</pre>
 
Line 885 ⟶ 1,063:
</pre>
[[File:Walsh_subplots.svg|center]]
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">
WalshMatrix = Nest[ArrayFlatten@{{#, #}, {#, -#}} &, 1, #] &;
WalshMatrix[4] // MatrixPlot
</syntaxhighlight>
 
{{out}}
[[File:Walsh4Mathematica.png|thumb|center]]
 
=={{header|MATLAB}}==
<syntaxhighlight lang="MATLAB">
walsh=@(x)hadamard(2^x);
imagesc(walsh(4));
</syntaxhighlight>
 
=={{header|Maxima}}==
Line 1,261 ⟶ 1,454:
| [[File:Walsh-matrix--order-5--sign-changes-sort-order--raku.svg|150px|thumb]]
|}
 
=={{header|RPL}}==
« DUP SIZE DUP 1 GET
SWAP 2 * 0 CON ROT ROT → w k
« 0 3 '''FOR''' t
'''IF''' t 3 == '''THEN''' -1 'w' STO* '''END'''
1 k SQ '''FOR''' z
z DUP 1 - k / IP k * +
t 2 MOD LASTARG / IP <span style="color:grey">@ can be replaced by IDIV2 on HP-49s</span>
k * SWAP k SQ * 2 * + +
w z GET
PUT
'''NEXT NEXT'''
» » '<span style="color:blue">NEXTW</span>' STO
« [[1 1][1 -1]]
'''WHILE''' SWAP 1 - DUP '''REPEAT'''
SWAP <span style="color:blue">NEXTW</span>
'''END''' DROP
» '<span style="color:blue">WALSH</span>' STO
 
4 <span style="color:blue">WALSH</span>
{{out}}
<pre>
1: [[ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
[ 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ]
[ 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 ]
[ 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 ]
[ 1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 ]
[ 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 ]
[ 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 ]
[ 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 ]
[ 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 ]
[ 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 ]
[ 1 1 -1 -1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 ]
[ 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 ]
[ 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 ]
[ 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 ]
[ 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 ]
[ 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 ]]
</pre>
 
=={{header|Wren}}==
Line 1,267 ⟶ 1,501:
===Wren-cli===
Text mode version.
<syntaxhighlight lang="ecmascriptwren">import "./matrix" for Matrix
import "./fmt" for Fmt
 
Line 1,439 ⟶ 1,673:
{{libheader|Wren-polygon}}
Image mode version.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "input" for Keyboard
import "graphics" for Canvas, Color
9,482

edits