/**
* Copyright (C) 2014 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.analytics.math.function;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;
import com.opengamma.analytics.math.matrix.DoubleMatrix1D;
import com.opengamma.analytics.util.AssertMatrix;
import com.opengamma.util.test.TestGroup;
/**
* Construct a curve a + b*x + c*x^2 (where a, b, and c are the parameters), then make some VectorFunctions
* that sample the curve at some values of x, thus providing a mapping from the model parameters to the curve
* value at the sample positions.
*/
@Test(groups = TestGroup.UNIT)
public class DoublesVectorFunctionProviderTest {
@Test
public void test() {
final ParameterizedCurve curve = new ParameterizedCurve() {
@Override
public Double evaluate(final Double x, final DoubleMatrix1D parameters) {
return parameters.getEntry(0) + parameters.getEntry(1) * x + parameters.getEntry(2) * x * x;
}
@Override
public int getNumberOfParameters() {
return 3;
}
};
final DoublesVectorFunctionProvider pro = new DoublesVectorFunctionProvider() {
@Override
public VectorFunction from(final double[] x) {
final ParameterizedCurveVectorFunction vf = new ParameterizedCurveVectorFunction(x, curve);
return vf;
}
};
//a = -2, b = 1, c = 0.5
final DoubleMatrix1D parms = new DoubleMatrix1D(-2.0, 1.0, 0.5);
//sample the curve at x = -1, 0, and 1
VectorFunction f = pro.from(new Double[] {-1.0, 0.0, 1.0 });
DoubleMatrix1D y = f.evaluate(parms);
AssertMatrix.assertEqualsVectors(new DoubleMatrix1D(-2.5, -2.0, -0.5), y, 1e-15);
final List<Double> l = new ArrayList<>(3);
l.add(0.0);
l.add(2.0);
l.add(4.0);
f = pro.from(l);
y = f.evaluate(parms);
AssertMatrix.assertEqualsVectors(new DoubleMatrix1D(-2.0, 2.0, 10.0), y, 1e-15);
}
}