%mathpiper,def="ReassembleListTerms"

//Retract("ReassembleListTerms",*);

10 # ReassembleListTerms( disassembly_IsList ) <--
[
    Local(vars,lst,powers,coeffs,ii,pows,coef,term);
    vars   := disassembly[1];
    powers := disassembly[2];
    coeffs := disassembly[3];
    lst    := {};
    For(ii:=1,ii<=Length(powers),ii++)
    [
        pows := powers[ii];
        coef := coeffs[ii];
        //Tell("     ",{pows,coef});
        term  := coef*Product(vars^pows);
        //Tell("          ",term);
        DestructiveAppend(lst,term);
    ];
    lst;
];

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output



%mathpiper_docs,name="ReassembleListTerms",categories="User Functions;Polynomials (Operations)"

*CMD ResassembleListTerms --- returns a list of the terms in an expression

*STD
*CALL
	ResassembleListTerms(disassembly)

*PARMS

{disassembly} -- a disassembly list generated by {DisassembleExpression}

*DESC

After an expression (usually, but not always, a polynomial) has been disassembled
by the function {DisassembleExpression}, the function 
{ReassembleListTerms} converts this into a list of the individual terms
in the expression.  

This is useful in itself, because a list of terms can be processed term-by-term
in any desired manner.

But also, the items in the disassembly may be transformed in various ways before
being reassembled.  This can facilitate automatic transformations of
polynomials or expressions into forms more convenient for further processing.

See the examples below.

The pair of functions, {DisassembleExpression} and {ReassembleListTerms}, 
together constitute a foundation for the analysis of polynomials and -- more
generally -- any expressions which can be regarded as sum or difference of terms.

*E.G.
    
In> ww:=x+2*Sqrt(x)+1
Result: x+2*Sqrt(x)+1
    This expression is really a disguised quadratic

In> ex:=DisassembleExpression(ww)
Result: {{x,Sqrt(x)},{{1,0},{0,1},{0,0}},{1,2,1}}
    The disassembly has recognized Sqrt(x) as a quasi-variable

In> exx:=Subst({x,Sqrt(x)},{t^2,t})ex
Result: {{t^2,t},{{1,0},{0,1},{0,0}},{1,2,1}}
    An algorithm could observe the list {x,Sqrt(x)} and automatically
    perform the substitution to make a change-of-variables

In> Sum(ReassembleListTerms(exx))
Result: t^2+2*t+1
    The expression has now been transformed into an obvious quadratic,
    which can now be solved.


*SEE MultiExpressionList, DisassembleExpression,MM
%/mathpiper_docs

    %output,preserve="false"
      
.   %/output




