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
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
djm...@comcast.net
http://home.comcast.net/~djmpark/
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 ]
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
@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...@12000.org> wrote:
> "Guido Walter Pettinari" <coccoinom...@gmail.com> wrote in messagenews:ho=
i2n9$qj7$1...@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=
SetAttributes[replacementFunction, Listable]
If you do that, you should get:
replacementFunction[HypergeometricPFQ[{a + b}, {d, e}, x],
a + b - c,
{a}]
{HypergeometricPFQ[{c}, {d, e}, x]}
Of course it is quite likely that one can still find cases where the
function does not work, but if and when they are found it should be easy
tot modify it to include them too.
Andrzej Kozlowski
>> "Guido Walter Pettinari" <coccoinom...@gmail.com> wrote in =
messagenews:ho=
> i2n9$qj7$1...@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 =
SetAttributes[replacementFunction, Listable]
make replacementFunction work also for functions that accept lists as
arguments.
Cheers,
Guido
On Mar 28, 11:56 am, Andrzej Kozlowski <a...@mimuw.edu.pl> wrote:
> I think all you need to do is add :
>
> SetAttributes[replacementFunction, Listable]
>
> If you do that, you should get:
>
> replacementFunction[HypergeometricPFQ[{a + b}, {d, e}, x],
> a + b - c,
> {a}]
>
> {HypergeometricPFQ[{c}, {d, e}, x]}
>
> Of course it is quite likely that one can still find cases where the
> function does not work, but if and when they are found it should be easy
> tot modify it to include them too.
>
> Andrzej Kozlowski
>
> On 28 Mar 2010, at 11:06, Guido Walter Pettinari wrote:
>
>
>
> > 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=
> > i2n9$qj...@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 =
=