# Calculate (analytically) derivative and integral of a polynomial in Java.

Most of the time, programmer’s life is miserable. You have to do things right, you have to do them soon, you cannot reinvent the wheel. This way most of the work is usually carried on with libraries taken somewhere, because it’s faster, because it is safer.

But still it is nice not to forget what is behind the libraries, if we don’t want to outsource our brain all the time.

From time to time I resist the temptation to look around for “what has been already done”
and I thought it was nice to have a simple java class to calculate the *analytical*
derivative of a given polynomial.

I define a Poly object that has a constructor like this:

```
Polynomial(double coeff[])
```

and the array is a list of the coefficients, from grade *0* to *n* . To be clear:

$$ a_0·x^0 + a_1·x^1 + a_2·x^2 $$

would be represented by such a vector of coefficients:

```
new double[] { 1, 2, 3 }
```

assuming:

$$ a_0 = 1 , a_1 =2 , a_2 = 3 $$

In other words, I want to be able to print something like:

```
var originalPoly = new Polynomial(new double[] { 1, 2, 3 });
System.out.println("Original poly:\t" + originalPoly);
System.out.println("Derivative:\t" + originalPoly.differentiate());
```

and have the resulting output:

```
Original poly: 1.0·x^0 + 2.0·x^1 + 3.0·x^2
Derivative: 2.0·x^0 + 6.0·x^1
```

Should I render it on TeX ?

Original poly: $$ 1.0·x^0 + 2.0·x^1 + 3.0·x^2 $$

Derivative: $$ 2.0·x^0 + 6.0·x^1 $$

If you are interested in the solution, the code is the following:

```
//Paolo Lulli 2019
public final class Polynomial {
private final int degree;
private final double coeff[];
public Polynomial(double coeff[]) {
this.coeff = coeff;
this.degree = coeff.length;
}
public int degree() {
return degree;
}
public double evaluate(double x) {
double result = 0;
for (int i = 0; i < degree; i++) {
result += coeff[i] * Math.pow(x, i);
}
return result;
}
public Polynomial differentiate() {
int derivativeDegree = degree - 1;
double derivativeCoeff[] = new double[derivativeDegree];
for (int i = 0; i < derivativeDegree; i++) {
derivativeCoeff[i] = coeff[i + 1] * (i + 1);
}
return new Polynomial(derivativeCoeff);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < degree; i++) {
if (i > 0) {
sb.append(" + ");
}
sb.append(coeff[i] + "·x^" + i);
}
return sb.toString();
}
}
```

Right that I was into it, it was worth calculating the integral itself, similar code. Below the method:

```
//Paolo Lulli 2019
public Polynomial integrate(double C) {
var integralDegree = degree + 1;
double integralCoeff[] = new double[integralDegree];
integralCoeff[0] = C;
for (int i = 2; i <= integralDegree; i++) {
integralCoeff[i - 1] = coeff[i - 2] / (i - 1);
}
return new Polynomial(integralCoeff);
}
```

## Why Java ?

Of course, you would ask, it’s so damn handier to have it in python or … But that’s the point, because people doesn’t use Java much for this kind of things, then it gets worse… I wanted something I’ll be grateful myself I have implemented. And. Is everybody happy scaling up Python in the ML world ? I mean, it’s easy to code some math, but when you need to retrieve the data from some corporate database or code something that you don’t want the customer to type in a shell ‘pip install’ this, ‘easy_install that’ , start virtual, start a docker so that you don’t screw your other that…

But it’s maybe just a feeling.

#### Related

#### Compile and use Apache Cassandra with Jdk 11

##### Apache Cassandra 4 and Java 11

`java`

`jdk11`

`Cassandra`

`databases`