Re: Substitute expressions with FullSimplify
- To: mathgroup at smc.vnet.net
- Subject: [mg108696] Re: Substitute expressions with FullSimplify
- From: Guido Walter Pettinari <coccoinomane at gmail.com>
- Date: Sun, 28 Mar 2010 04:06:31 -0500 (EST)
- References: <hoi2n9$qj7$1@smc.vnet.net> <hoklk6$snd$1@smc.vnet.net>
Thank you all for the answers! @Andrzej Thank you for pointing me to replacementFunction! It works well in most cases, however it seems to fail when the analyzed expression contains lists. For example, this works fine: replacementFunction[HypergeometricPFQ [ a + b, d, x ], a + b - c, {a}] -> HypergeometricPFQ[c, d, x] however: replacementFunction[HypergeometricPFQ[ {a + b}, {d, e}, x ], a + b - c, {a}] returns this error: Part::partw: Part 2 of {{{1},c}} does not exist. >> HypergeometricPFQ::hdiv: HypergeometricPFQ[{{{1},c}}[[2]],{{0},e},x] does not exist. Arguments are not consistent. >> I cannot avoid using a list as an argument since I need to compute 1F2. Moreover, I am afraid I have not the expertise to fix replacementFunction by myself. As a side note, the code for replacementFunction was messed up in you post: the = symbol was always repeated twice. This is the corrected version I am using: 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]]]]]] Example: expr = x/6 + y/6 - 1/2*(3 + x + y); replacementFunction[expr, x + y - r, {x, y}] // Simplify -> 1/6 (-9 - 2 r) @Nasser My example was probably too simple, as David Park noted. Let me consider two more complex expressions: expr1 = x^2/6 + y^2/6 - 1/2*(3 + x^2 + y^2) expr2 = x/6 + y/6 - 1/2*(3 + x + y) The simple use of ReplaceAll on both of them: expr1 /. x^2 + y^2 -> r expr2 /. x + y -> r produces an unmodified output because, as far as I know, ReplaceAll works synctatically i.e. it only looks for the exact expression to substitute and will ignore something simply related to that like x^2/6 + y^2/6. On the other hand, the use of FullSimplify: FullSimplify[expr1, x^2 + y^2 == r] FullSimplify[expr2, x + y == r] works only in the first case, where I succesfully obtain as an output the expression 1/6 (-9 - 2 r) , while it is useless in the x+y == r case. Using replacementFunction, I achieve what I am looking for: replacementFunction[expr, x + y - r, {x, y}] // Simplify -> 1/6 (-9 - 2 r) Cheers, Guido On Mar 27, 10:11 am, "Nasser M. Abbasi" <n... at 12000.org> wrote: > "Guido Walter Pettinari" <coccoinom... at gmail.com> wrote in messagenews:ho= i2n9$qj7$1 at smc.vnet.net... > > > > > 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 -> LeafCou= nt ] > > > 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? > > Good question. I do not know myself, may be a Mathematica expert can tell > us. But I am just curious, why do you consider > > Simplify [ x + y, x + y == r ] > > more "intelligent" than > > In[39]:= x + y /. {x + y -> r} > Out[39]= r > > ? > > --Nasser
- Follow-Ups:
- Re: Re: Substitute expressions with FullSimplify
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Re: Substitute expressions with FullSimplify