001    /*
002     * JNI_SVM-light - A Java Native Interface for SVM-light
003     * 
004     * Copyright (C) 2005 
005     * Tom Crecelius & Martin Theobald 
006     * Max-Planck Institute for Computer Science
007     * 
008     * This program is free software; you can redistribute it and/or modify it under
009     * the terms of the GNU General Public License as published by the Free Software
010     * Foundation.
011     * 
012     * This program is distributed in the hope that it will be useful, but WITHOUT
013     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014     * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
015     * details.
016     * 
017     * You should have received a copy of the GNU General Public License along with
018     * this program; if not, write to the Free Software Foundation, Inc., 51
019     * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020     */
021    
022    package jnisvmlight;
023    
024    /**
025     * A linear kernel. Computes the scalar (inner) product of two feature vectors
026     * and is the default inner kernel of all the other kernel implementations.
027     * 
028     * @author Tom Crecelius & Martin Theobald
029     */
030    public class LinearKernel extends Kernel {
031    
032      public double evaluate(FeatureVector v1, FeatureVector v2) {
033        int i = 0, j = 0;
034        double result = 0.0;
035        while (i < v1.size() && j < v2.size()) {
036          if (v1.getDimAt(i) > v2.getDimAt(j))
037            j++;
038          else if (v1.getDimAt(i) < v2.getDimAt(j))
039            i++;
040          else
041            result += (v1.getValueAt(i++) * v2.getValueAt(j++));
042        }
043        return result;
044      }
045    }