Question: replacing pattern in expression

For display only purposes, to get the Latex looking better, I replace the constant of integrations Maple using which is _C1, _C2., etc... to c[1],c[2],..... 

This is something I do at the very end of computation, just before I get the Latex of the expression.

The way I do this now, is not good. .And I am asking if there is a better way to do this replacement. In Mathematica, I can do this using a patterm where I tell it to replace  C?  by c[?] where ? means anything.

Is it possible to do something like this in Maple? Here is an example

sol:=dsolve(diff(x(t),t$3) = x(t));

This gives solution as

And just before ask for the Latex of this, I change it to 

This is done using a function. Please see worksheet below.

The problem with the current solution is that I do not know how many _C there are, so I assumed 10 as worst case, which works for my case. But I prefer a more general solution, may be using pattern in the subs command?


 

restart;

replace_all_C:=proc(expr::anything)
local n,N,c;
local new_expr;
local max_count :=10; #worst case, since do not know how many

    new_expr:=expr;
    for n from 1 to max_count do
        N:=convert(n,string);
        new_expr:=subs( :-parse(cat("_C",N))=:-parse(cat("c[",N,"]")),new_expr);
    od:
    return new_expr;
end proc:

sol:=dsolve(diff(x(t),t$3) = x(t));
tmp:=replace_all_C(sol)

x(t) = _C1*exp(t)+_C2*exp(-(1/2)*t)*sin((1/2)*3^(1/2)*t)+_C3*exp(-(1/2)*t)*cos((1/2)*3^(1/2)*t)

x(t) = c[1]*exp(t)+c[2]*exp(-(1/2)*t)*sin((1/2)*3^(1/2)*t)+c[3]*exp(-(1/2)*t)*cos((1/2)*3^(1/2)*t)

 


 

Download q.mw

And now I get the latex of tmp in the above, instead of sol which looks better.

 

 

Please Wait...