Debian Codegraph @

src/dr/math/matrixAlgebra/JacobiTransformation.java

raw symbols SCIP
1 /*
2 * JacobiTransformation.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.matrixAlgebra;
27
28 import dr.math.iterations.IterativeProcess;
29
30 /**
31 * JacobiTransformation
32 *
33 * @author Didier H. Besset
34 */
35 public class JacobiTransformation extends IterativeProcess
36 {
37 double[][] rows;
38 double[][] transform;
39 int p,q; //Indices of the largest off-diagonal element
40 /**
41 * Create a new instance for a given symmetric matrix.
42 * @param m DhbMatrixAlgebra.SymmetricMatrix
43 */
44 public JacobiTransformation (SymmetricMatrix m)
45 {
46 int n = m.rows();
47 rows = new double[n][n];
48 for ( int i = 0; i < n; i++)
49 {
50 for ( int j = 0; j < n; j++)
51 rows[i][j] = m.components[i][j];
52 }
53 }
54 /**
55 * @return double[]
56 */
57 public double[] eigenvalues ( )
58 {
59 int n = rows.length;
60 double[] eigenvalues = new double[n];
61 for ( int i = 0; i < n; i++ )
62 eigenvalues[i] = rows[i][i];
63 return eigenvalues;
64 }
65 /**
66 * @return DhbMatrixAlgebra.SymmetricMatrix
67 */
68 public Vector[] eigenvectors ( )
69 {
70 int n = rows.length;
71 Vector[] eigenvectors = new Vector[n];
72 double[] temp = new double[n];
73 for ( int i = 0; i < n; i++ )
74 {
75 for ( int j = 0; j < n; j++)
76 temp[j] = transform[j][i];
77 eigenvectors[i] = new Vector( temp);
78 }
79 return eigenvectors;
80 }
81 public double evaluateIteration()
82 {
83 double offDiagonal = largestOffDiagonal();
84 transform();
85 return offDiagonal;
86 }
87 /**
88 * @param m int
89 */
90 private void exchange ( int m)
91 {
92 int m1 = m + 1;
93 double temp = rows[m][m];
94 rows[m][m] = rows[m1][m1];
95 rows[m1][m1] = temp;
96 int n = rows.length;
97 for ( int i = 0; i < n; i++ )
98 {
99 temp = transform[i][m];
100 transform[i][m] = transform[i][m1];
101 transform[i][m1] = temp;
102 }
103 }
104 public void finalizeIterations ( )
105 {
106 int n = rows.length;
107 int bound = n - 1;
108 int i, m;
109 while ( bound >= 0 )
110 {
111 m = -1;
112 for ( i = 0; i < bound; i++ )
113 {
114 if ( Math.abs( rows[i][i]) < Math.abs( rows[i+1][i+1]) )
115 {
116 exchange( i);
117 m = i;
118 }
119 }
120 bound = m;
121 }
122 return;
123 }
124 public void initializeIterations()
125 {
126 transform = SymmetricMatrix.identityMatrix( rows.length).components;
127 }
128 /**
129 * @return double absolute value of the largest off diagonal element
130 */
131 private double largestOffDiagonal( )
132 {
133 double value = 0;
134 double r;
135 int n = rows.length;
136 for (int i = 0; i < n; i++)
137 {
138 for ( int j = 0; j < i; j++)
139 {
140 r = Math.abs( rows[i][j]);
141 if ( r > value )
142 {
143 value = r;
144 p = i;
145 q = j;
146 }
147 }
148 }
149 return value;
150 }
151 /**
152 * Returns a string representation of the system.
153 * @return java.lang.String
154 */
155 public String toString()
156 {
157 StringBuffer sb = new StringBuffer();
158 char[] separator = { '[', ' '};
159 int n = rows.length;
160 for ( int i = 0; i < n; i++)
161 {
162 separator[0] = '{';
163 for ( int j = 0; j <= i; j++)
164 {
165 sb.append( separator);
166 sb.append( rows[i][j]);
167 separator[0] = ' ';
168 }
169 sb.append('}');
170 sb.append('\n');
171 }
172 return sb.toString();
173 }
174 /**
175 * @return DhbMatrixAlgebra.SymmetricMatrix
176 */
177 private void transform ( )
178 {
179 double apq = rows[p][q];
180 if ( apq == 0 )
181 return;
182 double app = rows[p][p];
183 double aqq = rows[q][q];
184 double arp = ( aqq - app) * 0.5 / apq;
185 double t = arp > 0 ? 1 / ( Math.sqrt( arp * arp + 1) + arp)
186 : 1 / ( arp - Math.sqrt( arp * arp + 1));
187 double c = 1 / Math.sqrt( t * t + 1);
188 double s = t * c;
189 double tau = s / ( 1 + c);
190 rows[p][p] = app - t * apq;
191 rows[q][q] = aqq + t * apq;
192 rows[p][q] = 0;
193 rows[q][p] = 0;
194 int n = rows.length;
195 for ( int i = 0; i < n; i++ )
196 {
197 if ( i != p && i != q )
198 {
199 rows[p][i] = rows[i][p] - s *( rows[i][q]
200 + tau * rows[i][p]);
201 rows[q][i] = rows[i][q] + s *( rows[i][p]
202 - tau * rows[i][q]);
203 rows[i][p] = rows[p][i];
204 rows[i][q] = rows[q][i];
205 }
206 arp = transform[i][p];
207 aqq = transform[i][q];
208 transform[i][p] = arp - s * ( aqq + tau * arp);
209 transform[i][q] = aqq + s * ( arp - tau * aqq);
210 }
211 }
212 }