Multiply Two Integer Polynomials Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) The PPCG Site design is on its way - help us make it awesome! Sandbox for Proposed ChallengesTips for golfing in PythonTips for golfing in <all languages>Discrete Convolution or Polynomial MultiplicationPretty-printing polynomialsPrime polynomialsSymbolic Differentiation of PolynomialsSymbolic Integration of PolynomialsIrreducible polynomials over GF(5)PolynomialceptionSelf Referential PolynomialsAdd up two algebraic numbersМногочлены Чебышёва (Chebyshev Polynomials)Decompose Polynomials

Did Deadpool rescue all of the X-Force?

Hangman Game with C++

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Is there any word for a place full of confusion?

Most bit efficient text communication method?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

SF book about people trapped in a series of worlds they imagine

Why aren't air breathing engines used as small first stages?

How to install press fit bottom bracket into new frame

Question about debouncing - delay of state change

How would a mousetrap for use in space work?

Is there a kind of relay only consumes power when switching?

Can a new player join a group only when a new campaign starts?

Multiple OR (||) Conditions in If Statement

Trademark violation for app?

As a beginner, should I get a Squier Strat with a SSS config or a HSS?

How to compare two different files line by line in unix?

How come Sam didn't become Lord of Horn Hill?

Do wooden building fires get hotter than 600°C?

Disembodied hand growing fangs

NumericArray versus PackedArray in MMA12

Should I follow up with an employee I believe overracted to a mistake I made?

Combinatorics problem on counting.

What do you call the main part of a joke?



Multiply Two Integer Polynomials



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
The PPCG Site design is on its way - help us make it awesome!
Sandbox for Proposed ChallengesTips for golfing in PythonTips for golfing in <all languages>Discrete Convolution or Polynomial MultiplicationPretty-printing polynomialsPrime polynomialsSymbolic Differentiation of PolynomialsSymbolic Integration of PolynomialsIrreducible polynomials over GF(5)PolynomialceptionSelf Referential PolynomialsAdd up two algebraic numbersМногочлены Чебышёва (Chebyshev Polynomials)Decompose Polynomials










14












$begingroup$


Your task is to take two single-variable integer polynomial expressions and multiply them into their unsimplified first-term-major left-to-right expansion (A.K.A. FOIL in the case of binomials). Do not combine like terms or reorder the result. To be more explicit about the expansion, multiply the first term in the first expression by each term in the second, in order, and continue in the first expression until all terms have been multiplied by all other terms. Expressions will be given in a simplified LaTeX variant.



Each expression will be a sequence of terms separated by + (with exactly one space on each side) Each term will conform to the following regular expression: (PCRE notation)



-?d+x^d+


In plain English, the term is an optional leading - followed by one or more digits followed by x and a nonnegative integer power (with ^)



An example of a full expression:



6x^3 + 1337x^2 + -4x^1 + 2x^0


When plugged into LaTeX, you get $6x^3 + 1337x^2 + -4x^1 + 2x^0$



The output should also conform to this format.



Since brackets do not surround exponents in this format, LaTeX will actually render multi-digit exponents incorrectly. (e.g. 4x^3 + -2x^14 + 54x^28 + -4x^5 renders as $4x^3 + -2x^14 + 54x^28 + -4x^5$) You do not need to account for this and you should not include the brackets in your output.



Example Test Cases



5x^4
3x^23

15x^27



6x^2 + 7x^1 + -2x^0
1x^2 + -2x^3

6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3



3x^1 + 5x^2 + 2x^4 + 3x^0
3x^0

9x^1 + 15x^2 + 6x^4 + 9x^0



4x^3 + -2x^14 + 54x^28 + -4x^5
-0x^7

0x^10 + 0x^21 + 0x^35 + 0x^12



4x^3 + -2x^4 + 0x^255 + -4x^5
-3x^4 + 2x^2

-12x^7 + 8x^5 + 6x^8 + -4x^6 + 0x^259 + 0x^257 + 12x^9 + -8x^7


Rules and Assumptions



  • You may assume that all inputs conform to this exact format. Behavior for any other format is undefined for the purposes of this challenge.

    • It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format.


  • The order of the polynomials matters due to the expected order of the product expansion.

  • You must support input coefficients between $-128$ and $127$ and input exponents up to $255$.

    • Output coefficents between $-16,256$ and $16,384$ and exponents up to $510$ must therefore be supported.


  • You may assume each input polynomial contains no more than 16 terms

    • Therefore you must (at minimum) support up to 256 terms in the output


  • Terms with zero coefficients should be left as is, with exponents being properly combined

  • Negative zero is allowed in the input, but is indistinguishable from positive zero semantically. Always output positive zero. Do not omit zero terms.

Happy Golfing! Good luck!










share|improve this question











$endgroup$







  • 1




    $begingroup$
    related
    $endgroup$
    – H.PWiz
    Apr 10 at 17:25






  • 2




    $begingroup$
    @LuisfelipeDejesusMunoz I imagine not. Parsing is an integral part of the challenge and the OP says -- "It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format." (emphasis added)
    $endgroup$
    – Giuseppe
    Apr 10 at 18:01















14












$begingroup$


Your task is to take two single-variable integer polynomial expressions and multiply them into their unsimplified first-term-major left-to-right expansion (A.K.A. FOIL in the case of binomials). Do not combine like terms or reorder the result. To be more explicit about the expansion, multiply the first term in the first expression by each term in the second, in order, and continue in the first expression until all terms have been multiplied by all other terms. Expressions will be given in a simplified LaTeX variant.



Each expression will be a sequence of terms separated by + (with exactly one space on each side) Each term will conform to the following regular expression: (PCRE notation)



-?d+x^d+


In plain English, the term is an optional leading - followed by one or more digits followed by x and a nonnegative integer power (with ^)



An example of a full expression:



6x^3 + 1337x^2 + -4x^1 + 2x^0


When plugged into LaTeX, you get $6x^3 + 1337x^2 + -4x^1 + 2x^0$



The output should also conform to this format.



Since brackets do not surround exponents in this format, LaTeX will actually render multi-digit exponents incorrectly. (e.g. 4x^3 + -2x^14 + 54x^28 + -4x^5 renders as $4x^3 + -2x^14 + 54x^28 + -4x^5$) You do not need to account for this and you should not include the brackets in your output.



Example Test Cases



5x^4
3x^23

15x^27



6x^2 + 7x^1 + -2x^0
1x^2 + -2x^3

6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3



3x^1 + 5x^2 + 2x^4 + 3x^0
3x^0

9x^1 + 15x^2 + 6x^4 + 9x^0



4x^3 + -2x^14 + 54x^28 + -4x^5
-0x^7

0x^10 + 0x^21 + 0x^35 + 0x^12



4x^3 + -2x^4 + 0x^255 + -4x^5
-3x^4 + 2x^2

-12x^7 + 8x^5 + 6x^8 + -4x^6 + 0x^259 + 0x^257 + 12x^9 + -8x^7


Rules and Assumptions



  • You may assume that all inputs conform to this exact format. Behavior for any other format is undefined for the purposes of this challenge.

    • It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format.


  • The order of the polynomials matters due to the expected order of the product expansion.

  • You must support input coefficients between $-128$ and $127$ and input exponents up to $255$.

    • Output coefficents between $-16,256$ and $16,384$ and exponents up to $510$ must therefore be supported.


  • You may assume each input polynomial contains no more than 16 terms

    • Therefore you must (at minimum) support up to 256 terms in the output


  • Terms with zero coefficients should be left as is, with exponents being properly combined

  • Negative zero is allowed in the input, but is indistinguishable from positive zero semantically. Always output positive zero. Do not omit zero terms.

Happy Golfing! Good luck!










share|improve this question











$endgroup$







  • 1




    $begingroup$
    related
    $endgroup$
    – H.PWiz
    Apr 10 at 17:25






  • 2




    $begingroup$
    @LuisfelipeDejesusMunoz I imagine not. Parsing is an integral part of the challenge and the OP says -- "It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format." (emphasis added)
    $endgroup$
    – Giuseppe
    Apr 10 at 18:01













14












14








14


2



$begingroup$


Your task is to take two single-variable integer polynomial expressions and multiply them into their unsimplified first-term-major left-to-right expansion (A.K.A. FOIL in the case of binomials). Do not combine like terms or reorder the result. To be more explicit about the expansion, multiply the first term in the first expression by each term in the second, in order, and continue in the first expression until all terms have been multiplied by all other terms. Expressions will be given in a simplified LaTeX variant.



Each expression will be a sequence of terms separated by + (with exactly one space on each side) Each term will conform to the following regular expression: (PCRE notation)



-?d+x^d+


In plain English, the term is an optional leading - followed by one or more digits followed by x and a nonnegative integer power (with ^)



An example of a full expression:



6x^3 + 1337x^2 + -4x^1 + 2x^0


When plugged into LaTeX, you get $6x^3 + 1337x^2 + -4x^1 + 2x^0$



The output should also conform to this format.



Since brackets do not surround exponents in this format, LaTeX will actually render multi-digit exponents incorrectly. (e.g. 4x^3 + -2x^14 + 54x^28 + -4x^5 renders as $4x^3 + -2x^14 + 54x^28 + -4x^5$) You do not need to account for this and you should not include the brackets in your output.



Example Test Cases



5x^4
3x^23

15x^27



6x^2 + 7x^1 + -2x^0
1x^2 + -2x^3

6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3



3x^1 + 5x^2 + 2x^4 + 3x^0
3x^0

9x^1 + 15x^2 + 6x^4 + 9x^0



4x^3 + -2x^14 + 54x^28 + -4x^5
-0x^7

0x^10 + 0x^21 + 0x^35 + 0x^12



4x^3 + -2x^4 + 0x^255 + -4x^5
-3x^4 + 2x^2

-12x^7 + 8x^5 + 6x^8 + -4x^6 + 0x^259 + 0x^257 + 12x^9 + -8x^7


Rules and Assumptions



  • You may assume that all inputs conform to this exact format. Behavior for any other format is undefined for the purposes of this challenge.

    • It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format.


  • The order of the polynomials matters due to the expected order of the product expansion.

  • You must support input coefficients between $-128$ and $127$ and input exponents up to $255$.

    • Output coefficents between $-16,256$ and $16,384$ and exponents up to $510$ must therefore be supported.


  • You may assume each input polynomial contains no more than 16 terms

    • Therefore you must (at minimum) support up to 256 terms in the output


  • Terms with zero coefficients should be left as is, with exponents being properly combined

  • Negative zero is allowed in the input, but is indistinguishable from positive zero semantically. Always output positive zero. Do not omit zero terms.

Happy Golfing! Good luck!










share|improve this question











$endgroup$




Your task is to take two single-variable integer polynomial expressions and multiply them into their unsimplified first-term-major left-to-right expansion (A.K.A. FOIL in the case of binomials). Do not combine like terms or reorder the result. To be more explicit about the expansion, multiply the first term in the first expression by each term in the second, in order, and continue in the first expression until all terms have been multiplied by all other terms. Expressions will be given in a simplified LaTeX variant.



Each expression will be a sequence of terms separated by + (with exactly one space on each side) Each term will conform to the following regular expression: (PCRE notation)



-?d+x^d+


In plain English, the term is an optional leading - followed by one or more digits followed by x and a nonnegative integer power (with ^)



An example of a full expression:



6x^3 + 1337x^2 + -4x^1 + 2x^0


When plugged into LaTeX, you get $6x^3 + 1337x^2 + -4x^1 + 2x^0$



The output should also conform to this format.



Since brackets do not surround exponents in this format, LaTeX will actually render multi-digit exponents incorrectly. (e.g. 4x^3 + -2x^14 + 54x^28 + -4x^5 renders as $4x^3 + -2x^14 + 54x^28 + -4x^5$) You do not need to account for this and you should not include the brackets in your output.



Example Test Cases



5x^4
3x^23

15x^27



6x^2 + 7x^1 + -2x^0
1x^2 + -2x^3

6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3



3x^1 + 5x^2 + 2x^4 + 3x^0
3x^0

9x^1 + 15x^2 + 6x^4 + 9x^0



4x^3 + -2x^14 + 54x^28 + -4x^5
-0x^7

0x^10 + 0x^21 + 0x^35 + 0x^12



4x^3 + -2x^4 + 0x^255 + -4x^5
-3x^4 + 2x^2

-12x^7 + 8x^5 + 6x^8 + -4x^6 + 0x^259 + 0x^257 + 12x^9 + -8x^7


Rules and Assumptions



  • You may assume that all inputs conform to this exact format. Behavior for any other format is undefined for the purposes of this challenge.

    • It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format.


  • The order of the polynomials matters due to the expected order of the product expansion.

  • You must support input coefficients between $-128$ and $127$ and input exponents up to $255$.

    • Output coefficents between $-16,256$ and $16,384$ and exponents up to $510$ must therefore be supported.


  • You may assume each input polynomial contains no more than 16 terms

    • Therefore you must (at minimum) support up to 256 terms in the output


  • Terms with zero coefficients should be left as is, with exponents being properly combined

  • Negative zero is allowed in the input, but is indistinguishable from positive zero semantically. Always output positive zero. Do not omit zero terms.

