maths.functions.LinearVectorPolynomial Maven / Gradle / Ivy
package maths.functions;
import datasets.VectorDouble;
import datastructs.IVector;
import maths.VectorOperations;
/**
* class that represents a linear polynomial of the form
* f = w0 + w1*X1 + w2*X2+...worder-1*Xorder-1
*/
public class LinearVectorPolynomial implements IVectorRealFunction> {
/**
* Construct a vector polynomial: f = w0 + w1*X1 + w2*X2+...worder-1*Xorder-1
*/
public LinearVectorPolynomial(int order){
// we also need the constant coefficient
this.coeffs = new VectorDouble(order + 1, 0.0);
}
public Double evaluate(VectorDouble input){
return VectorOperations.dotProduct(this.coeffs, input);
}
@Override
public Double evaluate(IVector input){
return this.evaluate((VectorDouble) input);
}
/**
* Set the coefficients of the Polynomial
*/
public final void setCoeffs(VectorDouble coeffs){
this.coeffs = coeffs;
}
/**
* Set the coefficients of the function
*/
@Override
public final void setCoeffs(Double[] coeffs){
this.coeffs.set(coeffs);
}
/**
* Set the coefficients of the function
*/
@Override
public void setCoeffs(double[] coeffs){
this.coeffs.set(coeffs);
}
/**
* Returns the coefficients of the vector function
*/
@Override
public final VectorDouble getCoeffs(){
return this.coeffs;
}
/**
* Returns the number of coefficients
*/
@Override
public final int numCoeffs(){
return this.coeffs.size();
}
/**
* Returns the gradients with respect to the coefficients at the given data point
*/
@Override
public VectorDouble gradidents(IVector data){
VectorDouble rslt = new VectorDouble(data);
rslt.set(0, 1.0);
return rslt;
}
/**
* Returns the gradient with respect to the i-th coeff
*/
@Override
public double gradient(int i, IVector data){
if(i==0){
return 0.0;
}
//this is a linear model with respect to
//the weights so simply return the value of the feature
//for the i-th weight
return this.coeffs.get(i);
}
/**
* Returns the gradient with respect to the i-th coeff
*/
@Override
public double coeffGradient(int i, IVector data){
if(i==0){
return 1.0;
}
//this is a linear model with respect to
//the weights so simply return the value of the feature
//for the i-th weight
return data.get(i);
}
/**
* Compute the gradients with respect to the coefficients
*/
@Override
public VectorDouble coeffGradients(IVector data){
VectorDouble grads = new VectorDouble(this.coeffs.size(), 0.0);
for (int i = 0; i < grads.size(); i++) {
grads.set(i, this.coeffGradient(i, data));
}
return grads;
}
/**
* Returns the coeff-th coefficient
*/
@Override
public double getCoeff(int coeff){
return this.coeffs.get(coeff);
}
/**
* The coefficients of the vector
*/
private VectorDouble coeffs;
}