2
$\begingroup$

I am having a problem with the "Cases" command. If I execute the following code

Cases[(32 W[2, 0])/5  + 32 W[2, 0]^2, W[_, _] | W[_, _]^2, {0, 2}]
Coefficient[(32 W[2, 0])/5  + 32 W[2, 0]^2, 
Cases[(32 W[2, 0])/5  + 32 W[2, 0]^2, W[_, _] | W[_, _]^2, {0, 2}]]

then the outputs are as expected:

{W[2, 0], W[2, 0]^2}
{32/5, 32}

However, the following similar code

Cases[a/(3 + c)  W[2, 0] W[2, 3], W [__, ___] W[_, __], {0, 1}]
Coefficient[a/(3 + c) W[2, 0] W[2, 3], 
Cases[ a/(3 + c) W[2, 0] W[2, 3], W [__, ___] W[_, __], {0, 1}]]

yield nothing in the outputs. Specifically, I got:

{}
{}

The expected outputs should be:

{W[2, 0] W[2, 3]}
{a/(3+c)}

If I get rid of the coefficient 'a/(3 + c)' then this command works

Cases[ W[2, 0] W[2, 3], W [__, ___] W[_, __], {0, 1}]
Coefficient[W[2, 0] W[2, 3], 
Cases[ W[2, 0] W[2, 3], W [__, ___] W[_, __], {0, 1}]]

and the output is as expected:

{W[2, 0] W[2, 3]}
{1}

Is there any way to get the expected output for the second code block above? Thanks in advance for any help or suggestions !

$\endgroup$
5
  • 1
    $\begingroup$ I don't have an immediate solution but you can see why this doesn't work by using FullForm. The FullForm of your expression is Times[a,Power[Plus[3,c],-1],W[2,0],W[2,3]] and that of the pattern you're looking for is Times[W[Blank[],BlankSequence[]],W[BlankSequence[],BlankNullSequence[]]]. This doesn't match since the Times[...] in the expression has more arguments than in the pattern. $\endgroup$
    – banone
    Oct 15 at 10:19
  • 2
    $\begingroup$ change it to Cases[a/(3 + c) W[2, 0] W[2, 3], any_*W[__, ___] W[_, __], {0, 1}] then it will work. But I do not know about your use of Coefficient that follows. $\endgroup$
    – Nasser
    Oct 15 at 10:25
  • $\begingroup$ First, you need HoldPattern. But there is still a problem. Consider, if we give a list instead of an expression: Cases[{a/(3 + c), W[2, 0] W[2, 3]}, HoldPattern[ W[_, _] W[_, _]], 1] it works. But if we change the List to Times: Cases[Times @@ {a/(3 + c), W[2, 0] W[2, 3]}, HoldPattern[ W[_, _] W[_, _]], 1] it does not work. Please report this to : "support@wolfram.com" and post their answer herr. $\endgroup$ Oct 15 at 10:33
  • $\begingroup$ @DanielHuber I don't think this is a bug. Again, by using FullForm we can see what's going on. The FullForm of the list is List[Times[a,Power[Plus[3,c],-1]],Times[W[2,0],W[2,3]]] and that of the times expression is Times[a,Power[Plus[3,c],-1],W[2,0],W[2,3]]. Only the list-expression matches the pattern. $\endgroup$
    – banone
    Oct 15 at 11:11
  • 1
    $\begingroup$ @banone Hi, according to the help, Casesshould work on list as well as expressions: see Cases[expr,pattern,levelspec] $\endgroup$ Oct 15 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.