Hilbert Matrix

09 Nov, 2020
A Hilbert Matrix is a square matrix whose each element is a unit fraction.Properties:
It is a symmetric matrix.Its determinant value is always positive.Examples:Input : N = 2
Output : 1    0.5
         0.5  0.33                   

Input : N = 3
Output : 1.0000    0.5000    0.3333
         0.5000    0.3333    0.2500
         0.3333    0.2500    0.2000
Recommended: Please try your approach on {IDE} first, before moving on to the solution.Mathematically, Hilbert Matrix can be formed by the given formula: 
Let H be a Hilbert Matrix of NxN.
Then
H(i, j) = 1/(i+j-1)
Below is the basic implementation of the above formula.C++JavaC#C++// C++ program for Hilbert Matrix#include <bits/stdc++.h>using namespace std;  // Function that generates a Hilbert matrixvoid printMatrix(int n){    float H[n][n];      for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {              // using the formula to generate            // hilbert matrix            H[i][j] = (float)1.0 /                      ((i + 1) + (j + 1) - 1.0);        }    }      for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++)             cout << H[i][j] << " ";                cout << endl;    }}  // driver functionint main(){    int n = 3;    printMatrix(n);    return 0;}Java// Java program for// Hilbert Matriximport java.io.*;  class GFG {      // Function that generates // a Hilbert matrixstatic void printMatrix(int n){    float H[][] = new float[n][n];      for (int i = 0; i < n; i++)    {        for (int j = 0; j < n; j++)         {              // using the formula             // to generate            // hilbert matrix            H[i][j] = (float)1.0 /                       ((i + 1) + (j + 1) -                       (float)1.0);        }    }      for (int i = 0; i < n; i++)     {        for (int j = 0; j < n; j++)             System.out.print(H[i][j] + " ");         System.out.println();    }}  // Driver codepublic static void main (String[] args) {    int n = 3;    printMatrix(n);}}  // This code is contributed // by anuj_67.C#// C# program for Hilbert Matrixusing System;  class GFG {      // Function that generates // a Hilbert matrixstatic void printMatrix(int n){    float[,] H = new float[n, n];      for (int i = 0; i < n; i++)    {        for (int j = 0; j < n; j++)         {              // using the formula to generate            // hilbert matrix            H[i, j] = (float)1.0 /                      ((i + 1) + (j + 1) -                       (float)1.0);        }    }      for (int i = 0; i < n; i++)     {        for (int j = 0; j < n; j++)             Console.Write(H[i, j] + " ");         Console.WriteLine("");    }}  // Driver codepublic static void Main() {    int n = 3;    printMatrix(n);}}  // This code is contributed // by mitsOutput:1 0.5 0.333333 
0.5 0.333333 0.25 
0.333333 0.25 0.2
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Rnp62zKoSb4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>My Personal Notes
arrow_drop_upSave
It is a symmetric matrix.
Its determinant value is always positive.
Examples:
Input : N = 2
Output : 1    0.5
         0.5  0.33                   

Input : N = 3
Output : 1.0000    0.5000    0.3333
         0.5000    0.3333    0.2500
         0.3333    0.2500    0.2000

Mathematically, Hilbert Matrix can be formed by the given formula:
 
Let H be a Hilbert Matrix of NxN.
Then
H(i, j) = 1/(i+j-1)

Below is the basic implementation of the above formula.
C++
Java
C#
// C++ program for Hilbert Matrix#include <bits/stdc++.h>using namespace std;  // Function that generates a Hilbert matrixvoid printMatrix(int n){    float H[n][n];      for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {              // using the formula to generate            // hilbert matrix            H[i][j] = (float)1.0 /                      ((i + 1) + (j + 1) - 1.0);        }    }      for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++)             cout << H[i][j] << " ";                cout << endl;    }}  // driver functionint main(){    int n = 3;    printMatrix(n);    return 0;}
// Java program for// Hilbert Matriximport java.io.*;  class GFG {      // Function that generates // a Hilbert matrixstatic void printMatrix(int n){    float H[][] = new float[n][n];      for (int i = 0; i < n; i++)    {        for (int j = 0; j < n; j++)         {              // using the formula             // to generate            // hilbert matrix            H[i][j] = (float)1.0 /                       ((i + 1) + (j + 1) -                       (float)1.0);        }    }      for (int i = 0; i < n; i++)     {        for (int j = 0; j < n; j++)             System.out.print(H[i][j] + " ");         System.out.println();    }}  // Driver codepublic static void main (String[] args) {    int n = 3;    printMatrix(n);}}  // This code is contributed // by anuj_67.
// C# program for Hilbert Matrixusing System;  class GFG {      // Function that generates // a Hilbert matrixstatic void printMatrix(int n){    float[,] H = new float[n, n];      for (int i = 0; i < n; i++)    {        for (int j = 0; j < n; j++)         {              // using the formula to generate            // hilbert matrix            H[i, j] = (float)1.0 /                      ((i + 1) + (j + 1) -                       (float)1.0);        }    }      for (int i = 0; i < n; i++)     {        for (int j = 0; j < n; j++)             Console.Write(H[i, j] + " ");         Console.WriteLine("");    }}  // Driver codepublic static void Main() {    int n = 3;    printMatrix(n);}}  // This code is contributed // by mits
1 0.5 0.333333 
0.5 0.333333 0.25 
0.333333 0.25 0.2

YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Rnp62zKoSb4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
vt_m
Mithun Kumar
Mathematical
Matrix
Mathematical
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.