List of usage examples for org.apache.commons.math.analysis PolynomialFunction PolynomialFunction
public PolynomialFunction(double c[])
From source file:com.golemgame.util.LinearUtils.java
public static PolynomialFunction generateTranslatedFunction(double[] coefficients, double translation) { if (coefficients.length == 1 || translation == 0) { return new PolynomialFunction(coefficients); }// www . jav a 2s. com translation = -translation; for (int power = 1; power < coefficients.length; power++) { if (coefficients[power] != 0) { double[] expandedCoefficients = getBinomialTerms(power, 1, translation); for (int i = 0; i < power; i++) { expandedCoefficients[i] *= coefficients[power]; coefficients[i] += expandedCoefficients[i] * Math.pow(translation, power - i); } } } return new PolynomialFunction(coefficients); }
From source file:com.golemgame.functional.functions.PolynomialFunctionView.java
@Override public UnivariateRealFunction getFunction() { CollectionType coefficients = interpreter.getCoefficients(); int i = 0;//from w w w . j a va 2 s . c o m double[] co = new double[coefficients.getValues().size()]; for (DataType data : coefficients.getValues()) { if (data.getType() == DataType.Type.DOUBLE) { co[i] = ((DoubleType) data).getValue(); } i++; } if (co.length <= 0) co = new double[] { 0 }; return new PolynomialFunction(co); }
From source file:com.golemgame.properties.fengGUI.FunctionTab.java
private void buildFunctions() { dropDown.getList().clear();//from www .j av a 2s. co m //float functionWidth = 1;//pane.getMaxX()-pane.getMinX(); //float functionHeight = 2;///pane.getMaxY()-pane.getMinY(); ListItem<PropertyStore> line; ListItem<PropertyStore> flat; ListItem<PropertyStore> on; ListItem<PropertyStore> off; ListItem<PropertyStore> parabola; ListItem<PropertyStore> cubic; ListItem<PropertyStore> sin; ListItem<PropertyStore> cos; ListItem<PropertyStore> sqrt; // Collection<PropertyStore> functions = new ArrayList<PropertyStore>(); // if(zeroBased) { line = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_LINE", "Line"), buildStandardFunction(new PolynomialFunction(new double[] { 0, 1 }), "Line", zeroBased)); ///FunctionFactory.invertView(new PropertyStore(), ); ); flat = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_ZERO", "Zero"), buildStandardFunction(new PolynomialFunction(new double[] { 0 }), "Zero", zeroBased)); ///FunctionFactory.invertView(new PropertyStore(), ); ); on = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_100", "100%"), buildStandardFunction(new PolynomialFunction(new double[] { 1 }), "100%", zeroBased)); ///FunctionFactory.invertView(new PropertyStore(), ); ); off = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_NEG_100", "-100%"), buildStandardFunction(new PolynomialFunction(new double[] { -1 }), "-100%", zeroBased)); //FunctionFactory.invertView(new PropertyStore(), ); ); parabola = new ListItem<PropertyStore>( StringConstants.get("PROPERTIES.FUNCTION.FUN_PARABOLA", "Parabolic"), buildStandardFunction( new PolynomialFunction(new double[] { 0, 0, 1 }), "Parabolic", zeroBased)); ///FunctionFactory.invertView(new PropertyStore(), ); ); cubic = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_CUBIC", "Cubic"), buildStandardFunction(new PolynomialFunction(new double[] { 0, 0, 0, 1 }), "Cubic", zeroBased)); ///FunctionFactory.invertView(new PropertyStore(), ); ); sin = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_SINE", "Sine"), buildStandardFunction(new SinFunction(1f, 1f / (2f * FastMath.PI), 0), "Sine", false)); ///FunctionFactory.invertView(new PropertyStore(), ); ); cos = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_COS", "Cosine"), buildStandardFunction(new SinFunction(1f, 1f / (2f * FastMath.PI), FastMath.HALF_PI), "Cosine", false)); ///FunctionFactory.invertView(new PropertyStore(), ); ); sqrt = new ListItem<PropertyStore>(StringConstants.get("PROPERTIES.FUNCTION.FUN_SQRT", "Square Root"), buildStandardFunction(new SqrtFunction(), "Square Root", false)); ///FunctionFactory.invertView(new PropertyStore(), ); ); /* flat = new ListItem<PropertyStore>("Zero", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{0}) )); on = new ListItem<PropertyStore>("100%", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{functionHeight}) )); off = new ListItem<PropertyStore>("-100%", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{-functionHeight})) ); parabola = new ListItem<PropertyStore>("Parabolic", FunctionFactory.invertView(new PropertyStore(), LinearUtils.generateTranslatedFunction(new double[]{0,0,functionHeight*2f/functionWidth},functionWidth/2f))); cubic = new ListItem<PropertyStore>("Cubic", FunctionFactory.invertView(new PropertyStore(), LinearUtils.generateTranslatedFunction(new double[]{0,0,0,functionHeight*4f/functionWidth},functionWidth/2f))); sin = new ListItem<PropertyStore>("Sine", FunctionFactory.invertView(new PropertyStore(), new SinFunction(functionHeight/2f, functionWidth/(2f*FastMath.PI),0)) ); cos = new ListItem<PropertyStore>("Cosine", FunctionFactory.invertView(new PropertyStore(), new SinFunction(functionHeight/2f, functionWidth/(2f*FastMath.PI),FastMath.HALF_PI))); */ } //else { /* line = new ListItem<PropertyStore>("Line", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{0,1})) ); flat = new ListItem<PropertyStore>("Zero", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{0}) )); on = new ListItem<PropertyStore>("100%", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{functionHeight})) ); off = new ListItem<PropertyStore>("-100%", FunctionFactory.invertView(new PropertyStore(), new PolynomialFunction(new double[]{-functionHeight}) )); parabola = new ListItem<PropertyStore>("Parabolic", FunctionFactory.invertView(new PropertyStore(), LinearUtils.generateTranslatedFunction(new double[]{0,0,functionHeight*0.5f/functionWidth},0))); cubic = new ListItem<PropertyStore>("Cubic", FunctionFactory.invertView(new PropertyStore(), LinearUtils.generateTranslatedFunction(new double[]{0,0,0,functionHeight*0.5f/functionWidth},0))); sin = new ListItem<PropertyStore>("Sine", FunctionFactory.invertView(new PropertyStore(), new SinFunction(functionHeight/2f, functionWidth/(FastMath.PI),0)) ); cos = new ListItem<PropertyStore>("Cosine", FunctionFactory.invertView(new PropertyStore(), new SinFunction(functionHeight/2f, functionWidth/(FastMath.PI),FastMath.HALF_PI) )); */ } dropDown.addItem(line); dropDown.addItem(on); dropDown.addItem(flat); dropDown.addItem(off); dropDown.addItem(parabola); dropDown.addItem(cubic); dropDown.addItem(sin); dropDown.addItem(cos); dropDown.addItem(sqrt); if (!zeroBased) { showCalcFunctions(); } for (FunctionSettingsInterpreter function : StateManager.getMachineSpace().getFunctionRepository() .getFunctions()) { ListItem<PropertyStore> f = new ListItem<PropertyStore>(function.getFunctionName(), function.getStore()); dropDown.addItem(f); } }
From source file:org.jcurl.core.math.FunctionBridgeTest.java
public void testPoly() throws org.apache.commons.math.FunctionEvaluationException, MathException { final PolynomialFunction p0 = new PolynomialFunction(new double[] { 1, 0.1, 0.01, 0.001 }); final Polynome p1 = new PolynomeImpl(p0.getCoefficients()); final FunctionBridge b0 = FunctionBridge.newInstance(p0); final FunctionBridge b1 = FunctionBridge.newInstance(p1); for (double x = -100; x < 100; x += 0.9) { assertEquals(b0.at(x), b0.value(x), 1e-9); assertEquals(b1.at(x), b1.value(x), 1e-9); assertEquals(b0.at(x), b1.at(x), 1e-9); for (int i = 0; i < 100; i++) { assertEquals(b0.at(x, i), b1.at(x, i), 1e-9); }/*from w ww . j av a2 s. c o m*/ } }
From source file:org.jcurl.math.analysis.PolynomeTest.java
public void testCompare() throws FunctionEvaluationException { final double[] coeff = { 1.1, 1.2, 1.3 }; final double[] points = { -1.1, -1, 0, 1, 1.1, 100 }; final PolynomialFunction pa = new PolynomialFunction(coeff); final Polynome pj = new Polynome(coeff); for (int i = points.length - 1; i >= 0; i--) assertEquals("", pa.value(points[i]), pj.value(points[i]), 1e-11); final PolynomialFunction pa1 = pa.polynomialDerivative(); for (int i = points.length - 1; i >= 0; i--) assertEquals("", pa1.value(points[i]), pj.getC(1, points[i]), 1e-11); final PolynomialFunction pa2 = pa1.polynomialDerivative(); for (int i = points.length - 1; i >= 0; i--) assertEquals("", pa2.value(points[i]), pj.getC(2, points[i]), 1e-11); final PolynomialFunction pj1 = pj.polynomialDerivative(); for (int i = points.length - 1; i >= 0; i--) assertEquals("", pj1.value(points[i]), pj.getC(1, points[i]), 1e-11); final PolynomialFunction pj2 = pj1.polynomialDerivative(); for (int i = points.length - 1; i >= 0; i--) assertEquals("", pj2.value(points[i]), pj.getC(2, points[i]), 1e-11); }
From source file:org.jcurl.math.FunctionBridgeTest.java
public void testPoly() throws org.apache.commons.math.FunctionEvaluationException, MathException { final PolynomialFunction p0 = new PolynomialFunction(new double[] { 1, 0.1, 0.01, 0.001 }); final Polynome p1 = new Polynome(p0.getCoefficients()); final FunctionBridge b0 = FunctionBridge.newInstance(p0); final FunctionBridge b1 = FunctionBridge.newInstance(p1); for (double x = -100; x < 100; x += 0.9) { assertEquals(b0.at(x), b0.value(x), 1e-9); assertEquals(b1.at(x), b1.value(x), 1e-9); assertEquals(b0.at(x), b1.at(x), 1e-9); for (int i = 0; i < 100; i++) assertEquals(b0.at(i, x), b1.at(i, x), 1e-9); }/*w ww . ja v a2 s.co m*/ }