Related Articles

Related Articles

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

    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.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :