3
$\begingroup$

Here's an expression (clearly larger than I need for my point--sorry).

 ((2^(-11 - 2 \[FormalN] - 6 k) 5^(
    35 - 10 \[FormalN]) (1/2 + \[FormalN]) (1 + 5 \[FormalN]) \[Pi]^(
    3/2) Gamma[1/2 + \[FormalN]] Gamma[
     1 + 5 \[FormalN]])/(412770372565881276140091229890633764329641 \
(-1 + Sqrt[5]) (1 + Sqrt[5]) (1/10 + \[FormalN])^2 (1/
      6 + \[FormalN])^2 (1/5 + \[FormalN])^3 (3/10 + \[FormalN])^2 (2/
      5 + \[FormalN]) (3/5 + \[FormalN])^2 (7/10 + \[FormalN])^2 (4/
      5 + \[FormalN]) (5/6 + \[FormalN]) (9/
      10 + \[FormalN])^2 (1 + \[FormalN])^2 (11/10 + \[FormalN])^2 (7/
      6 + \[FormalN]) (6/5 + \[FormalN]) (13/10 + \[FormalN])^2 (17/
      10 + \[FormalN]) (19/10 + \[FormalN]) (2 + \[FormalN]) (21/
      10 + \[FormalN]) (23/10 + \[FormalN]) (1 + 2 \[FormalN])^3 (2 + 
      2 \[FormalN])^2 (3 + 2 \[FormalN]) (8 + 5 \[FormalN]) (13 + 
      5 \[FormalN]) (1/10 + k)^5 (3/10 + k)^5 (1/2 + k)^4 (3/5 + 
      k)^4 (7/10 + k)^4 (9/10 + k)^4 (1 + k) (11/10 + k)^4 (13/10 + 
      k)^4 (3/2 + k)^3 (8/5 + k)^4 (17/10 + k)^3 (19/10 + k)^3 (2 + 
      k) (21/10 + k)^3 (23/10 + k)^3 (5/2 + k)^2 (13/5 + k)^3 (27/10 +
       k)^2 (29/10 + k)^2 (31/10 + k)^2 (33/10 + k)^2 (7/2 + k) (18/
      5 + k)^2 (37/10 + k) (39/10 + k) (41/10 + k) (43/10 + k) (23/5 +
       k) (1/10 + \[FormalN] + k)^2 (3/10 + \[FormalN] + k)^2 (1/
      2 + \[FormalN] + k)^2 (3/5 + \[FormalN] + k)^2 (7/
      10 + \[FormalN] + k)^2 (9/10 + \[FormalN] + 
      k)^2 (1 + \[FormalN] + k)^2 (11/10 + \[FormalN] + k)^2 (13/
      10 + \[FormalN] + k)^2 (3/2 + \[FormalN] + k) (8/
      5 + \[FormalN] + k)^2 (17/10 + \[FormalN] + k) (19/
      10 + \[FormalN] + k) (2 + \[FormalN] + k) (21/10 + \[FormalN] + 
      k) (23/10 + \[FormalN] + k) (13/5 + \[FormalN] + k) Gamma[1/
     10]^3 Gamma[1/6]^4 Gamma[1/5]^4 Gamma[3/10]^3 Gamma[2/5]^4 Gamma[
     3/5]^9 Gamma[7/10]^3 Gamma[4/5]^4 Gamma[5/6]^4 Gamma[9/
     10]^3 Gamma[\[FormalN]] Gamma[1/10 + \[FormalN]]^2 Gamma[
     1/6 + \[FormalN]]^2 Gamma[1/5 + \[FormalN]]^3 Gamma[
     3/10 + \[FormalN]]^2 Gamma[2/5 + \[FormalN]]^3 Gamma[
     3/5 + \[FormalN]]^5 Gamma[7/10 + \[FormalN]]^2 Gamma[
     4/5 + \[FormalN]]^3 Gamma[5/6 + \[FormalN]]^2 Gamma[
     9/10 + \[FormalN]]^2 Gamma[1 + \[FormalN]] Gamma[
     1 + 2 \[FormalN]]^3 Gamma[1/10 + k]^5 Gamma[3/10 + k]^5 Gamma[
     1/2 + k]^5 Gamma[3/5 + k]^4 Gamma[7/10 + k]^5 Gamma[
     9/10 + k]^5 Gamma[1 + k]^8 Gamma[1/10 + \[FormalN] + k]^2 Gamma[
     3/10 + \[FormalN] + k]^2 Gamma[1/2 + \[FormalN] + k]^2 Gamma[
     3/5 + \[FormalN] + k]^2 Gamma[7/10 + \[FormalN] + k]^2 Gamma[
     9/10 + \[FormalN] + k]^2 Gamma[1 + \[FormalN] + k]^2))

