3
$\begingroup$

Update: After solving an equation I came up with a complicated expression, is it possible to only keep some specific terms of an expression ? As an example, if the expression is:

enter image description here

How can I keep only those terms that contain \[CapitalDelta] ? (In the output I expect to see an expanded form of the above expr only with terms that contain \[CapitalDelta])

$\endgroup$
2
$\begingroup$
expr = -Δ^2 - 3 ga^2 - gb^2 - Δ δa + δa^2 + Δ δb + δa δb + δb^2;
expr2 = -(Δ^2 - 3 ga^2 - gb^2)^2 - Δ δa + δa^2 + Δ δb + δa δb + δb^2 ;
expr3=(2 + Δ g + 9 Δ g^2 - 3 Δ^2 δa + 3 g^2 δa- 
   18 g^2 δa Sqrt[(4 (-Δ^2 - 3 g^3 - 3 g +Δ δa - δa + Δ δb+ δb δa - δa)^3)])^(1/3) ;

A combination of Replace, DeleteCases and FreeQ can be used to modify the sub-expressions $x_1 + \cdots + x_n$ at the desired level:

Replace[Expand @ expr, p_Plus:> DeleteCases[p, _?(FreeQ[Δ])], All]

-Δ^2 - Δ δa + Δ δb

Replace[Expand @ expr2, p_Plus:> DeleteCases[p, _?(FreeQ[Δ])], All]

6 ga^2 Δ^2 + 2 gb^2 Δ^2 - Δ^4 - Δ δa + Δ δb

Replace[Expand @ expr3, p_Plus:> DeleteCases[p, _?(FreeQ[Δ])], All]

(g Δ + 9 g^2 Δ - 3 Δ^2 δa - 36 g^2 δa Sqrt[(-Δ^2 + Δ δa)^3])^(1/3)

TeXForm @ %

$\small \sqrt[3]{-3 \Delta ^2 \text{$\delta $a}-36 \text{$\delta $a} g^2 \sqrt{\left(\Delta \text{$\delta $a}-\Delta ^2\right)^3}+9 \Delta g^2+\Delta g}$

Change All to {0,1} to keep the expression inside Sqrt intact:

Replace[Expand @ expr3, p_Plus:> DeleteCases[p,_?(FreeQ[Δ])], {0, 1}]

(g Δ + 9 g^2 Δ - 3 Δ^2 δa - 36 g^2 δa Sqrt[(-3 g - 3 g^3 - Δ^2 - 2 δa + Δ δa + δbδa + Δ δb)^3])^(1/3)

TeXForm @ %

$\small\sqrt[3]{-3 \Delta ^2 \text{$\delta $a}+9 \Delta g^2-36 \text{$\delta $a} g^2 \sqrt{\left(-\Delta ^2+\Delta \text{$\delta $a}+\Delta \text{$\delta $b}-2 \text{$\delta $a}+\text{$\delta $b$\delta $a}-3 g^3-3 g\right)^3}+\Delta g}$

$\endgroup$
  • 1
    $\begingroup$ Or Select[expr, !FreeQ[#, Δ]&] or Total@Cases[expr, _?(!FreeQ[#, Δ]&)] $\endgroup$ – Bob Hanlon May 21 at 2:56
  • $\begingroup$ @kglr Thanks, however, if I use a more complicatedterm it does not work. Not sure what is the problem. Try this for example -(Δ^2 - 3 ga^2 - gb^2)^2 - Δ δa + δa^2 + Δ δb + δa δb + δb^2+(Δ δa+1)^3 $\endgroup$ – user1436011 May 21 at 5:48
  • $\begingroup$ @user1436011, you can Expand or ExpandAll the input expression: DeleteCases[ _?(FreeQ[\CapitalDelta]])]@Expand[expr2] or DeleteCases[ _?(FreeQ[\[CapitalDelta]])]@ExpandAll[expr2] $\endgroup$ – kglr May 21 at 5:56
  • $\begingroup$ @kglr :Thanks for helping. Still for the following term, for example, it does not work (2 + Δ g + 9 Δ g^2 - 3 Δ^2 δa + 3 g^2 δa- 18 g^2 δa Sqrt[(4 (-Δ^2 - 3 g^3 - 3 g +Δ δa - δa + Δδb+ δbδa - δa)^3)])^(1/3); $\endgroup$ – user1436011 May 21 at 6:15
  • $\begingroup$ @kglr. The problem is that Mathematica cannot expand this exp3 completely. So, I do not know what the output should be. However, I expect to see only thoes terms that contain \[CapitalDelta]. When I run the above program, some terms without \[CapitalDelta] remain in the output. For example the term 2+3g^2 δa is also in the output. $\endgroup$ – user1436011 May 21 at 6:48
0
$\begingroup$

It might be even more concise to do something like this:

expr[[First/@Position[expr,\[CapitalDelta]]]]
$\endgroup$
  • $\begingroup$ Thanks, however, if I use a more complicated term it does not work. Not sure what is the problem. Try this for example (2 + Δ g + 9 Δ g^2 - 3 Δ^2 δa + 3 g^2 δa- Sqrt[4 (-Δ^2 - 3 g^3 - 3 g +Δ δa - δa + Δδb+ δbδa - δa)^3])^(1/3); $\endgroup$ – user1436011 May 21 at 6:23
  • $\begingroup$ That is a different problem. Your original question concerned terms as part of an expanded sum. This new example is about terms within an expression that cannot be expanded. As a result, this is feeling like subject-creep. It might be helpful to know more about what it is you are trying to accomplish. $\endgroup$ – Kevin Ausman May 21 at 17:34
  • $\begingroup$ I am new to Mathematica. Initially I thought the same code can be used for both terms. $\endgroup$ – user1436011 May 22 at 0:52

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.