| 1 | /* |
| 2 | * EigenDecomposition.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.evomodel.substmodel; |
| 27 | |
| 28 | import java.io.Serializable; |
| 29 | |
| 30 | /** |
| 31 | * @author Andrew Rambaut |
| 32 | * @author Alexei Drummond |
| 33 | * @Author Marc A. Suchard |
| 34 | * @version $Id$ |
| 35 | */ |
| 36 | public class EigenDecomposition implements Serializable { |
| 37 | |
| 38 | public EigenDecomposition(double[] evec, double[] ievc, double[] eval) { |
| 39 | Evec = evec; |
| 40 | Ievc = ievc; |
| 41 | Eval = eval; |
| 42 | } |
| 43 | |
| 44 | public EigenDecomposition copy() { |
| 45 | double[] evec = Evec.clone(); |
| 46 | double[] ievc = Ievc.clone(); |
| 47 | double[] eval = Eval.clone(); |
| 48 | |
| 49 | return new EigenDecomposition(evec, ievc, eval); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * This function returns the Eigen vectors. |
| 54 | * @return the array |
| 55 | */ |
| 56 | public final double[] getEigenVectors() { |
| 57 | return Evec; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * This function returns the inverse Eigen vectors. |
| 62 | * @return the array |
| 63 | */ |
| 64 | public final double[] getInverseEigenVectors() { |
| 65 | return Ievc; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * This function returns the Eigen values. |
| 70 | * @return the Eigen values |
| 71 | */ |
| 72 | public final double[] getEigenValues() { |
| 73 | return Eval; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * This function returns the normalization factor |
| 78 | * @return normalization factor |
| 79 | */ |
| 80 | public final double getNormalization() { return normalization; } |
| 81 | |
| 82 | /** |
| 83 | * This function rescales the eigen values; this is more stable than |
| 84 | * rescaling the original Q matrix, also O(stateCount) instead of O(stateCount^2) |
| 85 | */ |
| 86 | public void normalizeEigenValues(double scale) { |
| 87 | this.normalization = scale; |
| 88 | int dim = Eval.length; |
| 89 | for (int i = 0; i < dim; i++) { |
| 90 | Eval[i] /= scale; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Eigenvalues, eigenvectors, and inverse eigenvectors |
| 95 | private final double[] Evec; |
| 96 | private final double[] Ievc; |
| 97 | private final double[] Eval; |
| 98 | private double normalization = 1.0; |
| 99 | } |