| 1 | /* |
| 2 | * BlockDiagonalMatrixParameter.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.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | //import java.util.StringTokenizer; |
| 34 | |
| 35 | /** |
| 36 | * @author Marc Suchard |
| 37 | */ |
| 38 | public class BlockDiagonalMatrixParameter extends MatrixParameter { |
| 39 | |
| 40 | public final static String BLOCK_DIAGONAL_MATRIX_PARAMETER = "blockDiagonalMatrixParameter"; |
| 41 | |
| 42 | private final List<MatrixParameter> parameterList; |
| 43 | |
| 44 | public BlockDiagonalMatrixParameter(String name) { |
| 45 | super(name); |
| 46 | parameterList = new ArrayList<MatrixParameter>(); |
| 47 | rowOffset = new ArrayList<Integer>(); |
| 48 | colOffset = new ArrayList<Integer>(); |
| 49 | } |
| 50 | |
| 51 | private final List<Integer> rowOffset; |
| 52 | private final List<Integer> colOffset; |
| 53 | |
| 54 | // public BlockDiagonalMatrixParameter(String name, Parameter[] parameters) { |
| 55 | // super(name, parameters); |
| 56 | // dimensionsEstablished = true; |
| 57 | // } |
| 58 | |
| 59 | public double getParameterValue(int row, int col) { |
| 60 | |
| 61 | return 0; // TODO |
| 62 | } |
| 63 | |
| 64 | // public double[][] getParameterAsMatrix() { |
| 65 | // final int I = getRowDimension(); |
| 66 | // final int J = getColumnDimension(); |
| 67 | // double[][] parameterAsMatrix = new double[I][J]; |
| 68 | // for (int i = 0; i < I; i++) { |
| 69 | // for (int j = 0; j < J; j++) |
| 70 | // parameterAsMatrix[i][j] = getParameterValue(i, j); |
| 71 | // } |
| 72 | // return parameterAsMatrix; |
| 73 | // } |
| 74 | // |
| 75 | // public void setColumnDimension(int columnDimension) { |
| 76 | // if (dimensionsEstablished) { |
| 77 | // throw new IllegalArgumentException("Attempt to change dimensions after initialization"); |
| 78 | // } |
| 79 | // this.columnDimension = columnDimension; |
| 80 | // setupParameters(); |
| 81 | // } |
| 82 | // |
| 83 | // public void setRowDimension(int rowDimension) { |
| 84 | // if (dimensionsEstablished) { |
| 85 | // throw new IllegalArgumentException("Attempt to change dimensions after initialization"); |
| 86 | // } |
| 87 | // this.rowDimension = rowDimension; |
| 88 | // setupParameters(); |
| 89 | // } |
| 90 | // |
| 91 | // private void setupParameters() { |
| 92 | // if (columnDimension > 0 && rowDimension > 0) { |
| 93 | // dimensionsEstablished = true; |
| 94 | // |
| 95 | // for (int i = 0; i < rowDimension; i++) { |
| 96 | // Parameter row = new Parameter.Default(columnDimension, 0.0); |
| 97 | // row.addBounds(new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, columnDimension)); |
| 98 | // addParameter(row); |
| 99 | // } |
| 100 | // } |
| 101 | // } |
| 102 | |
| 103 | public int getColumnDimension() { |
| 104 | return columnDimension; |
| 105 | } |
| 106 | |
| 107 | public int getRowDimension() { |
| 108 | return rowDimension; |
| 109 | } |
| 110 | |
| 111 | // public String toSymmetricString() { |
| 112 | // StringBuilder sb = new StringBuilder("{"); |
| 113 | // int dim = getRowDimension(); |
| 114 | // int total = dim * (dim + 1) / 2; |
| 115 | // for (int i = 0; i < dim; i++) { |
| 116 | // for (int j = i; j < dim; j++) { |
| 117 | // sb.append(String.format("%5.4e", getParameterValue(i, j))); |
| 118 | // total--; |
| 119 | // if (total > 0) |
| 120 | // sb.append(","); |
| 121 | // } |
| 122 | // } |
| 123 | // sb.append("}"); |
| 124 | // return sb.toString(); |
| 125 | // } |
| 126 | |
| 127 | // public static BlockDiagonalMatrixParameter parseFromSymmetricString(String string) { |
| 128 | // String clip = string.replace("{", "").replace("}", "").trim(); |
| 129 | // StringTokenizer st = new StringTokenizer(clip, ","); |
| 130 | // int count = st.countTokens(); |
| 131 | // int dim = (-1 + (int) Math.sqrt(1 + 8 * count)) / 2; |
| 132 | // Parameter[] parameter = new Parameter[dim]; |
| 133 | // for (int i = 0; i < dim; i++) |
| 134 | // parameter[i] = new Parameter.Default(dim); |
| 135 | // for (int i = 0; i < dim; i++) { |
| 136 | // for (int j = i; j < dim; j++) { |
| 137 | // double datum = new Double(st.nextToken()); |
| 138 | // parameter[i].setParameterValue(j, datum); |
| 139 | // parameter[j].setParameterValue(i, datum); |
| 140 | // } |
| 141 | // } |
| 142 | // return new BlockDiagonalMatrixParameter(null, parameter); |
| 143 | // } |
| 144 | // |
| 145 | // public static BlockDiagonalMatrixParameter parseFromSymmetricDoubleArray(Object[] data) { |
| 146 | // |
| 147 | // int dim = (-1 + (int) Math.sqrt(1 + 8 * data.length)) / 2; |
| 148 | // Parameter[] parameter = new Parameter[dim]; |
| 149 | // for (int i = 0; i < dim; i++) |
| 150 | // parameter[i] = new Parameter.Default(dim); |
| 151 | // int index = 0; |
| 152 | // for (int i = 0; i < dim; i++) { |
| 153 | // for (int j = i; j < dim; j++) { |
| 154 | // double datum = (Double) data[index++]; |
| 155 | // parameter[i].setParameterValue(j, datum); |
| 156 | // parameter[j].setParameterValue(i, datum); |
| 157 | // } |
| 158 | // } |
| 159 | // return new BlockDiagonalMatrixParameter(null, parameter); |
| 160 | // } |
| 161 | |
| 162 | // private boolean dimensionsEstablished = false; |
| 163 | private int columnDimension = 0; |
| 164 | private int rowDimension = 0; |
| 165 | |
| 166 | |
| 167 | public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 168 | |
| 169 | public String getParserName() { |
| 170 | return BLOCK_DIAGONAL_MATRIX_PARAMETER; |
| 171 | } |
| 172 | |
| 173 | public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 174 | |
| 175 | final String name = xo.hasId() ? xo.getId() : null; |
| 176 | |
| 177 | BlockDiagonalMatrixParameter matrixParameter |
| 178 | = new BlockDiagonalMatrixParameter(name); |
| 179 | |
| 180 | for (int i = 0; i < xo.getChildCount(); i++) { |
| 181 | MatrixParameter parameter = (MatrixParameter) xo.getChild(i); |
| 182 | matrixParameter.addParameter(parameter); // TODO Double-check |
| 183 | } |
| 184 | |
| 185 | return matrixParameter; |
| 186 | } |
| 187 | |
| 188 | //************************************************************************ |
| 189 | // AbstractXMLObjectParser implementation |
| 190 | //************************************************************************ |
| 191 | |
| 192 | public String getParserDescription() { |
| 193 | return "A matrix parameter constructed from its component parameters."; |
| 194 | } |
| 195 | |
| 196 | public XMLSyntaxRule[] getSyntaxRules() { |
| 197 | return rules; |
| 198 | } |
| 199 | |
| 200 | private final XMLSyntaxRule[] rules = { |
| 201 | new ElementRule(MatrixParameter.class, 0, Integer.MAX_VALUE), |
| 202 | }; |
| 203 | |
| 204 | public Class getReturnType() { |
| 205 | return BlockDiagonalMatrixParameter.class; |
| 206 | } |
| 207 | }; |
| 208 | } |