Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Created April 11, 2017 19:42
Show Gist options
  • Save charlesreid1/70161e5bff6e688b3d968492c453d295 to your computer and use it in GitHub Desktop.
Save charlesreid1/70161e5bff6e688b3d968492c453d295 to your computer and use it in GitHub Desktop.
public class Polynomial {
public Polynomial add(Polynomial p) {
int degp = p.coeffs.length;
int degthis = this.coeffs.length;
int mindegree = Math.min(degp, degthis);
int maxdegree = Math.max(degp, degthis);
// Part I
double[] newcoeffs = new double[maxdegree];
for(int i = 0; i<mindegree; i++) {
newcoeffs[i] = p.coeffs[i]+this.coeffs[i];
}
// Part II
if( degp == maxdegree ) {
// Polynomial parameter is the larger of the two
for( int i = mindegree; i<maxdegree; i++) {
newcoeffs[i] = p.coeffs[i];
}
} else if( degthis==max ) {
// This Polynomial is the larger of the two
for( int i = mindegree; i<maxdegree; i++) {
newcoeffs[i] = this.coeffs[i];
}
}
Polynomial result = new Polynomial(newcoeffs);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment