view src/samer/mds/CovarianceTask.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
package samer.mds;

// import samer.core.*;
import samer.maths.*;
import samer.tools.*;

/**
	Transfer covariance matrix to MDS distances
	Assumes that elements are not normalised to unit variance
  */
public class CovarianceTask extends AnonymousTask
{
	int		N;
	double [] d;				// linear array of distances
	double [][] _C;			// matrix of covariances
	double [] var;			// array of variances
		
	/** link each object to all the others using distances in matrix, returns a task
		that can be used to refresh distances from original matrix */
	public CovarianceTask(MDSBase mds, Matrix C) {
		N = C.getRowDimension();
		
		d=new double[N*(N - 1)/2];
		var=new double[N];
		_C=C.getArray();
		
		mds.clearLinks(d);
		for (int k=0, i=0; i<N; i++) 
			for (int j=0; j<i; j++) mds.setLink(k++,i,j);
		
		run();
	}

	public Vec getDistances() { return new Vec.ForArray(d); }
	public void run() {
		for (int i=0; i<N; i++) var[i]=_C[i][i];
		for (int k=0, i=0; i<N; i++) {
			double [] Ci=_C[i];
			double vari=var[i];
			for (int j=0; j<i; j++)
				d[k++]=Math.sqrt(0.5*Math.log(vari*var[j]/(Ci[j]*Ci[j])));
		}
	}
}