Happy Golfing! Good luck!







code-golf math parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 11 at 17:35







Beefster

















asked Apr 10 at 17:06









BeefsterBeefster

2,8101346




2,8101346







  • 1




    $begingroup$
    related
    $endgroup$
    – H.PWiz
    Apr 10 at 17:25






  • 2




    $begingroup$
    @LuisfelipeDejesusMunoz I imagine not. Parsing is an integral part of the challenge and the OP says -- "It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format." (emphasis added)
    $endgroup$
    – Giuseppe
    Apr 10 at 18:01












  • 1




    $begingroup$
    related
    $endgroup$
    – H.PWiz
    Apr 10 at 17:25






  • 2




    $begingroup$
    @LuisfelipeDejesusMunoz I imagine not. Parsing is an integral part of the challenge and the OP says -- "It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format." (emphasis added)
    $endgroup$
    – Giuseppe
    Apr 10 at 18:01







1




1




$begingroup$
related
$endgroup$
– H.PWiz
Apr 10 at 17:25




$begingroup$
related
$endgroup$
– H.PWiz
Apr 10 at 17:25




2




2




$begingroup$
@LuisfelipeDejesusMunoz I imagine not. Parsing is an integral part of the challenge and the OP says -- "It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format." (emphasis added)
$endgroup$
– Giuseppe
Apr 10 at 18:01




$begingroup$
@LuisfelipeDejesusMunoz I imagine not. Parsing is an integral part of the challenge and the OP says -- "It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format." (emphasis added)
$endgroup$
– Giuseppe
Apr 10 at 18:01










14 Answers
14






active

oldest

votes


















4












$begingroup$


R, 159 153 148 bytes





function(P,Q,a=h(P),b=h(Q))paste0(b[1,]%o%a[1,],"x^",outer(b,a,"+")[2,,2,],collapse=" + ")
h=function(s,`/`=strsplit)sapply(el(s/" . ")/"x.",strtoi)


Try it online!



I really wanted to use outer, so there's almost surely a more efficient approach.






share|improve this answer











