{"id":26203,"date":"2017-10-26T20:25:37","date_gmt":"2017-10-26T14:55:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26203"},"modified":"2017-10-26T20:25:37","modified_gmt":"2017-10-26T14:55:37","slug":"c-programming-multiply-two-polynomials","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-multiply-two-polynomials\/","title":{"rendered":"C++ Programming &#8211; Multiply two polynomials"},"content":{"rendered":"<p>Given two polynomials represented by two arrays, write a function that multiplies given two polynomials.<span id=\"more-132532\"><\/span><\/p>\n<p>Example:<\/p>\n<pre>Input:  A[] = {5, 0, 10, 6} \r\n        B[] = {1, 2, 4} \r\nOutput: prod[] = {5, 10, 30, 26, 52, 24}\r\n\r\nThe first input array represents \"5 + 0x^1 + 10x^2 + 6x^3\"\r\nThe second array represents \"1 + 2x^1 + 4x^2\"<\/pre>\n<p>A simple solution is to one by one consider every term of first polynomial and multiply it with every term of second polynomial. Following is algorithm of this simple method.<\/p>\n<pre><strong>multiply(A[0..m-1], B[0..n01])<\/strong>\r\n1) Create a product array prod[] of size m+n-1.\r\n2) Initialize all entries in prod[] as 0.\r\n3) Travers array A[] and do following for every element A[i]\r\n...(3.a) Traverse array B[] and do following for every element B[j]\r\n          prod[i+j] = prod[i+j] + A[i] * B[j]\r\n4) Return prod[].<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>The following is C++ implementation of above algorithm.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Simple C++ program to multiply two polynomials<br\/>#include &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ A[] represents coefficients of first polynomial<br\/>\/\/ B[] represents coefficients of second polynomial<br\/>\/\/ m and n are sizes of A[] and B[] respectively<br\/>int *multiply(int A[], int B[], int m, int n)<br\/>{<br\/>   int *prod = new int[m+n-1];<br\/> <br\/>   \/\/ Initialize the porduct polynomial<br\/>   for (int i = 0; i&lt;m+n-1; i++)<br\/>     prod[i] = 0;<br\/> <br\/>   \/\/ Multiply two polynomials term by term<br\/> <br\/>   \/\/ Take ever term of first polynomial<br\/>   for (int i=0; i&lt;m; i++)<br\/>   {<br\/>     \/\/ Multiply the current term of first polynomial<br\/>     \/\/ with every term of second polynomial.<br\/>     for (int j=0; j&lt;n; j++)<br\/>         prod[i+j] += A[i]*B[j];<br\/>   }<br\/> <br\/>   return prod;<br\/>}<br\/> <br\/>\/\/ A utility function to print a polynomial<br\/>void printPoly(int poly[], int n)<br\/>{<br\/>    for (int i=0; i&lt;n; i++)<br\/>    {<br\/>       cout &lt;&lt; poly[i];<br\/>       if (i != 0)<br\/>        cout &lt;&lt; &quot;x^&quot; &lt;&lt; i ;<br\/>       if (i != n-1)<br\/>       cout &lt;&lt; &quot; + &quot;;<br\/>    }<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    \/\/ The following array represents polynomial 5 + 10x^2 + 6x^3<br\/>    int A[] = {5, 0, 10, 6};<br\/> <br\/>    \/\/ The following array represents polynomial 1 + 2x + 4x^2<br\/>    int B[] = {1, 2, 4};<br\/>    int m = sizeof(A)\/sizeof(A[0]);<br\/>    int n = sizeof(B)\/sizeof(B[0]);<br\/> <br\/>    cout &lt;&lt; &quot;First polynomial is \\n&quot;;<br\/>    printPoly(A, m);<br\/>    cout &lt;&lt; &quot;\\nSecond polynomial is \\n&quot;;<br\/>    printPoly(B, n);<br\/> <br\/>    int *prod = multiply(A, B, m, n);<br\/> <br\/>    cout &lt;&lt; &quot;\\nProduct polynomial is \\n&quot;;<br\/>    printPoly(prod, m+n-1);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output<\/p>\n<p>First polynomial is<br \/>\n5 + 0x^1 + 10x^2 + 6x^3<br \/>\nSecond polynomial is<br \/>\n1 + 2x^1 + 4x^2<br \/>\nProduct polynomial is<br \/>\n5 + 10x^1 + 30x^2 + 26x^3 + 52x^4 + 24x^5<br \/>\nTime complexity of the above solution is O(mn). If size of two polynomials same, then time complexity is O(n2).<\/p>\n<p>Can we do better?<br \/>\nThere are methods to do multiplication faster than O(n2) time. These methods are mainly based on divide and conquer. Following is one simple method that divides the given polynomial (of degree n) into two polynomials one containing lower degree terms(lower than n\/2) and other containing higher degree terns (higher than or equal to n\/2)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Let the two given polynomials be A and B.<br \/>\nFor simplicity, Let us assume that the given two polynomials are of<br \/>\nsame degree and have degree in powers of 2, i.e., n = 2i<\/p>\n<p>The polynomial &#8216;A&#8217; can be written as A0 + A1*xn\/2<br \/>\nThe polynomial &#8216;B&#8217; can be written as B0 + B1*xn\/2<\/p>\n<p>For example 1 + 10x + 6&#215;2 &#8211; 4&#215;3 + 5&#215;4 can be<br \/>\nwritten as (1 + 10x) + (6 &#8211; 4x + 5&#215;2)*x2<\/p>\n<p>A * B = (A0 + A1*xn\/2) * (B0 + B1*xn\/2)<br \/>\n= A0*B0 + A0*B1*xn\/2 + A1*B0*xn\/2 + A1*B1*xn<br \/>\n= A0*B0 + (A0*B1 + A1*B0)xn\/2 + A1*B1*xn<br \/>\nSo the above divide and conquer approach requires 4 multiplications and O(n) time to add all 4 results. Therefore the time complexity is T(n) = 4T(n\/2) + O(n). The solution of the recurrence is O(n2) which is same as the above simple solution.<\/p>\n<p>The idea is to reduce number of multiplications to 3 and make the recurrence as T(n) = 3T(n\/2) + O(n)<\/p>\n<p>How to reduce number of multiplications?<br \/>\nThis requires a little trick similar to Strassen\u2019s Matrix Multiplication. We do following 3 multiplications.<\/p>\n<p>X = (A0 + A1)*(B0 + B1) \/\/ First Multiplication<br \/>\nY = A0B0 \/\/ Second<br \/>\nZ = A1B1 \/\/ Third<\/p>\n<p>The missing middle term in above multiplication equation A0*B0 + (A0*B1 +<br \/>\nA1*B0)xn\/2 + A1*B1*xn can obtained using below.<br \/>\nA0B1 + A1B0 = X &#8211; Y &#8211; Z<br \/>\nSo the time taken by this algorithm is T(n) = 3T(n\/2) + O(n)<br \/>\nThe solution of above recurrence is O(nLg3) which is better than O(n2).<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming Multiply two polynomials &#8211; Mathematical Algorithms &#8211; A simple solution is to one by one consider every term of first polynomial and multiply<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,74058],"tags":[77780,77772,70840,77751,77752,76710,77754,7382,77777,77764,77630,77758,77770,76149,77631,77759,77776,77768,77773,77766,72928,74837,74835,75068,70828,74832,77761,77771,77105,77775,77750,76066,77756,77755,77769,77767,77765,77781,77779,77016,77774,77778,77753,77749,77760,77782,77757,75430,77763,77762],"class_list":["post-26203","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-adding-polynomials-calculator","tag-addition-of-polynomials","tag-addition-program-in-c","tag-algorithm-for-polynomial-addition-using-linked-list-in-data-structure","tag-bisection-method-c-program","tag-c","tag-c-addition-program","tag-c-code","tag-c-language-addition-program","tag-c-language-programs-pdf","tag-c-program-for-addition","tag-c-program-quadratic-equation","tag-c-programming-addition","tag-c-programming-examples-pdf","tag-c-programming-exercises","tag-c-programming-exercises-with-solutions-pdf","tag-c-programming-functions","tag-c-programming-pdf","tag-c-programming-problems-and-solutions-pdf","tag-c-programs-using-functions","tag-divide-and-conquer-algorithm","tag-function-c-programming","tag-function-in-c-programming-examples","tag-function-program-in-c","tag-functions-in-c-programming","tag-functions-in-c-programming-with-examples","tag-ing","tag-introduction-to-numerical-analysis-pdf","tag-math-functions-in-c","tag-multiplication-of-polynomials","tag-multiplication-program-in-c","tag-multiplication-table","tag-multiplying-polynomials","tag-numerical-analysis-book-pdf-free-download","tag-numerical-analysis-pdf","tag-numerical-recipes","tag-numerical-recipes-in-c","tag-polynomial-addition-algorithm-in-data-structure","tag-polynomial-addition-in-data-structure-pdf","tag-polynomial-addition-using-array-in-c","tag-polynomial-calculator","tag-polynomial-calculator-with-steps","tag-polynomial-in-standard-form-calculator","tag-polynomial-multiplication","tag-product-of-polynomials","tag-product-of-polynomials-formula","tag-sample-c-programs-pdf","tag-simple-addition-program-in-c","tag-simple-c-programming-exercises","tag-w3schools-c-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26203"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26203\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}