/*
 * CompoundSymmetricMatrix.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;

/**
 * @author Marc Suchard
 */
public class CompoundSymmetricMatrix extends MatrixParameter {

    private Parameter diagonalParameter;
    private Parameter offDiagonalParameter;

    private boolean asCorrelation = false;

    private int dim;

    public CompoundSymmetricMatrix(Parameter diagonals, Parameter offDiagonal, boolean asCorrelation) {
        super(MATRIX_PARAMETER);
        diagonalParameter = diagonals;
        offDiagonalParameter = offDiagonal;
        addParameter(diagonalParameter);
        addParameter(offDiagonal);
        dim = diagonalParameter.getDimension();
        this.asCorrelation = asCorrelation;
    }

    public double[] getAttributeValue() {
        double[] stats = new double[dim * dim];
        int index = 0;
        for (int i = 0; i < dim; i++) {
            for (int j = 0; j < dim; j++) {
                stats[index] = getParameterValue(i, j);
                index++;
            }
        }        
        return stats;
    }

    public double[] getDiagonals() {
        return diagonalParameter.getParameterValues();
    }

    public double getOffDiagonal() {
        return offDiagonalParameter.getParameterValue(0);
    }

    public double getParameterValue(int row, int col) {
        if (row != col) {
            if (asCorrelation) {
                return offDiagonalParameter.getParameterValue(0) *
                        Math.sqrt(diagonalParameter.getParameterValue(row) * diagonalParameter.getParameterValue(col));
            }
            return offDiagonalParameter.getParameterValue(0);
        }
        return diagonalParameter.getParameterValue(row);
    }

    public double[][] getParameterAsMatrix() {
        final int I = dim;
        double[][] parameterAsMatrix = new double[I][I];
        for (int i = 0; i < I; i++) {
            parameterAsMatrix[i][i] = getParameterValue(i, i);
            for (int j = i + 1; j < I; j++) {
                parameterAsMatrix[j][i] = parameterAsMatrix[i][j] = getParameterValue(i, j);
            }
        }
        return parameterAsMatrix;
    }

    public int getColumnDimension() {
        return diagonalParameter.getDimension();
    }

    public int getRowDimension() {
        return diagonalParameter.getDimension();
    }

}

@@ -1248,16 +1248,57 @@
 = false;
+%0A    private boolean asFisherCPC = false;
 %0A%0A    pr
@@ -2425,17 +2425,49 @@
 erValue(
-0
+getUpperTriangularIndex(row, col)
 ) *%0A    
@@ -2660,32 +2660,32 @@
 e(0);%0A        %7D%0A
-
         return d
@@ -2724,32 +2724,403 @@
 ue(row);%0A    %7D%0A%0A
+    private int getUpperTriangularIndex(int i, int j) %7B%0A        assert i != j;%0A        if (i %3C j) %7B%0A            return upperTriangularTransformation(i, j);%0A        %7D else %7B%0A            return upperTriangularTransformation(j, i);%0A        %7D%0A    %7D%0A%0A    private int upperTriangularTransformation(int i, int j) %7B%0A        return i * (2 * dim - i - 1) / 2 + (j - i - 1);%0A    %7D%0A%0A
     public doubl

Fix error in CompoundSymmetricMatrix.