public static Polynomial add ( Polynomial a , Polynomial b , Polynomial results ) { int N = Math . max ( a . size , b . size ) ; if ( results == null ) { results = new Polynomial ( N ) ; } else if ( results . size < N ) { throw new IllegalArgumentException ( "storage for results must be at least as large as the the largest polynomial" ) ; } else { for ( int i = N ; i < results . size ; i ++ ) { results . c [ i ] = 0 ; } } int M = Math . min ( a . size , b . size ) ; for ( int i = 0 ; i < M ; i ++ ) { results . c [ i ] = a . c [ i ] + b . c [ i ] ; } // copy the non-overlapping coefficients for the larger polynomial if ( a . size > b . size ) { for ( int i = b . size ; i < N ; i ++ ) results . [ i ] = . [ i ] ; } else { for ( int i = a . size ; i < N ; i ++ ) results . [ i ] = . [ i ] ; } return results ; }
Adds two polynomials together . The lengths of the polynomials do not need to be the zero but the missing coefficients are assumed to be zero .