2
$\begingroup$

This is a follow up to this question. In that question they use TagSetDelayed to give a symbol the distributive property. In my case this becomes

NonCommutativeMultiply /: Expand[NonCommutativeMultiply[a_,b_+c_]]:=Expand[NonCommutativeMultiply[a,b]]+
                                    Expand[NonCommutativeMultiply[a,c]]

Indeed when I input it it expands

a**(b + c)//Expand
(* ==> a**b + a**c *)

This is nice but when this expression appear in a sum this expansion doesn't happen and the effect is ruined.

d + a**(b + c)//Expand
(* ==> d + a**(b + c) *)

I tried a few things but nothing worked (including ExpandAll). I want a sum of many of these terms to distribute when I apply Expand.

$\endgroup$
3
  • 1
    $\begingroup$ Would it work if you simply defined NonCommutativeMultiply[a_, b_ + c_] := NonCommutativeMultiply[a, b] + NonCommutativeMultiply[a, c]? Then d + a ** (b + c) automatically returns d + a ** b + a ** c, no Expand required. $\endgroup$
    – MarcoB
    May 18, 2020 at 21:26
  • $\begingroup$ I almost forgot that worked lol. But it's still less versatile. It means if you want to modify expressions you would always have to work with the expanded form. This would work for my specific use case but I'm leaving this question open because I feel this could be useful if it worked. $\endgroup$ May 18, 2020 at 21:34
  • 1
    $\begingroup$ Can you include an example of an expression for which the simpler definition does NOT do what you want? Maybe your minimal working example was too minimal here. $\endgroup$
    – MarcoB
    May 18, 2020 at 21:40

0

Your Answer

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

Browse other questions tagged or ask your own question.