From: S. B. Gray on
Suppose I have a long complex expression in which terms like
(x^2+y^3-x^2y^2+Sqrt[z3+y2]) (for a simple example) appear many times
along with various powers and the reciprocals of it, etc. To make the
expression comprehensible and to make the computation faster, I would
like to substitute say "f1xyz" for it everywhere it appears. The normal
/. and -> substitutions and patterns are not adequate for this. Of
course at evaluation time I want to compute f1xyz only once and not have
the final formula revert to the original variables. How do I prevent that?

Also a welcome addition to Mathematica would be the ability to find these
repeated expressions automatically and put them in, because doing it
manually is very error-prone and slow.

Tips will be appreciated!

Steve Gray


From: David Park on
"The normal
/. and -> substitutions and patterns are not adequate for this."

That sounds like a completely unfounded statement so why don't you
demonstrate it?


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: S. B. Gray [mailto:stevebg(a)ROADRUNNER.COM]


Suppose I have a long complex expression in which terms like
(x^2+y^3-x^2y^2+Sqrt[z3+y2]) (for a simple example) appear many times
along with various powers and the reciprocals of it, etc. To make the
expression comprehensible and to make the computation faster, I would
like to substitute say "f1xyz" for it everywhere it appears. The normal
/. and -> substitutions and patterns are not adequate for this. Of
course at evaluation time I want to compute f1xyz only once and not have
the final formula revert to the original variables. How do I prevent that?

Also a welcome addition to Mathematica would be the ability to find these
repeated expressions automatically and put them in, because doing it
manually is very error-prone and slow.

Tips will be appreciated!

Steve Gray




From: Raffy on
On Jun 5, 4:31 am, "S. B. Gray" <stev...(a)ROADRUNNER.COM> wrote:
> Suppose I have a long complex expression in which terms like
> (x^2+y^3-x^2y^2+Sqrt[z3+y2]) (for a simple example) appear many times
> along with various powers and the reciprocals of it, etc. To make the
> expression comprehensible and to make the computation faster, I would
> like to substitute say "f1xyz" for it everywhere it appears. The normal
> /. and -> substitutions and patterns are not adequate for this. Of
> course at evaluation time I want to compute f1xyz only once and not have
> the final formula revert to the original variables. How do I prevent that=
?
>
> Also a welcome addition to Mathematica would be the ability to find these
> repeated expressions automatically and put them in, because doing it
> manually is very error-prone and slow.
>
> Tips will be appreciated!
>
> Steve Gray

Just curious:
expr = (* your expr *)
ByteCount[expr]
Share[expr]
ByteCount[expr]

Replace is not adequate because of speed? Or difficulty in generating
the replacement expressions?

From: Bob Hanlon on

Make the LHS of the rule as simple as possible

expr = Total[(x^2 + y^3 - x^2 y^2 + Sqrt[z3 + y2])^Range[-2, 2]];

While straight replacement isn't very effective

expr /. (x^2 + y^3 - x^2 y^2 + Sqrt[z3 + y2]) -> f1xyz

f1xyz + (-(x^2*y^2) + x^2 + y^3 + Sqrt[y2 + z3])^2 +
1/(-(x^2*y^2) + x^2 + y^3 + Sqrt[y2 + z3]) +
1/(-(x^2*y^2) + x^2 + y^3 + Sqrt[y2 + z3])^2 + 1

Using a simple LHS pattern that doesn't change in the various components of the FullForm representation works well

repl = Sqrt[z3 + y2] -> f1xyz - (x^2 + y^3 - x^2 y^2);

expr2 = expr /. repl

f1xyz^2+1/f1xyz^2+f1xyz+1/f1xyz+1

val = {x -> 3, y -> 1, y2 -> 6, z3 -> 10};

repl2 = Solve[Equal @@ repl, f1xyz][[1]] /. val

{f1xyz->5}

expr2 /. repl2

781/25

% == (expr /. val)

True


Bob Hanlon

---- "S. B. Gray" <stevebg(a)ROADRUNNER.COM> wrote:

=============
Suppose I have a long complex expression in which terms like
(x^2+y^3-x^2y^2+Sqrt[z3+y2]) (for a simple example) appear many times
along with various powers and the reciprocals of it, etc. To make the
expression comprehensible and to make the computation faster, I would
like to substitute say "f1xyz" for it everywhere it appears. The normal
/. and -> substitutions and patterns are not adequate for this. Of
course at evaluation time I want to compute f1xyz only once and not have
the final formula revert to the original variables. How do I prevent that?

Also a welcome addition to Mathematica would be the ability to find these
repeated expressions automatically and put them in, because doing it
manually is very error-prone and slow.

Tips will be appreciated!

Steve Gray



From: David Bailey on
On 06/06/10 11:42, David Park wrote:
> "The normal
> /. and -> substitutions and patterns are not adequate for this."
>
> That sounds like a completely unfounded statement so why don't you
> demonstrate it?
>
>
> David Park
> djmpark(a)comcast.net
> http://home.comcast.net/~djmpark/
>
>
> From: S. B. Gray [mailto:stevebg(a)ROADRUNNER.COM]
>
>
> Suppose I have a long complex expression in which terms like
> (x^2+y^3-x^2y^2+Sqrt[z3+y2]) (for a simple example) appear many times
> along with various powers and the reciprocals of it, etc. To make the
> expression comprehensible and to make the computation faster, I would
> like to substitute say "f1xyz" for it everywhere it appears. The normal
> /. and -> substitutions and patterns are not adequate for this. Of
> course at evaluation time I want to compute f1xyz only once and not have
> the final formula revert to the original variables. How do I prevent that?
>
> Also a welcome addition to Mathematica would be the ability to find these
> repeated expressions automatically and put them in, because doing it
> manually is very error-prone and slow.
>
> Tips will be appreciated!
>
> Steve Gray
>
>
>
>
In[10]:= x^2 + y^3 + b - x^2 y^2 + Sqrt[z3 + y2] +
Sqrt[x^2 + y^3 - x^2 y^2 + Sqrt[z3 + y2]] //.
x^2 + y^3 - x^2 y^2 + Sqrt[z3 + y2] -> flxyz

Out[10]= b + Sqrt[flxyz] + flxyz

Notice that Mathematica is not fooled by the fact that the 'b' term is
embedded in one of the expressions that need to be replaced. Notice also
that I used //. to obtain complete replacement. I was a little surprised
that this was necessary, but the idea of //. (ReplaceAllRepeated) is
that it looks again at subexpressions that have been changed.

Extracting common sub expressions from a larger expression is a fairly
tricky problem in general to do automatically, but you can get a fair
way with /. and //. .

The safest way to do this is to select the relevant subexpression using
repeated clicking (or Control-.) so that you pick up a whole, valid
expression, and then copy/paste it to the other side of the replacement
operator.

David Bailey

www.dbaileyconsultancy.co.uk