/*
 * Representer.java
 * Created on Apr 11, 2005
 */
package kernel;

import javax.vecmath.GMatrix;
import javax.vecmath.GVector;
import javax.vecmath.MismatchedSizeException;

/**
 * A representer with a kernel, data matrix, and vector of coefficients.
 */
public final class Representer implements Function {
    
    private final Kernel kernel;
    private final GMatrix data;
    private final GVector coeffs;

    public Representer(Kernel kernel, GMatrix data, GVector coeffs) {
        if (data.getNumCol() != coeffs.getSize()) {
            throw new MismatchedSizeException();
        }
        this.kernel = kernel;
        this.data = new GMatrix(data);
        this.coeffs = new GVector(coeffs);
    }
    
    public GVector coeffs() {
        return coeffs;
    }
    
    public double eval(GVector x) {
        double sum = 0;
        for(int i = 0; i < coeffs.getSize(); i++) {
            GVector dataPoint = new GVector(data.getNumRow());
            data.getColumn(i, dataPoint);
            sum += coeffs.getElement(i) * kernel.eval(dataPoint, x);
        }
        return sum;
    }

}
