Walsh matrix: Difference between revisions
→DOME: Removed an unused line. |
Realize in F# |
||
Line 52:
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Walsh matrix. Nigel Galloway: August 31st., 2023
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
let walsh()=let w2=matrix [[1.0;1.0];[1.0;-1.0]] in Seq.unfold(fun n->Some(n,w2.KroneckerProduct n)) w2
walsh() |> Seq.take 5 |> Seq.iter(fun n->printfn "%s" (n.ToMatrixString()))
</syntaxhighlight>
{{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 1 -1 -1
1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 .. 1 1
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 .. 1 -1
1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 .. -1 -1
1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 .. -1 1
1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 .. -1 -1
1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 .. -1 1
1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 .. 1 1
1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 .. 1 -1
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 .. -1 -1
1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 .. -1 1
1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -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|Raku}}==
<syntaxhighlight lang="raku" line>sub walsh (\m) { (map {$_?? -1 !! ' 1'}, map { :3(.base: 2) % 2 }, [X+&] ^2**m xx 2 ).batch: 2**m }
|
Revision as of 15:16, 31 August 2023
This page uses content from Wikipedia. The original article was at Walsh matrix. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
A Walsh matrix is a specific square matrix of dimensions 2n, where n is some particular natural number. The elements of the matrix are either +1 or −1 and its rows as well as columns are orthogonal, i.e. dot product is zero. Each row of a Walsh matrix corresponds to a Walsh function.
Walsh matrices are a special case of Hadamard matrices. The naturally ordered Hadamard (Walsh) matrix is defined by the recursive formula below, and the sequency-ordered Hadamard (Walsh) matrix is formed by rearranging the rows so that the number of sign changes in a row is in increasing order.
- To generate a naturally ordered Walsh matrix
Matrices of dimension 2k for k ∈ N are given by the recursive formula:
and in general
for 2 ≤ k ∈ N, where ⊗ denotes the Kronecker product.
- Task
- Write a routine that, given a natural number k, returns a naturally ordered Walsh matrix of order 2k.
- Display a few sample generated matrices.
-
- Traditionally, Walsh matrices use 1 & -1 to denote the different cell values in text mode, or green and red blocks in image mode. You may use whichever display mode is most convenient for your particular language.
- Stretch
- Also, optionally generate sequency ordered Walsh matrices.
-
- A sequency ordered Walsh matrix has the rows sorted by number of sign changes.
- See also
F#
// Walsh matrix. Nigel Galloway: August 31st., 2023
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
let walsh()=let w2=matrix [[1.0;1.0];[1.0;-1.0]] in Seq.unfold(fun n->Some(n,w2.KroneckerProduct n)) w2
walsh() |> Seq.take 5 |> Seq.iter(fun n->printfn "%s" (n.ToMatrixString()))
- Output:
1 1 1 -1 1 1 1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 1 1 1 1 1 1 1 1 1 1 -1 1 -1 1 -1 1 -1 1 1 -1 -1 1 1 -1 -1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 -1 1 -1 -1 1 -1 1 1 1 -1 -1 -1 -1 1 1 1 -1 -1 1 -1 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 .. 1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 .. 1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 .. -1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 .. -1 1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 .. -1 -1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 .. -1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 .. 1 1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 .. 1 -1 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 .. -1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 .. -1 1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 -1 .. 1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 .. 1 -1
Raku
sub walsh (\m) { (map {$_?? -1 !! ' 1'}, map { :3(.base: 2) % 2 }, [X+&] ^2**m xx 2 ).batch: 2**m }
sub natural (@row) { Same }
sub sign-changes (@row) { sum (1..^@row).map: { 1 if @row[$_] !== @row[$_ - 1] } }
use SVG;
for &natural, 'natural', &sign-changes, 'sequency' -> &sort, $sort {
for 2,4,5 -> $order {
# ASCII text
.put for "\nWalsh matrix - order $order ({exp($order,2)} x {exp($order,2)}), $sort order:", |walsh($order).sort: &sort;
# SVG image
my $side = 600;
my $scale = $side / 2**$order;
my $row = 0;
my @blocks;
my %C = ' 1' => '#0F0', '-1' => '#F00';
for walsh($order).sort: &sort -> @row {
my \x = $row++ * $scale;
for @row.kv {
my \y = $^k * $scale;
@blocks.push: (:rect[:x(x),:y(y),:width($scale),:height($scale),:fill(%C{$^v})]);
}
}
"walsh-matrix--order-{$order}--{$sort}-sort-order--raku.svg".IO.spurt:
SVG.serialize(:svg[:width($side),:height($side),:stroke<black>,:stroke-width<1>,|@blocks])
}
}
- Output:
Walsh matrix - order 2 (4 x 4), natural order: 1 1 1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 1 Walsh matrix - order 4 (16 x 16), natural order: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 1 -1 -1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 Walsh matrix - order 5 (32 x 32), natural order: 1 1 1 1 1 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-1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1
Wren
Wren-cli
Text mode version.
import "./matrix" for Matrix
import "./fmt" for Fmt
var walshMatrix = Fn.new { |n|
var walsh = Matrix.new(n, n, 0)
walsh[0, 0] = 1
var k = 1
while (k < n) {
for (i in 0...k) {
for (j in 0...k) {
walsh[i+k, j] = walsh[i, j]
walsh[i, j+k] = walsh[i, j]
walsh[i+k, j+k] = -walsh[i, j]
}
}
k = k + k
}
return walsh
}
var signChanges = Fn.new { |row|
var n = row.count
var sc = 0
for (i in 1...n) {
if (row[i-1] == -row[i]) sc = sc + 1
}
return sc
}
var walshCache = {} // to avoid calculating the Walsh matrix twice
for (order in [2, 4, 5]) {
var n = 1 << order
Fmt.print("Walsh matrix - order $d ($d x $d), natural order:", order, n, n)
var w = walshMatrix.call(n)
walshCache[order] = w
Fmt.mprint(w, 2, 0, "|", true)
System.print()
}
for (order in [2, 4, 5]) {
var n = 1 << order
Fmt.print("Walsh matrix - order $d ($d x $d), sequency order:", order, n, n)
var rows = walshCache[order].toList
rows.sort { |r1, r2| signChanges.call(r1) < signChanges.call(r2) }
Fmt.mprint(rows, 2, 0, "|", true)
System.print()
}
- Output:
Walsh matrix - order 2 (4 x 4), natural order: | 1 1 1 1| | 1 -1 1 -1| | 1 1 -1 -1| | 1 -1 -1 1| Walsh matrix - order 4 (16 x 16), natural order: | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1| | 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1| | 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1| | 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1| | 1 1 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1| | 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1| | 1 1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1| | 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1| | 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1| | 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1| | 1 1 -1 -1 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1| | 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1| | 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1| | 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1| | 1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1| | 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1| Walsh matrix - order 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 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 1 -1 1 1 -1| | 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1| | 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1| | 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1| | 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1| | 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1| | 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1| | 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1| | 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1 -1| | 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1| | 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1|
DOME
Image mode version.
import "dome" for Window
import "input" for Keyboard
import "graphics" for Canvas, Color
import "./matrix" for Matrix
import "./polygon" for Square
var walshMatrix = Fn.new { |n|
var walsh = Matrix.new(n, n, 0)
walsh[0, 0] = 1
var k = 1
while (k < n) {
for (i in 0...k) {
for (j in 0...k) {
walsh[i+k, j] = walsh[i, j]
walsh[i, j+k] = walsh[i, j]
walsh[i+k, j+k] = -walsh[i, j]
}
}
k = k + k
}
return walsh
}
var signChanges = Fn.new { |row|
var n = row.count
var sc = 0
for (i in 1...n) {
if (row[i-1] == -row[i]) sc = sc + 1
}
return sc
}
var WalshNaturalCache = {}
var WalshSequencyCache = {}
for (order in [2, 4, 5]) {
var n = 1 << order
var w = walshMatrix.call(n).toList
WalshNaturalCache[order] = w
}
for (order in [2, 4, 5]) {
var rows = WalshNaturalCache[order].toList
rows.sort { |r1, r2| signChanges.call(r1) < signChanges.call(r2) }
WalshSequencyCache[order] = rows
}
class WalshMatrix {
construct new() {
Window.title = "Walsh Matrix"
Window.resize(1020, 750)
Canvas.resize(1020, 750)
var bc = Color.black
for (natural in [true, false]) {
if (natural) {
Canvas.print("NATURAL ORDERING", 450, 10, Color.blue)
} else {
Canvas.print("SEQUENCY ORDERING", 450, 400, Color.blue)
}
var z = 10
for (order in [2, 4, 5]) {
var y = natural ? 30 : 420
var mat = natural ? WalshNaturalCache[order] : WalshSequencyCache[order]
var n = 1 << order
var size = 320 / n
for (row in mat) {
var x = z
for (i in row) {
var fc = (i == 1) ? Color.green : Color.red
var sq = Square.new(x, y, size)
sq.drawfill(fc, bc)
x = x + size
}
y = y + size
}
z = z + 340
}
}
}
init() {}
update() {}
draw(alpha) {}
}
var Game = WalshMatrix.new()