GrayCoding
Documentation for GrayCoding.
Introduction
Welcome to the documentation for GrayCoding!
What is GrayCocoding.jl?
GrayCoding is a formal Linear Algebraic framework for $q$-ry Gray Code. Encoding and Decooding of Gray codes can be treated as a special case of algebraic block coding.
- Encoding: $\textbf{g}=G \textbf{b}$
- Decoding: $\textbf{b}=B \textbf{g}$
This is still under active devlopment.
Resources for getting started
There are few ways to get started with GrayCoding:
Installation
Open a Julia session and enter
using Pkg; Pkg.add("GrayCoding")
this will download the package and all the necessary dependencies for you. Next you can import the package with
using GrayCoding
and you are ready to go.
Quickstart
using GrayCoding
Citation
If you use this package in your work, please cite it as
@software{nrethnakar2022GrayAlgebra,
author = {
Nivedita Rethnakar
},
title = {GrayCoding.jl: Algebra of Gray Coding and Applications},
month = {1},
year = {2022},
doi = {10.5281/zenodo.5989996},
url = {https://github.com/nivupai/GrayCoding.jl}
}
- Read TBD.
GrayCoding.G
GrayCoding.G2D
GrayCoding.Gcascaded
GrayCoding.GivensG
GrayCoding.Gn
GrayCoding.GrayMatrix
GrayCoding.bin2dec
GrayCoding.dec2bin
GrayCoding.dket
GrayCoding.dnamatrix
GrayCoding.gen_gray
GrayCoding.gray
GrayCoding.gray_ordering
GrayCoding.gray_recursion
GrayCoding.level2unitary
GrayCoding.matrixplot
GrayCoding.oneQubitU
GrayCoding.pam_encode
GrayCoding.quantumΓ
GrayCoding.reflect_code
GrayCoding.sequenceΓ
GrayCoding.toffoli_matrix
GrayCoding.πmatrix
GrayCoding.G
— MethodFind the Givens embedding ${}^{i}G_{j,k}(A)$
GrayCoding.G2D
— MethodConverts a gray mapped decimal number to simple binary mapped decimal number. This routine follows the inverse mapping of the function gray_ordering()
Parameters
- $n$ The number of digits in the equivalent binary mapping
- $g$ The gray mapped decimal number
- $d$ The simple binary mapped decimal number
julia> G2D.(0:7,3)
[0,1,3,2,7,6,4,5]
GrayCoding.Gcascaded
— MethodFor a given unitary matrix $U$, it finds the cascaded rotation matrix $C$ such that $C\times U =I$, except for the diagonal element of the $n$th element which is $\det(U)$. The matrix $C$ is obtained by the cascade of several (i.e., $2^{n-1}(2^{n}-1)$) two level Givens rotation matrices, namely,
\[C = \prod_{i=1}^{n-1} \prod_{j=0}^{n-1-i}{ {}^{i}G_{n-j,n-j-1}}\]
\[\prod_{i=1}^{n-1} \prod_{j=0}^{n-1-i}{ {}^{i}G_{n-j,n-j-1} U(n)} = \begin{pmatrix} 1 & \ldots & 0 \\ \vdots & \ddots & \vdots \\ 0 & \ldots & \det(U)\end{pmatrix}.\]
Parameters
- U – Input. Unitary matrix of size $n$
- Ic – Identity matix with the exception that the last diagonal entry ($n$th diagonal element) which is $\det(U)$.
- C – The cascaded rotation matrix ${}^{n-1}G_{n} \times {}^{n-2}G_{n-1} \times {}^{n-2}G_{n} \times \ldots \times {}^{2}G_{3} \times \ldots \times {}^{2}G_{n-1} \times {}^{2}G_{n} \times {}^{1}G_{2} \times \ldots \times {}^{1}G_{n-1} \times {}^{1}G_{n}$
Examples
julia> using LinearAlgebra
julia> N=4;A=rand(N,N)+im*rand(N,N);S=svd(A);U=S.U
4×4 Matrix{ComplexF64}:
-0.4-0.06im 0.23-0.73im -0.03-0.14im 0.39-0.27im
-0.61-0.32im 0.07+0.06im 0.32+0.02im -0.64-0.08im
-0.38-0.33im -0.48+0.38im -0.07-0.4im 0.46+0.07im
-0.2-0.26im -0.09-0.14im -0.7+0.47im -0.09+0.38im
julia> Gc,Ic=Gcascaded(U)
4×4 Matrix{ComplexF64}:
1.0+0.0im 0.0+0.0im -0.0+0.0im 0.0-0.0im
0.0+0.0im 1.0-0.0im 0.0+0.0im -0.0-0.0im
0.0-0.0im -0.0+0.0im 1.0+0.0im 0.0+0.0im
0.0-0.0im -0.0+0.0im 0.0-0.0im 0.4-0.9im
julia> det(A)
0.4166084175706718 - 0.9090860390575042im
GrayCoding.GivensG
— MethodFind the matrix which has a Givens matrix embedding ${}^{i}G_{j,k}(A)$.
For a given matrix $A$, a generic rotation matrix ${}^{i}G_{j,k}(A)$ is generated. The matrix ${}^{i}G_{j,k}(A)$ is such that it helps to selectively nullifies an element of matrix $V={}^{i}G_{j,k}(A) A$. That is, $V_{j,i}=0$ where $V={}^{i}G_{j,k}(A) A$. The fllowing Givens rotation matrix ${}^{i}\Gamma_{j,k}$
\[\begin{aligned} {}^{i}\Gamma_{j,k} &= \begin{pmatrix} {}^{i}g_{k,k} & {}^{i}g_{k,j} \\ {}^{i}g_{j,k} & {}^{i}g_{j,j}\end{pmatrix} \\ &=\frac{1}{\sqrt{\lvert a_{j,i} \rvert^{2}+ \lvert a_{k,i} \rvert^{2}}}\begin{pmatrix} a_{k,i}^{*} & a_{j,i}^{*} \\ -a_{j,i} & {}^{i}a_{k,i}\end{pmatrix}. \end{aligned}\]
is embedded in an identity matrix $I(n)$ to produce,
\[{}^{i}G_{j,k}(A) = \begin{pmatrix} 1 & 0 & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & 0 \\ 0 & 1 & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\ 0 & 0 & \ddots & {\color{red} {}^{i}g_{k,k}} & \ddots & {\color{red} {}^{i}g_{k,j}} & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots \ddots & & \ddots & \ddots & \vdots \\ 0 & 0 & \ddots & {\color{red} {}^{i}g_{j,k}} & \ddots & {\color{red} {}^{i}g_{j,j}} & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\ 0 & 0 & \ldots & \ldots & \ldots & \ldots & \ldots & 1 & 0 \\ 0 & 0 & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & 1 \end{pmatrix}.\]
Essentially, ${}^{i}G_{j,k}(A)$ is a modified identity matrix such that four non trivial elements are taken from the givens rotation matrix ${}^{i}\Gamma_{j,k}$.
GrayCoding.Gn
— MethodDecomposition of aribtrary unitary matrix $U(n) \in S(2^n)$, as a cascade of two level Givens matrices.
Using the following property,
\[\prod_{j=0}^{n-1}{ {}^{1}G_{n-j,n-j-1} U(n)} = \begin{pmatrix} 1 & 0 \\ 0 & U(n-1)\end{pmatrix}\]
\[\prod_{i=1}^{n-1} \prod_{j=0}^{n-1-i}{ {}^{i}G_{n-j,n-j-1} U(n)} = \begin{pmatrix} 1 & \ldots & 0 \\ \vdots & \ddots & \vdots \\ 0 & \ldots & \det(U)\end{pmatrix}.\]
There are $2^{n-1}(2^{n}-1)$ number of unitary two-level matrices (each of them formed by embedding a Givens rotaton matrix into indentity matrix). Note that $\sum_{i=1}^{2^{n}}{N-i}=2^{n-1}(2^{n}-1)$.
Parameters
- U – Input. Unitary matrix of size $2^n$
- V – Output unitary matrix in two level form
[1 0;0 U']
form whereU'
is a unitary matrix of size $2^{n-1}$. - Gm – The $(n-2)(n-1)$ sequence of Given matrices (in augmented form) $[{}^{1}G_{n}\lvert{}^{1}G_{n-1}\lvert\ldots\lvert{}^{1}G_{2}\lvert {}^{2}G_{n}\lvert{}^{2}G_{n-1}\lvert\ldots\lvert{}^{2}G_{3}\lvert\ldots\lvert{}^{n-2}G_{n}\lvert{}^{n-2}G_{n-1}\lvert{}^{n-1}G_{n}]$
- Gs – The $(n-2)(n-1)$ sequence of Given matrices (in augmented form) left to right $[ {}^{n-1}G_{n} \lvert {}^{n-2}G_{n-1} \lvert {}^{n-2}G_{n} \lvert \ldots \lvert {}^{2}G_{3} \lvert \ldots \lvert {}^{2}G_{n-1} \lvert {}^{2}G_{n} \lvert{}^{1}G_{2} \lvert \ldots \lvert {}^{1}G_{n-1} \lvert {}^{1}G_{n} ]$
Examples
julia> using LinearAlgebra
julia> N=4;A=rand(N,N)+im*rand(N,N);S=svd(A);U=S.U
julia> Gn(U)
GrayCoding.GrayMatrix
— FunctionGenerate Encoding and Decoding matrices for Gray Codes of alphabet.
julia> G,B,g,b=GrayMatrix(4, 2);
julia> G
4×4 Matrix{Int64}:
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1
julia> B
4×4 Matrix{Int64}:
1 0 0 0
1 1 0 0
0 1 1 0
0 0 1 1
julia> g
4×16 Matrix{Int64}:
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
julia> b
4×16 Matrix{Int64}:
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
GrayCoding.bin2dec
— MethodBinary to decimal number conversion. Input can be
- binary strings,
- binary digits or
- a vector of binary string or digits
julia> bin2dec([011,"0111",0111])
GrayCoding.dec2bin
— MethodDecimal to binary conversion
GrayCoding.dket
— MethodGiven an integer $x ∈ \mathbb{N}$ (i.e., natural number in decimal form), it performs a tensor product of the individual bits. Effectively, each of the bits in the binary representation expressed in a qubit vector gets tensor multipled. The qubit to vector mapping are as follows: $0 \equiv | 0 \rangle = \begin{pmatrix} 1 \\ 0 \end{pmatrix}$ and $1 \equiv | 1 \rangle = \begin{pmatrix} 0 \\ 1 \end{pmatrix}$
Example
$x=13$ in binary representation is given by $1101$. Thus $|13 \rangle \equiv |1101\rangle=|1\rangle \otimes |1\rangle \otimes |0\rangle \otimes |1\rangle$.
\[\begin{aligned} |13 \rangle &\equiv |1101\rangle \\ &=|1\rangle \otimes |1\rangle \otimes |0\rangle \otimes |1\rangle \\ &= \begin{pmatrix} 0 \\ 1 \end{pmatrix} \otimes \begin{pmatrix} 0 \\ 1 \end{pmatrix} \otimes \begin{pmatrix} 1 \\ 0 \end{pmatrix} \otimes \begin{pmatrix} 0 \\ 1 \end{pmatrix} \\ &= \begin{pmatrix} 0 \\ 0 \\ 0 \\ 1 \end{pmatrix} \otimes \begin{pmatrix} 0 \\ 1 \\ 0 \\ 0 \end{pmatrix} \\ &= \begin{pmatrix} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \end{pmatrix}^{\top} \end{aligned}\]
Turns out that this effectively result in a lone non zero entry at the index $13$ when counted from $0$ to $2^n$.
```julia-repl julia> dket(13) [0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
julia> dket(5,n=2) [0,0,0,0,0,1,0,0]
GrayCoding.dnamatrix
— MethodPlot the DNA codon matrix
julia> dnamatrix()
GrayCoding.gen_gray
— MethodGenerate Gray vectors
GrayCoding.gray
— MethodRecursive construction of binary Gray code digits.
Gray code $g[n]$ can be recursively constructed as follows. Start with $g[1] = (0,1) = (g_{1},g_{2})$
\[g[n+1] = 0g_{1},...,0g_{N−1},0g_{N},1g_{N},1g_{N−1},...,1g_{1}.\]
Examples
julia> gray(3)
3×8 Matrix{Int64}:
0 0 0 0 1 1 1 1
0 0 1 1 0 1 1 0
0 1 1 0 1 1 0 0
GrayCoding.gray_ordering
— MethodFor $n$ bits, with the corresponding decimal sequence $x=0,1,2,\ldots,2^{n-1}$, find the gray ordering sequence using the bitwise XOR
logic. Namely. $gπ= x ⊻ \lfloor x/2 \rfloor = x \oplus \lfloor x/2 \rfloor$ where $⊻$ is the bitwise XOR
operation.
julia> gray_ordering(3)
[0,1,3,2,6,7,5,4]
GrayCoding.gray_recursion
— MethodRecursive function to illustrate the reflection+shift property of Gray mapping.
Arguents
- n - The iteration number
n ≥ 0
- C - The decimal sequence of the gray mapped bits
- R - The reflected sequence (without shifting)
julia> C,R = gray_recursion(4)
GrayCoding.level2unitary
— MethodGiven unitary matrux $U(n) \in S(2^n)$, it uses a a repeated Givens rotations to get a to level matrix as follows.
\[\prod_{j=1}^{n-2}{ {}^{1}G_{n-j,n-j-1} U(n)} = \begin{pmatrix} 1 & 0 \\ 0 & U(n-1)\end{pmatrix}\]
Parameters
- U – Input. Unitary matrix of size $2^n$
- V – Output unitary matrix in two level form
[1 0;0 U']
form whereU'
is a unitary matrix of size $2^{n-1}$. - GG – The $n-2$ sequence of Given matrices (in augmented form) $[{}^{1}G_{n}\lvert{}^{1}G_{n-1}\lvert\ldots\lvert{}^{1}G_{2}]$
Examples
julia> level2unitary(U)
GrayCoding.matrixplot
— MethodPlots a matrix into a 2D with labels. Optional arguments including colors
julia> using Random;
julia> A= rand(0:9,10,10);
julia> matrixplot(A)
GrayCoding.oneQubitU
— MethodGenerates the unitary matrix corresponding to a one qubit operation at a specified bit, acting on an $n-$ register.
A one-qubit gate $U_2 \in SU(2)$ acting on $k$th qubit in an $n-$ qubit register correspond to a unitary matrix $U \in SU(2^n) = \mathbb{I}_{2}^{\otimes k} \otimes U_{2} \otimes \mathbb{I}_{2}^{\otimes n-k}$.
The unitary matrix
\[U_2 = \begin{pmatrix} \acute{a} & \acute{b} \\ -\acute{b}^{*} & \acute{a}^{*}\end{pmatrix} = \frac{1}{\lvert a \rvert^{2}+\lvert b \rvert^{2}}\begin{pmatrix} a & b \\ -b^{*} & a^{*}\end{pmatrix}\]
. Here $a,b \in \mathbb{C}, \lvert \acute{a} \rvert^{2}+\lvert \acute{b} \rvert^{2}=1$ is the embedded single qubit operation.
Arguments
- a,b - Elements of $U_2 = \begin{pmatrix} \acute{a} & \acute{b} \\ -\acute{b}^{*} & \acute{a}^{*}\end{pmatrix} = \frac{1}{\lvert a \rvert^{2}+\lvert b \rvert^{2}}\begin{pmatrix} a & b \\ -b^{*} & a^{*}\end{pmatrix}$. Here $a,b \in \mathbb{C}, \lvert \acute{a} \rvert^{2}+\lvert \acute{b} \rvert^{2}=1$.
- n – Number of registers $n \in \mathbb{N}$
- k – The position of the qubit $1 \le k \le n , k \in \mathbb{N}$
- U – Output $U_2 \in SU(2^n)$
Dependency
Uses the Kronecker.jl package
julia> oneQubitU(7+3im,1-2im,3,1)
8×8 Matrix{ComplexF64}:
0.9+0.4im 0.0+0.0im .0+0.0im .0+0.0im … .0-0.0im .0-0.0im 0.0-.0im
0.0+0.0im 0.9+0.4im .0+0.0im .0+0.0im .1-0.3im .0-0.0im 0.0-.0im
0.0+0.0im 0.0+0.0im .9+0.4im .0+0.0im .0-0.0im .1-0.3im 0.0-.0im
0.0+0.0im 0.0+0.0im .0+0.0im .9+0.4im .0-0.0im .0-0.0im 0.1-.3im
-0.1-0.3im -0.0-0.0im -.0-0.0im -.0-0.0im .0-0.0im .0-0.0im 0.0-.0im
-0.0-0.0im -0.1-0.3im -.0-0.0im -.0-0.0im … .9-0.4im .0-0.0im 0.0-.0im
-0.0-0.0im -0.0-0.0im -.1-0.3im -.0-0.0im .0-0.0im .9-0.4im 0.0-.0im
-0.0-0.0im -0.0-0.0im -.0-0.0im -.1-0.3im .0-0.0im .0-0.0im 0.9-.4im
julia> oneQubitU(3,4,3,2)
8×8 Matrix{Float64}:
0.6 0.0 0.8 0.0 0.0 0.0 0.0 0.0
0.0 0.6 0.0 0.8 0.0 0.0 0.0 0.0
-0.8 -0.0 0.6 0.0 -0.0 -0.0 0.0 0.0
-0.0 -0.8 0.0 0.6 -0.0 -0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.6 0.0 0.8 0.0
0.0 0.0 0.0 0.0 0.0 0.6 0.0 0.8
-0.0 -0.0 0.0 0.0 -0.8 -0.0 0.6 0.0
-0.0 -0.0 0.0 0.0 -0.0 -0.8 0.0 0.6
GrayCoding.pam_encode
— MethodPulse amplitude modulation (PAM) mapping. This is a type of digital modulation mapping used in Communication systems.
GrayCoding.quantumΓ
— MethodGiven a matrix $A$ (usually a unitary matrix) and indices $(i,j,k)$, find the $\Gamma$ Givens rotation matrix and the Givens matrix embedding ${}^{i}G_{j,k}(A)$ matrix.
For a given matrix $A$, a generic rotation matrix ${}^{i}G_{j,k}(A)$ is generated. The matrix ${}^{i}G_{j,k}(A)$ is such that it helps to selectively nullifies an element of matrix $V={}^{i}G_{j,k}(A) A$. That is, $V_{j,i}=0$ where $V={}^{i}G_{j,k}(A) A$. The fllowing Givens rotation matrix ${}^{i}\Gamma_{j,k}$
\[\begin{aligned} {}^{i}\Gamma_{j,k} &= \begin{pmatrix} {}^{i}g_{k,k} & {}^{i}g_{k,j} \\ {}^{i}g_{j,k} & {}^{i}g_{j,j}\end{pmatrix} \\ &=\frac{1}{\sqrt{\lvert a_{j,i} \rvert^{2}+ \lvert a_{k,i} \rvert^{2}}}\begin{pmatrix} a_{k,i}^{*} & a_{j,i}^{*} \\ -a_{j,i} & {}^{i}a_{k,i}\end{pmatrix}. \end{aligned}\]
is embedded in an identity matrix $I(n)$ to produce,
\[{}^{i}G_{j,k}(A) = \begin{pmatrix} 1 & 0 & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & 0 \\ 0 & 1 & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\ 0 & 0 & \ddots & {\color{red} {}^{i}g_{k,k}} & \ddots & {\color{red} {}^{i}g_{k,j}} & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots \ddots & & \ddots & \ddots & \vdots \\ 0 & 0 & \ddots & {\color{red} {}^{i}g_{j,k}} & \ddots & {\color{red} {}^{i}g_{j,j}} & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \vdots \\ 0 & 0 & \ldots & \ldots & \ldots & \ldots & \ldots & 1 & 0 \\ 0 & 0 & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & 1 \end{pmatrix}.\]
Essentially, ${}^{i}G_{j,k}(A)$ is a modified identity matrix such that four non trivial elements are taken from the givens rotation matrix ${}^{i}\Gamma_{j,k}$.
julia> using LinearAlgebra
julia> using GrayCoding
julia>n=2;N=2^n;A=rand(N,N)+im*rand(N,N);S=svd(A);U=S.U
4×4 Matrix{ComplexF64}:
-0.365903-0.405021im 0.442293-0.0769938im … 0.115307-0.288609im
-0.285173-0.35669im -0.671764+0.0698449im -0.384583+0.295428im
-0.196831-0.611652im -0.154487+0.0160399im 0.379159-0.121825im
-0.177839-0.221435im 0.536228-0.175044im -0.338822+0.62835im
julia> i,j,k=1,2,4
julia> Γ,G,GA=quantumΓ(U,i,j,k);
julia> round.(quantumΓ(S.U,1,2,4)[1],digits=1)
2×2 Matrix{ComplexF64}:
-0.3+0.4im -0.5+0.7im
0.5+0.7im -0.3-0.4im
julia> round.(quantumΓ(S.U,1,2,4)[2],digits=1)
4×4 Matrix{ComplexF64}:
1.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im
0.0+0.0im -0.3-0.4im 0.0+0.0im 0.5+0.7im
0.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+0.0im
0.0+0.0im -0.5+0.7im 0.0+0.0im -0.3+0.4im
julia> round.(quantumΓ(S.U,1,2,4)[3],digits=1)
4×4 Matrix{ComplexF64}:
-0.4-0.4im 0.4-0.1im 0.6+0.1im 0.1-0.3im
0000000 0.7+0.5im -0.2-0.4im -0.3+0.2im
-0.2-0.6im -0.2+0.0im -0.4-0.5im 0.4-0.1im
0.5+0.0im 0.2-0.2im -0.2-0.1im -0.1-0.8im
GrayCoding.reflect_code
— MethodReflected code.
julia>reflect_code(3)
[0,1,3,2,2,3,1,0]
GrayCoding.sequenceΓ
— MethodQuantum circuit decomposition. For nan arbitrary unitary matix for $n$ qubits. Arbitrary quantum circuit abstracted by unitary matrix $U$ decomposed by $2^{n-1}2^{n}$ unitary two-level matrices, each of which corresponds ${}^{i}\Gamma_{j,k}$. The program produce the $\Gamma$ matrix and the coefficients $i,j,k$. The quantum circuit of this decomposition can be visualized as a cascade (from left to right) of this matrix.
GrayCoding.toffoli_matrix
— MethodThe unitary matrix $U$ corresponding to of the 3 quibit Tiffoli quantum gate
\[U=\begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & {\color{red}0} & {\color{red}1} \\ 0 & 0 & 0 & 0 & 0 & 0 & {\color{red}1} & {\color{red}0} \\ \end{pmatrix}\]
GrayCoding.πmatrix
— FunctionPermutation matrix $P$ which when operated on the right will swap columns $(k,n)$. When operated n the left the rows $(k,n)$ will get swapped. Permutation matrices are derived from identity matrix by swapping one column (also results in one row swap).
Arguments
- $n$ – order of the matrix (square matrix of size $n \times n$)
- $k,m$ – column indices $1 \ge (k,m) \ge n$. When $k,m$ is a vector, multiple rows/columns gets swapped when operated on left/right.
Examples
julia> πmatrix(6,[2,3],[5,6])
6×6 Matrix{Float64}:
1.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 1.0 0.0
0.0 0.0 0.0 0.0 0.0 1.0
0.0 0.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 0.0 0.0
julia> A= rand(4,4)
4×4 Matrix{Float64}:
0.335342 0.880332 0.747449 0.886538
0.0326128 0.648394 0.87131 0.386876
0.876305 0.215459 0.247227 0.100161
0.707862 0.992129 0.411607 0.995662
julia> AA*πmatrix(4,2,4)
4×4 Matrix{Float64}:
0.335342 0.886538 0.747449 0.880332
0.0326128 0.386876 0.87131 0.648394
0.876305 0.100161 0.247227 0.215459
0.707862 0.995662 0.411607 0.992129
julia> πmatrix(4,1,3)*AA
4×4 Matrix{Float64}:
0.876305 0.215459 0.247227 0.100161
0.0326128 0.648394 0.87131 0.386876
0.335342 0.880332 0.747449 0.886538
0.707862 0.992129 0.411607 0.995662
Test
begin
R8=rand(8,8)
Q8=πmatrix(8,2,7)*πmatrix(8,5,8)*R8*πmatrix(8,2,7)*πmatrix(8,5,8)
R8[[2,5],[2,5]]
Q8[[7,8],[7,8]]
end