How to bring out all factors recursively except one particular term?











up vote
4
down vote

favorite
1












I want to write a function



keepOnly[expr_, keep_]


Such that



keepOnly[f[f2*g[g2*h[h2*keep, h1], g1], f1], keep]


becomes



f2*h2*g2*f[g[h[keep, h1], g1], f1]


In other words, we take all the factors out except for the term keep.










share|improve this question




















  • 1




    Are you sure that g1 in the list of multiplicative factors is not an error? The pattern suggests that it should be g2.
    – Shredderroy
    Dec 7 at 21:30










  • @Shredderroy Fixed. Thanks!
    – ablmf
    Dec 7 at 22:03















up vote
4
down vote

favorite
1












I want to write a function



keepOnly[expr_, keep_]


Such that



keepOnly[f[f2*g[g2*h[h2*keep, h1], g1], f1], keep]


becomes



f2*h2*g2*f[g[h[keep, h1], g1], f1]


In other words, we take all the factors out except for the term keep.










share|improve this question




















  • 1




    Are you sure that g1 in the list of multiplicative factors is not an error? The pattern suggests that it should be g2.
    – Shredderroy
    Dec 7 at 21:30










  • @Shredderroy Fixed. Thanks!
    – ablmf
    Dec 7 at 22:03













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





I want to write a function



keepOnly[expr_, keep_]


Such that



keepOnly[f[f2*g[g2*h[h2*keep, h1], g1], f1], keep]


becomes



f2*h2*g2*f[g[h[keep, h1], g1], f1]


In other words, we take all the factors out except for the term keep.










share|improve this question















I want to write a function



keepOnly[expr_, keep_]


Such that



keepOnly[f[f2*g[g2*h[h2*keep, h1], g1], f1], keep]


becomes



f2*h2*g2*f[g[h[keep, h1], g1], f1]


In other words, we take all the factors out except for the term keep.







pattern-matching expression-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 at 22:03

























asked Dec 7 at 20:27









ablmf

24417




24417








  • 1




    Are you sure that g1 in the list of multiplicative factors is not an error? The pattern suggests that it should be g2.
    – Shredderroy
    Dec 7 at 21:30










  • @Shredderroy Fixed. Thanks!
    – ablmf
    Dec 7 at 22:03














  • 1




    Are you sure that g1 in the list of multiplicative factors is not an error? The pattern suggests that it should be g2.
    – Shredderroy
    Dec 7 at 21:30










  • @Shredderroy Fixed. Thanks!
    – ablmf
    Dec 7 at 22:03








1




1




Are you sure that g1 in the list of multiplicative factors is not an error? The pattern suggests that it should be g2.
– Shredderroy
Dec 7 at 21:30




Are you sure that g1 in the list of multiplicative factors is not an error? The pattern suggests that it should be g2.
– Shredderroy
Dec 7 at 21:30












@Shredderroy Fixed. Thanks!
– ablmf
Dec 7 at 22:03




@Shredderroy Fixed. Thanks!
– ablmf
Dec 7 at 22:03










1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted










exp = f[f2*g[g2*h[h2*keep, h1], g1], f1];

FixedPoint[Replace[#, a_[b_. c_, d___] /; Not[FreeQ[c, keep]] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]




Alternatively,



FixedPoint[Replace[#, a_[b_. c_?(Not@*FreeQ[keep]), d___] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]







share|improve this answer























  • Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
    – Shredderroy
    Dec 7 at 21:27












  • I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
    – Shredderroy
    Dec 7 at 21:28










  • @Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
    – kglr
    Dec 7 at 21:57











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187510%2fhow-to-bring-out-all-factors-recursively-except-one-particular-term%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










exp = f[f2*g[g2*h[h2*keep, h1], g1], f1];

FixedPoint[Replace[#, a_[b_. c_, d___] /; Not[FreeQ[c, keep]] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]




Alternatively,



FixedPoint[Replace[#, a_[b_. c_?(Not@*FreeQ[keep]), d___] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]







share|improve this answer























  • Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
    – Shredderroy
    Dec 7 at 21:27












  • I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
    – Shredderroy
    Dec 7 at 21:28










  • @Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
    – kglr
    Dec 7 at 21:57















up vote
6
down vote



accepted










exp = f[f2*g[g2*h[h2*keep, h1], g1], f1];

FixedPoint[Replace[#, a_[b_. c_, d___] /; Not[FreeQ[c, keep]] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]




Alternatively,



FixedPoint[Replace[#, a_[b_. c_?(Not@*FreeQ[keep]), d___] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]







share|improve this answer























  • Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
    – Shredderroy
    Dec 7 at 21:27












  • I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
    – Shredderroy
    Dec 7 at 21:28










  • @Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
    – kglr
    Dec 7 at 21:57













up vote
6
down vote



accepted







up vote
6
down vote



accepted






exp = f[f2*g[g2*h[h2*keep, h1], g1], f1];

FixedPoint[Replace[#, a_[b_. c_, d___] /; Not[FreeQ[c, keep]] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]




Alternatively,



FixedPoint[Replace[#, a_[b_. c_?(Not@*FreeQ[keep]), d___] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]







share|improve this answer














exp = f[f2*g[g2*h[h2*keep, h1], g1], f1];

FixedPoint[Replace[#, a_[b_. c_, d___] /; Not[FreeQ[c, keep]] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]




Alternatively,



FixedPoint[Replace[#, a_[b_. c_?(Not@*FreeQ[keep]), d___] :> b a[c, d], {0, ∞}] &, exp]



f2 g2 h2 f[g[h[keep, h1], g1], f1]








share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 7 at 21:23

























answered Dec 7 at 20:55









kglr

175k9197402




175k9197402












  • Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
    – Shredderroy
    Dec 7 at 21:27












  • I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
    – Shredderroy
    Dec 7 at 21:28










  • @Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
    – kglr
    Dec 7 at 21:57


















  • Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
    – Shredderroy
    Dec 7 at 21:27












  • I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
    – Shredderroy
    Dec 7 at 21:28










  • @Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
    – kglr
    Dec 7 at 21:57
















Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
– Shredderroy
Dec 7 at 21:27






Doesn't ReplaceRepeated with pretty much the same patterns also do it? ReplaceRepeated[f[f2 * g[g2 * h[h2 * keep, h1], g1], f1], a_[b_ * c_?(Not@*FreeQ[keep]), d_] :> b * a[c, d]]
– Shredderroy
Dec 7 at 21:27














I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
– Shredderroy
Dec 7 at 21:28




I was a bit confused at first because the original question had g1 as one of the multiplicative factors, not g2.
– Shredderroy
Dec 7 at 21:28












@Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
– kglr
Dec 7 at 21:57




@Shredderroy, yes ReplaceRepeated also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]), so I used Replace FixedPoint combination.
– kglr
Dec 7 at 21:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187510%2fhow-to-bring-out-all-factors-recursively-except-one-particular-term%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix