Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Hilbert Matrix

  • Difficulty Level : Easy
  • Last Updated : 09 Nov, 2020

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.

    Examples:

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

    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++




    // 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;
    }

    Java




    // 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.

    C#




    // 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
    Output:
    1 0.5 0.333333 
    0.5 0.333333 0.25 
    0.333333 0.25 0.2
    




    My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!