Mercurial > hg > jslab
annotate src/samer/mds/CovarianceTask.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
rev | line source |
---|---|
samer@0 | 1 package samer.mds; |
samer@0 | 2 |
samer@0 | 3 // import samer.core.*; |
samer@0 | 4 import samer.maths.*; |
samer@0 | 5 import samer.tools.*; |
samer@0 | 6 |
samer@0 | 7 /** |
samer@0 | 8 Transfer covariance matrix to MDS distances |
samer@0 | 9 Assumes that elements are not normalised to unit variance |
samer@0 | 10 */ |
samer@0 | 11 public class CovarianceTask extends AnonymousTask |
samer@0 | 12 { |
samer@0 | 13 int N; |
samer@0 | 14 double [] d; // linear array of distances |
samer@0 | 15 double [][] _C; // matrix of covariances |
samer@0 | 16 double [] var; // array of variances |
samer@0 | 17 |
samer@0 | 18 /** link each object to all the others using distances in matrix, returns a task |
samer@0 | 19 that can be used to refresh distances from original matrix */ |
samer@0 | 20 public CovarianceTask(MDSBase mds, Matrix C) { |
samer@0 | 21 N = C.getRowDimension(); |
samer@0 | 22 |
samer@0 | 23 d=new double[N*(N - 1)/2]; |
samer@0 | 24 var=new double[N]; |
samer@0 | 25 _C=C.getArray(); |
samer@0 | 26 |
samer@0 | 27 mds.clearLinks(d); |
samer@0 | 28 for (int k=0, i=0; i<N; i++) |
samer@0 | 29 for (int j=0; j<i; j++) mds.setLink(k++,i,j); |
samer@0 | 30 |
samer@0 | 31 run(); |
samer@0 | 32 } |
samer@0 | 33 |
samer@0 | 34 public Vec getDistances() { return new Vec.ForArray(d); } |
samer@0 | 35 public void run() { |
samer@0 | 36 for (int i=0; i<N; i++) var[i]=_C[i][i]; |
samer@0 | 37 for (int k=0, i=0; i<N; i++) { |
samer@0 | 38 double [] Ci=_C[i]; |
samer@0 | 39 double vari=var[i]; |
samer@0 | 40 for (int j=0; j<i; j++) |
samer@0 | 41 d[k++]=Math.sqrt(0.5*Math.log(vari*var[j]/(Ci[j]*Ci[j]))); |
samer@0 | 42 } |
samer@0 | 43 } |
samer@0 | 44 } |