package net.sf.kerner.utils.math;

import java.util.ArrayList;
import java.util.List;

public class Polynomial {

	private final List<Double> elements;

	public Polynomial(int size) {
		elements = new ArrayList<Double>(size);
	}

	public Polynomial() {
		this(10);
	}
	
	public synchronized double calculate(double y){
		double result = 0;
		for (int x = elements.size() - 1; x > -1; x--) {
			double d = elements.get(x);
//			System.out.println("d="+d + ",x="+x);
			if(x > 0)
			result += (d * Math.pow(y, x));
			else
				result += d;
//			System.out.println("result="+result);
		}
		return result;
	}

	public synchronized void add(int pos, double element) {
		elements.add(pos, element);
	}

	public synchronized Polynomial getDerivate() {
		final Polynomial result = new Polynomial();
		for (int i = 1; i < elements.size(); i++) {
			double element = elements.get(i);
			result.add(i-1, element * i);
		}
		return result;
	}
	
	public synchronized double[] getPolynoms(){
		final double[] result = new double[elements.size()];
		for(int i=0;i<elements.size(); i++){
			result[i] = elements.get(i);
		}
		return result;
	}

	@Override
	public synchronized String toString() {
		final StringBuilder sb = new StringBuilder();
		for (int x = elements.size() - 1; x > -1; x--) {
			sb.append(String.format("%10.6f", elements.get(x)) + (x > 0 ? "x^" + x + " + " : ""));
		}
		return sb.toString();
	}

}

@@ -922,24 +922,281 @@
 esult;%0A%09%7D%0A%09%0A
+%09public synchronized Polynomial getDerivate(int nr)%7B%0A%09%09if(nr %3C 1)%7B%0A%09%09%09throw new IllegalArgumentException(%22nr must be %3E= 1%22);%0A%09%09%7D%0A%09%09Polynomial result = getDerivate();%0A%09%09for(int i = 1; i %3C nr; i++)%7B%0A%09%09%09result = result.getDerivate();%0A%09%09%7D%0A%09%09return result;%0A%09%7D%0A%09%0A
 %09public sync
@@ -1563,12 +1563,13 @@
 t(%22%25
-10.6
+22.20
 f%22, 
@@ -1608,19 +1608,17 @@
  + x + %22
- + 
++
 %22 : %22%22))
@@ -1645,16 +1645,38 @@
 String()
+.replaceAll(%22%5C%5Cs%22, %22%22)
 ;%0A%09%7D%0A%0A%7D%0A

Introduce Polynomial#getDerivate(int);