Implement the 2D Hadamard TransformIs this number evil?Is it an OVSF code?Generate a Walsh MatrixOptimize matrix chain multiplicationDo Matrix Multiplication!Compute the Matrix-VectorIs the matrix rank-one?Generate all square sub-matrices of a given sizeGenerate a Walsh MatrixSum of replicated matricesIs the Matrix Positive-Definite?Make all the squares explodeReorder a matrix, twice
Why might a vampiric race have low birthrates?
Tourist / simple city maps to print
Are independent variables necessarily "independent" and how does this relate to what's being predicted?
Why wasn't Captain America eating in the end of The Avengers?
Is it okay to have an email address called "SS"?
Multithreading program stuck in optimized mode but runs normally in -O0
Has an amateur ever taken off in a plane? Could they?
How do I to replicate this system of equations?
How to protect software from being deleted by antivirus?
Google Search Console is making up URLs which don't exist in my Sitemap and then complains that these pages have error
Error: Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.60-eap-25 in Ionic 3
If authors misuse diacritics, should I correct them or leave it as written?
Should I correct a mistake on an arXiv manuscript, that I found while refereeing it?
Are snow shoes useful in mountaineering?
Does microwaving food create particles that are not created when warming food by conventional means?
Dealing with recruiters who clearly didn't look at my resume
What is it called when someone asks for an opinion that almost everyone asked is going to have the same answer on?
Function defined everywhere but continuous nowhere
What is it called when at university there are two subjects being held at the same time?
Outlook not so good
Ethan Finds the Maximum Element
Do dams reduce the flow of river downstream?
Is 2FA via mobile phone still a good idea when phones are the most exposed device?
Are Cosets Isomorphic to One Another
Implement the 2D Hadamard Transform
Is this number evil?Is it an OVSF code?Generate a Walsh MatrixOptimize matrix chain multiplicationDo Matrix Multiplication!Compute the Matrix-VectorIs the matrix rank-one?Generate all square sub-matrices of a given sizeGenerate a Walsh MatrixSum of replicated matricesIs the Matrix Positive-Definite?Make all the squares explodeReorder a matrix, twice
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
$begingroup$
The Hadamard transform works similarly to the Fourier transform: it takes a vector and maps it to its frequency components, which are the Walsh functions. Instead of sine waves in the Fourier transform, the Walsh functions are discrete "square waves" so to speak. This makes it easier to compute.
The size of a Hadamard matrix is a power of two, 2^n x 2^n
. We can build a Hadamard matrix for a given n
recursively. $$H_1 = beginpmatrix1&1\1 & -1endpmatrix$$ And then build block matrices in the same pattern to get $$H_n = beginpmatrixH_n-1&H_n-1\H_n-1 & -H_n-1endpmatrix$$
For instance,
$$H_3 = beginpmatrix1&1&1&1&1&1&1&1\1&-1&1&-1&1&-1&1&-1\1&1&-1&-1&1&1&-1&-1\1&-1&-1&1&1&-1&-1&1\1&1&1&1&-1&-1&-1&-1\1&-1&1&-1&-1&1&-1&1\1&1&-1&-1&-1&-1&1&1\1&-1&-1&1&-1&1&1&-1endpmatrix = beginpmatrixH_2&H_2\H_2&-H_2endpmatrix$$
Note how the entries are always 1
or -1
and symmetric! Easy! Then the Hadamard transform is simply multiplying H_n
by a column vector and dividing by 2^n
.
In order to apply the Hadamard transform to 2D images, we have to apply the 1D HT across every column and then every row. In short, for an input matrix X, the output will be a new matrix Y given by two matrix multiplications:
$$Y = frac12^2nH_nXH_n$$
Another way to implement this is by first applying the HT to every column in the image separately. Then apply it to every row, pretending they were column vectors. For example, to compute the 2D HT of $$beginpmatrix2 & 3\ 2 & 5endpmatrix$$
we first do
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 2endpmatrix = beginpmatrix2 \ 0endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix3 \ 5endpmatrix = beginpmatrix4 \ -1endpmatrix quadrightarrowquad beginpmatrix2&4\0 & -1endpmatrix $$
which takes care of the columns, and then we do the rows,
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 4endpmatrix = beginpmatrix3 \ -1endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix0 \ -1endpmatrix = beginpmatrix-0.5 \ 0.5endpmatrix quadrightarrowquad beginpmatrix3&-1\-0.5 & 0.5endpmatrix $$
Finally, one can also compute this recursively using the fast Hadamard transform. It's up to you to find out if this is shorter or not.
Challenge
Your challenge is to perform the 2D Hadamard transform on a 2D array of arbitrary size 2^n x 2^n
, where n > 0
. Your code must accept the array as input (2D array or a flattened 1D array). Then perform the 2D HT and output the result.
In the spirit of flexibility, you have a choice for the output. Before multiplying by the prefactor 1/(2^(2n))
, the output array is all integers. You may either multiply by the prefactor and output the answer as floats (no formatting necessary), or you may output a string "1/X*"
followed by the array of integers, where X
is whatever the factor should be. The last test case shows this as an example.
You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program.
Test Cases
Input:
2,3
2,5
Output:
3,-1
-0.5,0.5
Input:
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
Output:
1,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
Input:
1,0,0,0
1,1,0,0
1,1,1,0
1,1,1,1
Output:
0.6250,0.1250,0.2500,0
-0.1250,0.1250,0,0
-0.2500,0,0.1250,0.1250
0,0,-0.1250,0.1250
Input:
0,0,0,0,0,0,0,1
0,0,0,0,0,0,2,0
0,0,0,0,0,3,0,0
0,0,0,0,4,0,0,0
0,0,0,5,0,0,0,0
0,0,6,0,0,0,0,0
0,7,0,0,0,0,0,0
8,0,0,0,0,0,0,0
Output:
1/64*
36,4,8,0,16,0,0,0
-4,-36,0,-8,0,-16,0,0
-8,0,-36,-4,0,0,-16,0
0,8,4,36,0,0,0,16
-16,0,0,0,-36,-4,-8,0
0,16,0,0,4,36,0,8
0,0,16,0,8,0,36,4
0,0,0,-16,0,-8,-4,-36
Scoring
This is code golf, so the smallest program in bytes wins.
code-golf matrix
$endgroup$
|
show 1 more comment
$begingroup$
The Hadamard transform works similarly to the Fourier transform: it takes a vector and maps it to its frequency components, which are the Walsh functions. Instead of sine waves in the Fourier transform, the Walsh functions are discrete "square waves" so to speak. This makes it easier to compute.
The size of a Hadamard matrix is a power of two, 2^n x 2^n
. We can build a Hadamard matrix for a given n
recursively. $$H_1 = beginpmatrix1&1\1 & -1endpmatrix$$ And then build block matrices in the same pattern to get $$H_n = beginpmatrixH_n-1&H_n-1\H_n-1 & -H_n-1endpmatrix$$
For instance,
$$H_3 = beginpmatrix1&1&1&1&1&1&1&1\1&-1&1&-1&1&-1&1&-1\1&1&-1&-1&1&1&-1&-1\1&-1&-1&1&1&-1&-1&1\1&1&1&1&-1&-1&-1&-1\1&-1&1&-1&-1&1&-1&1\1&1&-1&-1&-1&-1&1&1\1&-1&-1&1&-1&1&1&-1endpmatrix = beginpmatrixH_2&H_2\H_2&-H_2endpmatrix$$
Note how the entries are always 1
or -1
and symmetric! Easy! Then the Hadamard transform is simply multiplying H_n
by a column vector and dividing by 2^n
.
In order to apply the Hadamard transform to 2D images, we have to apply the 1D HT across every column and then every row. In short, for an input matrix X, the output will be a new matrix Y given by two matrix multiplications:
$$Y = frac12^2nH_nXH_n$$
Another way to implement this is by first applying the HT to every column in the image separately. Then apply it to every row, pretending they were column vectors. For example, to compute the 2D HT of $$beginpmatrix2 & 3\ 2 & 5endpmatrix$$
we first do
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 2endpmatrix = beginpmatrix2 \ 0endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix3 \ 5endpmatrix = beginpmatrix4 \ -1endpmatrix quadrightarrowquad beginpmatrix2&4\0 & -1endpmatrix $$
which takes care of the columns, and then we do the rows,
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 4endpmatrix = beginpmatrix3 \ -1endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix0 \ -1endpmatrix = beginpmatrix-0.5 \ 0.5endpmatrix quadrightarrowquad beginpmatrix3&-1\-0.5 & 0.5endpmatrix $$
Finally, one can also compute this recursively using the fast Hadamard transform. It's up to you to find out if this is shorter or not.
Challenge
Your challenge is to perform the 2D Hadamard transform on a 2D array of arbitrary size 2^n x 2^n
, where n > 0
. Your code must accept the array as input (2D array or a flattened 1D array). Then perform the 2D HT and output the result.
In the spirit of flexibility, you have a choice for the output. Before multiplying by the prefactor 1/(2^(2n))
, the output array is all integers. You may either multiply by the prefactor and output the answer as floats (no formatting necessary), or you may output a string "1/X*"
followed by the array of integers, where X
is whatever the factor should be. The last test case shows this as an example.
You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program.
Test Cases
Input:
2,3
2,5
Output:
3,-1
-0.5,0.5
Input:
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
Output:
1,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
Input:
1,0,0,0
1,1,0,0
1,1,1,0
1,1,1,1
Output:
0.6250,0.1250,0.2500,0
-0.1250,0.1250,0,0
-0.2500,0,0.1250,0.1250
0,0,-0.1250,0.1250
Input:
0,0,0,0,0,0,0,1
0,0,0,0,0,0,2,0
0,0,0,0,0,3,0,0
0,0,0,0,4,0,0,0
0,0,0,5,0,0,0,0
0,0,6,0,0,0,0,0
0,7,0,0,0,0,0,0
8,0,0,0,0,0,0,0
Output:
1/64*
36,4,8,0,16,0,0,0
-4,-36,0,-8,0,-16,0,0
-8,0,-36,-4,0,0,-16,0
0,8,4,36,0,0,0,16
-16,0,0,0,-36,-4,-8,0
0,16,0,0,4,36,0,8
0,0,16,0,8,0,36,4
0,0,0,-16,0,-8,-4,-36
Scoring
This is code golf, so the smallest program in bytes wins.
code-golf matrix
$endgroup$
9
$begingroup$
Nice first challenge!
$endgroup$
– Arnauld
Oct 13 at 22:39
2
$begingroup$
Related. Loosely related
$endgroup$
– Luis Mendo
Oct 14 at 10:38
$begingroup$
@Arnauld Thanks! I don't think I'm good enough to golf yet, but it's still fun seeing the other answers.
$endgroup$
– HiddenBabel
Oct 14 at 18:13
$begingroup$
"You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program." Does this include using build-in functions that return the matrix?
$endgroup$
– Kroppeb
Oct 14 at 19:12
3
$begingroup$
an upvote and applause for not requiring floating point
$endgroup$
– ngn
Oct 14 at 19:27
|
show 1 more comment
$begingroup$
The Hadamard transform works similarly to the Fourier transform: it takes a vector and maps it to its frequency components, which are the Walsh functions. Instead of sine waves in the Fourier transform, the Walsh functions are discrete "square waves" so to speak. This makes it easier to compute.
The size of a Hadamard matrix is a power of two, 2^n x 2^n
. We can build a Hadamard matrix for a given n
recursively. $$H_1 = beginpmatrix1&1\1 & -1endpmatrix$$ And then build block matrices in the same pattern to get $$H_n = beginpmatrixH_n-1&H_n-1\H_n-1 & -H_n-1endpmatrix$$
For instance,
$$H_3 = beginpmatrix1&1&1&1&1&1&1&1\1&-1&1&-1&1&-1&1&-1\1&1&-1&-1&1&1&-1&-1\1&-1&-1&1&1&-1&-1&1\1&1&1&1&-1&-1&-1&-1\1&-1&1&-1&-1&1&-1&1\1&1&-1&-1&-1&-1&1&1\1&-1&-1&1&-1&1&1&-1endpmatrix = beginpmatrixH_2&H_2\H_2&-H_2endpmatrix$$
Note how the entries are always 1
or -1
and symmetric! Easy! Then the Hadamard transform is simply multiplying H_n
by a column vector and dividing by 2^n
.
In order to apply the Hadamard transform to 2D images, we have to apply the 1D HT across every column and then every row. In short, for an input matrix X, the output will be a new matrix Y given by two matrix multiplications:
$$Y = frac12^2nH_nXH_n$$
Another way to implement this is by first applying the HT to every column in the image separately. Then apply it to every row, pretending they were column vectors. For example, to compute the 2D HT of $$beginpmatrix2 & 3\ 2 & 5endpmatrix$$
we first do
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 2endpmatrix = beginpmatrix2 \ 0endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix3 \ 5endpmatrix = beginpmatrix4 \ -1endpmatrix quadrightarrowquad beginpmatrix2&4\0 & -1endpmatrix $$
which takes care of the columns, and then we do the rows,
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 4endpmatrix = beginpmatrix3 \ -1endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix0 \ -1endpmatrix = beginpmatrix-0.5 \ 0.5endpmatrix quadrightarrowquad beginpmatrix3&-1\-0.5 & 0.5endpmatrix $$
Finally, one can also compute this recursively using the fast Hadamard transform. It's up to you to find out if this is shorter or not.
Challenge
Your challenge is to perform the 2D Hadamard transform on a 2D array of arbitrary size 2^n x 2^n
, where n > 0
. Your code must accept the array as input (2D array or a flattened 1D array). Then perform the 2D HT and output the result.
In the spirit of flexibility, you have a choice for the output. Before multiplying by the prefactor 1/(2^(2n))
, the output array is all integers. You may either multiply by the prefactor and output the answer as floats (no formatting necessary), or you may output a string "1/X*"
followed by the array of integers, where X
is whatever the factor should be. The last test case shows this as an example.
You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program.
Test Cases
Input:
2,3
2,5
Output:
3,-1
-0.5,0.5
Input:
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
Output:
1,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
Input:
1,0,0,0
1,1,0,0
1,1,1,0
1,1,1,1
Output:
0.6250,0.1250,0.2500,0
-0.1250,0.1250,0,0
-0.2500,0,0.1250,0.1250
0,0,-0.1250,0.1250
Input:
0,0,0,0,0,0,0,1
0,0,0,0,0,0,2,0
0,0,0,0,0,3,0,0
0,0,0,0,4,0,0,0
0,0,0,5,0,0,0,0
0,0,6,0,0,0,0,0
0,7,0,0,0,0,0,0
8,0,0,0,0,0,0,0
Output:
1/64*
36,4,8,0,16,0,0,0
-4,-36,0,-8,0,-16,0,0
-8,0,-36,-4,0,0,-16,0
0,8,4,36,0,0,0,16
-16,0,0,0,-36,-4,-8,0
0,16,0,0,4,36,0,8
0,0,16,0,8,0,36,4
0,0,0,-16,0,-8,-4,-36
Scoring
This is code golf, so the smallest program in bytes wins.
code-golf matrix
$endgroup$
The Hadamard transform works similarly to the Fourier transform: it takes a vector and maps it to its frequency components, which are the Walsh functions. Instead of sine waves in the Fourier transform, the Walsh functions are discrete "square waves" so to speak. This makes it easier to compute.
The size of a Hadamard matrix is a power of two, 2^n x 2^n
. We can build a Hadamard matrix for a given n
recursively. $$H_1 = beginpmatrix1&1\1 & -1endpmatrix$$ And then build block matrices in the same pattern to get $$H_n = beginpmatrixH_n-1&H_n-1\H_n-1 & -H_n-1endpmatrix$$
For instance,
$$H_3 = beginpmatrix1&1&1&1&1&1&1&1\1&-1&1&-1&1&-1&1&-1\1&1&-1&-1&1&1&-1&-1\1&-1&-1&1&1&-1&-1&1\1&1&1&1&-1&-1&-1&-1\1&-1&1&-1&-1&1&-1&1\1&1&-1&-1&-1&-1&1&1\1&-1&-1&1&-1&1&1&-1endpmatrix = beginpmatrixH_2&H_2\H_2&-H_2endpmatrix$$
Note how the entries are always 1
or -1
and symmetric! Easy! Then the Hadamard transform is simply multiplying H_n
by a column vector and dividing by 2^n
.
In order to apply the Hadamard transform to 2D images, we have to apply the 1D HT across every column and then every row. In short, for an input matrix X, the output will be a new matrix Y given by two matrix multiplications:
$$Y = frac12^2nH_nXH_n$$
Another way to implement this is by first applying the HT to every column in the image separately. Then apply it to every row, pretending they were column vectors. For example, to compute the 2D HT of $$beginpmatrix2 & 3\ 2 & 5endpmatrix$$
we first do
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 2endpmatrix = beginpmatrix2 \ 0endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix3 \ 5endpmatrix = beginpmatrix4 \ -1endpmatrix quadrightarrowquad beginpmatrix2&4\0 & -1endpmatrix $$
which takes care of the columns, and then we do the rows,
$$frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix2 \ 4endpmatrix = beginpmatrix3 \ -1endpmatrix quadquadquad frac12beginpmatrix1&1\1 & -1endpmatrixbeginpmatrix0 \ -1endpmatrix = beginpmatrix-0.5 \ 0.5endpmatrix quadrightarrowquad beginpmatrix3&-1\-0.5 & 0.5endpmatrix $$
Finally, one can also compute this recursively using the fast Hadamard transform. It's up to you to find out if this is shorter or not.
Challenge
Your challenge is to perform the 2D Hadamard transform on a 2D array of arbitrary size 2^n x 2^n
, where n > 0
. Your code must accept the array as input (2D array or a flattened 1D array). Then perform the 2D HT and output the result.
In the spirit of flexibility, you have a choice for the output. Before multiplying by the prefactor 1/(2^(2n))
, the output array is all integers. You may either multiply by the prefactor and output the answer as floats (no formatting necessary), or you may output a string "1/X*"
followed by the array of integers, where X
is whatever the factor should be. The last test case shows this as an example.
You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program.
Test Cases
Input:
2,3
2,5
Output:
3,-1
-0.5,0.5
Input:
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
Output:
1,0,0,0
0,0,0,0
0,0,0,0
0,0,0,0
Input:
1,0,0,0
1,1,0,0
1,1,1,0
1,1,1,1
Output:
0.6250,0.1250,0.2500,0
-0.1250,0.1250,0,0
-0.2500,0,0.1250,0.1250
0,0,-0.1250,0.1250
Input:
0,0,0,0,0,0,0,1
0,0,0,0,0,0,2,0
0,0,0,0,0,3,0,0
0,0,0,0,4,0,0,0
0,0,0,5,0,0,0,0
0,0,6,0,0,0,0,0
0,7,0,0,0,0,0,0
8,0,0,0,0,0,0,0
Output:
1/64*
36,4,8,0,16,0,0,0
-4,-36,0,-8,0,-16,0,0
-8,0,-36,-4,0,0,-16,0
0,8,4,36,0,0,0,16
-16,0,0,0,-36,-4,-8,0
0,16,0,0,4,36,0,8
0,0,16,0,8,0,36,4
0,0,0,-16,0,-8,-4,-36
Scoring
This is code golf, so the smallest program in bytes wins.
code-golf matrix
code-golf matrix
edited Oct 13 at 21:25
nimi
34.4k3 gold badges28 silver badges92 bronze badges
34.4k3 gold badges28 silver badges92 bronze badges
asked Oct 13 at 19:27
HiddenBabelHiddenBabel
3862 silver badges8 bronze badges
3862 silver badges8 bronze badges
9
$begingroup$
Nice first challenge!
$endgroup$
– Arnauld
Oct 13 at 22:39
2
$begingroup$
Related. Loosely related
$endgroup$
– Luis Mendo
Oct 14 at 10:38
$begingroup$
@Arnauld Thanks! I don't think I'm good enough to golf yet, but it's still fun seeing the other answers.
$endgroup$
– HiddenBabel
Oct 14 at 18:13
$begingroup$
"You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program." Does this include using build-in functions that return the matrix?
$endgroup$
– Kroppeb
Oct 14 at 19:12
3
$begingroup$
an upvote and applause for not requiring floating point
$endgroup$
– ngn
Oct 14 at 19:27
|
show 1 more comment
9
$begingroup$
Nice first challenge!
$endgroup$
– Arnauld
Oct 13 at 22:39
2
$begingroup$
Related. Loosely related
$endgroup$
– Luis Mendo
Oct 14 at 10:38
$begingroup$
@Arnauld Thanks! I don't think I'm good enough to golf yet, but it's still fun seeing the other answers.
$endgroup$
– HiddenBabel
Oct 14 at 18:13
$begingroup$
"You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program." Does this include using build-in functions that return the matrix?
$endgroup$
– Kroppeb
Oct 14 at 19:12
3
$begingroup$
an upvote and applause for not requiring floating point
$endgroup$
– ngn
Oct 14 at 19:27
9
9
$begingroup$
Nice first challenge!
$endgroup$
– Arnauld
Oct 13 at 22:39
$begingroup$
Nice first challenge!
$endgroup$
– Arnauld
Oct 13 at 22:39
2
2
$begingroup$
Related. Loosely related
$endgroup$
– Luis Mendo
Oct 14 at 10:38
$begingroup$
Related. Loosely related
$endgroup$
– Luis Mendo
Oct 14 at 10:38
$begingroup$
@Arnauld Thanks! I don't think I'm good enough to golf yet, but it's still fun seeing the other answers.
$endgroup$
– HiddenBabel
Oct 14 at 18:13
$begingroup$
@Arnauld Thanks! I don't think I'm good enough to golf yet, but it's still fun seeing the other answers.
$endgroup$
– HiddenBabel
Oct 14 at 18:13
$begingroup$
"You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program." Does this include using build-in functions that return the matrix?
$endgroup$
– Kroppeb
Oct 14 at 19:12
$begingroup$
"You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program." Does this include using build-in functions that return the matrix?
$endgroup$
– Kroppeb
Oct 14 at 19:12
3
3
$begingroup$
an upvote and applause for not requiring floating point
$endgroup$
– ngn
Oct 14 at 19:27
$begingroup$
an upvote and applause for not requiring floating point
$endgroup$
– ngn
Oct 14 at 19:27
|
show 1 more comment
9 Answers
9
active
oldest
votes
$begingroup$
Haskell, 111 102 97 92 bytes
import Data.Matrix
t x|n<-nrows x=h n*x*h n
h 1=1
h i|m<-h$div i 2=(/2)<$>m<->m<|>(m<->(-m))
This uses the Data.Matrix
module. The input matrix has to be of type Matrix Float
or Matrix Double
.
TIO doesn't provide Data.Matrix
, so no link.
Edit: -9 bytes thanks to @ceased to turn counterclockwis, -5 bytes thanks to @H.PWiz, -5 bytes thanks to @xnor
$endgroup$
$begingroup$
I am amazed thatidentity 1
is the shortest way to represent $left[1right]$, but it does seem to be.
$endgroup$
– Wheat Wizard
Oct 14 at 3:32
3
$begingroup$
@SriotchilismO'Zaic actually no,pure 1
is shorter. Heck, even1
does the trick.
$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
1
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
1
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
add a comment
|
$begingroup$
JavaScript (ES6), 114 bytes
This version is golfed a bit further by embedding the function $h$ within the callback of the deepest loop.
M=>(g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(h=m=>m?-h(m&~-m):z?v:M[i-1][x])(i++&(z?x:y)),i=s=0)&&s/i)))(g())
Try it online!
JavaScript (ES6), 161 ... 117 116 bytes
M=>(h=m=>m?-h(m&~-m):1,g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(z?v:M[i][x])*h(i++&(z?x:y)),s=i=0)&&s/i)))(g())
Try it online!
How?
Each 0-indexed cell $(x,y)$ of a Hadamard matrix $H_n$ is equal to:
$$H_n(x,y)=cases
1,&textif $(xAnd y)$ is evil\
-1,&textotherwise
$$
or:
$$H_n(x,y)=(-1)^operatornamepopcount(x And y)$$
where $And$ is a bitwise AND and $operatornamepopcount(m)$ is the number of bits set in $m$.
The function $h$ computes $(-1)^operatornamepopcount(m)$ recursively:
h = m => m ? -h(m & ~-m) : 1
(This is similar to my answer to Is this number evil?)
The function $g$ computes either $frac12^nH_nM$ or $frac12^nMH_n$, depending on the flag $z$:
g = z => // z = flag cleared on the 1st pass, set on the 2nd
M = // update M[]:
M.map((r, y) => // for each row r[] at position y in M[]:
r.map((_, x) => // for each cell at position x in r[]:
r.map(v => // for each value v in r[]:
s += // add to s:
(z ? v // v = M(i, y) if z is set,
: M[i][x]) * // or M(x, i) otherwise
h(i++ & (z ? x // multiplied by H(x, i) if z is set,
: y)), // or H(i, y) otherwise
// and increment i afterwards
s = i = 0 // start with s = i = 0
) // end of inner map() over the columns
&& s / i // yield s divided by the width of the matrix
) // end of outer map() over the columns
) // end of map() over the rows
We invoke it twice to compute $frac12^2nH_nMH_n$.
$endgroup$
3
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
add a comment
|
$begingroup$
MATL, 12 bytes
&nKYLw/Gy,Y*
Try it online! Or verify all test cases.
Explanation
Consider input [2,3; 2,5]
as an example.
&n % Input (implicit). Push number of rows and number of columns
% STACK: 2, 2
KYL % Hadamard matrix of specified size
% STACK: 2, [1 1; 1 -1]
w/ % Swap, divide element-wise
% STACK: [0.5 0.5; 0.5 -0.5]
G % Push input again
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5]
y % Duplicate from below
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5], [0.5 0.5; 0.5 -0.5]
, % Do twice
Y* % Matrix multiply
% End (implicit)
% STACK: [3 -1; -0.5 0.5]
% Display (implicit)
$endgroup$
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
add a comment
|
$begingroup$
Octave, 93 79 bytes
@(x)(r=f(f=@(f)@(x)@()[x=f(f)(x-1) x;x -x],11+~x())(log2(r=rows(x)))/r)*x*r
This computes $frac12^nH_nXfrac12^nH_n$
Try it online!
Octave, 47 33 bytes (uses builtin)
@(x)(r=hadamard(r=rows(x))/r)*x*r
Try it online!
$endgroup$
add a comment
|
$begingroup$
APL (Dyalog), 28 bytes
(⍵∘⌹+.×⌹)(,⍨⍪⊢,-)⍣(2⍟≢⍵)⍪1
Try it online!
$endgroup$
add a comment
|
$begingroup$
Brachylog, 23 bytes
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ
- Input: A list of columns
- Output: A list of rows
How:
It uses the Fast Hadamard Transformation. AFAIK Brachylog doesn't have matrix multiplication, nor binary operations so I doubt using the "evil numbers technique" will help shorten the code.
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ #
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ # Fast transform:
ᵐ # for each column:
ḍ # Split in 2
| # (if this fails, return the collum.
# This is the case in the final recursion step)
# Zip,
+ᵐ # and add each pair (adds the 2 subvectors)
-ᵐ # and sub each pair (subs the 2 subvectors)
⟨ ↰₁ᵐ ⟩ # and apply the transformation on both results
c # concat both transformed results.
/₂^m # and half each element
# Transpose
↰₁ᵐ # and apply the transformation again
Try it online!
$endgroup$
add a comment
|
$begingroup$
Jelly, 18 bytes
J’&þ`B§-*ðæ×⁺@÷L$⁺
A monadic Link accepting a list of lists of integers (any numbers really), the square input matrix of side $2^n$, which yields a list of lists of floats.
Try it online!
How?
It feels like one should be able to outer-product the dot-product of the binary representations of the 0-indexed coordinates, however to do so one would need to left-pad the binary representations with zeros which, I believe, costs too many bytes.
J’&þ`B§-*ðæ×⁺@÷L$⁺ - Link: list of lists of integers, M
J - range of length = [1,2,...,rows(M)]
’ - decrement (vectorises) = [0,1,...,rows(M)-1]
` - use left argument as both arguments of:
þ - outer-product with:
& - bit-wise AND [[0&0, 0&1, ...],[1&0, 1&1, ...], ...]
B - convert to binary (vectorises)
§ - sums (vectorises at depth 1) - i.e. bit-sums
- - literal minus one
* - exponentiate (vectorises) - i.e. the appropriately sized Hadamard matrix, H
ð - start a new dyadic chain - i.e. f(H, M)
æ× - matrix multiply - i.e. H×M
@ - with swapped arguments i.e. f(H×M, H):
⁺ - repeat previous link i.e. H×M×H
$ - last two links as a monad:
L - length (number of rows)
÷ - divide (vectorises)
⁺ - repeat the previous link (divide by the length again)
$endgroup$
add a comment
|
$begingroup$
R, 80 78 74 bytes
function(M)for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2
T%*%M%*%T
Try it online!
Calculates $frac12^nH_nXfrac12^nH_n$; constructs $frac12^nH_n$ by Kronecker product %x%
.
Looks like I can still golf, recalling that 1-2*!3:0
is shorter than c(1,1,1,-1)
as I realized in the comments to this answer.
$endgroup$
add a comment
|
$begingroup$
Pari/GP, 51 bytes
a->h=1;while(#h<#a,h=matconcat([h,h;h,-h])/2);h*a*h
Try it online!
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f194229%2fimplement-the-2d-hadamard-transform%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Haskell, 111 102 97 92 bytes
import Data.Matrix
t x|n<-nrows x=h n*x*h n
h 1=1
h i|m<-h$div i 2=(/2)<$>m<->m<|>(m<->(-m))
This uses the Data.Matrix
module. The input matrix has to be of type Matrix Float
or Matrix Double
.
TIO doesn't provide Data.Matrix
, so no link.
Edit: -9 bytes thanks to @ceased to turn counterclockwis, -5 bytes thanks to @H.PWiz, -5 bytes thanks to @xnor
$endgroup$
$begingroup$
I am amazed thatidentity 1
is the shortest way to represent $left[1right]$, but it does seem to be.
$endgroup$
– Wheat Wizard
Oct 14 at 3:32
3
$begingroup$
@SriotchilismO'Zaic actually no,pure 1
is shorter. Heck, even1
does the trick.
$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
1
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
1
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
add a comment
|
$begingroup$
Haskell, 111 102 97 92 bytes
import Data.Matrix
t x|n<-nrows x=h n*x*h n
h 1=1
h i|m<-h$div i 2=(/2)<$>m<->m<|>(m<->(-m))
This uses the Data.Matrix
module. The input matrix has to be of type Matrix Float
or Matrix Double
.
TIO doesn't provide Data.Matrix
, so no link.
Edit: -9 bytes thanks to @ceased to turn counterclockwis, -5 bytes thanks to @H.PWiz, -5 bytes thanks to @xnor
$endgroup$
$begingroup$
I am amazed thatidentity 1
is the shortest way to represent $left[1right]$, but it does seem to be.
$endgroup$
– Wheat Wizard
Oct 14 at 3:32
3
$begingroup$
@SriotchilismO'Zaic actually no,pure 1
is shorter. Heck, even1
does the trick.
$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
1
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
1
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
add a comment
|
$begingroup$
Haskell, 111 102 97 92 bytes
import Data.Matrix
t x|n<-nrows x=h n*x*h n
h 1=1
h i|m<-h$div i 2=(/2)<$>m<->m<|>(m<->(-m))
This uses the Data.Matrix
module. The input matrix has to be of type Matrix Float
or Matrix Double
.
TIO doesn't provide Data.Matrix
, so no link.
Edit: -9 bytes thanks to @ceased to turn counterclockwis, -5 bytes thanks to @H.PWiz, -5 bytes thanks to @xnor
$endgroup$
Haskell, 111 102 97 92 bytes
import Data.Matrix
t x|n<-nrows x=h n*x*h n
h 1=1
h i|m<-h$div i 2=(/2)<$>m<->m<|>(m<->(-m))
This uses the Data.Matrix
module. The input matrix has to be of type Matrix Float
or Matrix Double
.
TIO doesn't provide Data.Matrix
, so no link.
Edit: -9 bytes thanks to @ceased to turn counterclockwis, -5 bytes thanks to @H.PWiz, -5 bytes thanks to @xnor
edited Oct 16 at 16:18
answered Oct 13 at 21:21
niminimi
34.4k3 gold badges28 silver badges92 bronze badges
34.4k3 gold badges28 silver badges92 bronze badges
$begingroup$
I am amazed thatidentity 1
is the shortest way to represent $left[1right]$, but it does seem to be.
$endgroup$
– Wheat Wizard
Oct 14 at 3:32
3
$begingroup$
@SriotchilismO'Zaic actually no,pure 1
is shorter. Heck, even1
does the trick.
$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
1
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
1
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
add a comment
|
$begingroup$
I am amazed thatidentity 1
is the shortest way to represent $left[1right]$, but it does seem to be.
$endgroup$
– Wheat Wizard
Oct 14 at 3:32
3
$begingroup$
@SriotchilismO'Zaic actually no,pure 1
is shorter. Heck, even1
does the trick.
$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
1
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
1
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
$begingroup$
I am amazed that
identity 1
is the shortest way to represent $left[1right]$, but it does seem to be.$endgroup$
– Wheat Wizard
Oct 14 at 3:32
$begingroup$
I am amazed that
identity 1
is the shortest way to represent $left[1right]$, but it does seem to be.$endgroup$
– Wheat Wizard
Oct 14 at 3:32
3
3
$begingroup$
@SriotchilismO'Zaic actually no,
pure 1
is shorter. Heck, even 1
does the trick.$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
$begingroup$
@SriotchilismO'Zaic actually no,
pure 1
is shorter. Heck, even 1
does the trick.$endgroup$
– ceased to turn counterclockwis
Oct 14 at 15:28
1
1
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
$begingroup$
t x|Right m<-inverse$h$nrows x=m*x*m
$endgroup$
– H.PWiz
Oct 14 at 23:45
1
1
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
$begingroup$
Can you divide the matrix by 2 in the recursive step to avoid needing to scale or invert at the end?
$endgroup$
– xnor
Oct 15 at 22:33
add a comment
|
$begingroup$
JavaScript (ES6), 114 bytes
This version is golfed a bit further by embedding the function $h$ within the callback of the deepest loop.
M=>(g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(h=m=>m?-h(m&~-m):z?v:M[i-1][x])(i++&(z?x:y)),i=s=0)&&s/i)))(g())
Try it online!
JavaScript (ES6), 161 ... 117 116 bytes
M=>(h=m=>m?-h(m&~-m):1,g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(z?v:M[i][x])*h(i++&(z?x:y)),s=i=0)&&s/i)))(g())
Try it online!
How?
Each 0-indexed cell $(x,y)$ of a Hadamard matrix $H_n$ is equal to:
$$H_n(x,y)=cases
1,&textif $(xAnd y)$ is evil\
-1,&textotherwise
$$
or:
$$H_n(x,y)=(-1)^operatornamepopcount(x And y)$$
where $And$ is a bitwise AND and $operatornamepopcount(m)$ is the number of bits set in $m$.
The function $h$ computes $(-1)^operatornamepopcount(m)$ recursively:
h = m => m ? -h(m & ~-m) : 1
(This is similar to my answer to Is this number evil?)
The function $g$ computes either $frac12^nH_nM$ or $frac12^nMH_n$, depending on the flag $z$:
g = z => // z = flag cleared on the 1st pass, set on the 2nd
M = // update M[]:
M.map((r, y) => // for each row r[] at position y in M[]:
r.map((_, x) => // for each cell at position x in r[]:
r.map(v => // for each value v in r[]:
s += // add to s:
(z ? v // v = M(i, y) if z is set,
: M[i][x]) * // or M(x, i) otherwise
h(i++ & (z ? x // multiplied by H(x, i) if z is set,
: y)), // or H(i, y) otherwise
// and increment i afterwards
s = i = 0 // start with s = i = 0
) // end of inner map() over the columns
&& s / i // yield s divided by the width of the matrix
) // end of outer map() over the columns
) // end of map() over the rows
We invoke it twice to compute $frac12^2nH_nMH_n$.
$endgroup$
3
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
add a comment
|
$begingroup$
JavaScript (ES6), 114 bytes
This version is golfed a bit further by embedding the function $h$ within the callback of the deepest loop.
M=>(g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(h=m=>m?-h(m&~-m):z?v:M[i-1][x])(i++&(z?x:y)),i=s=0)&&s/i)))(g())
Try it online!
JavaScript (ES6), 161 ... 117 116 bytes
M=>(h=m=>m?-h(m&~-m):1,g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(z?v:M[i][x])*h(i++&(z?x:y)),s=i=0)&&s/i)))(g())
Try it online!
How?
Each 0-indexed cell $(x,y)$ of a Hadamard matrix $H_n$ is equal to:
$$H_n(x,y)=cases
1,&textif $(xAnd y)$ is evil\
-1,&textotherwise
$$
or:
$$H_n(x,y)=(-1)^operatornamepopcount(x And y)$$
where $And$ is a bitwise AND and $operatornamepopcount(m)$ is the number of bits set in $m$.
The function $h$ computes $(-1)^operatornamepopcount(m)$ recursively:
h = m => m ? -h(m & ~-m) : 1
(This is similar to my answer to Is this number evil?)
The function $g$ computes either $frac12^nH_nM$ or $frac12^nMH_n$, depending on the flag $z$:
g = z => // z = flag cleared on the 1st pass, set on the 2nd
M = // update M[]:
M.map((r, y) => // for each row r[] at position y in M[]:
r.map((_, x) => // for each cell at position x in r[]:
r.map(v => // for each value v in r[]:
s += // add to s:
(z ? v // v = M(i, y) if z is set,
: M[i][x]) * // or M(x, i) otherwise
h(i++ & (z ? x // multiplied by H(x, i) if z is set,
: y)), // or H(i, y) otherwise
// and increment i afterwards
s = i = 0 // start with s = i = 0
) // end of inner map() over the columns
&& s / i // yield s divided by the width of the matrix
) // end of outer map() over the columns
) // end of map() over the rows
We invoke it twice to compute $frac12^2nH_nMH_n$.
$endgroup$
3
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
add a comment
|
$begingroup$
JavaScript (ES6), 114 bytes
This version is golfed a bit further by embedding the function $h$ within the callback of the deepest loop.
M=>(g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(h=m=>m?-h(m&~-m):z?v:M[i-1][x])(i++&(z?x:y)),i=s=0)&&s/i)))(g())
Try it online!
JavaScript (ES6), 161 ... 117 116 bytes
M=>(h=m=>m?-h(m&~-m):1,g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(z?v:M[i][x])*h(i++&(z?x:y)),s=i=0)&&s/i)))(g())
Try it online!
How?
Each 0-indexed cell $(x,y)$ of a Hadamard matrix $H_n$ is equal to:
$$H_n(x,y)=cases
1,&textif $(xAnd y)$ is evil\
-1,&textotherwise
$$
or:
$$H_n(x,y)=(-1)^operatornamepopcount(x And y)$$
where $And$ is a bitwise AND and $operatornamepopcount(m)$ is the number of bits set in $m$.
The function $h$ computes $(-1)^operatornamepopcount(m)$ recursively:
h = m => m ? -h(m & ~-m) : 1
(This is similar to my answer to Is this number evil?)
The function $g$ computes either $frac12^nH_nM$ or $frac12^nMH_n$, depending on the flag $z$:
g = z => // z = flag cleared on the 1st pass, set on the 2nd
M = // update M[]:
M.map((r, y) => // for each row r[] at position y in M[]:
r.map((_, x) => // for each cell at position x in r[]:
r.map(v => // for each value v in r[]:
s += // add to s:
(z ? v // v = M(i, y) if z is set,
: M[i][x]) * // or M(x, i) otherwise
h(i++ & (z ? x // multiplied by H(x, i) if z is set,
: y)), // or H(i, y) otherwise
// and increment i afterwards
s = i = 0 // start with s = i = 0
) // end of inner map() over the columns
&& s / i // yield s divided by the width of the matrix
) // end of outer map() over the columns
) // end of map() over the rows
We invoke it twice to compute $frac12^2nH_nMH_n$.
$endgroup$
JavaScript (ES6), 114 bytes
This version is golfed a bit further by embedding the function $h$ within the callback of the deepest loop.
M=>(g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(h=m=>m?-h(m&~-m):z?v:M[i-1][x])(i++&(z?x:y)),i=s=0)&&s/i)))(g())
Try it online!
JavaScript (ES6), 161 ... 117 116 bytes
M=>(h=m=>m?-h(m&~-m):1,g=z=>M=M.map((r,y)=>r.map((_,x)=>r.map(v=>s+=(z?v:M[i][x])*h(i++&(z?x:y)),s=i=0)&&s/i)))(g())
Try it online!
How?
Each 0-indexed cell $(x,y)$ of a Hadamard matrix $H_n$ is equal to:
$$H_n(x,y)=cases
1,&textif $(xAnd y)$ is evil\
-1,&textotherwise
$$
or:
$$H_n(x,y)=(-1)^operatornamepopcount(x And y)$$
where $And$ is a bitwise AND and $operatornamepopcount(m)$ is the number of bits set in $m$.
The function $h$ computes $(-1)^operatornamepopcount(m)$ recursively:
h = m => m ? -h(m & ~-m) : 1
(This is similar to my answer to Is this number evil?)
The function $g$ computes either $frac12^nH_nM$ or $frac12^nMH_n$, depending on the flag $z$:
g = z => // z = flag cleared on the 1st pass, set on the 2nd
M = // update M[]:
M.map((r, y) => // for each row r[] at position y in M[]:
r.map((_, x) => // for each cell at position x in r[]:
r.map(v => // for each value v in r[]:
s += // add to s:
(z ? v // v = M(i, y) if z is set,
: M[i][x]) * // or M(x, i) otherwise
h(i++ & (z ? x // multiplied by H(x, i) if z is set,
: y)), // or H(i, y) otherwise
// and increment i afterwards
s = i = 0 // start with s = i = 0
) // end of inner map() over the columns
&& s / i // yield s divided by the width of the matrix
) // end of outer map() over the columns
) // end of map() over the rows
We invoke it twice to compute $frac12^2nH_nMH_n$.
edited Oct 14 at 10:57
answered Oct 13 at 21:11
ArnauldArnauld
96.2k7 gold badges114 silver badges390 bronze badges
96.2k7 gold badges114 silver badges390 bronze badges
3
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
add a comment
|
3
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
3
3
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
$begingroup$
"If (x&y) is evil" :D
$endgroup$
– Jonathan Allan
Oct 14 at 16:31
add a comment
|
$begingroup$
MATL, 12 bytes
&nKYLw/Gy,Y*
Try it online! Or verify all test cases.
Explanation
Consider input [2,3; 2,5]
as an example.
&n % Input (implicit). Push number of rows and number of columns
% STACK: 2, 2
KYL % Hadamard matrix of specified size
% STACK: 2, [1 1; 1 -1]
w/ % Swap, divide element-wise
% STACK: [0.5 0.5; 0.5 -0.5]
G % Push input again
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5]
y % Duplicate from below
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5], [0.5 0.5; 0.5 -0.5]
, % Do twice
Y* % Matrix multiply
% End (implicit)
% STACK: [3 -1; -0.5 0.5]
% Display (implicit)
$endgroup$
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
add a comment
|
$begingroup$
MATL, 12 bytes
&nKYLw/Gy,Y*
Try it online! Or verify all test cases.
Explanation
Consider input [2,3; 2,5]
as an example.
&n % Input (implicit). Push number of rows and number of columns
% STACK: 2, 2
KYL % Hadamard matrix of specified size
% STACK: 2, [1 1; 1 -1]
w/ % Swap, divide element-wise
% STACK: [0.5 0.5; 0.5 -0.5]
G % Push input again
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5]
y % Duplicate from below
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5], [0.5 0.5; 0.5 -0.5]
, % Do twice
Y* % Matrix multiply
% End (implicit)
% STACK: [3 -1; -0.5 0.5]
% Display (implicit)
$endgroup$
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
add a comment
|
$begingroup$
MATL, 12 bytes
&nKYLw/Gy,Y*
Try it online! Or verify all test cases.
Explanation
Consider input [2,3; 2,5]
as an example.
&n % Input (implicit). Push number of rows and number of columns
% STACK: 2, 2
KYL % Hadamard matrix of specified size
% STACK: 2, [1 1; 1 -1]
w/ % Swap, divide element-wise
% STACK: [0.5 0.5; 0.5 -0.5]
G % Push input again
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5]
y % Duplicate from below
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5], [0.5 0.5; 0.5 -0.5]
, % Do twice
Y* % Matrix multiply
% End (implicit)
% STACK: [3 -1; -0.5 0.5]
% Display (implicit)
$endgroup$
MATL, 12 bytes
&nKYLw/Gy,Y*
Try it online! Or verify all test cases.
Explanation
Consider input [2,3; 2,5]
as an example.
&n % Input (implicit). Push number of rows and number of columns
% STACK: 2, 2
KYL % Hadamard matrix of specified size
% STACK: 2, [1 1; 1 -1]
w/ % Swap, divide element-wise
% STACK: [0.5 0.5; 0.5 -0.5]
G % Push input again
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5]
y % Duplicate from below
% STACK: [0.5 0.5; 0.5 -0.5], [2,3; 2,5], [0.5 0.5; 0.5 -0.5]
, % Do twice
Y* % Matrix multiply
% End (implicit)
% STACK: [3 -1; -0.5 0.5]
% Display (implicit)
edited Oct 14 at 10:34
answered Oct 13 at 22:22
Luis MendoLuis Mendo
78.8k8 gold badges98 silver badges306 bronze badges
78.8k8 gold badges98 silver badges306 bronze badges
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
add a comment
|
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
I don't know MATL. But it might be shorter to get an inverse, instead of dividing by the number of rows. (both get the same result)
$endgroup$
– H.PWiz
Oct 15 at 0:26
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
$begingroup$
@H.PWiz Thanks for the idea! I didn't know that. Unfortunately, computing the inverse matrix (and getting rid of the extra number of rows) would require more characters
$endgroup$
– Luis Mendo
Oct 15 at 8:47
add a comment
|
$begingroup$
Octave, 93 79 bytes
@(x)(r=f(f=@(f)@(x)@()[x=f(f)(x-1) x;x -x],11+~x())(log2(r=rows(x)))/r)*x*r
This computes $frac12^nH_nXfrac12^nH_n$
Try it online!
Octave, 47 33 bytes (uses builtin)
@(x)(r=hadamard(r=rows(x))/r)*x*r
Try it online!
$endgroup$
add a comment
|
$begingroup$
Octave, 93 79 bytes
@(x)(r=f(f=@(f)@(x)@()[x=f(f)(x-1) x;x -x],11+~x())(log2(r=rows(x)))/r)*x*r
This computes $frac12^nH_nXfrac12^nH_n$
Try it online!
Octave, 47 33 bytes (uses builtin)
@(x)(r=hadamard(r=rows(x))/r)*x*r
Try it online!
$endgroup$
add a comment
|
$begingroup$
Octave, 93 79 bytes
@(x)(r=f(f=@(f)@(x)@()[x=f(f)(x-1) x;x -x],11+~x())(log2(r=rows(x)))/r)*x*r
This computes $frac12^nH_nXfrac12^nH_n$
Try it online!
Octave, 47 33 bytes (uses builtin)
@(x)(r=hadamard(r=rows(x))/r)*x*r
Try it online!
$endgroup$
Octave, 93 79 bytes
@(x)(r=f(f=@(f)@(x)@()[x=f(f)(x-1) x;x -x],11+~x())(log2(r=rows(x)))/r)*x*r
This computes $frac12^nH_nXfrac12^nH_n$
Try it online!
Octave, 47 33 bytes (uses builtin)
@(x)(r=hadamard(r=rows(x))/r)*x*r
Try it online!
edited Oct 14 at 7:41
answered Oct 14 at 7:06
ceilingcatceilingcat
4,6171 gold badge13 silver badges24 bronze badges
4,6171 gold badge13 silver badges24 bronze badges
add a comment
|
add a comment
|
$begingroup$
APL (Dyalog), 28 bytes
(⍵∘⌹+.×⌹)(,⍨⍪⊢,-)⍣(2⍟≢⍵)⍪1
Try it online!
$endgroup$
add a comment
|
$begingroup$
APL (Dyalog), 28 bytes
(⍵∘⌹+.×⌹)(,⍨⍪⊢,-)⍣(2⍟≢⍵)⍪1
Try it online!
$endgroup$
add a comment
|
$begingroup$
APL (Dyalog), 28 bytes
(⍵∘⌹+.×⌹)(,⍨⍪⊢,-)⍣(2⍟≢⍵)⍪1
Try it online!
$endgroup$
APL (Dyalog), 28 bytes
(⍵∘⌹+.×⌹)(,⍨⍪⊢,-)⍣(2⍟≢⍵)⍪1
Try it online!
answered Oct 15 at 21:50
H.PWizH.PWiz
10.9k2 gold badges14 silver badges53 bronze badges
10.9k2 gold badges14 silver badges53 bronze badges
add a comment
|
add a comment
|
$begingroup$
Brachylog, 23 bytes
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ
- Input: A list of columns
- Output: A list of rows
How:
It uses the Fast Hadamard Transformation. AFAIK Brachylog doesn't have matrix multiplication, nor binary operations so I doubt using the "evil numbers technique" will help shorten the code.
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ #
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ # Fast transform:
ᵐ # for each column:
ḍ # Split in 2
| # (if this fails, return the collum.
# This is the case in the final recursion step)
# Zip,
+ᵐ # and add each pair (adds the 2 subvectors)
-ᵐ # and sub each pair (subs the 2 subvectors)
⟨ ↰₁ᵐ ⟩ # and apply the transformation on both results
c # concat both transformed results.
/₂^m # and half each element
# Transpose
↰₁ᵐ # and apply the transformation again
Try it online!
$endgroup$
add a comment
|
$begingroup$
Brachylog, 23 bytes
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ
- Input: A list of columns
- Output: A list of rows
How:
It uses the Fast Hadamard Transformation. AFAIK Brachylog doesn't have matrix multiplication, nor binary operations so I doubt using the "evil numbers technique" will help shorten the code.
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ #
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ # Fast transform:
ᵐ # for each column:
ḍ # Split in 2
| # (if this fails, return the collum.
# This is the case in the final recursion step)
# Zip,
+ᵐ # and add each pair (adds the 2 subvectors)
-ᵐ # and sub each pair (subs the 2 subvectors)
⟨ ↰₁ᵐ ⟩ # and apply the transformation on both results
c # concat both transformed results.
/₂^m # and half each element
# Transpose
↰₁ᵐ # and apply the transformation again
Try it online!
$endgroup$
add a comment
|
$begingroup$
Brachylog, 23 bytes
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ
- Input: A list of columns
- Output: A list of rows
How:
It uses the Fast Hadamard Transformation. AFAIK Brachylog doesn't have matrix multiplication, nor binary operations so I doubt using the "evil numbers technique" will help shorten the code.
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ #
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ # Fast transform:
ᵐ # for each column:
ḍ # Split in 2
| # (if this fails, return the collum.
# This is the case in the final recursion step)
# Zip,
+ᵐ # and add each pair (adds the 2 subvectors)
-ᵐ # and sub each pair (subs the 2 subvectors)
⟨ ↰₁ᵐ ⟩ # and apply the transformation on both results
c # concat both transformed results.
/₂^m # and half each element
# Transpose
↰₁ᵐ # and apply the transformation again
Try it online!
$endgroup$
Brachylog, 23 bytes
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ
- Input: A list of columns
- Output: A list of rows
How:
It uses the Fast Hadamard Transformation. AFAIK Brachylog doesn't have matrix multiplication, nor binary operations so I doubt using the "evil numbers technique" will help shorten the code.
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ↰₁ᵐ #
ḍ⟨+ᵐ↰₁ᵐ-ᵐ⟩c/₂ᵐᵐ # Fast transform:
ᵐ # for each column:
ḍ # Split in 2
| # (if this fails, return the collum.
# This is the case in the final recursion step)
# Zip,
+ᵐ # and add each pair (adds the 2 subvectors)
-ᵐ # and sub each pair (subs the 2 subvectors)
⟨ ↰₁ᵐ ⟩ # and apply the transformation on both results
c # concat both transformed results.
/₂^m # and half each element
# Transpose
↰₁ᵐ # and apply the transformation again
Try it online!
answered Oct 14 at 19:10
KroppebKroppeb
1,5084 silver badges10 bronze badges
1,5084 silver badges10 bronze badges
add a comment
|
add a comment
|
$begingroup$
Jelly, 18 bytes
J’&þ`B§-*ðæ×⁺@÷L$⁺
A monadic Link accepting a list of lists of integers (any numbers really), the square input matrix of side $2^n$, which yields a list of lists of floats.
Try it online!
How?
It feels like one should be able to outer-product the dot-product of the binary representations of the 0-indexed coordinates, however to do so one would need to left-pad the binary representations with zeros which, I believe, costs too many bytes.
J’&þ`B§-*ðæ×⁺@÷L$⁺ - Link: list of lists of integers, M
J - range of length = [1,2,...,rows(M)]
’ - decrement (vectorises) = [0,1,...,rows(M)-1]
` - use left argument as both arguments of:
þ - outer-product with:
& - bit-wise AND [[0&0, 0&1, ...],[1&0, 1&1, ...], ...]
B - convert to binary (vectorises)
§ - sums (vectorises at depth 1) - i.e. bit-sums
- - literal minus one
* - exponentiate (vectorises) - i.e. the appropriately sized Hadamard matrix, H
ð - start a new dyadic chain - i.e. f(H, M)
æ× - matrix multiply - i.e. H×M
@ - with swapped arguments i.e. f(H×M, H):
⁺ - repeat previous link i.e. H×M×H
$ - last two links as a monad:
L - length (number of rows)
÷ - divide (vectorises)
⁺ - repeat the previous link (divide by the length again)
$endgroup$
add a comment
|
$begingroup$
Jelly, 18 bytes
J’&þ`B§-*ðæ×⁺@÷L$⁺
A monadic Link accepting a list of lists of integers (any numbers really), the square input matrix of side $2^n$, which yields a list of lists of floats.
Try it online!
How?
It feels like one should be able to outer-product the dot-product of the binary representations of the 0-indexed coordinates, however to do so one would need to left-pad the binary representations with zeros which, I believe, costs too many bytes.
J’&þ`B§-*ðæ×⁺@÷L$⁺ - Link: list of lists of integers, M
J - range of length = [1,2,...,rows(M)]
’ - decrement (vectorises) = [0,1,...,rows(M)-1]
` - use left argument as both arguments of:
þ - outer-product with:
& - bit-wise AND [[0&0, 0&1, ...],[1&0, 1&1, ...], ...]
B - convert to binary (vectorises)
§ - sums (vectorises at depth 1) - i.e. bit-sums
- - literal minus one
* - exponentiate (vectorises) - i.e. the appropriately sized Hadamard matrix, H
ð - start a new dyadic chain - i.e. f(H, M)
æ× - matrix multiply - i.e. H×M
@ - with swapped arguments i.e. f(H×M, H):
⁺ - repeat previous link i.e. H×M×H
$ - last two links as a monad:
L - length (number of rows)
÷ - divide (vectorises)
⁺ - repeat the previous link (divide by the length again)
$endgroup$
add a comment
|
$begingroup$
Jelly, 18 bytes
J’&þ`B§-*ðæ×⁺@÷L$⁺
A monadic Link accepting a list of lists of integers (any numbers really), the square input matrix of side $2^n$, which yields a list of lists of floats.
Try it online!
How?
It feels like one should be able to outer-product the dot-product of the binary representations of the 0-indexed coordinates, however to do so one would need to left-pad the binary representations with zeros which, I believe, costs too many bytes.
J’&þ`B§-*ðæ×⁺@÷L$⁺ - Link: list of lists of integers, M
J - range of length = [1,2,...,rows(M)]
’ - decrement (vectorises) = [0,1,...,rows(M)-1]
` - use left argument as both arguments of:
þ - outer-product with:
& - bit-wise AND [[0&0, 0&1, ...],[1&0, 1&1, ...], ...]
B - convert to binary (vectorises)
§ - sums (vectorises at depth 1) - i.e. bit-sums
- - literal minus one
* - exponentiate (vectorises) - i.e. the appropriately sized Hadamard matrix, H
ð - start a new dyadic chain - i.e. f(H, M)
æ× - matrix multiply - i.e. H×M
@ - with swapped arguments i.e. f(H×M, H):
⁺ - repeat previous link i.e. H×M×H
$ - last two links as a monad:
L - length (number of rows)
÷ - divide (vectorises)
⁺ - repeat the previous link (divide by the length again)
$endgroup$
Jelly, 18 bytes
J’&þ`B§-*ðæ×⁺@÷L$⁺
A monadic Link accepting a list of lists of integers (any numbers really), the square input matrix of side $2^n$, which yields a list of lists of floats.
Try it online!
How?
It feels like one should be able to outer-product the dot-product of the binary representations of the 0-indexed coordinates, however to do so one would need to left-pad the binary representations with zeros which, I believe, costs too many bytes.
J’&þ`B§-*ðæ×⁺@÷L$⁺ - Link: list of lists of integers, M
J - range of length = [1,2,...,rows(M)]
’ - decrement (vectorises) = [0,1,...,rows(M)-1]
` - use left argument as both arguments of:
þ - outer-product with:
& - bit-wise AND [[0&0, 0&1, ...],[1&0, 1&1, ...], ...]
B - convert to binary (vectorises)
§ - sums (vectorises at depth 1) - i.e. bit-sums
- - literal minus one
* - exponentiate (vectorises) - i.e. the appropriately sized Hadamard matrix, H
ð - start a new dyadic chain - i.e. f(H, M)
æ× - matrix multiply - i.e. H×M
@ - with swapped arguments i.e. f(H×M, H):
⁺ - repeat previous link i.e. H×M×H
$ - last two links as a monad:
L - length (number of rows)
÷ - divide (vectorises)
⁺ - repeat the previous link (divide by the length again)
edited Oct 14 at 17:42
answered Oct 14 at 17:36
Jonathan AllanJonathan Allan
62.3k5 gold badges45 silver badges191 bronze badges
62.3k5 gold badges45 silver badges191 bronze badges
add a comment
|
add a comment
|
$begingroup$
R, 80 78 74 bytes
function(M)for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2
T%*%M%*%T
Try it online!
Calculates $frac12^nH_nXfrac12^nH_n$; constructs $frac12^nH_n$ by Kronecker product %x%
.
Looks like I can still golf, recalling that 1-2*!3:0
is shorter than c(1,1,1,-1)
as I realized in the comments to this answer.
$endgroup$
add a comment
|
$begingroup$
R, 80 78 74 bytes
function(M)for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2
T%*%M%*%T
Try it online!
Calculates $frac12^nH_nXfrac12^nH_n$; constructs $frac12^nH_n$ by Kronecker product %x%
.
Looks like I can still golf, recalling that 1-2*!3:0
is shorter than c(1,1,1,-1)
as I realized in the comments to this answer.
$endgroup$
add a comment
|
$begingroup$
R, 80 78 74 bytes
function(M)for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2
T%*%M%*%T
Try it online!
Calculates $frac12^nH_nXfrac12^nH_n$; constructs $frac12^nH_n$ by Kronecker product %x%
.
Looks like I can still golf, recalling that 1-2*!3:0
is shorter than c(1,1,1,-1)
as I realized in the comments to this answer.
$endgroup$
R, 80 78 74 bytes
function(M)for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2
T%*%M%*%T
Try it online!
Calculates $frac12^nH_nXfrac12^nH_n$; constructs $frac12^nH_n$ by Kronecker product %x%
.
Looks like I can still golf, recalling that 1-2*!3:0
is shorter than c(1,1,1,-1)
as I realized in the comments to this answer.
edited Oct 15 at 16:27
answered Oct 14 at 16:27
GiuseppeGiuseppe
19.4k3 gold badges16 silver badges71 bronze badges
19.4k3 gold badges16 silver badges71 bronze badges
add a comment
|
add a comment
|
$begingroup$
Pari/GP, 51 bytes
a->h=1;while(#h<#a,h=matconcat([h,h;h,-h])/2);h*a*h
Try it online!
$endgroup$
add a comment
|
$begingroup$
Pari/GP, 51 bytes
a->h=1;while(#h<#a,h=matconcat([h,h;h,-h])/2);h*a*h
Try it online!
$endgroup$
add a comment
|
$begingroup$
Pari/GP, 51 bytes
a->h=1;while(#h<#a,h=matconcat([h,h;h,-h])/2);h*a*h
Try it online!
$endgroup$
Pari/GP, 51 bytes
a->h=1;while(#h<#a,h=matconcat([h,h;h,-h])/2);h*a*h
Try it online!
answered Oct 15 at 23:56
alephalphaalephalpha
22.9k3 gold badges33 silver badges98 bronze badges
22.9k3 gold badges33 silver badges98 bronze badges
add a comment
|
add a comment
|
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f194229%2fimplement-the-2d-hadamard-transform%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
9
$begingroup$
Nice first challenge!
$endgroup$
– Arnauld
Oct 13 at 22:39
2
$begingroup$
Related. Loosely related
$endgroup$
– Luis Mendo
Oct 14 at 10:38
$begingroup$
@Arnauld Thanks! I don't think I'm good enough to golf yet, but it's still fun seeing the other answers.
$endgroup$
– HiddenBabel
Oct 14 at 18:13
$begingroup$
"You may not import the Hadamard matrices from some convenient file that already contains them; you must build them inside the program." Does this include using build-in functions that return the matrix?
$endgroup$
– Kroppeb
Oct 14 at 19:12
3
$begingroup$
an upvote and applause for not requiring floating point
$endgroup$
– ngn
Oct 14 at 19:27