| 1 | ///* |
| 2 | // * NewMatrixParameter.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.inference.model; |
| 27 | // |
| 28 | //import dr.xml.*; |
| 29 | // |
| 30 | //import java.util.StringTokenizer; |
| 31 | ////import org.w3c.dom.Document; |
| 32 | ////import org.w3c.dom.Element; |
| 33 | ////import dr.xml.*; |
| 34 | // |
| 35 | ///** |
| 36 | // * @author Marc Suchard |
| 37 | // */ |
| 38 | //public class NewMatrixParameter extends Parameter.Default { |
| 39 | // |
| 40 | // public final static String MATRIX_PARAMETER = "matrixParameter"; |
| 41 | // |
| 42 | // public NewMatrixParameter(String name) { |
| 43 | // super(name); |
| 44 | // } |
| 45 | // |
| 46 | // public NewMatrixParameter(String name, double[] parameter, int numRows, int numCols) { |
| 47 | // super(parameter); |
| 48 | // this.numRows = numRows; |
| 49 | // this.numCols = numCols; |
| 50 | // } |
| 51 | // |
| 52 | // private int numRows = 0; |
| 53 | // private int numCols = 0; |
| 54 | // |
| 55 | // public double getParameterValue(int row, int col) { |
| 56 | // return getParameterValue(row * numCols + col); // Stores in row-major |
| 57 | // } |
| 58 | // |
| 59 | // public double[][] getParameterAsMatrix() { |
| 60 | //// final int I = getRowDimension(); |
| 61 | //// final int J = getColumnDimension(); |
| 62 | //// double[][] parameterAsMatrix = new double[I][J]; |
| 63 | //// for (int i = 0; i < I; i++) { |
| 64 | //// for (int j = 0; j < J; j++) |
| 65 | //// parameterAsMatrix[i][j] = getParameterValue(i, j); |
| 66 | //// } |
| 67 | //// return parameterAsMatrix; |
| 68 | // double[][] parameterAsMatris = new double[numRows][numCols]; |
| 69 | // for (int i = 0; i < numRows; i++) { |
| 70 | // for (int j = 0; j < numCols; j++) { |
| 71 | // parameterAsMatris[i][j] = getParameterValue(i * numCols + j); |
| 72 | // } |
| 73 | // } |
| 74 | // return parameterAsMatris; |
| 75 | // } |
| 76 | // |
| 77 | // public int getColumnDimension() { |
| 78 | // return numCols; |
| 79 | // } |
| 80 | // |
| 81 | // public int getRowDimension() { |
| 82 | // return numRows; |
| 83 | // } |
| 84 | // |
| 85 | // public String toSymmetricString() { |
| 86 | // StringBuffer sb = new StringBuffer("{"); |
| 87 | // int dim = getRowDimension(); |
| 88 | // int total = dim * (dim + 1) / 2; |
| 89 | // for (int i = 0; i < dim; i++) { |
| 90 | // for (int j = i; j < dim; j++) { |
| 91 | // sb.append(String.format("%5.4e", getParameterValue(i, j))); |
| 92 | // total--; |
| 93 | // if (total > 0) |
| 94 | // sb.append(","); |
| 95 | // } |
| 96 | // } |
| 97 | // sb.append("}"); |
| 98 | // return sb.toString(); |
| 99 | // } |
| 100 | // |
| 101 | // public static NewMatrixParameter parseFromSymmetricString(String string) { |
| 102 | // String clip = string.replace("{", "").replace("}", "").trim(); |
| 103 | // StringTokenizer st = new StringTokenizer(clip, ","); |
| 104 | // int count = st.countTokens(); |
| 105 | // int dim = (-1 + (int) Math.sqrt(1 + 8 * count)) / 2; |
| 106 | //// Parameter[] parameter = new Parameter[dim]; |
| 107 | //// for (int i = 0; i < dim; i++) |
| 108 | //// parameter[i] = new Parameter.Default(dim); |
| 109 | //// for (int i = 0; i < dim; i++) { |
| 110 | //// for (int j = i; j < dim; j++) { |
| 111 | //// double datum = new Double(st.nextToken()); |
| 112 | //// parameter[i].setParameterValue(j, datum); |
| 113 | //// parameter[j].setParameterValue(i, datum); |
| 114 | //// } |
| 115 | //// } |
| 116 | // double[] data = new double[dim * dim]; |
| 117 | // for (int i = 0; i < dim; i++) { |
| 118 | // for (int j = i; j < dim; j++) { |
| 119 | // double datum = new Double(st.nextToken()); |
| 120 | // data[i * dim + j] = datum; |
| 121 | // data[j * dim + i] = datum; |
| 122 | // } |
| 123 | // } |
| 124 | // return new NewMatrixParameter(null, data, dim, dim); |
| 125 | // } |
| 126 | // |
| 127 | // public static NewMatrixParameter parseFromSymmetricDoubleArray(Object[] inData) { |
| 128 | // |
| 129 | // int dim = (-1 + (int) Math.sqrt(1 + 8 * inData.length)) / 2; |
| 130 | //// Parameter[] parameter = new Parameter[dim]; |
| 131 | //// for (int i = 0; i < dim; i++) |
| 132 | //// parameter[i] = new Parameter.Default(dim); |
| 133 | // int index = 0; |
| 134 | //// for (int i = 0; i < dim; i++) { |
| 135 | //// for (int j = i; j < dim; j++) { |
| 136 | //// double datum = (Double) data[index++]; |
| 137 | //// parameter[i].setParameterValue(j, datum); |
| 138 | //// parameter[j].setParameterValue(i, datum); |
| 139 | //// } |
| 140 | //// } |
| 141 | //// return new MatrixParameter(null, parameter); |
| 142 | // double[] data = new double[dim * dim]; |
| 143 | // for (int i = 0; i < dim; i++) { |
| 144 | // for (int j = i; j < dim; j++) { |
| 145 | // double datum = (Double) inData[index++]; |
| 146 | // data[i * dim + j] = datum; |
| 147 | // data[j * dim + i] = datum; |
| 148 | // } |
| 149 | // } |
| 150 | // return new NewMatrixParameter(null, data, dim, dim); |
| 151 | // } |
| 152 | // |
| 153 | // // ************************************************************** |
| 154 | // // XMLElement IMPLEMENTATION |
| 155 | // // ************************************************************** |
| 156 | // |
| 157 | //// public Element createElement(Document d) { |
| 158 | //// throw new RuntimeException("Not implemented yet!"); |
| 159 | //// } |
| 160 | // |
| 161 | // public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 162 | // |
| 163 | // public String getParserName() { |
| 164 | // return MATRIX_PARAMETER; |
| 165 | // } |
| 166 | // |
| 167 | // public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 168 | // |
| 169 | //// MatrixParameter matrixParameter = new MatrixParameter(MATRIX_PARAMETER); |
| 170 | // |
| 171 | // int numRows = xo.getChildCount(); |
| 172 | // int numCols = 0; |
| 173 | // |
| 174 | // double[] values = null; |
| 175 | // |
| 176 | // for (int i = 0; i < numRows; i++) { |
| 177 | // Parameter parameter = (Parameter) xo.getChild(i); |
| 178 | // if (values == null) { |
| 179 | // numCols = parameter.getDimension(); |
| 180 | // values = new double[numCols * numRows]; |
| 181 | // } else { |
| 182 | // if (numCols != parameter.getDimension()) { |
| 183 | // throw new XMLParseException( |
| 184 | // "All parameters must have the same dimension to construct a rectangular matrix"); |
| 185 | // } |
| 186 | // } |
| 187 | // double[] newValues = parameter.getParameterValues(); |
| 188 | // System.arraycopy(newValues, 0, values, i * numCols, numCols); |
| 189 | // } |
| 190 | // |
| 191 | // String name = (xo.hasId() ? xo.getId() : MATRIX_PARAMETER); |
| 192 | // |
| 193 | // return new NewMatrixParameter(name, values, numRows, numCols); |
| 194 | // } |
| 195 | // |
| 196 | // //************************************************************************ |
| 197 | // // AbstractXMLObjectParser implementation |
| 198 | // //************************************************************************ |
| 199 | // |
| 200 | // public String getParserDescription() { |
| 201 | // return "A matrix parameter constructed from its component parameters."; |
| 202 | // } |
| 203 | // |
| 204 | // public XMLSyntaxRule[] getSyntaxRules() { |
| 205 | // return rules; |
| 206 | // } |
| 207 | // |
| 208 | // private final XMLSyntaxRule[] rules = { |
| 209 | // new ElementRule(Parameter.class, 1, Integer.MAX_VALUE), |
| 210 | // }; |
| 211 | // |
| 212 | // public Class getReturnType() { |
| 213 | // return NewMatrixParameter.class; |
| 214 | // } |
| 215 | // }; |
| 216 | // |
| 217 | // |
| 218 | //} |