Created
April 11, 2017 19:42
-
-
Save charlesreid1/70161e5bff6e688b3d968492c453d295 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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