MathGroup Archive 2010

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

Search the Archive

Re: Substitute expressions with FullSimplify

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108673] Re: Substitute expressions with FullSimplify
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sat, 27 Mar 2010 05:08:50 -0500 (EST)

FullSimplify is neither intended for this purpose nor an effective way to achieve it. The best way, in my opinion, is to use on of a number of "replacement functions" that have been posted over the years by Daniel Lichtblau, which relies on PolynomialReduce.  Here is (I think) the latest version (which has been slightly modified by me):

replacementFunction[expr_, rep_, vars_] :==
 Module[{num == Numerator[expr], den == Denominator[expr],
   hed == Head[expr], base, expon},
  If[PolynomialQ[num, vars] &&
    PolynomialQ[den, vars] && ! NumberQ[den],
   replacementFunction[num, rep, vars]/
    replacementFunction[den, rep, vars],
   If[hed ====== Power && Length[expr] ==== 2,
    base == replacementFunction[expr[[1]], rep, vars];
    expon == replacementFunction[expr[[2]], rep, vars];
    PolynomialReduce[base^expon, rep, vars][[2]],
    If[Head[Evaluate[hed]] ====== Symbol &&
      MemberQ[Attributes[Evaluate[hed]], NumericFunction],
     Map[replacementFunction[#, rep, vars] &, expr],
     PolynomialReduce[expr, rep, vars][[2]]]]]]

Note that you have to write your replacement rule as a "difference" i.e. x^2+y^2====r becomes x^2+y^2-r etc. Here are some replacements:

replacementFunction[Exp[x^2 + y^2], x^2 + y^2 - r, {x, y}]

E^r

replacementFunction[Sin[x + y], x + y - r, {x, y}]

Sin[r]

(The actual function posted by Daniel could not deal with the last example).

Andrzej Kozlowski



On 26 Mar 2010, at 11:36, Guido Walter Pettinari wrote:

> 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: Printing large PDF files generated from Mathematica
  • Next by Date: Re: Substitute expressions with FullSimplify
  • Previous by thread: Re: Substitute expressions with FullSimplify
  • Next by thread: Re: Substitute expressions with FullSimplify