/* * MatrixParameter.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.inference.model; import dr.xml.*; import java.util.StringTokenizer; /** * @author Marc Suchard * @author Max Tolkoff */ public class MatrixParameter extends CompoundParameter implements MatrixParameterInterface { public final static String MATRIX_PARAMETER = "matrixParameter"; public MatrixParameter(String name) { super(name); } public MatrixParameter(String name, Parameter[] parameters) { super(name, parameters); rowDimension=parameters[0].getDimension(); columnDimension=parameters.length; dimensionsEstablished = true; } public MatrixParameter(String name, int row, int column){ super(name); setDimensions(row, column); } public MatrixParameter(String name, int row, int column, double a){ super(name); setDimensions(row, column, a); } public void setParameterValue(int row, int column, double a) { getParameter(column).setParameterValue(row, a); } public void setParameterValueQuietly(int row, int column, double a){ getParameter(column).setParameterValueQuietly(row, a); } public void setParameterValueNotifyChangedAll(int row, int column, double val){ getParameter(column).setParameterValueNotifyChangedAll(row, val); } public static MatrixParameter recast(String name, CompoundParameter compoundParameter) { final int count = compoundParameter.getParameterCount(); Parameter[] parameters = new Parameter[count]; for (int i = 0; i < count; ++i) { parameters[i] = compoundParameter.getParameter(i); } return new MatrixParameter(name, parameters); } public double getParameterValue(int row, int col) { return getParameter(col).getParameterValue(row); } public double[] getRowValues(int row){ int colDim=getColumnDimension(); double[] rowValues=new double[colDim]; for (int i = 0; i 0 && rowDimension > 0) { dimensionsEstablished = true; for (int i = 0; i < rowDimension; i++) { Parameter row = new Parameter.Default(columnDimension, 0.0); row.addBounds(new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, columnDimension)); addParameter(row); } } } //TODO rewrite so that it doesn't destroy existing parameters public void setDimensions(int rowDim, int colDim){ setDimensions(rowDim, colDim, 0.0); } public void setDimensions(int rowDim, int colDim, double a){ rowDimension=rowDim; columnDimension=colDim; for (int i = 0; i < colDim; i++) { Parameter column = new Parameter.Default(rowDim, a); column.addBounds(new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, rowDim)); addParameter(column); } } public int getColumnDimension() { return getParameterCount(); } public int getRowDimension() { return getParameter(0).getDimension(); } @Override public int getUniqueParameterCount() { return getParameterCount(); } @Override public Parameter getUniqueParameter(int index) { return super.getParameter(index); } @Override public void copyParameterValues(double[] destination, int offset) { final int length = getDimension(); for (int i = 0; i < length; ++i) { destination[offset + i] = getParameterValue(i); } } @Override public void setAllParameterValuesQuietly(double[] values, int offset) { for (int i = 0; i < getDimension(); ++i) { setParameterValueQuietly(i, values[offset + i]); } } public static String toSymmetricString(MatrixParameterInterface mat) { StringBuilder sb = new StringBuilder("{"); int dim = mat.getRowDimension(); int total = dim * (dim + 1) / 2; for (int i = 0; i < dim; i++) { for (int j = i; j < dim; j++) { sb.append(String.format("%5.4e", mat.getParameterValue(i, j))); total--; if (total > 0) sb.append(","); } } sb.append("}"); return sb.toString(); } public String toSymmetricString() { return toSymmetricString(this); } public static MatrixParameter parseFromSymmetricString(String string) { String clip = string.replace("{", "").replace("}", "").trim(); StringTokenizer st = new StringTokenizer(clip, ","); int count = st.countTokens(); int dim = (-1 + (int) Math.sqrt(1 + 8 * count)) / 2; Parameter[] parameter = new Parameter[dim]; for (int i = 0; i < dim; i++) parameter[i] = new Parameter.Default(dim); for (int i = 0; i < dim; i++) { for (int j = i; j < dim; j++) { double datum = new Double(st.nextToken()); parameter[i].setParameterValue(j, datum); parameter[j].setParameterValue(i, datum); } } return new MatrixParameter(null, parameter); } public static MatrixParameter parseFromSymmetricDoubleArray(Object[] data) { int dim = (-1 + (int) Math.sqrt(1 + 8 * data.length)) / 2; Parameter[] parameter = new Parameter[dim]; for (int i = 0; i < dim; i++) parameter[i] = new Parameter.Default(dim); int index = 0; for (int i = 0; i < dim; i++) { for (int j = i; j < dim; j++) { double datum = (Double) data[index++]; parameter[i].setParameterValue(j, datum); parameter[j].setParameterValue(i, datum); } } return new MatrixParameter(null, parameter); } public void rowMultiply(double a, int row){ rowMultiplyQuietly(a, row); fireParameterChangedEvent(); } public void columnMultiply(double a, int col){ columnMultiplyQuietly(a,col); fireParameterChangedEvent(); } public TransposedMatrixParameter transpose(){ return TransposedMatrixParameter.recast(null, this); } // ************************************************************** // Matrix Operations // ************************************************************** public MatrixParameter add(MatrixParameter Right) { if (Right.getRowDimension() != getRowDimension() || getColumnDimension() != Right.getColumnDimension()){ throw new RuntimeException("You cannot add a " + getRowDimension() +" by " + getColumnDimension() + " matrix to a " + Right.getRowDimension() + " by " + Right.getColumnDimension() + " matrix."); } MatrixParameter answer=new MatrixParameter(null); answer.setDimensions(getRowDimension(), getColumnDimension()); for (int i = 0; i