Walsh matrix: Difference between revisions
Content deleted Content added
Ada implementation |
|||
(18 intermediate revisions by 11 users not shown) | |||
Line 1:
{{draft task|Matrices}}
{{Wikipedia|Walsh matrix}}
Line 50:
;* [[Kronecker product|Related task: Kronecker product]]
=={{header|Ada}}==
This solution demonstrates Ada enumerations for matrix values, for how to display the matrix (with numbers or as "red" (`#`) and green (`_`), and as array indices. It also illustrates usage of a default value of a procedure parameter.
<syntaxhighlight lang="ada">
pragma Ada_2022;
with Ada.Containers.Generic_Array_Sort;
with Ada.Text_IO;
procedure Walsh_Matrices is
package IO renames Ada.Text_IO;
-- SECTION
-- Definition of a Walsh Matrix
type Walsh_Value is (Red, Green);
Opposite : constant array (Walsh_Value) of Walsh_Value :=
[Red => Green, Green => Red];
type Walsh_Matrix is
array (Positive range <>, Positive range <>) of Walsh_Value;
-- SECTION
-- Sorting the matrix
-- In order to avail ourselves of Ada's standard library,
-- we define an auxiliary data
-- that records the number of sign changes in a given row.
-- We sort an array of **that** structure, then
-- use that to sort the matrix.
type Sort_Data_Record is record
Row : Positive;
Sign_Changes : Natural;
end record;
type Sort_Data_Array is array (Positive range <>) of Sort_Data_Record;
function "<" (Left, Right : Sort_Data_Record) return Boolean is
(Left.Sign_Changes < Right.Sign_Changes);
procedure Sort_By_Sequency is new Ada.Containers.Generic_Array_Sort
(Index_Type => Positive, Element_Type => Sort_Data_Record,
Array_Type => Sort_Data_Array);
function Number_Of_Sign_Changes
(Matrix : Walsh_Matrix; Row : Positive) return Natural
is
Result : Natural := 0;
begin
for Col in 2 .. Matrix'Last (2) loop
Result :=
@ + (if Matrix (Row, Col) = Matrix (Row, Col - 1) then 0 else 1);
end loop;
return Result;
end Number_Of_Sign_Changes;
procedure Sort_Matrix_By_Sequency (Matrix : in out Walsh_Matrix) is
Sort_Data : Sort_Data_Array (Matrix'Range (1)) :=
[for Ith in Matrix'Range (1) =>
(Row => Ith,
Sign_Changes => Number_Of_Sign_Changes (Matrix, Ith))];
Temp : Walsh_Matrix (Matrix'Range (1), Matrix'Range (2));
begin
Sort_By_Sequency (Sort_Data);
for Row in Matrix'Range (1) loop
for Col in Matrix'Range (2) loop
Temp (Row, Col) := Matrix (Sort_Data (Row).Row, Col);
end loop;
end loop;
Matrix := Temp;
end Sort_Matrix_By_Sequency;
-- SECTION
-- Displaying a Walsh Matrix
type Display is (Number, Color);
-- for the colors we imitate the Algol implementation
Output : constant array (Walsh_Value, Display) of String (1 .. 2) :=
[Red => [Number => "-1", Color => " #"],
Green => [Number => " 1", Color => " _"]];
procedure Put (W : Walsh_Matrix; Kind : Display := Number) is
begin
for Row in W'Range (1) loop
IO.Put ("( ");
for Col in W'Range (2) loop
IO.Put (Output (W (Row, Col), Kind) & " ");
end loop;
IO.Put_Line (" )");
end loop;
end Put;
-- SECTION
-- Creating a Walsh Matrix
procedure Fill (W : in out Walsh_Matrix; Row, Col, Dim : Positive) is
-- recursively fills a 2^Dim square submatrix of W,
-- starting from the given row and column
begin
if Dim = 1 then
W (Row, Col) := Green;
W (Row, Col + 1) := Green;
W (Row + 1, Col) := Green;
W (Row + 1, Col + 1) := Red;
else
Fill (W, Row, Col, Dim - 1);
Fill (W, Row, Col + 2**(Dim - 1), Dim - 1);
Fill (W, Row + 2**(Dim - 1), Col, Dim - 1);
Fill (W, Row + 2**(Dim - 1), Col + 2**(Dim - 1), Dim - 1);
for R in Row + 2**(Dim - 1) .. Row + 2**Dim - 1 loop
for C in Col + 2**(Dim - 1) .. Col + 2**Dim - 1 loop
W (R, C) := Opposite (@);
end loop;
end loop;
end if;
end Fill;
function New_Matrix (Dimension : Positive) return Walsh_Matrix is
Result : Walsh_Matrix (1 .. 2**Dimension, 1 .. 2**Dimension);
begin
Fill (Result, Result'First (1), Result'First (2), Dimension);
return Result;
end New_Matrix;
-- SECTION
-- a few values
W_1 : Walsh_Matrix := New_Matrix (1);
W_2 : Walsh_Matrix := New_Matrix (2);
W_5 : Walsh_Matrix := New_Matrix (5);
begin
Put (W_1, Color);
IO.New_Line;
Sort_Matrix_By_Sequency (W_1);
Put (W_1, Color);
IO.New_Line;
Put (W_2, Color);
IO.New_Line;
Sort_Matrix_By_Sequency (W_2);
Put (W_2, Color);
IO.New_Line;
Put (W_5, Color);
IO.New_Line;
Put (W_5);
IO.New_Line;
Sort_Matrix_By_Sequency (W_5);
Put (W_5, Color);
IO.New_Line;
end Walsh_Matrices;
</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 -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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 )
( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ )
( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ # # # # # # # # # # # # # # # # )
( _ _ _ _ _ _ _ _ # # # # # # # # # # # # # # # # _ _ _ _ _ _ _ _ )
( _ _ _ _ _ _ _ _ # # # # # # # # _ _ _ _ _ _ _ _ # # # # # # # # )
( _ _ _ _ # # # # # # # # _ _ _ _ _ _ _ _ # # # # # # # # _ _ _ _ )
( _ _ _ _ # # # # # # # # _ _ _ _ # # # # _ _ _ _ _ _ _ _ # # # # )
( _ _ _ _ # # # # _ _ _ _ # # # # # # # # _ _ _ _ # # # # _ _ _ _ )
( _ _ _ _ # # # # _ _ _ _ # # # # _ _ _ _ # # # # _ _ _ _ # # # # )
( _ _ # # # # _ _ _ _ # # # # _ _ _ _ # # # # _ _ _ _ # # # # _ _ )
( _ _ # # # # _ _ _ _ # # # # _ _ # # _ _ _ _ # # # # _ _ _ _ # # )
( _ _ # # # # _ _ # # _ _ _ _ # # # # _ _ _ _ # # _ _ # # # # _ _ )
( _ _ # # # # _ _ # # _ _ _ _ # # _ _ # # # # _ _ # # _ _ _ _ # # )
( _ _ # # _ _ # # # # _ _ # # _ _ _ _ # # _ _ # # # # _ _ # # _ _ )
( _ _ # # _ _ # # # # _ _ # # _ _ # # _ _ # # _ _ _ _ # # _ _ # # )
( _ _ # # _ _ # # _ _ # # _ _ # # # # _ _ # # _ _ # # _ _ # # _ _ )
( _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # )
( _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ _ # # _ )
( _ # # _ _ # # _ _ # # _ _ # # _ # _ _ # # _ _ # # _ _ # # _ _ # )
( _ # # _ _ # # _ # _ _ # # _ _ # # _ _ # # _ _ # _ # # _ _ # # _ )
( _ # # _ _ # # _ # _ _ # # _ _ # _ # # _ _ # # _ # _ _ # # _ _ # )
( _ # # _ # _ _ # # _ _ # _ # # _ _ # # _ # _ _ # # _ _ # _ # # _ )
( _ # # _ # _ _ # # _ _ # _ # # _ # _ _ # _ # # _ _ # # _ # _ _ # )
( _ # # _ # _ _ # _ # # _ # _ _ # # _ _ # _ # # _ # _ _ # _ # # _ )
( _ # # _ # _ _ # _ # # _ # _ _ # _ # # _ # _ _ # _ # # _ # _ _ # )
( _ # _ # # _ # _ _ # _ # # _ # _ _ # _ # # _ # _ _ # _ # # _ # _ )
( _ # _ # # _ # _ _ # _ # # _ # _ # _ # _ _ # _ # # _ # _ _ # _ # )
( _ # _ # # _ # _ # _ # _ _ # _ # # _ # _ _ # _ # _ # _ # # _ # _ )
( _ # _ # # _ # _ # _ # _ _ # _ # _ # _ # # _ # _ # _ # _ _ # _ # )
( _ # _ # _ # _ # # _ # _ # _ # _ _ # _ # _ # _ # # _ # _ # _ # _ )
( _ # _ # _ # _ # # _ # _ # _ # _ # _ # _ # _ # _ _ # _ # _ # _ # )
( _ # _ # _ # _ # _ # _ # _ # _ # # _ # _ # _ # _ # _ # _ # _ # _ )
( _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # _ # )
</pre>
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # construct Walsh Matrices #
CO BEGIN code from the Kronecker product task CO
# multiplies in-place the elements of the matrix a by the scaler b #
OP *:= = ( REF[,]INT a, INT b )REF[,]INT:
BEGIN
FOR i FROM 1 LWB a TO 1 UPB a DO
FOR j FROM 2 LWB a TO 2 UPB a DO
a[ i, j ] *:= b
OD
OD;
a
END # *:= # ;
# returns the Kronecker Product of the two matrices a and b #
# the result will have lower bounds of 1 #
PRIO X = 6;
OP X = ( [,]INT a, b )[,]INT:
BEGIN
# normalise the matrices to have lower bounds of 1 #
[,]INT m = a[ AT 1, AT 1 ];
[,]INT n = b[ AT 1, AT 1 ];
# construct the result #
INT r 1 size = 1 UPB n;
INT r 2 size = 2 UPB n;
[ 1 : 1 UPB m * 1 UPB n, 1 : 2 UPB m * 2 UPB n ]INT k;
FOR i FROM 1 LWB m TO 1 UPB m DO
FOR j FROM 2 LWB m TO 2 UPB m DO
( k[ 1 + ( ( i - 1 ) * r 1 size ) : i * r 1 size
, 1 + ( ( j - 1 ) * r 2 size ) : j * r 2 size
] := n
) *:= m[ i, j ]
OD
OD;
k
END # X # ;
CO END code from the Kronecker product task CO
# returns a Walsh matrix of oreder n #
OP WALSH = ( INT n )[,]INT:
BEGIN
[,]INT w1 = ( ( 1, 1 )
, ( 1, -1 )
);
FLEX[ 1 : 0, 1 : 0 ]INT result := 1;
FOR order TO n DO
result := result X w1
OD;
result
END # WALSH # ;
# returns Walsh matrix a sorted into sequency order #
OP SEQUENCYSORT = ( [,]INT a )[,]INT:
BEGIN
# sort the rows of the matrix into order of the number of sign #
# changes in the row #
[,]INT w = a[ AT 1, AT 1 ]; # normalise the matrix to have #
# lower bounds of 1 #
[ 1 : 1 UPB w, 1 : 2 UPB w ]INT result;
# construct the resullt with the rows in order of the number of #
# the number of sign changes in the original #
# note the number of changes is unique and in 0 .. UPB a - 1 #
FOR row FROM 1 TO 1 UPB w DO
INT changes := 0;
INT curr := w[ row, 1 ];
FOR col FROM 2 TO 2 UPB w DO
IF curr /= w[ row, col ] THEN
changes +:= 1;
curr := w[ row, col ]
FI
OD;
result[ changes + 1, : ] := w[ row, : ]
OD;
result
END # SEQUENCYSORT # ;
CO returns r encoded with 1 = "_" and -1 = "#" CO
OP TOWSTRING = ( []INT r )STRING:
BEGIN
STRING result := "";
FOR j FROM LWB r TO UPB r DO
result +:= IF r[ j ] > 0 THEN "_" ELSE "#" FI
OD;
result
END # TOWSTRING # ;
# show the natural order and sequency order Walsh matrices of order 5 #
[,]INT w5 = WALSH 5;
[,]INT s5 = SEQUENCYSORT w5;
FOR row FROM 1 TO 1 UPB s5 DO
print( ( TOWSTRING w5[ row, : ], " ", TOWSTRING s5[ row, : ], newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
________________________________ ________________________________
_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_# ________________################
__##__##__##__##__##__##__##__## ________################________
_##__##__##__##__##__##__##__##_ ________########________########
____####____####____####____#### ____########________########____
_#_##_#__#_##_#__#_##_#__#_##_#_ ____########____####________####
__####____####____####____####__ ____####____########____####____
_##_#__#_##_#__#_##_#__#_##_#__# ____####____####____####____####
________########________######## __####____####____####____####__
_#_#_#_##_#_#_#__#_#_#_##_#_#_#_ __####____####__##____####____##
__##__####__##____##__####__##__ __####__##____####____##__####__
_##__##_#__##__#_##__##_#__##__# __####__##____##__####__##____##
____########________########____ __##__####__##____##__####__##__
_#_##_#_#_#__#_#_#_##_#_#_#__#_# __##__####__##__##__##____##__##
__####__##____##__####__##____## __##__##__##__####__##__##__##__
_##_#__##__#_##__##_#__##__#_##_ __##__##__##__##__##__##__##__##
________________################ _##__##__##__##__##__##__##__##_
_#_#_#_#_#_#_#_##_#_#_#_#_#_#_#_ _##__##__##__##_#__##__##__##__#
__##__##__##__####__##__##__##__ _##__##_#__##__##__##__#_##__##_
_##__##__##__##_#__##__##__##__# _##__##_#__##__#_##__##_#__##__#
____####____########____####____ _##_#__##__#_##__##_#__##__#_##_
_#_##_#__#_##_#_#_#__#_##_#__#_# _##_#__##__#_##_#__#_##__##_#__#
__####____####__##____####____## _##_#__#_##_#__##__#_##_#__#_##_
_##_#__#_##_#__##__#_##_#__#_##_ _##_#__#_##_#__#_##_#__#_##_#__#
________################________ _#_##_#__#_##_#__#_##_#__#_##_#_
_#_#_#_##_#_#_#_#_#_#_#__#_#_#_# _#_##_#__#_##_#_#_#__#_##_#__#_#
__##__####__##__##__##____##__## _#_##_#_#_#__#_##_#__#_#_#_##_#_
_##__##_#__##__##__##__#_##__##_ _#_##_#_#_#__#_#_#_##_#_#_#__#_#
____########____####________#### _#_#_#_##_#_#_#__#_#_#_##_#_#_#_
_#_##_#_#_#__#_##_#__#_#_#_##_#_ _#_#_#_##_#_#_#_#_#_#_#__#_#_#_#
__####__##____####____##__####__ _#_#_#_#_#_#_#_##_#_#_#_#_#_#_#_
_##_#__##__#_##_#__#_##__##_#__# _#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
void display(const std::vector<std::vector<int32_t>>& matrix) {
for ( const std::vector<int32_t>& row : matrix ) {
for ( const int32_t& element : row ) {
std::cout << std::setw(3) << element;
}
std::cout << std::endl;;
}
std::cout << std::endl;;
}
uint32_t sign_change_count(const std::vector<int32_t>& row) {
uint32_t sign_changes = 0;
for ( uint64_t i = 1; i < row.size(); ++i ) {
if ( row[i - 1] == -row[i] ) {
sign_changes++;
}
}
return sign_changes;
}
std::vector<std::vector<int32_t>> walsh_matrix(const uint32_t& size) {
std::vector<std::vector<int32_t>> walsh = { size, std::vector<int32_t>(size, 0) };
walsh[0][0] = 1;
uint32_t k = 1;
while ( k < size ) {
for ( uint32_t i = 0; i < k; ++i ) {
for ( uint32_t j = 0; j < k; ++j ) {
walsh[i + k][j] = walsh[i][j];
walsh[i][j + k] = walsh[i][j];
walsh[i + k][j + k] = -walsh[i][j];
}
}
k += k;
}
return walsh;
}
int main() {
for ( const std::string type : { "Natural", "Sequency" } ) {
for ( const uint32_t order : { 2, 4, 5 } ) {
uint32_t size = 1 << order;
std::cout << "Walsh matrix of order " << order << ", " << type << " order:" << std::endl;
std::vector<std::vector<int32_t>> walsh = walsh_matrix(size);
if ( type == "Sequency" ) {
std::sort(walsh.begin(), walsh.end(),
[](const std::vector<int32_t> &row1, const std::vector<int32_t> &row2) {
return sign_change_count(row1) < sign_change_count(row2);
});
}
display(walsh);
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
Walsh matrix of order 2, Natural order:
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
Walsh matrix of order 4, 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
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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
</pre>
=={{header|F_Sharp|F#}}==
Line 159 ⟶ 741:
{{out}}
[[File:Walsh matrices.png|thumb|center]]
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">REM Text mode version.
Sub Imprime(w() As Integer)
Dim As Integer i, j, ub = Ubound(w)
Print "Walsh matrix - order " & Fix(Sqr(ub)) & " (" & ub & "x" & ub & "), Natural order:"
For i = 0 To ub-1
For j = 0 To ub-1
Print Using "###"; w(i, j);
Next j
Print
Next i
Print
End Sub
Sub WalshMatrix(n As Integer)
Dim walsh(0 To n, 0 To n) As Integer
walsh(0,0) = 1
Dim As Integer i, j, k
k = 1
While k < n
For i = 0 To k-1
For j = 0 To k-1
walsh(i+k, j) = walsh(i, j)
walsh(i, j+k) = walsh(i, j)
walsh(i+k, j+k) = -walsh(i, j)
Next j
Next i
k *= 2
Wend
Imprime(walsh())
End Sub
Dim As Integer n = 4
n = 4: WalshMatrix(n)
n = 16: WalshMatrix(n)
n = 32: WalshMatrix(n)
Sleep</syntaxhighlight>
{{out}}
<pre>Walsh matrix - order 2 (4x4), Natural order:
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
Walsh matrix - order 4 (16x16), 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 -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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
</pre>
=={{header|J}}==
<syntaxhighlight lang=J>kp1=: [: ,./^:2 */ NB. Victor Cerovski, 2010-02-26
walsh=: {{(_1^3=i.2 2)&kp1^:y 1}}
sequencyorder=: /: 2 ~:/\"1 ]</syntaxhighlight>
Small examples (time is not an issue here, but page estate is an issue):
<syntaxhighlight lang=J> walsh 0
1
walsh 1
1 1
1 _1
walsh 2
1 1 1 1
1 _1 1 _1
1 1 _1 _1
1 _1 _1 1
walsh 3
1 1 1 1 1 1 1 1
1 _1 1 _1 1 _1 1 _1
1 1 _1 _1 1 1 _1 _1
1 _1 _1 1 1 _1 _1 1
1 1 1 1 _1 _1 _1 _1
1 _1 1 _1 _1 1 _1 1
1 1 _1 _1 _1 _1 1 1
1 _1 _1 1 _1 1 1 _1
sequencyorder walsh 3
1 1 1 1 1 1 1 1
1 1 1 1 _1 _1 _1 _1
1 1 _1 _1 _1 _1 1 1
1 1 _1 _1 1 1 _1 _1
1 _1 _1 1 1 _1 _1 1
1 _1 _1 1 _1 1 1 _1
1 _1 1 _1 _1 1 _1 1
1 _1 1 _1 1 _1 1 _1</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class WalshMatrix {
public static void main(String[] args) {
for ( String type : List.of( "Natural", "Sequency" ) ) {
for ( int order : List.of( 2, 4, 5 ) ) {
int size = 1 << order;
System.out.println("Walsh matrix of order " + order + ", " + type + " order:");
List<List<Integer>> walsh = walshMatrix(size);
if ( type.equals("Sequency") ) {
Collections.sort(walsh, rowComparator);
}
display(walsh);
}
}
}
private static List<List<Integer>> walshMatrix(int size) {
List<List<Integer>> walsh = IntStream.range(0, size).boxed()
.map( i -> new ArrayList<Integer>(Collections.nCopies(size, 0)) ).collect(Collectors.toList());
walsh.get(0).set(0, 1);
int k = 1;
while ( k < size ) {
for ( int i = 0; i < k; i++ ) {
for ( int j = 0; j < k; j++ ) {
walsh.get(i + k).set(j, walsh.get(i).get(j));
walsh.get(i).set(j + k, walsh.get(i).get(j));
walsh.get(i + k).set(j + k, -walsh.get(i).get(j));
}
}
k += k;
}
return walsh;
}
private static int signChangeCount(List<Integer> row) {
int signChanges = 0;
for ( int i = 1; i < row.size(); i++ ) {
if ( row.get(i - 1) == -row.get(i) ) {
signChanges += 1;
}
}
return signChanges;
}
private static Comparator<List<Integer>> rowComparator =
(one, two) -> Integer.compare(signChangeCount(one), signChangeCount(two));
private static void display(List<List<Integer>> matrix) {
for ( List<Integer> row : matrix ) {
for ( int element : row ) {
System.out.print(String.format("%3d", element));
}
System.out.println();
}
System.out.println();
}
}
</syntaxhighlight>
{{ out }}
<pre>
Walsh matrix of order 2, Natural order:
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
Walsh matrix of order 4, Natural order:
1 1 1 1 1 1 1 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
</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>
=={{header|Julia}}==
Line 254 ⟶ 1,337:
</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}}==
Using altern_kronecker as defined in Kronecker product task
<syntaxhighlight lang="maxima">
/* Function that attempts to implement recursion but only works for n when already called for every antecessor */
auxwalsh(n):=if n=1 then w[1]:matrix([1,1],[1,-1]) else
block(w[2]:matrix([1,1,1,1],[1,-1,1,-1],[1,1,-1,-1],[1,-1,-1,1]),w[n]:altern_kronecker(w[1],w[n-1]),w[n])$
/* Function that guarantees an output for integer n */
walsh(n):=block(makelist(auxwalsh(i),i,1,n),last(%%))$
/* Examples */
walsh(4)$
wxdraw2d(palette = [red,gray,green], image(%,0,0,30,30))$
walsh(6)$
wxdraw2d(palette = [red,gray,green], image(%,0,0,30,30))$
</syntaxhighlight>
[[File:Walsh4Maxima.png|thumb|center]]
[[File:Walsh6Maxima.png|thumb|center]]
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
use strict; # https://www.rosettacode.org/wiki/Walsh_matrix
use warnings;
use List::AllUtils qw( bundle_by pairwise nsort_by );
sub Kronecker
{
my ($ac, $bc) = map scalar($_->[0]->@*), my ($A, $B) = @_;
return [ bundle_by { [ @_ ] } $ac * $bc, pairwise { $a * $b }
@{[ map { map { ($_) x $bc } (@$_) x @$B } @$A ]}, # left side
@{[ ( map { (@$_) x $ac } @$B ) x @$A ]} ]; # right side
}
sub Walsh # Task - write a routine that, given k, returns Walsh of 2**k
{
my $k = shift;
$k > 0 ? Kronecker [ [1,1],[1,-1] ], Walsh( $k - 1 ) : [[1]];
}
for my $k ( 1, 3, 2, 4 ) # test code out of order just for fun
{
printf '%3d'x@$_ . "\n", @$_ for [], (my $w = Walsh($k))->@*, [];
print nsort_by { scalar(() = /(.)\1*/g) }
map { join '', (0, '_', '#')[@$_], "\n" } $w->@*;
}</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
________________
________########
____########____
____####____####
__####____####__
__####__##____##
__##__####__##__
__##__##__##__##
_##__##__##__##_
_##__##_#__##__#
_##_#__##__#_##_
_##_#__#_##_#__#
_#_##_#__#_##_#_
_#_##_#_#_#__#_#
_#_#_#_##_#_#_#_
_#_#_#_#_#_#_#_#
</pre>
=={{header|Phix}}==
Line 511 ⟶ 1,728:
| [[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 517 ⟶ 1,775:
===Wren-cli===
Text mode version.
<syntaxhighlight lang="
import "./fmt" for Fmt
Line 689 ⟶ 1,947:
{{libheader|Wren-polygon}}
Image mode version.
<syntaxhighlight lang="
import "input" for Keyboard
import "graphics" for Canvas, Color
|