MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Substitute expressions with FullSimplify

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108672] Re: Substitute expressions with FullSimplify
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sat, 27 Mar 2010 05:08:38 -0500 (EST)

Guido,

Your example is a little too simple. Why not just use x + y -> r? 

This is an area where I'm not a total expert and you may get good methods
from others, but I will try to offer something. When FullSimplify is good,
then it is very very good, but when it is bad it is horrible. I find it
difficult to use ComplexityFunction and make it do precisely what I want. It
is rather like a crude  hammer, when one needs a fine screwdriver.

Here us a routine that substitutes by solving equations. One secret to it
use is to apply it selectively and in order to substitute in an expression.

SubstituteSolve[eqns_List, eliminate_List][expr_] :=
 Module[{result, workexpr, resultrules, neweqns},
  neweqns = {result == expr, Sequence @@ eqns};
  resultrules = 
   Solve[{result == expr, Sequence @@ eqns}, result, eliminate];
  result /. First@resultrules]  

Here is a somewhat complicated example:

expr = x + y + Sin[(x + y)^-2] + (x + y)^3 // ExpandAll 
x + x^3 + y + 3 x^2 y + 3 x y^2 + y^3 + Sin[1/(x^2 + 2 x y + y^2)] 

We don't want to try solving with a variable inside a Sin function so we
apply the routine to the argument of Sin first and then apply it to the
entire remaining expression.

MapAt[SubstituteSolve[{x + y == r}, {x}][#] &, expr, {{7, 1}}] 
SubstituteSolve[{x + y == r}, {x}][%] 
x + x^3 + y + 3 x^2 y + 3 x y^2 + y^3 + Sin[1/r^2] 
r + r^3 + Sin[1/r^2] 

I know that this method can have problems, especially if there are multiple
solutions. But I doubt if FullSimplify will handle those cases either. And
for special cases you might modify the routine, say pick a specific
solution, and apply it to specific portions of the initial expression.

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: Guido Walter Pettinari [mailto:coccoinomane at gmail.com] 


Hello world!

This is my first post in this group, but it has been a while since I
started reading it. I always found it quite useful, therefore I wish
to thank everibody for their contributions!

Here is my problem. Let's say I have an expression. I would like to
substitute all the occurences of a given subexpression in this
expression with a symbol. I want to do it in an intelligent way, i.e.
by using FullSimplify instead of ReplaceAll.

If my expression is:

x^2 + y^2

I know that:

FullSimplify [ x^2 + y^2,   x^2 + y^2 == r ]

will produce 'r' as a result, which is what I want.

However, if my expression is

x + y ,

then

FullSimplify [ x + y,   x + y == r ]

produces 'x + y' and not 'r' ! I tried to use

FullSimplify [ x + y,   x + y == r, ComplexityFunction -> LeafCount ]

but I still get 'x+y' as a result.

Do you have any idea on how to substitute x+y with r in an expression?

Thank you very much,

Guido




  • Prev by Date: Re: Substitute expressions with FullSimplify
  • Next by Date: Re: Managing packages in the workbench
  • Previous by thread: Re: Substitute expressions with FullSimplify
  • Next by thread: Re: Substitute expressions with FullSimplify