Debian Codegraph @

src/dr/inference/multidimensionalscaling/MassivelyParallelMDSImpl.java

raw symbols SCIP
1 /*
2 * MassivelyParallelMDSImpl.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.multidimensionalscaling;
27
28 import dr.math.matrixAlgebra.Vector;
29
30 /**
31 * MassivelyParallelMDSImpl
32 *
33 * @author Marc Suchard
34 * @author Andrew Rambaut
35 * @version $Id$
36 * <p/>
37 * $HeadURL$
38 * <p/>
39 * $LastChangedBy$
40 * $LastChangedDate$
41 * $LastChangedRevision$
42 */
43 public class MassivelyParallelMDSImpl implements MultiDimensionalScalingCore {
44
45 private NativeMDSSingleton singleton = null;
46 private int instance = -1; // Get instance # via initialization
47
48 MassivelyParallelMDSImpl() {
49 singleton = NativeMDSSingleton.loadLibrary();
50 }
51
52 @Override
53 public void initialize(int embeddingDimension, int locationCount, long flags) {
54 instance = singleton.initialize(embeddingDimension, locationCount, flags);
55 this.observationCount = (locationCount * (locationCount - 1)) / 2;
56 }
57
58 @Override
59 public void setPairwiseData(double[] observations) {
60 singleton.setPairwiseData(instance, observations);
61 }
62
63 @Override
64 public void setParameters(double[] parameters) {
65 precision = parameters[0];
66 singleton.setParameters(instance, parameters); // Necessary for truncation
67 }
68
69 @Override
70 public double[] getPairwiseData() {
71 return singleton.getPairwiseData(instance);
72 }
73
74 @Override
75 public void updateLocation(int locationIndex, double[] location) {
76 singleton.updateLocations(instance, locationIndex, location);
77 }
78
79 @Override
80 public double calculateLogLikelihood() {
81 double sumOfIncrements = singleton.getSumOfIncrements(instance);
82
83 return 0.5 * (Math.log(precision) - Math.log(2 * Math.PI)) * observationCount - sumOfIncrements;
84 }
85
86 @Override
87 public void storeState() {
88 singleton.storeState(instance);
89 storedPrecision = precision;
90 }
91
92 @Override
93 public void restoreState() {
94 singleton.restoreState(instance);
95 precision = storedPrecision;
96 }
97
98 @Override
99 public void acceptState() {
100 singleton.acceptState(instance);
101 }
102
103 @Override
104 public void getGradient(double[] location) {
105 singleton.getLocationGradient(instance, location);
106
107 if (CHECK_GRADIENT) {
108 for (double x : location) {
109 if (Double.isNaN(x) || Double.isInfinite(x)) {
110 System.err.println("Poor gradient value: " + x);
111 System.err.println(new Vector(location));
112 if (CHECK_GRADIENT_KILL) {
113 System.exit(-1);
114 } else {
115 break;
116 }
117 }
118 }
119 }
120 }
121
122 @Override
123 public void makeDirty() {
124 singleton.makeDirty(instance);
125 }
126
127 private int observationCount;
128 private double precision;
129 private double storedPrecision;
130
131 private static final boolean CHECK_GRADIENT = false;
132 private static final boolean CHECK_GRADIENT_KILL = true;
133 }