Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Generate a Krawtchouk matrix
| ResourceFunction["KrawtchoukMatrix"][n] returns an n×n Krawtchouk matrix. | 
A 4×4 Krawtchouk matrix:
| In[1]:= | ![ResourceFunction["KrawtchoukMatrix"][4]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/3ed0b50f2530c09e.png) | 
| Out[1]= |  | 
Visualize the entries of a Krawtchouk matrix:
| In[2]:= | ![MatrixPlot[ResourceFunction["KrawtchoukMatrix"][20]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/5823ac131256ca79.png) | 
| Out[2]= |  | 
A normalized Krawtchouk matrix:
| In[3]:= | ![ResourceFunction["KrawtchoukMatrix"][6, "Normalized" -> True] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/147e7e6cf4c2a8ce.png) | 
| Out[3]= |  | 
The normalized matrix is both symmetric and orthogonal:
| In[4]:= | ![{SymmetricMatrixQ[%], OrthogonalMatrixQ[%]}](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/39d391a86e2979f8.png) | 
| Out[4]= |  | 
By default, an exact matrix is computed:
| In[5]:= | ![ResourceFunction["KrawtchoukMatrix"][5] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/75a702cb17c010e6.png) | 
| Out[5]= |  | 
Use machine precision:
| In[6]:= | ![ResourceFunction["KrawtchoukMatrix"][5, WorkingPrecision -> MachinePrecision] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/21a5014c3febb6ea.png) | 
| Out[6]= |  | 
Use arbitrary precision:
| In[7]:= | ![ResourceFunction["KrawtchoukMatrix"][5, WorkingPrecision -> 20] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/77eb15db6659cafc.png) | 
| Out[7]= |  | 
Define a symmetrized version of the Krawtchouk matrix:
| In[8]:= | ![symmetrizedKrawtchouk[n_] := ResourceFunction["KrawtchoukMatrix"][n] . DiagonalMatrix[Binomial[n - 1, Range[0, n - 1]]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/07e19faf30d618b3.png) | 
The symmetrized Krawtchouk matrix is symmetric, as its name implies:
| In[9]:= | ![symmetrizedKrawtchouk[6] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/4ec41cc616f2308b.png) | 
| Out[9]= |  | 
| In[10]:= | ![SymmetricMatrixQ[%]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/56e54fde04502604.png) | 
| Out[10]= |  | 
The symmetrized Krawtchouk matrix gives the coefficients of the bivariate polynomial (1+x+y-x y)n-1:
| In[11]:= | ![With[{n = 7}, (1 + x + y - x y)^(n - 1) == symmetrizedKrawtchouk[n] . (x^Range[0, n - 1]) . (y^
     Range[0, n - 1])] // Simplify](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/5c11163d81543e84.png) | 
| Out[11]= |  | 
Demonstrate a recursive method for generating the symmetrized Krawtchouk matrix:
| In[12]:= | ![With[{n = 7}, BlockMap[Total[#, 2] &, ArrayPad[
    KroneckerProduct[symmetrizedKrawtchouk[n], {{1, 1}, {1, -1}}], 1], {2, 2}] == symmetrizedKrawtchouk[n + 1]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/15b7d0cf66ffa03f.png) | 
| Out[12]= |  | 
The Kac matrix is a tridiagonal matrix whose subdiagonal and superdiagonal entries are consecutive integers:
| In[13]:= | ![kac[n_] := With[{r = Range[n - 1]}, SparseArray[{Band[{2, 1}] -> r, Band[{1, 2}] -> Reverse[r]}, {n, n}]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/733f0c4d42a8d5c8.png) | 
| In[14]:= | ![kac[6] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/71022ea452fa27f9.png) | 
| Out[14]= |  | 
The Kac matrix can be diagonalized by the symmetrized Krawtchouk matrix:
| In[15]:= | ![symmetrizedKrawtchouk[6] . kac[6] . Inverse[symmetrizedKrawtchouk[6]] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/364da763a5b16684.png) | 
| Out[15]= |  | 
Columns of the Krawtchouk matrix can be determined from the coefficients of the polynomial (1+x)n-j-1(1-x)j:
| In[16]:= | ![With[{n = 5}, Transpose[
  Table[CoefficientList[(1 + x)^(n - j - 1) (1 - x)^j, x], {j, 0, n - 1}]]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/52a133d0732c1c2b.png) | 
| Out[16]= |  | 
| In[17]:= | ![% == ResourceFunction["KrawtchoukMatrix"][5]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/43ae6daba35667c1.png) | 
| Out[17]= |  | 
The entries of the Krawtchouk matrix can be expressed in terms of the Krawtchouk polynomial, which can be expressed in terms of Hypergeometric2F1:
| In[18]:= | ![With[{n = 5}, Table[Binomial[n - 1, j - 1] Hypergeometric2F1[1 - j, 1 - k, 1 - n, 2], {j, n}, {k, n}]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/31a8e4717fb89846.png) | 
| Out[18]= |  | 
| In[19]:= | ![% == ResourceFunction["KrawtchoukMatrix"][5]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/1d053bf18957e0bd.png) | 
| Out[19]= |  | 
Generate the Krawtchouk matrix through a recursive definition:
| In[20]:= | ![With[{n = 5}, Array[Which[#1 == 0, 1, #2 == 0, Binomial[n - 1, #1], True, #0[#1, #2 - 1] - #0[#1 - 1, #2] - #0[#1 - 1, #2 - 1]] &, {n,
    n}, {0, 0}]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/7da32514b469c481.png) | 
| Out[20]= |  | 
| In[21]:= | ![% == ResourceFunction["KrawtchoukMatrix"][5]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/5bfcc75149770d5b.png) | 
| Out[21]= |  | 
The product of a Krawtchouk matrix with itself is a constant multiple of the identity matrix of the same size:
| In[22]:= | ![Table[With[{km = ResourceFunction["KrawtchoukMatrix"][n]}, km . km == 2^(n - 1) IdentityMatrix[n]], {n, 2, 9}]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/64fa8a6bafa060df.png) | 
| Out[22]= |  | 
Generate a Krawtchouk matrix from a Hadamard matrix, using condensation of the rows and columns with the same binary weights (number of 1's in the binary representation):
| In[23]:= | ![n = 7;
hm = 2^((n - 1)/2)
    HadamardMatrix[2^(n - 1), Method -> "BitComplement"];
idx = Values[
    KeySort[GroupBy[
      Table[{k, DigitCount[k, 2, 1]}, {k, 0, 2^(n - 1) - 1}], Last -> First]]] + 1;
km = Table[Total[hm[[ji, ki]], 2], {ji, idx}, {ki, idx}] . DiagonalMatrix[1/Binomial[n - 1, Range[0, n - 1]]]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/5d192730feecb37b.png) | 
| Out[24]= |  | 
Verify that the result is a Krawtchouk matrix:
| In[25]:= | ![km == ResourceFunction["KrawtchoukMatrix"][n]](https://www.wolframcloud.com/obj/resourcesystem/images/5c1/5c177871-288f-42a2-8aab-0ff17dbd8ac9/4f413bfec9a6b7e7.png) | 
| Out[25]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License