How to bring out all factors recursively except one particular term?
up vote
4
down vote
favorite
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
add a comment |
up vote
4
down vote
favorite
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
1
Are you sure thatg1
in the list of multiplicative factors is not an error? The pattern suggests that it should beg2
.
– Shredderroy
yesterday
@Shredderroy Fixed. Thanks!
– ablmf
yesterday
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
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
pattern-matching expression-manipulation
edited yesterday
asked yesterday
ablmf
23917
23917
1
Are you sure thatg1
in the list of multiplicative factors is not an error? The pattern suggests that it should beg2
.
– Shredderroy
yesterday
@Shredderroy Fixed. Thanks!
– ablmf
yesterday
add a comment |
1
Are you sure thatg1
in the list of multiplicative factors is not an error? The pattern suggests that it should beg2
.
– Shredderroy
yesterday
@Shredderroy Fixed. Thanks!
– ablmf
yesterday
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
yesterday
Are you sure that
g1
in the list of multiplicative factors is not an error? The pattern suggests that it should be g2
.– Shredderroy
yesterday
@Shredderroy Fixed. Thanks!
– ablmf
yesterday
@Shredderroy Fixed. Thanks!
– ablmf
yesterday
add a comment |
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]
Doesn'tReplaceRepeated
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
yesterday
I was a bit confused at first because the original question hadg1
as one of the multiplicative factors, notg2
.
– Shredderroy
yesterday
@Shredderroy, yesReplaceRepeated
also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]
), so I usedReplace
FixedPoint
combination.
– kglr
yesterday
add a comment |
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]
Doesn'tReplaceRepeated
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
yesterday
I was a bit confused at first because the original question hadg1
as one of the multiplicative factors, notg2
.
– Shredderroy
yesterday
@Shredderroy, yesReplaceRepeated
also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]
), so I usedReplace
FixedPoint
combination.
– kglr
yesterday
add a comment |
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]
Doesn'tReplaceRepeated
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
yesterday
I was a bit confused at first because the original question hadg1
as one of the multiplicative factors, notg2
.
– Shredderroy
yesterday
@Shredderroy, yesReplaceRepeated
also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]
), so I usedReplace
FixedPoint
combination.
– kglr
yesterday
add a comment |
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]
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]
edited yesterday
answered yesterday
kglr
175k9197402
175k9197402
Doesn'tReplaceRepeated
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
yesterday
I was a bit confused at first because the original question hadg1
as one of the multiplicative factors, notg2
.
– Shredderroy
yesterday
@Shredderroy, yesReplaceRepeated
also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]
), so I usedReplace
FixedPoint
combination.
– kglr
yesterday
add a comment |
Doesn'tReplaceRepeated
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
yesterday
I was a bit confused at first because the original question hadg1
as one of the multiplicative factors, notg2
.
– Shredderroy
yesterday
@Shredderroy, yesReplaceRepeated
also works an is simpler. It doesn't work with the pattern I had tried (a_[b_. c_, d___]
), so I usedReplace
FixedPoint
combination.
– kglr
yesterday
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
yesterday
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
yesterday
I was a bit confused at first because the original question had
g1
as one of the multiplicative factors, not g2
.– Shredderroy
yesterday
I was a bit confused at first because the original question had
g1
as one of the multiplicative factors, not g2
.– Shredderroy
yesterday
@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
yesterday
@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
yesterday
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Are you sure that
g1
in the list of multiplicative factors is not an error? The pattern suggests that it should beg2
.– Shredderroy
yesterday
@Shredderroy Fixed. Thanks!
– ablmf
yesterday