Generate Circulant Matrix from given Array
Last Updated :
25 May, 2022
Given an array A[], the task is to find the circulant matrix made by this array.
A circulant matrix is a square matrix of order N x N, where each column is composed of the same elements, but each column is rotated one element to the bottom relative to the preceding column. It is a particular kind of Toeplitz matrix.
Here is the general form of a circulant matrix.
General structure of Circulant Matrix
Examples:
Input: a[] = {2, 3, 4, 5}.
Output: Then the resultant circulant matrix should be:
2 3 4 5
3 4 5 2
4 5 2 3
5 2 3 4
Input: a[] = {0, 4, 0, 7, 9, 12, 17}.
Output: The resultant circulant matrix should be:
0 4 0 7 9 12 17
4 0 7 9 12 17 0
0 7 9 12 17 0 4
7 9 12 17 0 4 0
9 12 17 0 4 0 7
12 17 0 4 0 7 9
17 0 4 0 7 9 12
Approach: This is a simple implementation based problem based on the following idea:
For each ith column, insert the first element of the array at ith row and insert all the other elements iterating through the column in a circular manner.
Follow the below steps to solve the problem:
- Initialize an empty matrix (say c[][])of order N.
- Iterate through the columns from i = 0 to N-1:
- Iterate through the rows using a nested loop from j = 0 to N-1.
- if (i > 0), then assign c[j][i] = c[j - 1][i - 1].
- else, assign c[j][i] = c[N - 1][i - 1].
- At last, display the circulant matrix.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach
#include <iostream>
using namespace std;
// Circulant function defined here
void circulant(int arr[], int n)
{
// Initializing an empty
// 2D matrix of order n
int c[n][n];
for (int k = 0; k <= n - 1; k++)
c[k][0] = arr[k];
// Forming the circulant matrix
for (int i = 1; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
if (j - 1 >= 0)
c[j][i] = c[j - 1][i - 1];
else
c[j][i] = c[n - 1][i - 1];
}
}
// Displaying the circulant matrix
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
cout << c[i][j] << "\t";
}
cout << "\n";
}
}
// Driver Code
int main()
{
int N = 4;
int A[] = { 2, 3, 4, 5 };
// Function call
circulant(A, N);
return 0;
}
// This code is contributed by Rohit Pradhan
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Circulant function defined here
public static void circulant(int arr[], int n)
{
// Initializing an empty
// 2D matrix of order n
int c[][] = new int[n][n];
for (int k = 0; k <= n - 1; k++)
c[k][0] = arr[k];
// Forming the circulant matrix
for (int i = 1; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
if (j - 1 >= 0)
c[j][i] = c[j - 1][i - 1];
else
c[j][i] = c[n - 1][i - 1];
}
}
// Displaying the circulant matrix
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
System.out.print(c[i][j] + "\t");
}
System.out.println();
}
}
// Driver code
public static void main(String[] args)
{
int N = 4;
int A[] = { 2, 3, 4, 5 };
circulant(A, N);
}
}
Python3
# Python code to implement the approach
# Circulant function defined here
def circulant( arr, n):
# Initializing an empty
# 2D matrix of order n
c = [[0 for i in range(n)] for j in range(n)]
for k in range(n):
c[k][0] = arr[k]
# Forming the circulant matrix
for i in range(1,n):
for j in range(n):
if (j - 1 >= 0):
c[j][i] = c[j - 1][i - 1]
else:
c[j][i] = c[n - 1][i - 1]
# Displaying the circulant matrix
for i in range(n):
for j in range(n):
print(c[i][j] ,end="\t")
print()
# Driver code
N = 4
A = [2, 3, 4, 5 ]
circulant(A, N)
# This code is contributed by rohitsingh07052
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Circulant function defined here
public static void circulant(int[] arr, int n)
{
// Initializing an empty
// 2D matrix of order n
int[,] c = new int[n, n];
for (int k = 0; k <= n - 1; k++)
c[k, 0] = arr[k];
// Forming the circulant matrix
for (int i = 1; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
if (j - 1 >= 0)
c[j, i] = c[j - 1, i - 1];
else
c[j, i] = c[n - 1, i - 1];
}
}
// Displaying the circulant matrix
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
int N = 4;
int[] A = { 2, 3, 4, 5 };
circulant(A, N);
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// JavaScript code to implement the approach
// Circulant function defined here
function circulant(arr, n)
{
// Initializing an empty
// 2D matrix of order n
let c = new Array(n).fill(0).map(()=>new Array(n));
for (let k = 0; k <= n - 1; k++)
c[k][0] = arr[k];
// Forming the circulant matrix
for (let i = 1; i <= n - 1; i++) {
for (let j = 0; j <= n - 1; j++) {
if (j - 1 >= 0)
c[j][i] = c[j - 1][i - 1];
else
c[j][i] = c[n - 1][i - 1];
}
}
// Displaying the circulant matrix
for (let i = 0; i <= n - 1; i++) {
for (let j = 0; j <= n - 1; j++) {
document.write(`${c[i][j]} \t`)
}
document.write("</br>")
}
}
// Driver Code
let N = 4
let A = [ 2, 3, 4, 5 ]
// Function call
circulant(A, N)
// This code is contributed by shinjanpatra
</script>
Output2 5 4 3
3 2 5 4
4 3 2 5
5 4 3 2
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Create Spiral Matrix from Array Given an array arr[] and two values n and m, the task is to fill a matrix of size n*m in a spiral (or circular) fashion (clockwise) with given array elements.Examples: Input: n = 4, m = 4, arr[]= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]Output: [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16,
8 min read
Convert given upper triangular Matrix to 1D Array Given an upper triangular matrix M[][] of dimensions N * N, the task is to convert it into an one-dimensional array storing only non-zero elements from the matrix. Examples: Input: M[][] = {{1, 2, 3, 4}, {0, 5, 6, 7}, {0, 0, 8, 9}, {0, 0, 0, 10}}Output: Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Colu
12 min read
Convert given lower triangular Matrix to 1D array Given a lower triangular matrix M[][] of dimension N * N, the task is to convert it into a one-dimensional array by storing only non-zero elements. Examples: Input: M[][] = {{1, 0, 0, 0}, {2, 3, 0, 0}, {4, 5, 6, 0}, {7, 8, 9, 10}} Output:Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}Column-wise: {1, 2, 4
13 min read
Print lower triangular matrix pattern from given array Given an array, print the array in 2D form where upper triangle has values 0 and lower triangle has values increasing prefix sizes (First row has prefix of size 1, second row has prefix of size 2, ..)Examples : Input : 1 2 3 4 5 Output : 1 0 0 0 0 1 2 0 0 0 1 2 3 0 0 1 2 3 4 0 1 2 3 4 5 Input : 1 2
6 min read
Print a given matrix in counter-clock wise spiral form Given a 2D array, print it in counter-clock wise spiral form. See the following examples.Examples : Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7 Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output: 1 7 13 14 15 16 17 18 12 6 5 4 3 2 8 9 10 11 Ex
15+ min read
Create Spiral Matrix from Array using Direction Arrays Given an array arr[] and two values n and m, the task is to fill a matrix of size n*m in a spiral (or circular) fashion (clockwise) with given array elements.Examples: Input: n = 4, m = 4, arr []= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]Output : [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16
8 min read
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 hal
6 min read
Generate an array of size N according to the given rules Given a number N, the task is to create an array arr[] of size N, where the value of the element at every index i is filled according to the following rules: arr[i] = ((i - 1) - k), where k is the index of arr[i - 1] that has appeared second most recently. This rule is applied when arr[i - 1] is pre
10 min read
Convert a Matrix into another Matrix of given dimensions Given a matrix, mat[][] of size N * M and two positive integers A and B, the task is to construct a matrix of size A * B from the given matrix elements. If multiple solutions exist, then print any one of them. Otherwise, print -1. Input: mat[][] = { { 1, 2, 3, 4, 5, 6 } }, A = 2, B = 3 Output: { { 1
6 min read
Print a given matrix in zigzag form Given a 2D array, print it in zigzag form. Examples : Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Output : 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 Input : 10 24 32 50 6 17 99 10 11 Output : 10 24 32 17 6 50 99 10 11 CPP // C++ program to print // matrix in zig-zag form #inc
7 min read