希尔伯特矩阵是一个方阵,其每个元素都是单位分数。
特性:
- 它是一个对称矩阵。
- 其决定因素值始终为正。
例子:
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
从数学上讲,希尔伯特矩阵可以由以下公式形成:
Let H be a Hilbert Matrix of NxN. Then H(i, j) = 1/(i+j-1)
以下是上述公式的基本实现。
C++
// C++ program for Hilbert Matrix #include
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
输出:1 0.5 0.333333 0.5 0.333333 0.25 0.333333 0.25 0.2