# Pattern matching using Cases: why does this fail? [closed]

I have objects that look like, for example:

F2 = 2 d[x] ^ d[y] + 3 d[x] ^ d[z]


where ^ is Wedge (and not exponentiation). The variables inside of d[...] are not known beforehand and the number of terms is also not known; I would like to extract the coefficients that lie in front of these terms (here 2 and 3). The real objects that I have to work with are enormous, so I would like to do this as efficiently as possible, so I try to extract what differentials I have like this:

Cases[ Level[F2, 1] , _ d[a_] ^ d[b_] -> d[a] ^ d[b] ]


This returns: { d[x] ^ d[y], d[x] ^ d[z] }. So, now I can use Coefficient and all is well.

However, if I have an object like this:

F2 = 2 d[x] ^ d[y] + 3 e ^ e


I would like to be returned the list: { d[x] ^ d[y], e ^ e }; so, I try:

Cases[ Level[F2, 1] , _ d1_[a_] ^ d2_[b_] -> d1[a] ^ d2[b] ]


But this does not work. It returns an empty list. Why? And is there a way to make this work?

Edit

So, contrary to my claim, this does actually work as intended, except that an expression like:

F2 = d[x] ^ d[y]


returns an empty list. I naively expected the code I wrote above to handle an implicit coefficient as well as explicit ones. I needed to modify my code to:

Cases[ Level[F2, 1] , _ d1_[a_] ^ d2_[b_] | d1_[a_] ^ d2_[b_] -> d1[a] ^ d2[b] ]


Thanks for the help! I'll probably delete the question in due course...

## closed as off-topic by Dr. belisarius, MarcoB, m_goldberg, Daniel Lichtblau, EdmundMar 3 '16 at 1:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – MarcoB, Daniel Lichtblau, Edmund
If this question can be reworded to fit the rules in the help center, please edit the question.

• Your example works on my machine. Try to either remove any definitions you have for d1, d2, a and b, or use :> instead of ->. – Marius Ladegård Meyer Mar 2 '16 at 19:53
• It works for me, too. – march Mar 2 '16 at 19:54
• Hang on. Let me see if I'm being dumb... – Zorawar Mar 2 '16 at 19:58
• Still works here :p – Marius Ladegård Meyer Mar 2 '16 at 20:25
• Did you, by any chance, use exponentiation in your last line of code (in Cases)? Because if I use exponentiation I do get an empty list, while using Wedge returns the desired terms. – Berg Mar 2 '16 at 21:13

## 1 Answer

Is it not much easier and faster to simply use the following?

List@@F2/._Wedge->1


A possible solution for the more general problem described in the comments would (perhabs) be:

Replace[F2, a_*w_Wedge -> CustomSimplify[a]*w, {1}]


which applies some custom simplifications to the coefficients of the Wedge-products. One can change the pattern to a_. if the coefficient 1 should also be transformed.

• Yes, but my question is a simplified version of my real needs :) I need to simplify the expressions but Mathematica finds simplifying p-forms very difficult to do, so my strategy is to extract coefficients, simplify them individually and then recombine them; so, I need to track what the coefficients are in front of also. Probably I'm still doing it inefficiently, though... – Zorawar Mar 2 '16 at 22:05
• In effect, for F2 above, what I need is something like: Simplify d[x] ^ d[y] + Simplify d[x] ^ d[z]. But where the actual coefficients are functions of the variables x, y, z, ... and not just 2 and 3 :) – Zorawar Mar 2 '16 at 22:08
• I changed my reply a bit. Those patterns with default values are neat way to simplify code, see the tutorial on Patterns and Transformation Rules. – Berg Mar 2 '16 at 23:19
• Interesting. Thanks! I'm going to delete the question soon, but thanks for the suggestion! – Zorawar Mar 3 '16 at 20:31