1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.linear;
import org.apache.commons.math3.analysis.function.Sqrt;
import org.apache.commons.math3.util.MathArrays;
/**
* This class implements the standard Jacobi (diagonal) preconditioner. For a matrix A<sub>ij</sub>,
* this preconditioner is M = diag(1 / A<sub>11</sub>, 1 / A<sub>22</sub>, …).
*
* @since 3.0
*/
public class JacobiPreconditioner extends RealLinearOperator {
/** The diagonal coefficients of the preconditioner. */
private final ArrayRealVector diag;
/**
* Creates a new instance of this class.
*
* @param diag the diagonal coefficients of the linear operator to be preconditioned
* @param deep {@code true} if a deep copy of the above array should be performed
*/
public JacobiPreconditioner(final double[] diag, final boolean deep) {
this.diag = new ArrayRealVector(diag, deep);
}
/**
* Creates a new instance of this class. This method extracts the diagonal coefficients of the
* specified linear operator. If {@code a} does not extend {@link AbstractRealMatrix}, then the
* coefficients of the underlying matrix are not accessible, coefficient extraction is made by
* matrix-vector products with the basis vectors (and might therefore take some time). With
* matrices, direct entry access is carried out.
*
* @param a the linear operator for which the preconditioner should be built
* @return the diagonal preconditioner made of the inverse of the diagonal coefficients of the
* specified linear operator
* @throws NonSquareOperatorException if {@code a} is not square
*/
public static JacobiPreconditioner create(final RealLinearOperator a)
throws NonSquareOperatorException {
final int n = a.getColumnDimension();
if (a.getRowDimension() != n) {
throw new NonSquareOperatorException(a.getRowDimension(), n);
}
final double[] diag = new double[n];
if (a instanceof AbstractRealMatrix) {
final AbstractRealMatrix m = (AbstractRealMatrix) a;
for (int i = 0; i < n; i++) {
diag[i] = m.getEntry(i, i);
}
} else {
final ArrayRealVector x = new ArrayRealVector(n);
for (int i = 0; i < n; i++) {
x.set(0.);
x.setEntry(i, 1.);
diag[i] = a.operate(x).getEntry(i);
}
}
return new JacobiPreconditioner(diag, false);
}
/** {@inheritDoc} */
@Override
public int getColumnDimension() {
return diag.getDimension();
}
/** {@inheritDoc} */
@Override
public int getRowDimension() {
return diag.getDimension();
}
/** {@inheritDoc} */
@Override
public RealVector operate(final RealVector x) {
// Dimension check is carried out by ebeDivide
return new ArrayRealVector(MathArrays.ebeDivide(x.toArray(), diag.toArray()), false);
}
/**
* Returns the square root of {@code this} diagonal operator. More precisely, this method
* returns P = diag(1 / √A<sub>11</sub>, 1 / √A<sub>22</sub>, …).
*
* @return the square root of {@code this} preconditioner
* @since 3.1
*/
public RealLinearOperator sqrt() {
final RealVector sqrtDiag = diag.map(new Sqrt());
return new RealLinearOperator() {
/** {@inheritDoc} */
@Override
public RealVector operate(final RealVector x) {
return new ArrayRealVector(
MathArrays.ebeDivide(x.toArray(), sqrtDiag.toArray()), false);
}
/** {@inheritDoc} */
@Override
public int getRowDimension() {
return sqrtDiag.getDimension();
}
/** {@inheritDoc} */
@Override
public int getColumnDimension() {
return sqrtDiag.getDimension();
}
};
}
}
|