Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. expandNCM[(h : NonCommutativeMultiply)[a___, b_Plus, c___]] :=
  2. Distribute[h[a, b, c], Plus, h, Plus, ExpandNCM[h[##]] &]
  3.  
  4. expandNCM[(h : NonCommutativeMultiply)[a___, b_Times, c___]] :=
  5. Most[b] ExpandNCM[h[a, Last[b], c]]
  6.  
  7. expandNCM[a_] := ExpandAll[a]
  8.  
  9. compressNCM[expr_] := expr /. NonCommutativeMultiply[x__] :>
  10. StringJoin@(ToString /@ {x})
  11.  
  12. toNCM[expr_] :=
  13. expr /. str_String :>
  14. NonCommutativeMultiply @@ (ToExpression /@ Characters[str])
  15.  
  16. encm = expandNCM[(a ** b + c ** d) ** (e ** f + g ** h)]
  17.  
  18. % // compressNCM
  19.  
  20. encm == % // toNCM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement