Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

CosineDist.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft;
00024 
00032 public class CosineDist extends ChunkDist 
00033 {
00034     public CosineDist() {}
00035     public CosineDist(ChunkDist o) { super(o); }
00036     public CosineDist(int[] i) { super(i); }
00037     public CosineDist(ChunkDist o, int[] i) { super(o,i); }
00038 
00039     public double distance(Chunk ch1, Chunk ch2) 
00040     {
00041         FeatChunk c1 = null;
00042         FeatChunk c2 = null; 
00043 
00044         // this only works on FeatChunks
00045         try
00046         {
00047             c1 = (FeatChunk)ch1;
00048             c2 = (FeatChunk)ch2;
00049         }
00050         catch(ClassCastException e)
00051         {
00052             return super.distance(ch1, ch2);
00053         }
00054 
00055         double[] f1 = c1.getFeatures(featdim);
00056         double[] f2 = c2.getFeatures(featdim);
00057         double dist = 0;
00058         double normf1 = 0;
00059         double normf2 = 0;
00060 
00061         for(int i = 0; i < f1.length; i++)
00062         {
00063             dist += f1[i]*f2[i];
00064             normf1 +=  f1[i]*f1[i];
00065             normf2 +=  f2[i]*f2[i];
00066         }
00067         
00068         dist = super.distance(ch1, ch2) + 1 - dist/Math.sqrt(normf1*normf2);
00069 
00070         return dist;
00071     }
00072 }

Generated on Tue Feb 6 19:02:25 2007 for MEAPsoft by doxygen1.2.18