$endgroup$




















    4












    $begingroup$


    Haskell, 131 122 bytes





    (%)=drop
    f s=do(a,t)<-reads s;(i,u)<-reads$2%t;(a,i):f(3%u)
    p!q=3%do(a,i)<-f p;(b,j)<-f q;" + "++shows(a*b)"x^"++show(i+j)


    Try it online!



    f parses a polynomial from a string, ! multiplies two of them and formats the result.



    H.PWiz saved 9 bytes. Thanks!



    Ungolfed





    type Monomial = (Int, Int) -- a^i
    type Polynomial = [Monomial]

    parse :: String -> Polynomial
    parse s = do (a, s') <- reads s
    (i, s'') <- reads (drop 2 s')
    (a, i) : parse (drop 3 s'')

    (!) :: String -> String -> String
    p!q = drop 3 (concat terms)
    where terms = [term (a*b) (i+j) | (a,i) <- p', (b,j) <- q']
    term a i = concat [" + ", show a, "x^", show i]
    p' = parse p
    q' = parse q






    share|improve this answer











    $endgroup$












    • $begingroup$
      129 bytes
      $endgroup$
      – H.PWiz
      Apr 12 at 16:12






    • 1




      $begingroup$
      even better
      $endgroup$
      – H.PWiz
      Apr 12 at 16:14


















    2












    $begingroup$


    Ruby, 102 100 98 bytes





    ->a,ba.scan(w=/(.*?)x.(d+)/).mapx*?+


    Try it online!



    How?



    First step: get all the numbers from both polynomials: scan returns the numbers as an array of pairs of strings.
    Then, do a cartesian product of the 2 lists. Now we have all the numbers where we need them, but still in the wrong order.



    Example: if we multiply 3x^4 by -5x^2, we get the numbers as [["3","4"],["-5","2"]], the first idea was to zip and flatten this list, and then put the numbers into an expression to be evaluated as [3*-5, 4+2]. Actually, we don't need to reorder the numbers, we could do it inside the expression by using a temporary variable: the expression becomes [3*(z=4,-5),z+2].



    After evaluating these expressions, we get the coefficient and exponent, we need to join them using "x^", and then join all the tems using "+".






    share|improve this answer











    $endgroup$




















      2












      $begingroup$

      Haskell, 124 121 bytes



      import Data.Lists
      f!x=map f.splitOn x
      z=read!"x^"!"+"
      a#b=drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q)


      Note: TIO lacks Data.Lists, so I import Data.Lists.Split and Data.List: Try it online!



      Edit: -3 bytes thanks to @Lynn.






      share|improve this answer











      $endgroup$












      • $begingroup$
        This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
        $endgroup$
        – Lynn
        Apr 12 at 15:40






      • 1




        $begingroup$
        @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
        $endgroup$
        – nimi
        Apr 12 at 21:43


















      1












      $begingroup$

      Pyth - 39 bytes



      LmsMcdK"x^"%2cb)j" + "m++*FhdKsedCM*FyM


      Try it online.






      share|improve this answer









      $endgroup$




















        1












        $begingroup$


        JavaScript (Babel Node), 118 bytes



        Takes input as (a)(b).





        a=>b=>(g=s=>[...s.matchAll(/(-?d+)x.(d+)/g)])(a).flatMap(([_,x,p])=>g(b).map(([_,X,P])=>x*X+'x^'+-(-p-P))).join` + `


        Try it online!






        share|improve this answer









        $endgroup$




















          1












          $begingroup$


          Python 2, 193 bytes





          import re
          f=re.finditer
          lambda a,b:' + '.join(' + '.join(`int(m.group(1))*int(n.group(1))`+'x^'+`int(m.group(2))+int(n.group(2))`for n in f('(-?d+)x^(d+)',b))for m in f('(-?d+)x^(d+)',a))


          Try it online!



          Side note: First time doing a code golf challenge, so sorry if the attempt sucks haha






          share|improve this answer









          $endgroup$








          • 3




            $begingroup$
            Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
            $endgroup$
            – Giuseppe
            Apr 10 at 20:38











          • $begingroup$
            save 12 bytes
            $endgroup$
            – Noodle9
            Apr 11 at 10:23






          • 1




            $begingroup$
            Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
            $endgroup$
            – Jo King
            Apr 11 at 11:23


















          1












          $begingroup$


          Retina, 110 bytes



          SS+(?=.*n(.+))
          $1#$&
          |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
          $1$4$.($2*$5*)x^$.($3*_$6*
          --|-(0)
          $1


          Try it online! Explanation:



          SS+(?=.*n(.+))
          $1#$&


          Prefix each term in the first input with a #, a copy of the second input, and a space. This means that all of the terms in copies of the second input are preceded by a space and none of the terms from the first input are.



          |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
          $1$4$.($2*$5*)x^$.($3*_$6*


          Match all of the copies of terms in the second input and their corresponding term from the first input. Concatenate any - signs, multiply the coefficients, and add the indices. Finally join all of the resulting substitutions with the string  + .



          --|-(0)
          $1


          Delete any pairs of -s and convert -0 to 0.






          share|improve this answer









          $endgroup$




















            1












            $begingroup$


            SNOBOL4 (CSNOBOL4), 192 176 bytes



            	P =INPUT
            Q =INPUT
            D =SPAN(-1234567890)
            P P D . K ARB D . W REM . P :F(O)
            B =Q
            B B D . C ARB D . E REM . B :F(P)
            O =O ' + ' K * C 'x^' W + E :(B)
            O O ' + ' REM . OUTPUT
            END


            Try it online!



            	P =INPUT				;* read P
            Q =INPUT ;* read Q
            D =SPAN(-1234567890) ;* save PATTERN for Digits (or a - sign); equivalent to [0-9\-]+
            P P D . K ARB D . W REM . P :F(O) ;* save the Koefficient and the poWer, saving the REMainder as P, or if no match, goto O
            B =Q ;* set B = Q
            B B D . C ARB D . E REM . B :F(P) ;* save the Coefficient and the powEr, saving the REMainder as B, or if no match, goto P
            O =O ' + ' K * C 'x^' W + E :(B) ;* accumulate the output
            O O ' + ' REM . OUTPUT ;* match ' + ' and OUTPUT the REMainder
            END





            share|improve this answer











            $endgroup$




















              1












              $begingroup$


              Perl 6, 114 bytes





              my&g=*.match(/(-?d+)x^(d+)/,:g)».caps».Map;join " + ",map "[*] $_»0x^[+] $_»1",(g($^a)X g $^b)


              Try it online!






              share|improve this answer









              $endgroup$








              • 1




                $begingroup$
                86 bytes
                $endgroup$
                – Jo King
                Apr 11 at 11:03


















              1












              $begingroup$


              Python 2, 130 bytes





              lambda a,b:' + '.join([`v*V`+'x^'+`k+K`for V,K in g(a)for v,k in g(b)])
              g=lambda s:[map(int,t.split('x^'))for t in s.split(' + ')]


              Try it online!






              share|improve this answer









              $endgroup$




















                1












                $begingroup$


                C# (Visual C# Interactive Compiler), 192 190 bytes





                n=>m=>string.Join(g=" + ",from a in n.Split(g)from b in m.Split(g)select f(a.Split(p="x^")[0])*f(b.Split(p)[0])+p+(f(a.Split(p)[1])+f(b.Split(p)[1])));Func<string,int>f=int.Parse;string p,g;


                Query syntax seems to be a byte shorter than method syntax.



                Try it online!






                share|improve this answer











                $endgroup$












                • $begingroup$
                  Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                  $endgroup$
                  – Expired Data
                  Apr 11 at 12:26


















                1












                $begingroup$


                Jelly, 28 bytes



                ṣ”+ṣ”xV$€)p/ZPSƭ€j⁾x^Ʋ€j“ + 


                Try it online!



                Full program. Takes the two polynomials as a list of two strings.



                Explanation (expanded form)



                ṣ”+ṣ”xV$€µ€p/ZPSƭ€j⁾x^Ʋ€j“ + ” Arguments: x
                µ Monadic chain.
                € Map the monadic link over the argument.
                Note that this will "pop" the previous chain, so
                it will really act as a link rather than a
                sub-chain.
                ṣ”+ ṣ, right = '+'.
                Split the left argument on each occurrence of
                the right.
                Note that strings in Jelly are lists of
                single-character Python strings.
                € Map the monadic link over the argument.
                $ Make a non-niladic monadic chain of at least
                two links.
                ṣ”x ṣ, right = 'x'.
                Split the left argument on each occurrence of
                the right.
                V Evaluate the argument as a niladic link.
                / Reduce the dyadic link over the argument.
                p Cartesian product of left and right arguments.
                € Map the monadic link over the argument.
                Ʋ Make a non-niladic monadic chain of at least
                four links.
                Z Transpose the argument.
                € Map the monadic link over the argument.
                ƭ At the first call, call the first link. At the
                second call, call the second link. Rinse and
                repeat.
                P Product: ;1×/$
                S Sum: ;0+/$
                j⁾x^ j, right = "x^".
                Put the right argument between the left one's
                elements and concatenate the result.
                j“ + ” j, right = " + ".
                Put the right argument between the left one's
                elements and concatenate the result.


                Aliasing



                ) is the same as µ€.

                A trailing is implied and can be omitted.



                Algorithm



                Let's say we have this input:



                ["6x^2 + 7x^1 + -2x^0", "1x^2 + -2x^3"]


                The first procedure is the Parsing, applied to each of the two polynomials. Let's handle the first one, "6x^2 + 7x^1 + -2x^0":



                The first step is to split the string by '+', so as to separate the terms. This results in:



                ["6x^2 ", " 7x^1 ", " -2x^0"]


                The next step is to split each string by 'x', to separate the coefficient from the exponent. The result is this:



                [["6", "^2 "], [" 7", "^1 "], [" -2", "^0"]]


                Currently, it looks like there's a lot of trash in these strings, but that trash is actually unimportant. These strings are all going to be evaluated as niladic Jelly links. Trivially, the spaces are unimportant, as they are not between the digits of the numbers. So we could as well evaluate the below and still get the same result:



                [["6", "^2"], ["7", "^1"], ["-2", "^0"]]


                The ^s look a bit more disturbing, but they actually don't do anything either! Well, ^ is the bitwise XOR atom, however niladic chains act like monadic links, except that the first link actually becomes the argument, instead of taking an argument, if it's niladic. If it's not, then the link will have an argument of 0. The exponents have the ^s as their first char, and ^ isn't niladic, so the argument is assumed to be 0. The rest of the string, i.e. the number, is the right argument of ^. So, for example, ^2 is $0text XOR 2=2$. Obviously, $0text XOR n=n$. All the exponents are integer, so we are fine. Therefore, evaluating this instead of the above won't change the result:



                [["6", "2"], ["7", "1"], ["-2", "0"]]


                Here we go:



                [[6, 2], [7, 1], [-2, 0]]


                This step will also convert "-0" to 0.



                Since we're parsing both inputs, the result after the Parsing is going to be this:



                [[[6, 2], [7, 1], [-2, 0]], [[1, 2], [-2, 3]]]


                The Parsing is now complete. The next procedure is the Multiplication.



                We first take the Cartesian product of these two lists:



                [[[6, 2], [1, 2]], [[6, 2], [-2, 3]], [[7, 1], [1, 2]], [[7, 1], [-2, 3]], [[-2, 0], [1, 2]], [[-2, 0], [-2, 3]]]


                Many pairs are made, each with one element from the left list and one from the right, in order. This also happens to be the intended order of the output. This challenge really asks us to apply multiplicative distributivity, as we're asked not to further process the result after that.



                The pairs in each pair represent terms we want to multiply, with the first element being the coefficient and the second being the exponent. To multiply the terms, we multiply the coefficients and add the exponents together ($ax^cbx^d=abx^cx^d=ab(x^cx^d)=(ab)x^c+d$). How do we do that? Let's handle the second pair, [[6, 2], [-2, 3]].



                We first transpose the pair:



                [[6, -2], [2, 3]]


                We then take the product of the first pair, and the sum of the second:



                [-12, 5]


                The relevant part of the code, PSƭ€, doesn't actually reset its counter for each pair of terms, but, since they're pairs, it doesn't need to.



                Handling all pairs of terms, we have:



                [[6, 4], [-12, 5], [7, 3], [-14, 4], [-2, 2], [4, 3]]


                Here, the Multiplication is done, as we don't have to combine like terms. The final procedure is the Prettyfying.



                We first join each pair with "x^":



                [[6, 'x', '^', 4], [-12, 'x', '^', 5], [7, 'x', '^', 3], [-14, 'x', '^', 4], [-2, 'x', '^', 2], [4, 'x', '^', 3]]


                Then we join the list with " + ":



                [6, 'x', '^', 4, ' ', '+', ' ', -12, 'x', '^', 5, ' ', '+', ' ', 7, 'x', '^', 3, ' ', '+', ' ', -14, 'x', '^', 4, ' ', '+', ' ', -2, 'x', '^', 2, ' ', '+', ' ', 4, 'x', '^', 3]


                Notice how we've still got numbers in the list, so it's not really a string. However, Jelly has a process called "stringification", ran right at the end of execution of a program to print the result out. For a list of depth 1, it really just converts each element to its string representation and concatenates the strings together, so we get the desired output:



                6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3





                share|improve this answer











                $endgroup$




















                  1












                  $begingroup$

                  JavaScript, 112 110 bytes



                  I found two alternatives with the same length. Call with currying syntax: f(A)(B)



                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `





                  f=
                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                  console.log( f('5x^4')('3x^23') )
                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `





                  f=
                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                  console.log( f('5x^4')('3x^23') )
                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                  -2 bytes (Luis): Remove spaces around split delimiter.




                  JavaScript, 112 bytes



                  Using String.prototype.matchAll.



                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `





                  f=
                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                  console.log( f('5x^4')('3x^23') )
                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )








                  share|improve this answer











                  $endgroup$








                  • 1




                    $begingroup$
                    split' + ' => split'+' to save 2 bytes
                    $endgroup$
                    – Luis felipe De jesus Munoz
                    Apr 10 at 20:37











                  • $begingroup$
                    @Arnauld Seems fine without them
                    $endgroup$
                    – Embodiment of Ignorance
                    Apr 10 at 22:26










                  • $begingroup$
                    @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                    $endgroup$
                    – Arnauld
                    Apr 10 at 22:27











                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "200"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182972%2fmultiply-two-integer-polynomials%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  14 Answers
                  14






                  active

                  oldest

                  votes








                  14 Answers
                  14






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  4












                  $begingroup$


                  R, 159 153 148 bytes





                  function(P,Q,a=h(P),b=h(Q))paste0(b[1,]%o%a[1,],"x^",outer(b,a,"+")[2,,2,],collapse=" + ")
                  h=function(s,`/`=strsplit)sapply(el(s/" . ")/"x.",strtoi)


                  Try it online!



                  I really wanted to use outer, so there's almost surely a more efficient approach.






                  share|improve this answer











                  $endgroup$

















                    4












                    $begingroup$


                    R, 159 153 148 bytes





                    function(P,Q,a=h(P),b=h(Q))paste0(b[1,]%o%a[1,],"x^",outer(b,a,"+")[2,,2,],collapse=" + ")
                    h=function(s,`/`=strsplit)sapply(el(s/" . ")/"x.",strtoi)


                    Try it online!



                    I really wanted to use outer, so there's almost surely a more efficient approach.






                    share|improve this answer











                    $endgroup$















                      4












                      4








                      4





                      $begingroup$


                      R, 159 153 148 bytes





                      function(P,Q,a=h(P),b=h(Q))paste0(b[1,]%o%a[1,],"x^",outer(b,a,"+")[2,,2,],collapse=" + ")
                      h=function(s,`/`=strsplit)sapply(el(s/" . ")/"x.",strtoi)


                      Try it online!



                      I really wanted to use outer, so there's almost surely a more efficient approach.






                      share|improve this answer











                      $endgroup$




                      R, 159 153 148 bytes





                      function(P,Q,a=h(P),b=h(Q))paste0(b[1,]%o%a[1,],"x^",outer(b,a,"+")[2,,2,],collapse=" + ")
                      h=function(s,`/`=strsplit)sapply(el(s/" . ")/"x.",strtoi)


                      Try it online!



                      I really wanted to use outer, so there's almost surely a more efficient approach.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 11 at 17:49

























                      answered Apr 10 at 17:45









                      GiuseppeGiuseppe

                      17.9k31153




                      17.9k31153





















                          4












                          $begingroup$


                          Haskell, 131 122 bytes





                          (%)=drop
                          f s=do(a,t)<-reads s;(i,u)<-reads$2%t;(a,i):f(3%u)
                          p!q=3%do(a,i)<-f p;(b,j)<-f q;" + "++shows(a*b)"x^"++show(i+j)


                          Try it online!



                          f parses a polynomial from a string, ! multiplies two of them and formats the result.



                          H.PWiz saved 9 bytes. Thanks!



                          Ungolfed





                          type Monomial = (Int, Int) -- a^i
                          type Polynomial = [Monomial]

                          parse :: String -> Polynomial
                          parse s = do (a, s') <- reads s
                          (i, s'') <- reads (drop 2 s')
                          (a, i) : parse (drop 3 s'')

                          (!) :: String -> String -> String
                          p!q = drop 3 (concat terms)
                          where terms = [term (a*b) (i+j) | (a,i) <- p', (b,j) <- q']
                          term a i = concat [" + ", show a, "x^", show i]
                          p' = parse p
                          q' = parse q






                          share|improve this answer











                          $endgroup$












                          • $begingroup$
                            129 bytes
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:12






                          • 1




                            $begingroup$
                            even better
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:14















                          4












                          $begingroup$


                          Haskell, 131 122 bytes





                          (%)=drop
                          f s=do(a,t)<-reads s;(i,u)<-reads$2%t;(a,i):f(3%u)
                          p!q=3%do(a,i)<-f p;(b,j)<-f q;" + "++shows(a*b)"x^"++show(i+j)


                          Try it online!



                          f parses a polynomial from a string, ! multiplies two of them and formats the result.



                          H.PWiz saved 9 bytes. Thanks!



                          Ungolfed





                          type Monomial = (Int, Int) -- a^i
                          type Polynomial = [Monomial]

                          parse :: String -> Polynomial
                          parse s = do (a, s') <- reads s
                          (i, s'') <- reads (drop 2 s')
                          (a, i) : parse (drop 3 s'')

                          (!) :: String -> String -> String
                          p!q = drop 3 (concat terms)
                          where terms = [term (a*b) (i+j) | (a,i) <- p', (b,j) <- q']
                          term a i = concat [" + ", show a, "x^", show i]
                          p' = parse p
                          q' = parse q






                          share|improve this answer











                          $endgroup$












                          • $begingroup$
                            129 bytes
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:12






                          • 1




                            $begingroup$
                            even better
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:14













                          4












                          4








                          4





                          $begingroup$


                          Haskell, 131 122 bytes





                          (%)=drop
                          f s=do(a,t)<-reads s;(i,u)<-reads$2%t;(a,i):f(3%u)
                          p!q=3%do(a,i)<-f p;(b,j)<-f q;" + "++shows(a*b)"x^"++show(i+j)


                          Try it online!



                          f parses a polynomial from a string, ! multiplies two of them and formats the result.



                          H.PWiz saved 9 bytes. Thanks!



                          Ungolfed





                          type Monomial = (Int, Int) -- a^i
                          type Polynomial = [Monomial]

                          parse :: String -> Polynomial
                          parse s = do (a, s') <- reads s
                          (i, s'') <- reads (drop 2 s')
                          (a, i) : parse (drop 3 s'')

                          (!) :: String -> String -> String
                          p!q = drop 3 (concat terms)
                          where terms = [term (a*b) (i+j) | (a,i) <- p', (b,j) <- q']
                          term a i = concat [" + ", show a, "x^", show i]
                          p' = parse p
                          q' = parse q






                          share|improve this answer











                          $endgroup$




                          Haskell, 131 122 bytes





                          (%)=drop
                          f s=do(a,t)<-reads s;(i,u)<-reads$2%t;(a,i):f(3%u)
                          p!q=3%do(a,i)<-f p;(b,j)<-f q;" + "++shows(a*b)"x^"++show(i+j)


                          Try it online!



                          f parses a polynomial from a string, ! multiplies two of them and formats the result.



                          H.PWiz saved 9 bytes. Thanks!



                          Ungolfed





                          type Monomial = (Int, Int) -- a^i
                          type Polynomial = [Monomial]

                          parse :: String -> Polynomial
                          parse s = do (a, s') <- reads s
                          (i, s'') <- reads (drop 2 s')
                          (a, i) : parse (drop 3 s'')

                          (!) :: String -> String -> String
                          p!q = drop 3 (concat terms)
                          where terms = [term (a*b) (i+j) | (a,i) <- p', (b,j) <- q']
                          term a i = concat [" + ", show a, "x^", show i]
                          p' = parse p
                          q' = parse q







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 15 at 13:04

























                          answered Apr 10 at 19:09









                          LynnLynn

                          51k899234




                          51k899234











                          • $begingroup$
                            129 bytes
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:12






                          • 1




                            $begingroup$
                            even better
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:14
















                          • $begingroup$
                            129 bytes
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:12






                          • 1




                            $begingroup$
                            even better
                            $endgroup$
                            – H.PWiz
                            Apr 12 at 16:14















                          $begingroup$
                          129 bytes
                          $endgroup$
                          – H.PWiz
                          Apr 12 at 16:12




                          $begingroup$
                          129 bytes
                          $endgroup$
                          – H.PWiz
                          Apr 12 at 16:12




                          1




                          1




                          $begingroup$
                          even better
                          $endgroup$
                          – H.PWiz
                          Apr 12 at 16:14




                          $begingroup$
                          even better
                          $endgroup$
                          – H.PWiz
                          Apr 12 at 16:14











                          2












                          $begingroup$


                          Ruby, 102 100 98 bytes





                          ->a,ba.scan(w=/(.*?)x.(d+)/).mapx*?+


                          Try it online!



                          How?



                          First step: get all the numbers from both polynomials: scan returns the numbers as an array of pairs of strings.
                          Then, do a cartesian product of the 2 lists. Now we have all the numbers where we need them, but still in the wrong order.



                          Example: if we multiply 3x^4 by -5x^2, we get the numbers as [["3","4"],["-5","2"]], the first idea was to zip and flatten this list, and then put the numbers into an expression to be evaluated as [3*-5, 4+2]. Actually, we don't need to reorder the numbers, we could do it inside the expression by using a temporary variable: the expression becomes [3*(z=4,-5),z+2].



                          After evaluating these expressions, we get the coefficient and exponent, we need to join them using "x^", and then join all the tems using "+".






                          share|improve this answer











                          $endgroup$

















                            2












                            $begingroup$


                            Ruby, 102 100 98 bytes





                            ->a,ba.scan(w=/(.*?)x.(d+)/).mapx*?+


                            Try it online!



                            How?



                            First step: get all the numbers from both polynomials: scan returns the numbers as an array of pairs of strings.
                            Then, do a cartesian product of the 2 lists. Now we have all the numbers where we need them, but still in the wrong order.



                            Example: if we multiply 3x^4 by -5x^2, we get the numbers as [["3","4"],["-5","2"]], the first idea was to zip and flatten this list, and then put the numbers into an expression to be evaluated as [3*-5, 4+2]. Actually, we don't need to reorder the numbers, we could do it inside the expression by using a temporary variable: the expression becomes [3*(z=4,-5),z+2].



                            After evaluating these expressions, we get the coefficient and exponent, we need to join them using "x^", and then join all the tems using "+".






                            share|improve this answer











                            $endgroup$















                              2












                              2








                              2





                              $begingroup$


                              Ruby, 102 100 98 bytes





                              ->a,ba.scan(w=/(.*?)x.(d+)/).mapx*?+


                              Try it online!



                              How?



                              First step: get all the numbers from both polynomials: scan returns the numbers as an array of pairs of strings.
                              Then, do a cartesian product of the 2 lists. Now we have all the numbers where we need them, but still in the wrong order.



                              Example: if we multiply 3x^4 by -5x^2, we get the numbers as [["3","4"],["-5","2"]], the first idea was to zip and flatten this list, and then put the numbers into an expression to be evaluated as [3*-5, 4+2]. Actually, we don't need to reorder the numbers, we could do it inside the expression by using a temporary variable: the expression becomes [3*(z=4,-5),z+2].



                              After evaluating these expressions, we get the coefficient and exponent, we need to join them using "x^", and then join all the tems using "+".






                              share|improve this answer











                              $endgroup$




                              Ruby, 102 100 98 bytes





                              ->a,ba.scan(w=/(.*?)x.(d+)/).mapx*?+


                              Try it online!



                              How?



                              First step: get all the numbers from both polynomials: scan returns the numbers as an array of pairs of strings.
                              Then, do a cartesian product of the 2 lists. Now we have all the numbers where we need them, but still in the wrong order.



                              Example: if we multiply 3x^4 by -5x^2, we get the numbers as [["3","4"],["-5","2"]], the first idea was to zip and flatten this list, and then put the numbers into an expression to be evaluated as [3*-5, 4+2]. Actually, we don't need to reorder the numbers, we could do it inside the expression by using a temporary variable: the expression becomes [3*(z=4,-5),z+2].



                              After evaluating these expressions, we get the coefficient and exponent, we need to join them using "x^", and then join all the tems using "+".







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Apr 11 at 21:25

























                              answered Apr 10 at 17:40









                              G BG B

                              8,2961429




                              8,2961429





















                                  2












                                  $begingroup$

                                  Haskell, 124 121 bytes



                                  import Data.Lists
                                  f!x=map f.splitOn x
                                  z=read!"x^"!"+"
                                  a#b=drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q)


                                  Note: TIO lacks Data.Lists, so I import Data.Lists.Split and Data.List: Try it online!



                                  Edit: -3 bytes thanks to @Lynn.






                                  share|improve this answer











                                  $endgroup$












                                  • $begingroup$
                                    This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
                                    $endgroup$
                                    – Lynn
                                    Apr 12 at 15:40






                                  • 1




                                    $begingroup$
                                    @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
                                    $endgroup$
                                    – nimi
                                    Apr 12 at 21:43















                                  2












                                  $begingroup$

                                  Haskell, 124 121 bytes



                                  import Data.Lists
                                  f!x=map f.splitOn x
                                  z=read!"x^"!"+"
                                  a#b=drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q)


                                  Note: TIO lacks Data.Lists, so I import Data.Lists.Split and Data.List: Try it online!



                                  Edit: -3 bytes thanks to @Lynn.






                                  share|improve this answer











                                  $endgroup$












                                  • $begingroup$
                                    This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
                                    $endgroup$
                                    – Lynn
                                    Apr 12 at 15:40






                                  • 1




                                    $begingroup$
                                    @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
                                    $endgroup$
                                    – nimi
                                    Apr 12 at 21:43













                                  2












                                  2








                                  2





                                  $begingroup$

                                  Haskell, 124 121 bytes



                                  import Data.Lists
                                  f!x=map f.splitOn x
                                  z=read!"x^"!"+"
                                  a#b=drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q)


                                  Note: TIO lacks Data.Lists, so I import Data.Lists.Split and Data.List: Try it online!



                                  Edit: -3 bytes thanks to @Lynn.






                                  share|improve this answer











                                  $endgroup$



                                  Haskell, 124 121 bytes



                                  import Data.Lists
                                  f!x=map f.splitOn x
                                  z=read!"x^"!"+"
                                  a#b=drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q)


                                  Note: TIO lacks Data.Lists, so I import Data.Lists.Split and Data.List: Try it online!



                                  Edit: -3 bytes thanks to @Lynn.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Apr 12 at 21:42

























                                  answered Apr 10 at 17:42









                                  niminimi

                                  32.8k32489




                                  32.8k32489











                                  • $begingroup$
                                    This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
                                    $endgroup$
                                    – Lynn
                                    Apr 12 at 15:40






                                  • 1




                                    $begingroup$
                                    @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
                                    $endgroup$
                                    – nimi
                                    Apr 12 at 21:43
















                                  • $begingroup$
                                    This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
                                    $endgroup$
                                    – Lynn
                                    Apr 12 at 15:40






                                  • 1




                                    $begingroup$
                                    @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
                                    $endgroup$
                                    – nimi
                                    Apr 12 at 21:43















                                  $begingroup$
                                  This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
                                  $endgroup$
                                  – Lynn
                                  Apr 12 at 15:40




                                  $begingroup$
                                  This is actually 123 bytes! f!x=map f.splitOn x and then z=read!"x^"!"+" saves a byte; for the last line drop 3$do[u,v]<-z a;[p,q]<-z b;" + "++shows(u*p)"x^"++show(v+q) saves two more. 120 bytes
                                  $endgroup$
                                  – Lynn
                                  Apr 12 at 15:40




                                  1




                                  1




                                  $begingroup$
                                  @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
                                  $endgroup$
                                  – nimi
                                  Apr 12 at 21:43




                                  $begingroup$
                                  @Lynn: the TIO version imports Data.List instead of Data.Lists, so it's +1 byte.
                                  $endgroup$
                                  – nimi
                                  Apr 12 at 21:43











                                  1












                                  $begingroup$

                                  Pyth - 39 bytes



                                  LmsMcdK"x^"%2cb)j" + "m++*FhdKsedCM*FyM


                                  Try it online.






                                  share|improve this answer









                                  $endgroup$

















                                    1












                                    $begingroup$

                                    Pyth - 39 bytes



                                    LmsMcdK"x^"%2cb)j" + "m++*FhdKsedCM*FyM


                                    Try it online.






                                    share|improve this answer









                                    $endgroup$















                                      1












                                      1








                                      1





                                      $begingroup$

                                      Pyth - 39 bytes



                                      LmsMcdK"x^"%2cb)j" + "m++*FhdKsedCM*FyM


                                      Try it online.






                                      share|improve this answer









                                      $endgroup$



                                      Pyth - 39 bytes



                                      LmsMcdK"x^"%2cb)j" + "m++*FhdKsedCM*FyM


                                      Try it online.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 10 at 17:16









                                      MaltysenMaltysen

                                      21.5k445116




                                      21.5k445116





















                                          1












                                          $begingroup$


                                          JavaScript (Babel Node), 118 bytes



                                          Takes input as (a)(b).





                                          a=>b=>(g=s=>[...s.matchAll(/(-?d+)x.(d+)/g)])(a).flatMap(([_,x,p])=>g(b).map(([_,X,P])=>x*X+'x^'+-(-p-P))).join` + `


                                          Try it online!






                                          share|improve this answer









                                          $endgroup$

















                                            1












                                            $begingroup$


                                            JavaScript (Babel Node), 118 bytes



                                            Takes input as (a)(b).





                                            a=>b=>(g=s=>[...s.matchAll(/(-?d+)x.(d+)/g)])(a).flatMap(([_,x,p])=>g(b).map(([_,X,P])=>x*X+'x^'+-(-p-P))).join` + `


                                            Try it online!






                                            share|improve this answer









                                            $endgroup$















                                              1












                                              1








                                              1





                                              $begingroup$


                                              JavaScript (Babel Node), 118 bytes



                                              Takes input as (a)(b).





                                              a=>b=>(g=s=>[...s.matchAll(/(-?d+)x.(d+)/g)])(a).flatMap(([_,x,p])=>g(b).map(([_,X,P])=>x*X+'x^'+-(-p-P))).join` + `


                                              Try it online!






                                              share|improve this answer









                                              $endgroup$




                                              JavaScript (Babel Node), 118 bytes



                                              Takes input as (a)(b).





                                              a=>b=>(g=s=>[...s.matchAll(/(-?d+)x.(d+)/g)])(a).flatMap(([_,x,p])=>g(b).map(([_,X,P])=>x*X+'x^'+-(-p-P))).join` + `


                                              Try it online!







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Apr 10 at 18:30









                                              ArnauldArnauld

                                              81.6k797336




                                              81.6k797336





















                                                  1












                                                  $begingroup$


                                                  Python 2, 193 bytes





                                                  import re
                                                  f=re.finditer
                                                  lambda a,b:' + '.join(' + '.join(`int(m.group(1))*int(n.group(1))`+'x^'+`int(m.group(2))+int(n.group(2))`for n in f('(-?d+)x^(d+)',b))for m in f('(-?d+)x^(d+)',a))


                                                  Try it online!



                                                  Side note: First time doing a code golf challenge, so sorry if the attempt sucks haha






                                                  share|improve this answer









                                                  $endgroup$








                                                  • 3




                                                    $begingroup$
                                                    Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
                                                    $endgroup$
                                                    – Giuseppe
                                                    Apr 10 at 20:38











                                                  • $begingroup$
                                                    save 12 bytes
                                                    $endgroup$
                                                    – Noodle9
                                                    Apr 11 at 10:23






                                                  • 1




                                                    $begingroup$
                                                    Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
                                                    $endgroup$
                                                    – Jo King
                                                    Apr 11 at 11:23















                                                  1












                                                  $begingroup$


                                                  Python 2, 193 bytes





                                                  import re
                                                  f=re.finditer
                                                  lambda a,b:' + '.join(' + '.join(`int(m.group(1))*int(n.group(1))`+'x^'+`int(m.group(2))+int(n.group(2))`for n in f('(-?d+)x^(d+)',b))for m in f('(-?d+)x^(d+)',a))


                                                  Try it online!



                                                  Side note: First time doing a code golf challenge, so sorry if the attempt sucks haha






                                                  share|improve this answer









                                                  $endgroup$








                                                  • 3




                                                    $begingroup$
                                                    Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
                                                    $endgroup$
                                                    – Giuseppe
                                                    Apr 10 at 20:38











                                                  • $begingroup$
                                                    save 12 bytes
                                                    $endgroup$
                                                    – Noodle9
                                                    Apr 11 at 10:23






                                                  • 1




                                                    $begingroup$
                                                    Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
                                                    $endgroup$
                                                    – Jo King
                                                    Apr 11 at 11:23













                                                  1












                                                  1








                                                  1





                                                  $begingroup$


                                                  Python 2, 193 bytes





                                                  import re
                                                  f=re.finditer
                                                  lambda a,b:' + '.join(' + '.join(`int(m.group(1))*int(n.group(1))`+'x^'+`int(m.group(2))+int(n.group(2))`for n in f('(-?d+)x^(d+)',b))for m in f('(-?d+)x^(d+)',a))


                                                  Try it online!



                                                  Side note: First time doing a code golf challenge, so sorry if the attempt sucks haha






                                                  share|improve this answer









                                                  $endgroup$




                                                  Python 2, 193 bytes





                                                  import re
                                                  f=re.finditer
                                                  lambda a,b:' + '.join(' + '.join(`int(m.group(1))*int(n.group(1))`+'x^'+`int(m.group(2))+int(n.group(2))`for n in f('(-?d+)x^(d+)',b))for m in f('(-?d+)x^(d+)',a))


                                                  Try it online!



                                                  Side note: First time doing a code golf challenge, so sorry if the attempt sucks haha







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Apr 10 at 20:01









                                                  GotCubesGotCubes

                                                  513




                                                  513







                                                  • 3




                                                    $begingroup$
                                                    Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
                                                    $endgroup$
                                                    – Giuseppe
                                                    Apr 10 at 20:38











                                                  • $begingroup$
                                                    save 12 bytes
                                                    $endgroup$
                                                    – Noodle9
                                                    Apr 11 at 10:23






                                                  • 1




                                                    $begingroup$
                                                    Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
                                                    $endgroup$
                                                    – Jo King
                                                    Apr 11 at 11:23












                                                  • 3




                                                    $begingroup$
                                                    Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
                                                    $endgroup$
                                                    – Giuseppe
                                                    Apr 10 at 20:38











                                                  • $begingroup$
                                                    save 12 bytes
                                                    $endgroup$
                                                    – Noodle9
                                                    Apr 11 at 10:23






                                                  • 1




                                                    $begingroup$
                                                    Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
                                                    $endgroup$
                                                    – Jo King
                                                    Apr 11 at 11:23







                                                  3




                                                  3




                                                  $begingroup$
                                                  Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
                                                  $endgroup$
                                                  – Giuseppe
                                                  Apr 10 at 20:38





                                                  $begingroup$
                                                  Welcome to PPCG! I'm not much of a python programmer, but there's probably some room for improvement. Perhaps you can find help at Tips for Golfing in Python or Tips for Golfing in <all languages>! Hope you enjoy the time you spend here :-)
                                                  $endgroup$
                                                  – Giuseppe
                                                  Apr 10 at 20:38













                                                  $begingroup$
                                                  save 12 bytes
                                                  $endgroup$
                                                  – Noodle9
                                                  Apr 11 at 10:23




                                                  $begingroup$
                                                  save 12 bytes
                                                  $endgroup$
                                                  – Noodle9
                                                  Apr 11 at 10:23




                                                  1




                                                  1




                                                  $begingroup$
                                                  Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
                                                  $endgroup$
                                                  – Jo King
                                                  Apr 11 at 11:23




                                                  $begingroup$
                                                  Some quick golfing for 161 bytes. Though looking at the other python answers, re.finditer might not be the shortest approach
                                                  $endgroup$
                                                  – Jo King
                                                  Apr 11 at 11:23











                                                  1












                                                  $begingroup$


                                                  Retina, 110 bytes



                                                  SS+(?=.*n(.+))
                                                  $1#$&
                                                  |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                  $1$4$.($2*$5*)x^$.($3*_$6*
                                                  --|-(0)
                                                  $1


                                                  Try it online! Explanation:



                                                  SS+(?=.*n(.+))
                                                  $1#$&


                                                  Prefix each term in the first input with a #, a copy of the second input, and a space. This means that all of the terms in copies of the second input are preceded by a space and none of the terms from the first input are.



                                                  |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                  $1$4$.($2*$5*)x^$.($3*_$6*


                                                  Match all of the copies of terms in the second input and their corresponding term from the first input. Concatenate any - signs, multiply the coefficients, and add the indices. Finally join all of the resulting substitutions with the string  + .



                                                  --|-(0)
                                                  $1


                                                  Delete any pairs of -s and convert -0 to 0.






                                                  share|improve this answer









                                                  $endgroup$

















                                                    1












                                                    $begingroup$


                                                    Retina, 110 bytes



                                                    SS+(?=.*n(.+))
                                                    $1#$&
                                                    |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                    $1$4$.($2*$5*)x^$.($3*_$6*
                                                    --|-(0)
                                                    $1


                                                    Try it online! Explanation:



                                                    SS+(?=.*n(.+))
                                                    $1#$&


                                                    Prefix each term in the first input with a #, a copy of the second input, and a space. This means that all of the terms in copies of the second input are preceded by a space and none of the terms from the first input are.



                                                    |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                    $1$4$.($2*$5*)x^$.($3*_$6*


                                                    Match all of the copies of terms in the second input and their corresponding term from the first input. Concatenate any - signs, multiply the coefficients, and add the indices. Finally join all of the resulting substitutions with the string  + .



                                                    --|-(0)
                                                    $1


                                                    Delete any pairs of -s and convert -0 to 0.






                                                    share|improve this answer









                                                    $endgroup$















                                                      1












                                                      1








                                                      1





                                                      $begingroup$


                                                      Retina, 110 bytes



                                                      SS+(?=.*n(.+))
                                                      $1#$&
                                                      |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                      $1$4$.($2*$5*)x^$.($3*_$6*
                                                      --|-(0)
                                                      $1


                                                      Try it online! Explanation:



                                                      SS+(?=.*n(.+))
                                                      $1#$&


                                                      Prefix each term in the first input with a #, a copy of the second input, and a space. This means that all of the terms in copies of the second input are preceded by a space and none of the terms from the first input are.



                                                      |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                      $1$4$.($2*$5*)x^$.($3*_$6*


                                                      Match all of the copies of terms in the second input and their corresponding term from the first input. Concatenate any - signs, multiply the coefficients, and add the indices. Finally join all of the resulting substitutions with the string  + .



                                                      --|-(0)
                                                      $1


                                                      Delete any pairs of -s and convert -0 to 0.






                                                      share|improve this answer









                                                      $endgroup$




                                                      Retina, 110 bytes



                                                      SS+(?=.*n(.+))
                                                      $1#$&
                                                      |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                      $1$4$.($2*$5*)x^$.($3*_$6*
                                                      --|-(0)
                                                      $1


                                                      Try it online! Explanation:



                                                      SS+(?=.*n(.+))
                                                      $1#$&


                                                      Prefix each term in the first input with a #, a copy of the second input, and a space. This means that all of the terms in copies of the second input are preceded by a space and none of the terms from the first input are.



                                                      |" + "L$v` (-?)(d+)x.(d+).*?#(-?)(d+)x.(d+)
                                                      $1$4$.($2*$5*)x^$.($3*_$6*


                                                      Match all of the copies of terms in the second input and their corresponding term from the first input. Concatenate any - signs, multiply the coefficients, and add the indices. Finally join all of the resulting substitutions with the string  + .



                                                      --|-(0)
                                                      $1


                                                      Delete any pairs of -s and convert -0 to 0.







                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Apr 10 at 20:34









                                                      NeilNeil

                                                      83k745179




                                                      83k745179





















                                                          1












                                                          $begingroup$


                                                          SNOBOL4 (CSNOBOL4), 192 176 bytes



                                                          	P =INPUT
                                                          Q =INPUT
                                                          D =SPAN(-1234567890)
                                                          P P D . K ARB D . W REM . P :F(O)
                                                          B =Q
                                                          B B D . C ARB D . E REM . B :F(P)
                                                          O =O ' + ' K * C 'x^' W + E :(B)
                                                          O O ' + ' REM . OUTPUT
                                                          END


                                                          Try it online!



                                                          	P =INPUT				;* read P
                                                          Q =INPUT ;* read Q
                                                          D =SPAN(-1234567890) ;* save PATTERN for Digits (or a - sign); equivalent to [0-9\-]+
                                                          P P D . K ARB D . W REM . P :F(O) ;* save the Koefficient and the poWer, saving the REMainder as P, or if no match, goto O
                                                          B =Q ;* set B = Q
                                                          B B D . C ARB D . E REM . B :F(P) ;* save the Coefficient and the powEr, saving the REMainder as B, or if no match, goto P
                                                          O =O ' + ' K * C 'x^' W + E :(B) ;* accumulate the output
                                                          O O ' + ' REM . OUTPUT ;* match ' + ' and OUTPUT the REMainder
                                                          END





                                                          share|improve this answer











                                                          $endgroup$

















                                                            1












                                                            $begingroup$


                                                            SNOBOL4 (CSNOBOL4), 192 176 bytes



                                                            	P =INPUT
                                                            Q =INPUT
                                                            D =SPAN(-1234567890)
                                                            P P D . K ARB D . W REM . P :F(O)
                                                            B =Q
                                                            B B D . C ARB D . E REM . B :F(P)
                                                            O =O ' + ' K * C 'x^' W + E :(B)
                                                            O O ' + ' REM . OUTPUT
                                                            END


                                                            Try it online!



                                                            	P =INPUT				;* read P
                                                            Q =INPUT ;* read Q
                                                            D =SPAN(-1234567890) ;* save PATTERN for Digits (or a - sign); equivalent to [0-9\-]+
                                                            P P D . K ARB D . W REM . P :F(O) ;* save the Koefficient and the poWer, saving the REMainder as P, or if no match, goto O
                                                            B =Q ;* set B = Q
                                                            B B D . C ARB D . E REM . B :F(P) ;* save the Coefficient and the powEr, saving the REMainder as B, or if no match, goto P
                                                            O =O ' + ' K * C 'x^' W + E :(B) ;* accumulate the output
                                                            O O ' + ' REM . OUTPUT ;* match ' + ' and OUTPUT the REMainder
                                                            END





                                                            share|improve this answer











                                                            $endgroup$















                                                              1












                                                              1








                                                              1





                                                              $begingroup$


                                                              SNOBOL4 (CSNOBOL4), 192 176 bytes



                                                              	P =INPUT
                                                              Q =INPUT
                                                              D =SPAN(-1234567890)
                                                              P P D . K ARB D . W REM . P :F(O)
                                                              B =Q
                                                              B B D . C ARB D . E REM . B :F(P)
                                                              O =O ' + ' K * C 'x^' W + E :(B)
                                                              O O ' + ' REM . OUTPUT
                                                              END


                                                              Try it online!



                                                              	P =INPUT				;* read P
                                                              Q =INPUT ;* read Q
                                                              D =SPAN(-1234567890) ;* save PATTERN for Digits (or a - sign); equivalent to [0-9\-]+
                                                              P P D . K ARB D . W REM . P :F(O) ;* save the Koefficient and the poWer, saving the REMainder as P, or if no match, goto O
                                                              B =Q ;* set B = Q
                                                              B B D . C ARB D . E REM . B :F(P) ;* save the Coefficient and the powEr, saving the REMainder as B, or if no match, goto P
                                                              O =O ' + ' K * C 'x^' W + E :(B) ;* accumulate the output
                                                              O O ' + ' REM . OUTPUT ;* match ' + ' and OUTPUT the REMainder
                                                              END





                                                              share|improve this answer











                                                              $endgroup$




                                                              SNOBOL4 (CSNOBOL4), 192 176 bytes



                                                              	P =INPUT
                                                              Q =INPUT
                                                              D =SPAN(-1234567890)
                                                              P P D . K ARB D . W REM . P :F(O)
                                                              B =Q
                                                              B B D . C ARB D . E REM . B :F(P)
                                                              O =O ' + ' K * C 'x^' W + E :(B)
                                                              O O ' + ' REM . OUTPUT
                                                              END


                                                              Try it online!



                                                              	P =INPUT				;* read P
                                                              Q =INPUT ;* read Q
                                                              D =SPAN(-1234567890) ;* save PATTERN for Digits (or a - sign); equivalent to [0-9\-]+
                                                              P P D . K ARB D . W REM . P :F(O) ;* save the Koefficient and the poWer, saving the REMainder as P, or if no match, goto O
                                                              B =Q ;* set B = Q
                                                              B B D . C ARB D . E REM . B :F(P) ;* save the Coefficient and the powEr, saving the REMainder as B, or if no match, goto P
                                                              O =O ' + ' K * C 'x^' W + E :(B) ;* accumulate the output
                                                              O O ' + ' REM . OUTPUT ;* match ' + ' and OUTPUT the REMainder
                                                              END






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Apr 11 at 1:22

























                                                              answered Apr 10 at 18:43









                                                              GiuseppeGiuseppe

                                                              17.9k31153




                                                              17.9k31153





















                                                                  1












                                                                  $begingroup$


                                                                  Perl 6, 114 bytes





                                                                  my&g=*.match(/(-?d+)x^(d+)/,:g)».caps».Map;join " + ",map "[*] $_»0x^[+] $_»1",(g($^a)X g $^b)


                                                                  Try it online!






                                                                  share|improve this answer









                                                                  $endgroup$








                                                                  • 1




                                                                    $begingroup$
                                                                    86 bytes
                                                                    $endgroup$
                                                                    – Jo King
                                                                    Apr 11 at 11:03















                                                                  1












                                                                  $begingroup$


                                                                  Perl 6, 114 bytes





                                                                  my&g=*.match(/(-?d+)x^(d+)/,:g)».caps».Map;join " + ",map "[*] $_»0x^[+] $_»1",(g($^a)X g $^b)


                                                                  Try it online!






                                                                  share|improve this answer









                                                                  $endgroup$








                                                                  • 1




                                                                    $begingroup$
                                                                    86 bytes
                                                                    $endgroup$
                                                                    – Jo King
                                                                    Apr 11 at 11:03













                                                                  1












                                                                  1








                                                                  1





                                                                  $begingroup$


                                                                  Perl 6, 114 bytes





                                                                  my&g=*.match(/(-?d+)x^(d+)/,:g)».caps».Map;join " + ",map "[*] $_»0x^[+] $_»1",(g($^a)X g $^b)


                                                                  Try it online!






                                                                  share|improve this answer









                                                                  $endgroup$




                                                                  Perl 6, 114 bytes





                                                                  my&g=*.match(/(-?d+)x^(d+)/,:g)».caps».Map;join " + ",map "[*] $_»0x^[+] $_»1",(g($^a)X g $^b)


                                                                  Try it online!







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Apr 11 at 3:10









                                                                  bb94bb94

                                                                  1,169713




                                                                  1,169713







                                                                  • 1




                                                                    $begingroup$
                                                                    86 bytes
                                                                    $endgroup$
                                                                    – Jo King
                                                                    Apr 11 at 11:03












                                                                  • 1




                                                                    $begingroup$
                                                                    86 bytes
                                                                    $endgroup$
                                                                    – Jo King
                                                                    Apr 11 at 11:03







                                                                  1




                                                                  1




                                                                  $begingroup$
                                                                  86 bytes
                                                                  $endgroup$
                                                                  – Jo King
                                                                  Apr 11 at 11:03




                                                                  $begingroup$
                                                                  86 bytes
                                                                  $endgroup$
                                                                  – Jo King
                                                                  Apr 11 at 11:03











                                                                  1












                                                                  $begingroup$


                                                                  Python 2, 130 bytes





                                                                  lambda a,b:' + '.join([`v*V`+'x^'+`k+K`for V,K in g(a)for v,k in g(b)])
                                                                  g=lambda s:[map(int,t.split('x^'))for t in s.split(' + ')]


                                                                  Try it online!






                                                                  share|improve this answer









                                                                  $endgroup$

















                                                                    1












                                                                    $begingroup$


                                                                    Python 2, 130 bytes





                                                                    lambda a,b:' + '.join([`v*V`+'x^'+`k+K`for V,K in g(a)for v,k in g(b)])
                                                                    g=lambda s:[map(int,t.split('x^'))for t in s.split(' + ')]


                                                                    Try it online!






                                                                    share|improve this answer









                                                                    $endgroup$















                                                                      1












                                                                      1








                                                                      1





                                                                      $begingroup$


                                                                      Python 2, 130 bytes





                                                                      lambda a,b:' + '.join([`v*V`+'x^'+`k+K`for V,K in g(a)for v,k in g(b)])
                                                                      g=lambda s:[map(int,t.split('x^'))for t in s.split(' + ')]


                                                                      Try it online!






                                                                      share|improve this answer









                                                                      $endgroup$




                                                                      Python 2, 130 bytes





                                                                      lambda a,b:' + '.join([`v*V`+'x^'+`k+K`for V,K in g(a)for v,k in g(b)])
                                                                      g=lambda s:[map(int,t.split('x^'))for t in s.split(' + ')]


                                                                      Try it online!







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Apr 11 at 4:29









                                                                      Chas BrownChas Brown

                                                                      5,2391523




                                                                      5,2391523





















                                                                          1












                                                                          $begingroup$


                                                                          C# (Visual C# Interactive Compiler), 192 190 bytes





                                                                          n=>m=>string.Join(g=" + ",from a in n.Split(g)from b in m.Split(g)select f(a.Split(p="x^")[0])*f(b.Split(p)[0])+p+(f(a.Split(p)[1])+f(b.Split(p)[1])));Func<string,int>f=int.Parse;string p,g;


                                                                          Query syntax seems to be a byte shorter than method syntax.



                                                                          Try it online!






                                                                          share|improve this answer











                                                                          $endgroup$












                                                                          • $begingroup$
                                                                            Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                                                                            $endgroup$
                                                                            – Expired Data
                                                                            Apr 11 at 12:26















                                                                          1












                                                                          $begingroup$


                                                                          C# (Visual C# Interactive Compiler), 192 190 bytes





                                                                          n=>m=>string.Join(g=" + ",from a in n.Split(g)from b in m.Split(g)select f(a.Split(p="x^")[0])*f(b.Split(p)[0])+p+(f(a.Split(p)[1])+f(b.Split(p)[1])));Func<string,int>f=int.Parse;string p,g;


                                                                          Query syntax seems to be a byte shorter than method syntax.



                                                                          Try it online!






                                                                          share|improve this answer











                                                                          $endgroup$












                                                                          • $begingroup$
                                                                            Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                                                                            $endgroup$
                                                                            – Expired Data
                                                                            Apr 11 at 12:26













                                                                          1












                                                                          1








                                                                          1





                                                                          $begingroup$


                                                                          C# (Visual C# Interactive Compiler), 192 190 bytes





                                                                          n=>m=>string.Join(g=" + ",from a in n.Split(g)from b in m.Split(g)select f(a.Split(p="x^")[0])*f(b.Split(p)[0])+p+(f(a.Split(p)[1])+f(b.Split(p)[1])));Func<string,int>f=int.Parse;string p,g;


                                                                          Query syntax seems to be a byte shorter than method syntax.



                                                                          Try it online!






                                                                          share|improve this answer











                                                                          $endgroup$




                                                                          C# (Visual C# Interactive Compiler), 192 190 bytes





                                                                          n=>m=>string.Join(g=" + ",from a in n.Split(g)from b in m.Split(g)select f(a.Split(p="x^")[0])*f(b.Split(p)[0])+p+(f(a.Split(p)[1])+f(b.Split(p)[1])));Func<string,int>f=int.Parse;string p,g;


                                                                          Query syntax seems to be a byte shorter than method syntax.



                                                                          Try it online!







                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Apr 11 at 14:42

























                                                                          answered Apr 10 at 22:17









                                                                          Embodiment of IgnoranceEmbodiment of Ignorance

                                                                          3,024127




                                                                          3,024127











                                                                          • $begingroup$
                                                                            Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                                                                            $endgroup$
                                                                            – Expired Data
                                                                            Apr 11 at 12:26
















                                                                          • $begingroup$
                                                                            Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                                                                            $endgroup$
                                                                            – Expired Data
                                                                            Apr 11 at 12:26















                                                                          $begingroup$
                                                                          Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                                                                          $endgroup$
                                                                          – Expired Data
                                                                          Apr 11 at 12:26




                                                                          $begingroup$
                                                                          Each expression will be a sequence of terms separated by + (with exactly one space on each side) 190 bytes
                                                                          $endgroup$
                                                                          – Expired Data
                                                                          Apr 11 at 12:26











                                                                          1












                                                                          $begingroup$


                                                                          Jelly, 28 bytes



                                                                          ṣ”+ṣ”xV$€)p/ZPSƭ€j⁾x^Ʋ€j“ + 


                                                                          Try it online!



                                                                          Full program. Takes the two polynomials as a list of two strings.



                                                                          Explanation (expanded form)



                                                                          ṣ”+ṣ”xV$€µ€p/ZPSƭ€j⁾x^Ʋ€j“ + ” Arguments: x
                                                                          µ Monadic chain.
                                                                          € Map the monadic link over the argument.
                                                                          Note that this will "pop" the previous chain, so
                                                                          it will really act as a link rather than a
                                                                          sub-chain.
                                                                          ṣ”+ ṣ, right = '+'.
                                                                          Split the left argument on each occurrence of
                                                                          the right.
                                                                          Note that strings in Jelly are lists of
                                                                          single-character Python strings.
                                                                          € Map the monadic link over the argument.
                                                                          $ Make a non-niladic monadic chain of at least
                                                                          two links.
                                                                          ṣ”x ṣ, right = 'x'.
                                                                          Split the left argument on each occurrence of
                                                                          the right.
                                                                          V Evaluate the argument as a niladic link.
                                                                          / Reduce the dyadic link over the argument.
                                                                          p Cartesian product of left and right arguments.
                                                                          € Map the monadic link over the argument.
                                                                          Ʋ Make a non-niladic monadic chain of at least
                                                                          four links.
                                                                          Z Transpose the argument.
                                                                          € Map the monadic link over the argument.
                                                                          ƭ At the first call, call the first link. At the
                                                                          second call, call the second link. Rinse and
                                                                          repeat.
                                                                          P Product: ;1×/$
                                                                          S Sum: ;0+/$
                                                                          j⁾x^ j, right = "x^".
                                                                          Put the right argument between the left one's
                                                                          elements and concatenate the result.
                                                                          j“ + ” j, right = " + ".
                                                                          Put the right argument between the left one's
                                                                          elements and concatenate the result.


                                                                          Aliasing



                                                                          ) is the same as µ€.

                                                                          A trailing is implied and can be omitted.



                                                                          Algorithm



                                                                          Let's say we have this input:



                                                                          ["6x^2 + 7x^1 + -2x^0", "1x^2 + -2x^3"]


                                                                          The first procedure is the Parsing, applied to each of the two polynomials. Let's handle the first one, "6x^2 + 7x^1 + -2x^0":



                                                                          The first step is to split the string by '+', so as to separate the terms. This results in:



                                                                          ["6x^2 ", " 7x^1 ", " -2x^0"]


                                                                          The next step is to split each string by 'x', to separate the coefficient from the exponent. The result is this:



                                                                          [["6", "^2 "], [" 7", "^1 "], [" -2", "^0"]]


                                                                          Currently, it looks like there's a lot of trash in these strings, but that trash is actually unimportant. These strings are all going to be evaluated as niladic Jelly links. Trivially, the spaces are unimportant, as they are not between the digits of the numbers. So we could as well evaluate the below and still get the same result:



                                                                          [["6", "^2"], ["7", "^1"], ["-2", "^0"]]


                                                                          The ^s look a bit more disturbing, but they actually don't do anything either! Well, ^ is the bitwise XOR atom, however niladic chains act like monadic links, except that the first link actually becomes the argument, instead of taking an argument, if it's niladic. If it's not, then the link will have an argument of 0. The exponents have the ^s as their first char, and ^ isn't niladic, so the argument is assumed to be 0. The rest of the string, i.e. the number, is the right argument of ^. So, for example, ^2 is $0text XOR 2=2$. Obviously, $0text XOR n=n$. All the exponents are integer, so we are fine. Therefore, evaluating this instead of the above won't change the result:



                                                                          [["6", "2"], ["7", "1"], ["-2", "0"]]


                                                                          Here we go:



                                                                          [[6, 2], [7, 1], [-2, 0]]


                                                                          This step will also convert "-0" to 0.



                                                                          Since we're parsing both inputs, the result after the Parsing is going to be this:



                                                                          [[[6, 2], [7, 1], [-2, 0]], [[1, 2], [-2, 3]]]


                                                                          The Parsing is now complete. The next procedure is the Multiplication.



                                                                          We first take the Cartesian product of these two lists:



                                                                          [[[6, 2], [1, 2]], [[6, 2], [-2, 3]], [[7, 1], [1, 2]], [[7, 1], [-2, 3]], [[-2, 0], [1, 2]], [[-2, 0], [-2, 3]]]


                                                                          Many pairs are made, each with one element from the left list and one from the right, in order. This also happens to be the intended order of the output. This challenge really asks us to apply multiplicative distributivity, as we're asked not to further process the result after that.



                                                                          The pairs in each pair represent terms we want to multiply, with the first element being the coefficient and the second being the exponent. To multiply the terms, we multiply the coefficients and add the exponents together ($ax^cbx^d=abx^cx^d=ab(x^cx^d)=(ab)x^c+d$). How do we do that? Let's handle the second pair, [[6, 2], [-2, 3]].



                                                                          We first transpose the pair:



                                                                          [[6, -2], [2, 3]]


                                                                          We then take the product of the first pair, and the sum of the second:



                                                                          [-12, 5]


                                                                          The relevant part of the code, PSƭ€, doesn't actually reset its counter for each pair of terms, but, since they're pairs, it doesn't need to.



                                                                          Handling all pairs of terms, we have:



                                                                          [[6, 4], [-12, 5], [7, 3], [-14, 4], [-2, 2], [4, 3]]


                                                                          Here, the Multiplication is done, as we don't have to combine like terms. The final procedure is the Prettyfying.



                                                                          We first join each pair with "x^":



                                                                          [[6, 'x', '^', 4], [-12, 'x', '^', 5], [7, 'x', '^', 3], [-14, 'x', '^', 4], [-2, 'x', '^', 2], [4, 'x', '^', 3]]


                                                                          Then we join the list with " + ":



                                                                          [6, 'x', '^', 4, ' ', '+', ' ', -12, 'x', '^', 5, ' ', '+', ' ', 7, 'x', '^', 3, ' ', '+', ' ', -14, 'x', '^', 4, ' ', '+', ' ', -2, 'x', '^', 2, ' ', '+', ' ', 4, 'x', '^', 3]


                                                                          Notice how we've still got numbers in the list, so it's not really a string. However, Jelly has a process called "stringification", ran right at the end of execution of a program to print the result out. For a list of depth 1, it really just converts each element to its string representation and concatenates the strings together, so we get the desired output:



                                                                          6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3





                                                                          share|improve this answer











                                                                          $endgroup$

















                                                                            1












                                                                            $begingroup$


                                                                            Jelly, 28 bytes



                                                                            ṣ”+ṣ”xV$€)p/ZPSƭ€j⁾x^Ʋ€j“ + 


                                                                            Try it online!



                                                                            Full program. Takes the two polynomials as a list of two strings.



                                                                            Explanation (expanded form)



                                                                            ṣ”+ṣ”xV$€µ€p/ZPSƭ€j⁾x^Ʋ€j“ + ” Arguments: x
                                                                            µ Monadic chain.
                                                                            € Map the monadic link over the argument.
                                                                            Note that this will "pop" the previous chain, so
                                                                            it will really act as a link rather than a
                                                                            sub-chain.
                                                                            ṣ”+ ṣ, right = '+'.
                                                                            Split the left argument on each occurrence of
                                                                            the right.
                                                                            Note that strings in Jelly are lists of
                                                                            single-character Python strings.
                                                                            € Map the monadic link over the argument.
                                                                            $ Make a non-niladic monadic chain of at least
                                                                            two links.
                                                                            ṣ”x ṣ, right = 'x'.
                                                                            Split the left argument on each occurrence of
                                                                            the right.
                                                                            V Evaluate the argument as a niladic link.
                                                                            / Reduce the dyadic link over the argument.
                                                                            p Cartesian product of left and right arguments.
                                                                            € Map the monadic link over the argument.
                                                                            Ʋ Make a non-niladic monadic chain of at least
                                                                            four links.
                                                                            Z Transpose the argument.
                                                                            € Map the monadic link over the argument.
                                                                            ƭ At the first call, call the first link. At the
                                                                            second call, call the second link. Rinse and
                                                                            repeat.
                                                                            P Product: ;1×/$
                                                                            S Sum: ;0+/$
                                                                            j⁾x^ j, right = "x^".
                                                                            Put the right argument between the left one's
                                                                            elements and concatenate the result.
                                                                            j“ + ” j, right = " + ".
                                                                            Put the right argument between the left one's
                                                                            elements and concatenate the result.


                                                                            Aliasing



                                                                            ) is the same as µ€.

                                                                            A trailing is implied and can be omitted.



                                                                            Algorithm



                                                                            Let's say we have this input:



                                                                            ["6x^2 + 7x^1 + -2x^0", "1x^2 + -2x^3"]


                                                                            The first procedure is the Parsing, applied to each of the two polynomials. Let's handle the first one, "6x^2 + 7x^1 + -2x^0":



                                                                            The first step is to split the string by '+', so as to separate the terms. This results in:



                                                                            ["6x^2 ", " 7x^1 ", " -2x^0"]


                                                                            The next step is to split each string by 'x', to separate the coefficient from the exponent. The result is this:



                                                                            [["6", "^2 "], [" 7", "^1 "], [" -2", "^0"]]


                                                                            Currently, it looks like there's a lot of trash in these strings, but that trash is actually unimportant. These strings are all going to be evaluated as niladic Jelly links. Trivially, the spaces are unimportant, as they are not between the digits of the numbers. So we could as well evaluate the below and still get the same result:



                                                                            [["6", "^2"], ["7", "^1"], ["-2", "^0"]]


                                                                            The ^s look a bit more disturbing, but they actually don't do anything either! Well, ^ is the bitwise XOR atom, however niladic chains act like monadic links, except that the first link actually becomes the argument, instead of taking an argument, if it's niladic. If it's not, then the link will have an argument of 0. The exponents have the ^s as their first char, and ^ isn't niladic, so the argument is assumed to be 0. The rest of the string, i.e. the number, is the right argument of ^. So, for example, ^2 is $0text XOR 2=2$. Obviously, $0text XOR n=n$. All the exponents are integer, so we are fine. Therefore, evaluating this instead of the above won't change the result:



                                                                            [["6", "2"], ["7", "1"], ["-2", "0"]]


                                                                            Here we go:



                                                                            [[6, 2], [7, 1], [-2, 0]]


                                                                            This step will also convert "-0" to 0.



                                                                            Since we're parsing both inputs, the result after the Parsing is going to be this:



                                                                            [[[6, 2], [7, 1], [-2, 0]], [[1, 2], [-2, 3]]]


                                                                            The Parsing is now complete. The next procedure is the Multiplication.



                                                                            We first take the Cartesian product of these two lists:



                                                                            [[[6, 2], [1, 2]], [[6, 2], [-2, 3]], [[7, 1], [1, 2]], [[7, 1], [-2, 3]], [[-2, 0], [1, 2]], [[-2, 0], [-2, 3]]]


                                                                            Many pairs are made, each with one element from the left list and one from the right, in order. This also happens to be the intended order of the output. This challenge really asks us to apply multiplicative distributivity, as we're asked not to further process the result after that.



                                                                            The pairs in each pair represent terms we want to multiply, with the first element being the coefficient and the second being the exponent. To multiply the terms, we multiply the coefficients and add the exponents together ($ax^cbx^d=abx^cx^d=ab(x^cx^d)=(ab)x^c+d$). How do we do that? Let's handle the second pair, [[6, 2], [-2, 3]].



                                                                            We first transpose the pair:



                                                                            [[6, -2], [2, 3]]


                                                                            We then take the product of the first pair, and the sum of the second:



                                                                            [-12, 5]


                                                                            The relevant part of the code, PSƭ€, doesn't actually reset its counter for each pair of terms, but, since they're pairs, it doesn't need to.



                                                                            Handling all pairs of terms, we have:



                                                                            [[6, 4], [-12, 5], [7, 3], [-14, 4], [-2, 2], [4, 3]]


                                                                            Here, the Multiplication is done, as we don't have to combine like terms. The final procedure is the Prettyfying.



                                                                            We first join each pair with "x^":



                                                                            [[6, 'x', '^', 4], [-12, 'x', '^', 5], [7, 'x', '^', 3], [-14, 'x', '^', 4], [-2, 'x', '^', 2], [4, 'x', '^', 3]]


                                                                            Then we join the list with " + ":



                                                                            [6, 'x', '^', 4, ' ', '+', ' ', -12, 'x', '^', 5, ' ', '+', ' ', 7, 'x', '^', 3, ' ', '+', ' ', -14, 'x', '^', 4, ' ', '+', ' ', -2, 'x', '^', 2, ' ', '+', ' ', 4, 'x', '^', 3]


                                                                            Notice how we've still got numbers in the list, so it's not really a string. However, Jelly has a process called "stringification", ran right at the end of execution of a program to print the result out. For a list of depth 1, it really just converts each element to its string representation and concatenates the strings together, so we get the desired output:



                                                                            6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3





                                                                            share|improve this answer











                                                                            $endgroup$















                                                                              1












                                                                              1








                                                                              1





                                                                              $begingroup$


                                                                              Jelly, 28 bytes



                                                                              ṣ”+ṣ”xV$€)p/ZPSƭ€j⁾x^Ʋ€j“ + 


                                                                              Try it online!



                                                                              Full program. Takes the two polynomials as a list of two strings.



                                                                              Explanation (expanded form)



                                                                              ṣ”+ṣ”xV$€µ€p/ZPSƭ€j⁾x^Ʋ€j“ + ” Arguments: x
                                                                              µ Monadic chain.
                                                                              € Map the monadic link over the argument.
                                                                              Note that this will "pop" the previous chain, so
                                                                              it will really act as a link rather than a
                                                                              sub-chain.
                                                                              ṣ”+ ṣ, right = '+'.
                                                                              Split the left argument on each occurrence of
                                                                              the right.
                                                                              Note that strings in Jelly are lists of
                                                                              single-character Python strings.
                                                                              € Map the monadic link over the argument.
                                                                              $ Make a non-niladic monadic chain of at least
                                                                              two links.
                                                                              ṣ”x ṣ, right = 'x'.
                                                                              Split the left argument on each occurrence of
                                                                              the right.
                                                                              V Evaluate the argument as a niladic link.
                                                                              / Reduce the dyadic link over the argument.
                                                                              p Cartesian product of left and right arguments.
                                                                              € Map the monadic link over the argument.
                                                                              Ʋ Make a non-niladic monadic chain of at least
                                                                              four links.
                                                                              Z Transpose the argument.
                                                                              € Map the monadic link over the argument.
                                                                              ƭ At the first call, call the first link. At the
                                                                              second call, call the second link. Rinse and
                                                                              repeat.
                                                                              P Product: ;1×/$
                                                                              S Sum: ;0+/$
                                                                              j⁾x^ j, right = "x^".
                                                                              Put the right argument between the left one's
                                                                              elements and concatenate the result.
                                                                              j“ + ” j, right = " + ".
                                                                              Put the right argument between the left one's
                                                                              elements and concatenate the result.


                                                                              Aliasing



                                                                              ) is the same as µ€.

                                                                              A trailing is implied and can be omitted.



                                                                              Algorithm



                                                                              Let's say we have this input:



                                                                              ["6x^2 + 7x^1 + -2x^0", "1x^2 + -2x^3"]


                                                                              The first procedure is the Parsing, applied to each of the two polynomials. Let's handle the first one, "6x^2 + 7x^1 + -2x^0":



                                                                              The first step is to split the string by '+', so as to separate the terms. This results in:



                                                                              ["6x^2 ", " 7x^1 ", " -2x^0"]


                                                                              The next step is to split each string by 'x', to separate the coefficient from the exponent. The result is this:



                                                                              [["6", "^2 "], [" 7", "^1 "], [" -2", "^0"]]


                                                                              Currently, it looks like there's a lot of trash in these strings, but that trash is actually unimportant. These strings are all going to be evaluated as niladic Jelly links. Trivially, the spaces are unimportant, as they are not between the digits of the numbers. So we could as well evaluate the below and still get the same result:



                                                                              [["6", "^2"], ["7", "^1"], ["-2", "^0"]]


                                                                              The ^s look a bit more disturbing, but they actually don't do anything either! Well, ^ is the bitwise XOR atom, however niladic chains act like monadic links, except that the first link actually becomes the argument, instead of taking an argument, if it's niladic. If it's not, then the link will have an argument of 0. The exponents have the ^s as their first char, and ^ isn't niladic, so the argument is assumed to be 0. The rest of the string, i.e. the number, is the right argument of ^. So, for example, ^2 is $0text XOR 2=2$. Obviously, $0text XOR n=n$. All the exponents are integer, so we are fine. Therefore, evaluating this instead of the above won't change the result:



                                                                              [["6", "2"], ["7", "1"], ["-2", "0"]]


                                                                              Here we go:



                                                                              [[6, 2], [7, 1], [-2, 0]]


                                                                              This step will also convert "-0" to 0.



                                                                              Since we're parsing both inputs, the result after the Parsing is going to be this:



                                                                              [[[6, 2], [7, 1], [-2, 0]], [[1, 2], [-2, 3]]]


                                                                              The Parsing is now complete. The next procedure is the Multiplication.



                                                                              We first take the Cartesian product of these two lists:



                                                                              [[[6, 2], [1, 2]], [[6, 2], [-2, 3]], [[7, 1], [1, 2]], [[7, 1], [-2, 3]], [[-2, 0], [1, 2]], [[-2, 0], [-2, 3]]]


                                                                              Many pairs are made, each with one element from the left list and one from the right, in order. This also happens to be the intended order of the output. This challenge really asks us to apply multiplicative distributivity, as we're asked not to further process the result after that.



                                                                              The pairs in each pair represent terms we want to multiply, with the first element being the coefficient and the second being the exponent. To multiply the terms, we multiply the coefficients and add the exponents together ($ax^cbx^d=abx^cx^d=ab(x^cx^d)=(ab)x^c+d$). How do we do that? Let's handle the second pair, [[6, 2], [-2, 3]].



                                                                              We first transpose the pair:



                                                                              [[6, -2], [2, 3]]


                                                                              We then take the product of the first pair, and the sum of the second:



                                                                              [-12, 5]


                                                                              The relevant part of the code, PSƭ€, doesn't actually reset its counter for each pair of terms, but, since they're pairs, it doesn't need to.



                                                                              Handling all pairs of terms, we have:



                                                                              [[6, 4], [-12, 5], [7, 3], [-14, 4], [-2, 2], [4, 3]]


                                                                              Here, the Multiplication is done, as we don't have to combine like terms. The final procedure is the Prettyfying.



                                                                              We first join each pair with "x^":



                                                                              [[6, 'x', '^', 4], [-12, 'x', '^', 5], [7, 'x', '^', 3], [-14, 'x', '^', 4], [-2, 'x', '^', 2], [4, 'x', '^', 3]]


                                                                              Then we join the list with " + ":



                                                                              [6, 'x', '^', 4, ' ', '+', ' ', -12, 'x', '^', 5, ' ', '+', ' ', 7, 'x', '^', 3, ' ', '+', ' ', -14, 'x', '^', 4, ' ', '+', ' ', -2, 'x', '^', 2, ' ', '+', ' ', 4, 'x', '^', 3]


                                                                              Notice how we've still got numbers in the list, so it's not really a string. However, Jelly has a process called "stringification", ran right at the end of execution of a program to print the result out. For a list of depth 1, it really just converts each element to its string representation and concatenates the strings together, so we get the desired output:



                                                                              6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3





                                                                              share|improve this answer











                                                                              $endgroup$




                                                                              Jelly, 28 bytes



                                                                              ṣ”+ṣ”xV$€)p/ZPSƭ€j⁾x^Ʋ€j“ + 


                                                                              Try it online!



                                                                              Full program. Takes the two polynomials as a list of two strings.



                                                                              Explanation (expanded form)



                                                                              ṣ”+ṣ”xV$€µ€p/ZPSƭ€j⁾x^Ʋ€j“ + ” Arguments: x
                                                                              µ Monadic chain.
                                                                              € Map the monadic link over the argument.
                                                                              Note that this will "pop" the previous chain, so
                                                                              it will really act as a link rather than a
                                                                              sub-chain.
                                                                              ṣ”+ ṣ, right = '+'.
                                                                              Split the left argument on each occurrence of
                                                                              the right.
                                                                              Note that strings in Jelly are lists of
                                                                              single-character Python strings.
                                                                              € Map the monadic link over the argument.
                                                                              $ Make a non-niladic monadic chain of at least
                                                                              two links.
                                                                              ṣ”x ṣ, right = 'x'.
                                                                              Split the left argument on each occurrence of
                                                                              the right.
                                                                              V Evaluate the argument as a niladic link.
                                                                              / Reduce the dyadic link over the argument.
                                                                              p Cartesian product of left and right arguments.
                                                                              € Map the monadic link over the argument.
                                                                              Ʋ Make a non-niladic monadic chain of at least
                                                                              four links.
                                                                              Z Transpose the argument.
                                                                              € Map the monadic link over the argument.
                                                                              ƭ At the first call, call the first link. At the
                                                                              second call, call the second link. Rinse and
                                                                              repeat.
                                                                              P Product: ;1×/$
                                                                              S Sum: ;0+/$
                                                                              j⁾x^ j, right = "x^".
                                                                              Put the right argument between the left one's
                                                                              elements and concatenate the result.
                                                                              j“ + ” j, right = " + ".
                                                                              Put the right argument between the left one's
                                                                              elements and concatenate the result.


                                                                              Aliasing



                                                                              ) is the same as µ€.

                                                                              A trailing is implied and can be omitted.



                                                                              Algorithm



                                                                              Let's say we have this input:



                                                                              ["6x^2 + 7x^1 + -2x^0", "1x^2 + -2x^3"]


                                                                              The first procedure is the Parsing, applied to each of the two polynomials. Let's handle the first one, "6x^2 + 7x^1 + -2x^0":



                                                                              The first step is to split the string by '+', so as to separate the terms. This results in:



                                                                              ["6x^2 ", " 7x^1 ", " -2x^0"]


                                                                              The next step is to split each string by 'x', to separate the coefficient from the exponent. The result is this:



                                                                              [["6", "^2 "], [" 7", "^1 "], [" -2", "^0"]]


                                                                              Currently, it looks like there's a lot of trash in these strings, but that trash is actually unimportant. These strings are all going to be evaluated as niladic Jelly links. Trivially, the spaces are unimportant, as they are not between the digits of the numbers. So we could as well evaluate the below and still get the same result:



                                                                              [["6", "^2"], ["7", "^1"], ["-2", "^0"]]


                                                                              The ^s look a bit more disturbing, but they actually don't do anything either! Well, ^ is the bitwise XOR atom, however niladic chains act like monadic links, except that the first link actually becomes the argument, instead of taking an argument, if it's niladic. If it's not, then the link will have an argument of 0. The exponents have the ^s as their first char, and ^ isn't niladic, so the argument is assumed to be 0. The rest of the string, i.e. the number, is the right argument of ^. So, for example, ^2 is $0text XOR 2=2$. Obviously, $0text XOR n=n$. All the exponents are integer, so we are fine. Therefore, evaluating this instead of the above won't change the result:



                                                                              [["6", "2"], ["7", "1"], ["-2", "0"]]


                                                                              Here we go:



                                                                              [[6, 2], [7, 1], [-2, 0]]


                                                                              This step will also convert "-0" to 0.



                                                                              Since we're parsing both inputs, the result after the Parsing is going to be this:



                                                                              [[[6, 2], [7, 1], [-2, 0]], [[1, 2], [-2, 3]]]


                                                                              The Parsing is now complete. The next procedure is the Multiplication.



                                                                              We first take the Cartesian product of these two lists:



                                                                              [[[6, 2], [1, 2]], [[6, 2], [-2, 3]], [[7, 1], [1, 2]], [[7, 1], [-2, 3]], [[-2, 0], [1, 2]], [[-2, 0], [-2, 3]]]


                                                                              Many pairs are made, each with one element from the left list and one from the right, in order. This also happens to be the intended order of the output. This challenge really asks us to apply multiplicative distributivity, as we're asked not to further process the result after that.



                                                                              The pairs in each pair represent terms we want to multiply, with the first element being the coefficient and the second being the exponent. To multiply the terms, we multiply the coefficients and add the exponents together ($ax^cbx^d=abx^cx^d=ab(x^cx^d)=(ab)x^c+d$). How do we do that? Let's handle the second pair, [[6, 2], [-2, 3]].



                                                                              We first transpose the pair:



                                                                              [[6, -2], [2, 3]]


                                                                              We then take the product of the first pair, and the sum of the second:



                                                                              [-12, 5]


                                                                              The relevant part of the code, PSƭ€, doesn't actually reset its counter for each pair of terms, but, since they're pairs, it doesn't need to.



                                                                              Handling all pairs of terms, we have:



                                                                              [[6, 4], [-12, 5], [7, 3], [-14, 4], [-2, 2], [4, 3]]


                                                                              Here, the Multiplication is done, as we don't have to combine like terms. The final procedure is the Prettyfying.



                                                                              We first join each pair with "x^":



                                                                              [[6, 'x', '^', 4], [-12, 'x', '^', 5], [7, 'x', '^', 3], [-14, 'x', '^', 4], [-2, 'x', '^', 2], [4, 'x', '^', 3]]


                                                                              Then we join the list with " + ":



                                                                              [6, 'x', '^', 4, ' ', '+', ' ', -12, 'x', '^', 5, ' ', '+', ' ', 7, 'x', '^', 3, ' ', '+', ' ', -14, 'x', '^', 4, ' ', '+', ' ', -2, 'x', '^', 2, ' ', '+', ' ', 4, 'x', '^', 3]


                                                                              Notice how we've still got numbers in the list, so it's not really a string. However, Jelly has a process called "stringification", ran right at the end of execution of a program to print the result out. For a list of depth 1, it really just converts each element to its string representation and concatenates the strings together, so we get the desired output:



                                                                              6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3






                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited Apr 11 at 20:46

























                                                                              answered Apr 10 at 22:06









                                                                              Erik the OutgolferErik the Outgolfer

                                                                              33.1k429106




                                                                              33.1k429106





















                                                                                  1












                                                                                  $begingroup$

                                                                                  JavaScript, 112 110 bytes



                                                                                  I found two alternatives with the same length. Call with currying syntax: f(A)(B)



                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  -2 bytes (Luis): Remove spaces around split delimiter.




                                                                                  JavaScript, 112 bytes



                                                                                  Using String.prototype.matchAll.



                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )








                                                                                  share|improve this answer











                                                                                  $endgroup$








                                                                                  • 1




                                                                                    $begingroup$
                                                                                    split' + ' => split'+' to save 2 bytes
                                                                                    $endgroup$
                                                                                    – Luis felipe De jesus Munoz
                                                                                    Apr 10 at 20:37











                                                                                  • $begingroup$
                                                                                    @Arnauld Seems fine without them
                                                                                    $endgroup$
                                                                                    – Embodiment of Ignorance
                                                                                    Apr 10 at 22:26










                                                                                  • $begingroup$
                                                                                    @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                                                                                    $endgroup$
                                                                                    – Arnauld
                                                                                    Apr 10 at 22:27















                                                                                  1












                                                                                  $begingroup$

                                                                                  JavaScript, 112 110 bytes



                                                                                  I found two alternatives with the same length. Call with currying syntax: f(A)(B)



                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  -2 bytes (Luis): Remove spaces around split delimiter.




                                                                                  JavaScript, 112 bytes



                                                                                  Using String.prototype.matchAll.



                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )








                                                                                  share|improve this answer











                                                                                  $endgroup$








                                                                                  • 1




                                                                                    $begingroup$
                                                                                    split' + ' => split'+' to save 2 bytes
                                                                                    $endgroup$
                                                                                    – Luis felipe De jesus Munoz
                                                                                    Apr 10 at 20:37











                                                                                  • $begingroup$
                                                                                    @Arnauld Seems fine without them
                                                                                    $endgroup$
                                                                                    – Embodiment of Ignorance
                                                                                    Apr 10 at 22:26










                                                                                  • $begingroup$
                                                                                    @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                                                                                    $endgroup$
                                                                                    – Arnauld
                                                                                    Apr 10 at 22:27













                                                                                  1












                                                                                  1








                                                                                  1





                                                                                  $begingroup$

                                                                                  JavaScript, 112 110 bytes



                                                                                  I found two alternatives with the same length. Call with currying syntax: f(A)(B)



                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  -2 bytes (Luis): Remove spaces around split delimiter.




                                                                                  JavaScript, 112 bytes



                                                                                  Using String.prototype.matchAll.



                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )








                                                                                  share|improve this answer











                                                                                  $endgroup$



                                                                                  JavaScript, 112 110 bytes



                                                                                  I found two alternatives with the same length. Call with currying syntax: f(A)(B)



                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  -2 bytes (Luis): Remove spaces around split delimiter.




                                                                                  JavaScript, 112 bytes



                                                                                  Using String.prototype.matchAll.



                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `





                                                                                  f=
                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )








                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(a=>P(B).map(b=>a[0]*b[0]+'x^'+(a[1]- -b[1]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  f=
                                                                                  A=>B=>(P=x=>x.split`+`.map(x=>x.split`x^`))(A).flatMap(([c,e])=>P(B).map(([C,E])=>c*C+'x^'+(e- -E))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  f=
                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )





                                                                                  f=
                                                                                  A=>B=>(P=x=>[...x.matchAll(/(S+)x.(S+)/g)])(A).flatMap(a=>P(B).map(b=>a[1]*b[1]+'x^'+(a[2]- -b[2]))).join` + `

                                                                                  console.log( f('5x^4')('3x^23') )
                                                                                  console.log( f('6x^2 + 7x^1 + -2x^0')('1x^2 + -2x^3') )
                                                                                  console.log( f('3x^1 + 5x^2 + 2x^4 + 3x^0')('3x^0') )
                                                                                  console.log( f('4x^3 + -2x^14 + 54x^28 + -4x^5')('-0x^7') )
                                                                                  console.log( f('4x^3 + -2x^4 + 0x^255 + -4x^5')('-3x^4 + 2x^2') )






                                                                                  share|improve this answer














                                                                                  share|improve this answer



                                                                                  share|improve this answer








                                                                                  edited Apr 11 at 22:33

























                                                                                  answered Apr 10 at 19:25









                                                                                  darrylyeodarrylyeo

                                                                                  5,3641034




                                                                                  5,3641034







                                                                                  • 1




                                                                                    $begingroup$
                                                                                    split' + ' => split'+' to save 2 bytes
                                                                                    $endgroup$
                                                                                    – Luis felipe De jesus Munoz
                                                                                    Apr 10 at 20:37











                                                                                  • $begingroup$
                                                                                    @Arnauld Seems fine without them
                                                                                    $endgroup$
                                                                                    – Embodiment of Ignorance
                                                                                    Apr 10 at 22:26










                                                                                  • $begingroup$
                                                                                    @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                                                                                    $endgroup$
                                                                                    – Arnauld
                                                                                    Apr 10 at 22:27












                                                                                  • 1




                                                                                    $begingroup$
                                                                                    split' + ' => split'+' to save 2 bytes
                                                                                    $endgroup$
                                                                                    – Luis felipe De jesus Munoz
                                                                                    Apr 10 at 20:37











                                                                                  • $begingroup$
                                                                                    @Arnauld Seems fine without them
                                                                                    $endgroup$
                                                                                    – Embodiment of Ignorance
                                                                                    Apr 10 at 22:26










                                                                                  • $begingroup$
                                                                                    @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                                                                                    $endgroup$
                                                                                    – Arnauld
                                                                                    Apr 10 at 22:27







                                                                                  1




                                                                                  1




                                                                                  $begingroup$
                                                                                  split' + ' => split'+' to save 2 bytes
                                                                                  $endgroup$
                                                                                  – Luis felipe De jesus Munoz
                                                                                  Apr 10 at 20:37





                                                                                  $begingroup$
                                                                                  split' + ' => split'+' to save 2 bytes
                                                                                  $endgroup$
                                                                                  – Luis felipe De jesus Munoz
                                                                                  Apr 10 at 20:37













                                                                                  $begingroup$
                                                                                  @Arnauld Seems fine without them
                                                                                  $endgroup$
                                                                                  – Embodiment of Ignorance
                                                                                  Apr 10 at 22:26




                                                                                  $begingroup$
                                                                                  @Arnauld Seems fine without them
                                                                                  $endgroup$
                                                                                  – Embodiment of Ignorance
                                                                                  Apr 10 at 22:26












                                                                                  $begingroup$
                                                                                  @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                                                                                  $endgroup$
                                                                                  – Arnauld
                                                                                  Apr 10 at 22:27




                                                                                  $begingroup$
                                                                                  @EmbodimentofIgnorance My bad, I misread Luis' comment. I thought it was about the join.
                                                                                  $endgroup$
                                                                                  – Arnauld
                                                                                  Apr 10 at 22:27

















                                                                                  draft saved

                                                                                  draft discarded
















































                                                                                  If this is an answer to a challenge…



                                                                                  • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                                                  • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                                    Explanations of your answer make it more interesting to read and are very much encouraged.


                                                                                  • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.


                                                                                  More generally…



                                                                                  • …Please make sure to answer the question and provide sufficient detail.


                                                                                  • …Avoid asking for help, clarification or responding to other answers (use comments instead).




                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function ()
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182972%2fmultiply-two-integer-polynomials%23new-answer', 'question_page');

                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  Popular posts from this blog

                                                                                  រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

                                                                                  Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

                                                                                  Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221