2
$\begingroup$

I have the following expression:

Subscript[x, i] + (-Subscript[x, -1 + i] + Subscript[x, 
    i]) Subscript[\[Zeta], 
  1 + i] + ((Subscript[gf, -1 + i] + Subscript[gh, 
     i]) Subscript[\[Zeta], 1 + i] - 
  Subscript[gf, 
   i] (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 1 + i]) - 
  Subscript[gh, 
   1 + i] (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 1 + i]))/
 L

i.e., enter image description here

And I want to collect the terms in a manner so that I get the following equivalent form of the last expression:

enter image description here

What would be the Mathematica command to go from expression 1 to expression 2?

$\endgroup$

2 Answers 2

2
$\begingroup$

There is no single command for this. You must do this by hand. "Coefficient" is your friend here.

First we get the different coefficients:

c1 = Coefficient[ex, (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 1 + i])];
c2 = Coefficient[ex - (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 
       1 + i]) c1, (Subscript[gf, -1 + i] + Subscript[gh, i])];
c3 = Coefficient[ex - (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 
       1 + i]) c1 - (Subscript[gf, -1 + i] + Subscript[gh, i]) c2, 
   Subscript[\[Zeta], 1 + i]];
c4 = ex - (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 
       1 + i]) c1 - (Subscript[gf, -1 + i] + Subscript[gh, i]) c2 - 
    Subscript[\[Zeta], 1 + i] c3 // Simplify;

Then we assemble the wanted expression:

{1, Subscript[\[Zeta], 1 + i], (Subscript[gf, -1 + i] + Subscript[gh, i]), (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 1 + i])} . {c4, c3, 
  c2, c1}

enter image description here

$\endgroup$
1
  • $\begingroup$ That is a great solution, thanks Daniel! $\endgroup$
    – TDH
    Commented Apr 29, 2022 at 12:27
2
$\begingroup$

Consider the TreeForm of expr:

expr = Subscript[x, 
   i] + (-Subscript[x, -1 + i] + Subscript[x, i]) Subscript[\[Zeta], 
    1 + i] + ((Subscript[gf, -1 + i] + 
        Subscript[gh, i]) Subscript[\[Zeta], 1 + i] - 
     Subscript[gf, 
       i] (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 1 + i]) -
      Subscript[gh, 
       1 + i] (1 + Subscript[\[Zeta], 1 + i] + 
        Subscript[\[Eta], 1 + i]))/L

enter image description here

I have labeled the Tree such that the L in the denominator is marked as "p" and this branch needs to appear with "q". As well as "p" needs to appear as a multiplier for the simplified form of "r" and "s".

The following step is required:

Subscript[gf, 
   i] (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 1 + i]) - 
  Subscript[gh, 
   1 + i] (1 + Subscript[\[Zeta], 1 + i] + Subscript[\[Eta], 
     1 + i]) // Simplify

$$\left(\text{gf}_i-\text{gh}_{i+1}\right) \left(\zeta _{i+1}+\eta _{i+1}+1\right)$$

Initially I wanted to MapAt the function Simplify at this location but I am still struggling with it so some rule-therapy is in order:

expr /. Times[Power[p_], Plus[q_, r_, s_]] :> Plus[
   Times[Power[p], q], Times[Power[p], Simplify[Plus[r, s]]]]

$$-\frac{\left(\text{gf}_i+\text{gh}_{i+1}\right) \left(\zeta _{i+1}+\eta _{i+1}+1\right)}{L}+\frac{\zeta _{i+1} \left(\text{gf}_{i-1}+\text{gh}_i\right)}{L}+\zeta _{i+1} \left(x_i-x_{i-1}\right)+x_i$$

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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