001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022
023package org.biojava.stats.svm;
024
025import java.util.Iterator;
026import java.util.List;
027
028/**
029 * This kernel computes the sum of the dot products between items of two lists
030 * at corresponding indexes. We define k(x, null) to be zero for when list
031 * elements are null.
032 *
033 * @author Matthew Pocock
034 */
035public class ListSumKernel extends NestedKernel {
036    public double evaluate(Object a, Object b) {
037      List l1 = (List) a;
038      List l2 = (List) b;
039      
040      double dot = 0.0;
041      SVMKernel k = getNestedKernel();
042      
043      Iterator i1 = l1.iterator();
044      Iterator i2 = l2.iterator();
045      
046      while(i1.hasNext() && i2.hasNext()) {
047        Object o1 = i1.next();
048        Object o2 = i2.next();
049        if(o1 != null && o2 != null) {
050          dot += k.evaluate(i1.next(), i2.next());
051        }
052      }
053      
054      return dot;
055    }
056
057    public String toString() {
058      return "List kernel K(x, y, k) = sum_i(k(x_i, y_i))"
059        + "; k = " + getNestedKernel().toString();
060    }
061}