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[1] ^ e[2]

I would like to be returned the list: { d[x] ^ d[y], e[1] ^ e[2] }; 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?


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, Edmund Mar 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.

  • 2
    $\begingroup$ Your example works on my machine. Try to either remove any definitions you have for d1, d2, a and b, or use :> instead of ->. $\endgroup$ – Marius Ladegård Meyer Mar 2 '16 at 19:53
  • $\begingroup$ It works for me, too. $\endgroup$ – march Mar 2 '16 at 19:54
  • $\begingroup$ Hang on. Let me see if I'm being dumb... $\endgroup$ – Zorawar Mar 2 '16 at 19:58
  • 2
    $\begingroup$ Still works here :p $\endgroup$ – Marius Ladegård Meyer Mar 2 '16 at 20:25
  • 1
    $\begingroup$ 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. $\endgroup$ – Berg Mar 2 '16 at 21:13

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


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.

  • $\begingroup$ 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... $\endgroup$ – Zorawar Mar 2 '16 at 22:05
  • $\begingroup$ In effect, for F2 above, what I need is something like: Simplify[2] d[x] ^ d[y] + Simplify[3] d[x] ^ d[z]. But where the actual coefficients are functions of the variables x, y, z, ... and not just 2 and 3 :) $\endgroup$ – Zorawar Mar 2 '16 at 22:08
  • $\begingroup$ 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. $\endgroup$ – Berg Mar 2 '16 at 23:19
  • $\begingroup$ Interesting. Thanks! I'm going to delete the question soon, but thanks for the suggestion! $\endgroup$ – Zorawar Mar 3 '16 at 20:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.