View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.linear;
23  
24  import org.hipparchus.exception.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  
27  /** This class implements Hilbert Matrices as {@link RealLinearOperator}. */
28  public class HilbertMatrix
29      implements RealLinearOperator {
30  
31      /** The size of the matrix. */
32      private final int n;
33  
34      /**
35       * Creates a new instance of this class.
36       *
37       * @param n Size of the matrix to be created..
38       */
39      public HilbertMatrix(final int n) {
40          this.n = n;
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public int getColumnDimension() {
46          return n;
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public int getRowDimension() {
52          return n;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public RealVector operate(final RealVector x) {
58          if (x.getDimension() != n) {
59              throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
60                                                     x.getDimension(), n);
61          }
62          final double[] y = new double[n];
63          for (int i = 0; i < n; i++) {
64              double pos = 0.;
65              double neg = 0.;
66              for (int j = 0; j < n; j++) {
67                  final double xj = x.getEntry(j);
68                  final double coeff = 1. / (i + j + 1.);
69                  // Positive and negative values are sorted out in order to limit
70                  // catastrophic cancellations (do not forget that Hilbert
71                  // matrices are *very* ill-conditioned!
72                  if (xj > 0.) {
73                      pos += coeff * xj;
74                  } else {
75                      neg += coeff * xj;
76                  }
77              }
78              y[i] = pos + neg;
79          }
80          return new ArrayRealVector(y, false);
81      }
82  }