Search code examples
symbolic-mathmaxima

Maxima: How to turn all coefficients of a polynomial positive


For a maths project I am currently using the CAS Maxima (wxMaxima). As the project is almost finished I would like to remain with Maxima, but there is one problem left:

The issue is that I have to convert a certain polynomial P by making all its coefficients positive. I.e. adding up the absolutes of all coefficients (but not taking the absolute value of the whole polynomial), for example

P(...)=-15x^3+3y^2-4x^2

turns to

P'(...)=15x^3+3y^2+4x^2

I could not find an implemented function that would help me with this. And could not find a solution by implementing it with a map function. Do you know a way to solve this issue?

Thank you for your help! Jonas


Solution

  • You can calculate a sum of absolute values:

    P:-15*x^3+3*y^2-4*x^2;
    P2:sum(abs(args(P)[i]),i,1,length(args(P)));
    >> 3*y^2+15*x^2*abs(x)+4*x^2
    

    (unfortunately, here is abs(x) but you can use subst(x,abs(x),P2))

    The same with map:

    P2:map(abs,P);
    

    Or convert an expresstion to string and replace "-" to "+":

    s:string(P);
    s2:ssubst("+","-",s);
    P2:eval_string(s2);
    >> 3*y^2+15*x^3+4*x^2