1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.linear;
23
24 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.util.ArithmeticUtils;
27 import org.hipparchus.util.CombinatoricsUtils;
28
29
30
31
32
33 public class InverseHilbertMatrix
34 implements RealLinearOperator {
35
36
37 private final int n;
38
39
40
41
42
43
44 public InverseHilbertMatrix(final int n) {
45 this.n = n;
46 }
47
48
49 @Override
50 public int getColumnDimension() {
51 return n;
52 }
53
54
55
56
57
58
59
60
61
62 public long getEntry(final int i, final int j) {
63 long val = i + j + 1;
64 long aux = CombinatoricsUtils.binomialCoefficient(n + i, n - j - 1);
65 val = ArithmeticUtils.mulAndCheck(val, aux);
66 aux = CombinatoricsUtils.binomialCoefficient(n + j, n - i - 1);
67 val = ArithmeticUtils.mulAndCheck(val, aux);
68 aux = CombinatoricsUtils.binomialCoefficient(i + j, i);
69 val = ArithmeticUtils.mulAndCheck(val, aux);
70 val = ArithmeticUtils.mulAndCheck(val, aux);
71 return ((i + j) & 1) == 0 ? val : -val;
72 }
73
74
75 @Override
76 public int getRowDimension() {
77 return n;
78 }
79
80
81 @Override
82 public RealVector operate(final RealVector x) {
83 if (x.getDimension() != n) {
84 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
85 x.getDimension(), n);
86 }
87 final double[] y = new double[n];
88 for (int i = 0; i < n; i++) {
89 double pos = 0.;
90 double neg = 0.;
91 for (int j = 0; j < n; j++) {
92 final double xj = x.getEntry(j);
93 final long coeff = getEntry(i, j);
94 final double daux = coeff * xj;
95
96
97
98 if (daux > 0.) {
99 pos += daux;
100 } else {
101 neg += daux;
102 }
103 }
104 y[i] = pos + neg;
105 }
106 return new ArrayRealVector(y, false);
107 }
108 }