Question: Better way to simplify expressions returned from proc() with non-global symbols

What is the best way to handle this?  Many times one wants to return an expression from inside a proc, which uses local symbls, back to the user (global context).

The problem is, if one tries to simplify this returned expression, adding assumptions on some of the symbols in the expression, it does not work. Since the symbol used in the assuming is global, while the symbol inside the returned expression was local to the proc.

Even though the symbols look the same on the screen, they are actually different symbols, so the simplify does not work as expected.

Here is a simple example

restart;
foo:=proc()
local x;
  return exp(2*sqrt(1/x^2)*x*ln(x)) + exp(sqrt(1/x^2)*x*ln(x)) ;
end proc;

sol:=foo();

simplify(sol) assuming x>0

The above does not work. Since the "x" in assuming x>0 is global, while the "x" in the expression was local to the proc. So even though they look same, they are different symbols.

The standard way to handle this, is to pass the "x" to be used to build the solution, from the user to the proc(), so that when the expression is returned, the "x" used will be the global one. Like this

restart;
foo:=proc(x)
  return exp(2*sqrt(1/x^2)*x*ln(x)) + exp(sqrt(1/x^2)*x*ln(x)) ;
end proc;

sol:=foo(x);

simplify(sol) assuming x>0

Now it works:

But this method is not practical all the time. Suppose the local proc wants to generate an expression with other symbols in it, that the user does not know about. Say alpha, beta, z, eta, and so on. The user does not care about having to pass every possible symbol down on each call.  

Is there a way to tell assuming, that the symbols in assumptions command, are to be taken from the expression itself, and not to be global ones?    i.e. when doing 

simplify(sol) assuming x>0

I want Maple to take that "x" in assuming to be the "x" inside the expression only, and not a global "x".  

This will make life much simpler.  I remember seeing other use of assuming where this could be done, but I can't find it yet.

edit:

This is  just one example, where returning expression with new symbol from local proc can be required sometimes.

This is similar to using constant of integrations _C1 by dsolve when it returns a solution.

But those _C1 are all predefined as system/global symbols. But there can be cases where one needs to use new symbols. 

An example is where int() timesout or it does not produce result.

In this case, instead of leaving it as is (since I need to use the result and do not want Maple to keep evaluating it in the expression it is in), so  I replace int(integrand,x) with Int( new_integrand , alpha=0...x) where new_integrand is the same as integrand but with each in it, is replaced by new symbol alpha

This symbol alpha has to be local to the proc (it was not passed down by user). 

Maple uses _Z sometimes for such a cases, which I do not like. (it looks bad in Latex)

Here is an example

foo:=proc(x)
local int_result,alpha;
local integrand:=1/ln(x^2+1);

int_result:= int(integrand,x);
if has(int_result,'int') then  #did not integrate
   int_result:=Int(subs(x=alpha,integrand),alpha=0..x);
fi;

  return int_result;
end proc:

int_result:=foo(x)

The expression returned contains symbol alpha, which is local to the proc.  Only way around this, is to have the user pass in alpha as well as x, just in case it is needed, which is not practical to do.

This is just one example of many. Another example is when doing some transformation internally (change of variables) to convert the ode from one form to another, and I need to return those intermediate results back to caller. These substitutions use local symbols.

As I said, I could have used _Z or some other system/global known symbol for this. But I do not like how this looks in Latex. And if I need another symbol for another case, I have to look for another one.  Instead, I just make my own local symbols and use them in the expression. (Except for constant of integrations, I use those _C1,_C2, etc....

 

Please Wait...