1
$\begingroup$

From expressions like i σ[1, 1] + p ** x σ[1, 1], I want to be able to extract and separate any functions of p and x. For example, it would return a list of triplets {i,σ [1, 1], 1}, {1,σ[1, 1], p ** x}, but it should work for generic polynomial functions of x and p. Is this possible?

I want to be able to pattern match a product like A p**p** ... x**x ... **x where A is the coefficient and the noncommutative product is always ordered with p before x but has an unknown number of terms x and p.

More examples:

p**p**x 2y -> {2y, 1, p**p**x} 
 
σ[1,1] 2y -> {2y, σ[1,1], 1}
  
g -> {g, 1, 1}  

p -> {1, 1, p}

p**p**x -> {1, 1, p**p**x}
$\endgroup$
4
  • 3
    $\begingroup$ Does Cases[expr, a_. NonCommutativeMultiply[___, p, ___, x, ___]] work for you? $\endgroup$
    – Domen
    Commented Mar 20 at 15:28
  • $\begingroup$ @Domen yes thanks I think I figured it out $\endgroup$
    – Luca
    Commented Mar 26 at 0:15
  • $\begingroup$ @Domen is there a way to make it return 1 when a match is not found? I am using the rule /. a_ s_[Sigma] NonCommutativeMultiply[i___, p, j___, x, k___] -> {a,s,NonCommutativeMultiply[i, p, j, x, k]} but sometimes a, s or NonCommutative... may be missing, in which case I want a 1 in the list $\endgroup$
    – Luca
    Commented Mar 26 at 0:50
  • 1
    $\begingroup$ Can you please edit the question and include an example of your expression? The one that contains both kind of terms. $\endgroup$
    – Domen
    Commented Mar 26 at 8:45

1 Answer 1

0
$\begingroup$
expr = i σ[1, 1] + p ** x σ[1, 1]
Cases[expr, Optional[a_, 1] (σ : σ[__]) *
 (b : NonCommutativeMultiply[___, p, ___, x, ___]) | 
 (Optional[a_, 1] (σ : σ[__]) b_ : 1) :> {a, σ, b}]
(* {{i, σ[1, 1], 1}, {1, σ[1, 1], p ** x}} *)
$\endgroup$
4
  • $\begingroup$ Thanks. for some reason Cases[p ** x, NonCommutativeMultiply[, p, ___, x, ___]] doesn't work when it is only an expression of p and x on its own. it also doesn't work for Cases[p, NonCommutativeMultiply[, p, , x, ___]]. Is there a way to make Cases[p, NonCommutativeMultiply[, p, ___, x, ___]] return p instead of {}? $\endgroup$
    – Luca
    Commented Mar 26 at 11:53
  • $\begingroup$ That's why I told you to please include all possible types of expressions and expected output in your question above :) I'll fix my answer after you do that. $\endgroup$
    – Domen
    Commented Mar 26 at 12:01
  • $\begingroup$ Thanks, I found a way by just using cases to extract the expression and add it to a list and then delete it one by one. For example I extract p**x and then delete it and then do the same for $\sigma$. But for some reason Cases does not work when the expression is just the symbol I am looking for. For example Cases[p,p] returns {} $\endgroup$
    – Luca
    Commented Mar 26 at 12:09
  • $\begingroup$ do you know how to get around this Domen? $\endgroup$
    – Luca
    Commented Apr 5 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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