/*
 * MassivelyParallelMDSImpl.java
 *
 * Copyright (c) 2002-2014 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.app.beagle.multidimensionalscaling;

/**
 * MassivelyParallelMDSImpl
 *
 * @author Marc Suchard
 * @author Andrew Rambaut
 * @version $Id$
 *          <p/>
 *          $HeadURL$
 *          <p/>
 *          $LastChangedBy$
 *          $LastChangedDate$
 *          $LastChangedRevision$
 */
public class MassivelyParallelMDSImpl implements MultiDimensionalScalingCore {

    private NativeMDSSingleton singleton = null;
    private int instance = -1; // Get instance # via initialization
    private final long flags = 0;

    public MassivelyParallelMDSImpl() {
        singleton = NativeMDSSingleton.loadLibrary();
    }

    @Override
    public void initialize(int embeddingDimension, int locationCount) {
        instance = singleton.initialize(embeddingDimension, locationCount, flags);
    }

    @Override
    public void setPairwiseData(double[] observations) {
        singleton.setPairwiseData(instance, observations);
    }

    @Override
    public void setParameters(double[] parameters) {
        singleton.setParameters(instance, parameters);
    }

    @Override
    public void updateLocation(int locationIndex, double[] location) {
        singleton.updateLocations(instance, locationIndex, location);
    }

    @Override
    public double calculateLogLikelihood() {
        return singleton.calculateLogLikelihood(instance);
    }

    @Override
    public void storeState() {
        singleton.storeState(instance);
    }

    @Override
    public void restoreState() {
        singleton.restoreState(instance);
    }

    @Override
    public void makeDirty() {
        singleton.makeDirty(instance);
    }

}

@@ -1796,30 +1796,105 @@
 nt, flags);%0A
+        this.observationCount = (locationCount * (locationCount - 1)) / 2;%0A
     %7D%0A
-
 %0A    @Overri
@@ -2082,24 +2082,61 @@
 rameters) %7B%0A
+        precision = parameters%5B0%5D;%0A//
         sing
@@ -2177,16 +2177,33 @@
 meters);
+ // Not necessary
 %0A    %7D%0A%0A
@@ -2435,14 +2435,38 @@
     
-return
+double sumOfSquaredResiduals =
  sin
@@ -2497,32 +2497,184 @@
 ihood(instance);
+  // Really just returns SSR%0A%0A        return (0.5 * Math.log(precision) * observationCount) -%0A                (0.5 * precision * sumOfSquaredResiduals);
 %0A    %7D%0A%0A    @Ove
@@ -2742,32 +2742,69 @@
 tate(instance);%0A
+        storedPrecision = precision;%0A
     %7D%0A%0A    @Over
@@ -2879,24 +2879,61 @@
 (instance);%0A
+        precision = storedPrecision;%0A
     %7D%0A%0A    @
@@ -3010,17 +3010,118 @@
 tance);%0A
-
     %7D%0A%0A
+    private int observationCount;%0A    private double precision;%0A    private double storedPrecision;%0A%0A
 %7D%0A

Move Math.log(double) from C++ back to Java to match bit-stream in MDS