Open In App

Generate Hadamard matrix of given order

Hadamard matrix is a square matrix with the unique property that any two of its rows are orthogonal. In geometric terms that means that the two rows denote two perpendicular vectors and in combinatorial terms, it means that the two rows have matching elements in exactly half places and the other half elements are mismatching.

Some important properties of a Hadamard matrix are:



H2m
{ {H2m-1 ,  H2m-1}
  {H2m-1, -H2m-1}}

Given a non-negative integer M, the task is to generate a Hadamard matrix of order 2M.



Examples: 

Input: M = 2
Output:
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1

Input: M = 3
Output:

Hadamard matrix of order 8

 

Approach: This is a simple implementation based problem. The idea is to use the above relation and iterate from order 1 to 2M to generate a Hadamard matrix of order  2M.

Follow the steps mentioned below to implement the idea.

Below is the implementation of the above approach.




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
void generate(int M)
{
    // Computing n = 2^M
    // using pow() method of Math class
    int n = pow(2, M);
 
    // Initializing a matrix of order n
    vector<vector<int> > hadamard(n, vector<int>(n));
   
    // Initializing the 0th column and
    // 0th row element as 1
    hadamard[0][0] = 1;
    for (int k = 1; k < n; k += k) {
 
        // Loop to copy elements to
        // other quarters of the matrix
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < k; j++) {
                hadamard[i + k][j] = hadamard[i][j];
                hadamard[i][j + k] = hadamard[i][j];
                hadamard[i + k][j + k] = -hadamard[i][j];
            }
        }
    }
    // Displaying the final hadamard matrix
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << hadamard[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int M = 2;
 
    // Function call
    generate(M);
    return 0;
}
 
// This code is contributed by Ishan Khandelwal




// Java code to implement the approach
 
import java.util.*;
 
class GFG {
 
    public static void generate(int M)
    {
        // Computing n = 2^M
        // using pow() method of Math class
        int n = (int)Math.pow(2, M);
 
        // Initializing a matrix of order n
        int[][] hadamard = new int[n][n];
 
        // Initializing the 0th column and
        // 0th row element as 1
        hadamard[0][0] = 1;
        for (int k = 1; k < n; k += k) {
 
            // Loop to copy elements to
            // other quarters of the matrix
            for (int i = 0; i < k; i++) {
                for (int j = 0; j < k; j++) {
                    hadamard[i + k][j]
                        = hadamard[i][j];
                    hadamard[i][j + k]
                        = hadamard[i][j];
                    hadamard[i + k][j + k]
                        = -hadamard[i][j];
                }
            }
        }
 
        // Displaying the final hadamard matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(hadamard[i][j] + " ");
            }
            System.out.println();
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int M = 2;
 
        // Function call
        generate(M);
    }
}




# Python3 code to implement the approach
def generate(M):
 
    # Computing n = 2^M
    n = 2 ** M
 
    # Initializing a matrix of order n
    hadamard = [ [0] * n for _ in range(n)]
     
    # Initializing the 0th column and
    # 0th row element as 1
    hadamard[0][0] = 1
     
    k = 1
    while (k  < n):
 
        # Loop to copy elements to
        # other quarters of the matrix
        for i in range(k):
            for j in range(k):
                hadamard[i + k][j] = hadamard[i][j];
                hadamard[i][j + k] = hadamard[i][j];
                hadamard[i + k][j + k] = -hadamard[i][j];
        k *= 2
 
    # Displaying the final hadamard matrix
    for i in range(n):
        for j in range(n):
            print(hadamard[i][j], end = " ")
        print()
 
# Driver code
M = 2;
 
# Function call
generate(M);
 
# This code is contributed by phasing17




// C# code to implement the approach
using System;
 
public class GFG{
 
    public static void generate(int M)
    {
        // Computing n = 2^M
        // using pow() method of Math class
        int n = (int)Math.Pow(2, M);
 
        // Initializing a matrix of order n
          int[,] hadamard = new int[n, n];
 
        // Initializing the 0th column and
        // 0th row element as 1
        hadamard[0,0] = 1;
        for (int k = 1; k < n; k += k) {
 
            // Loop to copy elements to
            // other quarters of the matrix
            for (int i = 0; i < k; i++) {
                for (int j = 0; j < k; j++) {
                    hadamard[i + k,j]
                        = hadamard[i,j];
                    hadamard[i,j + k]
                        = hadamard[i,j];
                    hadamard[i + k,j + k]
                        = -hadamard[i,j];
                }
            }
        }
 
        // Displaying the final hadamard matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                Console.Write(hadamard[i,j] + " ");
            }
            Console.WriteLine();
        }
    }
 
    // Driver code
    static public void Main (){
 
        int M = 2;
 
        // Function call
        generate(M);
    }
}
 
// This code is contributed by hrithikgarg03188.




<script>
// Javascript code to implement the approach
function generate(M)
{
 
    // Computing n = 2^M
    // using pow() method of Math class
    let n = Math.pow(2, M);
 
    // Initializing a matrix of order n
    let hadamard = new Array(n).fill(0).map(() => new Array(n));
 
    // Initializing the 0th column and
    // 0th row element as 1
    hadamard[0][0] = 1;
    for (let k = 1; k < n; k += k) {
 
        // Loop to copy elements to
        // other quarters of the matrix
        for (let i = 0; i < k; i++) {
            for (let j = 0; j < k; j++) {
                hadamard[i + k][j]
                    = hadamard[i][j];
                hadamard[i][j + k]
                    = hadamard[i][j];
                hadamard[i + k][j + k]
                    = -hadamard[i][j];
            }
        }
    }
 
    // Displaying the final hadamard matrix
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            document.write(hadamard[i][j] + " ");
        }
        document.write("<br>");
    }
}
 
// Driver code
 
let M = 2;
 
// Function call
generate(M);
 
// This code is contributed by gfgking.
</script>

Output
1 1 1 1 
1 -1 1 -1 
1 1 -1 -1 
1 -1 -1 1 

Time Complexity: O(2M)
Auxiliary Space: O(2M)


Article Tags :