Hilbert Matrix

A Hilbert Matrix is a square matrix whose each element is a unit fraction.
Properties:

  1. It is a symmetric matrix.
  2. Its determinant value is always positive.
  3. 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++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program for Hilbert Matrix
    #include <bits/stdc++.h>
    using namespace std;
      
    // Function that generates a Hilbert matrix
    void 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 function
    int main()
    {
        int n = 3;
        printMatrix(n);
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program for
    // Hilbert Matrix
    import java.io.*;
      
    class GFG 
    {
          
    // Function that generates 
    // a Hilbert matrix
    static 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 code
    public static void main (String[] args) 
    {
        int n = 3;
        printMatrix(n);
    }
    }
      
    // This code is contributed 
    // by anuj_67.

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program for Hilbert Matrix
    using System;
      
    class GFG 
    {
          
    // Function that generates 
    // a Hilbert matrix
    static 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 code
    public static void Main() 
    {
        int n = 3;
        printMatrix(n);
    }
    }
      
    // This code is contributed 
    // by mits

    chevron_right

    
    

    Output:

    1 0.5 0.333333 
    0.5 0.333333 0.25 
    0.333333 0.25 0.2
    


    My Personal Notes arrow_drop_up

    Maths is the language of nature

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Improved By : vt_m, Mithun Kumar



    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.