001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.linear.kernel;
031
032import java.util.List;
033
034import no.uib.cipr.matrix.DenseVector;
035import no.uib.cipr.matrix.Vector;
036
037import org.openimaj.math.matrix.GramSchmidtProcess;
038import org.openimaj.util.pair.IndependentPair;
039
040import cern.colt.Arrays;
041
042/**
043 *
044 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
045 */
046public class LinearVectorKernel implements VectorKernel{
047
048        @Override
049        public Double apply(IndependentPair<double[], double[]> in) {
050                double[] first = in.firstObject();
051                double[] second = in.secondObject();
052                return new DenseVector(first,false).dot(new DenseVector(second,false));
053        }
054
055        /**
056         * On the plane
057         * @param supports
058         * @param weights 
059         * @param bias
060         * @param setValues the dimentions of the point set to 0
061         * @return a point on the plane
062         */
063        public static double[] getPlanePoint(List<double[]> supports, List<Double> weights, double bias, double ... setValues) {
064                if(supports.size() == 0) throw new RuntimeException("Can't estimate plane point without supports");
065                
066                double[] w = getDirection(supports, weights);
067                double[] x = new double[w.length];
068                double resid = 0;
069                int index = 0;
070                for (int i = 0; i < w.length; i++) {
071                        
072                        if(Double.isNaN(setValues[i])){
073                                index = i;
074                        } else {
075                                resid += (setValues[i] * w[i]);
076                                x[i] = setValues[i];
077                        }
078                }
079                if(w[index] == 0) return new double[w.length];
080                x[index] = (bias + resid)/ -w[index];
081                return x;
082        }
083
084        /**
085         * @param supports
086         * @param weights 
087         * @return the vectors defining the plane
088         */
089        public static Vector[] getPlaneDirections(List<double[]> supports, List<Double> weights) {
090                double[] dir = getDirection(supports, weights);
091                int ind = 0;
092//              for (int i = 0; i < weights.size(); i++) {
093//                      double[] ds = supports.get(i);
094//                      System.out.println("Support " + ind++ + ": " + weights.get(i) + " * " +Arrays.toString(ds) );
095//              }
096//              System.out.println("Number of supports: " + supports.size() + " direction: " + Arrays.toString(dir));
097                Vector[] all = GramSchmidtProcess.perform(dir);
098                Vector[] ret = new Vector[all.length-1];
099                for (int i = 0; i < ret.length; i++) {
100                        ret[i] = all[i+1];
101                }
102                return ret;
103        }
104        
105
106        public static double[] getDirection(List<double[]> supports, List<Double> weights) {
107                Vector ret = null;
108                for (int i = 0; i < supports.size(); i++) {
109                        double[] sup = supports.get(i);
110                        double weight = weights.get(i);
111                        DenseVector scale = new DenseVector(sup).scale(weight);
112                        if(ret == null){
113                                ret = scale;
114                        } else{
115                                ret.add(scale);
116                        }
117                }
118                double[] retdata = new DenseVector(ret).getData();
119                return retdata;
120        }
121
122}