I want to replace the occurrence of

(-1 + Sqrt[5]) (1 + Sqrt[5])

in it by its value of 4. A naive replacement doesn't seem to accomplish it.

$\endgroup$
5
$\begingroup$

Illustrate with a simpler expression, please. Anyway, to illustrate:

expr = 2/(3 (-1 + Sqrt[5]) (1 + Sqrt[5]))
d = Denominator[expr]
FullForm[d]  (* ah, we see the problem *)
d /. {Times[x___, -1 + Sqrt[5], 1 + Sqrt[5], y___] -> Times[x, 4, y]}
$\endgroup$
3
$\begingroup$

When you wish to replace something just in a numerator or a denominator, you need to separate the two, e.g.

{numer, denom} = {Numerator[#], Denominator[#]}& @ expr;

then you can operate on them individually. In this case, that is exactly what is needed for ReplaceAll to find your sub-expression, e.g.

denom /. (-1 + Sqrt[5]) (1 + Sqrt[5]) -> bob

I used bob here as it is easy to spot that it is actually doing what you expect. Then, you can reform the fraction via

numer / (denom /. (-1 + Sqrt[5]) (1 + Sqrt[5]) -> 4)
$\endgroup$
  • $\begingroup$ +1 for practicing safe computing (with bob). $\endgroup$ – JimB Aug 23 '16 at 19:49
  • $\begingroup$ That does not actually address his problem, which is that Times has multiple arguments. $\endgroup$ – Alan Aug 23 '16 at 19:49
  • $\begingroup$ rcollyer writes "When you wish to replace something JUST (emphasis added) in a numerator or a denominator". I'd be happy to do the replacement also in the numerator if the term occurred there. I guess you can't just rely on Mathematica finding the term in question in a complicated expression (not necessarily a ratio), without providing some guidance on where it occurs in the expression. $\endgroup$ – Paul B. Slater Aug 23 '16 at 20:02
  • $\begingroup$ @Alan actually it addresses his question: "how do you make a replacement in only the denominator?" Yes, a more general replacement is the fully correct solution, which someone kindly provided, but isolating part of an expression is a useful skill. $\endgroup$ – rcollyer Aug 23 '16 at 20:54
  • $\begingroup$ @PaulB.Slater actually, you can, you just have specify the pattern correctly. $\endgroup$ – rcollyer Aug 23 '16 at 20:55
1
$\begingroup$

I am surprised this question dating 3 years back still has only some kind of hack solutions requiring to disassemble the fraction and reassemble the fractions. And such approach would fail if one has fractions of fractions, or any complicated algebraic structure.

The analysis of the full form shows that the problem comes from that when in the denominator, the -1 power is assigned to each term, hence the expression is not found.

So as a much cleaner solution, I suggest replacing not only A->4, but also 1/A->1/4. Reusing the answers above:

expr = 2/(3 (-1 + Sqrt[5]) (1 + Sqrt[5]));
rule = (-1 + Sqrt[5]) (1 + Sqrt[5]) -> 4;

expr /. rule
expr /. (1/rule[[1]] -> 1/rule[[2]])

Out:

2/(3 (-1 + Sqrt[5]) (1 + Sqrt[5]))
1/6

And it should work everywhere!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.