3
$\begingroup$

Here is an expression (one of many) that involves two complicated terms:

\begin{multline}\text{expression} = \frac{\log \left(\frac{c}{d}\right) \left(2 a d F(a,b,c)+a^2 \left(a^2-2 a (b+c)+b^2+4 b c+c^2\right)\right)}{F(a,b,c)^2} \\ +\frac{\log \left(\frac{a}{b}\right) \left((a+b-c) F(a,b,c)+2 a \left(-2 a^2+a (4 b+c)-2 b^2+b c+c^2\right)\right)}{2 F(a,b,c)^2}\end{multline}

expr = ((a^2 (a^2 + b^2 + 4 b c + c^2 - 2 a (b + c)) + 2 a d f[a, b, c]) Log[c/d])/f[a, b, c]^2 + ((2 a (-2 a^2 - 2 b^2 + b c + c^2 + a (4 b + c)) + (a + b - c) f[a, b, c]) Log[a/b])/(2 f[a, b, c]^2)

They're organized by the logarithms in front. I need to find a rule that replaces F[a,b,c] with a+b+c only in the numerators.

I tried rule = Power[F[a_,b_,c_],p_]/;p>0 :> a + b + c. But expr/.rule fails to match in the numerator (because it has no exponent). What can I do to properly match?

$\endgroup$
1
$\begingroup$

Also this way:

 expr = ((a^2 (a^2 + b^2 + 4 b c + c^2 - 2 a (b + c)) + 
       2 a d f[a, b, c]) Log[c/d])/
   f[a, b, c]^2 + ((2 a (-2 a^2 - 2 b^2 + b c + c^2 + 
          a (4 b + c)) + (a + b - c) f[a, b, c]) Log[
      a/b])/(2 f[a, b, c]^2); 

g[expr_] := Simplify[Numerator[expr] /. f[___] -> a + b + c]/
 Denominator[expr]

Now

Map[g, expr]

(*  (((a + b - c) (a + b + c) + 
    2 a (-2 a^2 - 2 b^2 + b c + c^2 + a (4 b + c))) Log[a/b])/(
 2 f[a, b, c]^2) + (
 a (a (a^2 + b^2 + 4 b c + c^2 - 2 a (b + c)) + 2 (a + b + c) d) Log[
   c/d])/f[a, b, c]^2   *)

Have fun!

$\endgroup$
5
$\begingroup$

the following is not bullet proof but works for your example and I think the trick might also be of use when working out more robust variants:

expr /. Power[n_, d_?Negative] :> Power[n /. f -> ff, d] /.  f -> Plus /. ff -> f

I have often used similar tricks in matching operations: when the stuff you want to not match is so much easier to define do a replacement for it to keep it from matching the actual transformation. Then do that transformation for what is left and finally restore the stuff you didn't want to match...

It probably is fair to note that I of course did not make up that trick myself. I have first seen it in fairly sophisticated symbolic packages used in high energy physics written in FORM. But I guess the idea is most probably as old as the idea of manipulating mathematical expressions with computer programms by pattern matching...

$\endgroup$
2
$\begingroup$

This seems to work, at least for the example you provide.

expr = ((a^2 (a^2 + b^2 + 4 b c + c^2 - 2 a (b + c)) + 2 a d f[a, b, c]) Log[c/d])/f[a, b, c]^2 + ((2 a (-2 a^2 - 2 b^2 + b c + c^2 + a (4 b + c)) + (a + b - c) f[a, b, c]) Log[a/b])/(2 f[a, b, c]^2)


Apart@With[{exp = Together@expr}, 
           ReplaceAll[Numerator[exp], f[a, b, c] :> a + b + c]/
           Denominator[exp]]

(* -(((-a^2 + 4 a^3 - 2 a b - 8 a^2 b - b^2 + 4 a b^2 - 2 a^2 c - 
     2 a b c + c^2 - 2 a c^2) Log[a/b])/(2 f[a, b, c]^2)) + (
 a (a^3 - 2 a^2 b + a b^2 - 2 a^2 c + 4 a b c + a c^2 + 2 a d + 
    2 b d + 2 c d) Log[c/d])/f[a, b, c]^2 *)
$\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.