Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The below only works if you remove the Flat attribute of NonCommutativeMultiply</strong> (Which is something I did by mistake during testing... a rookie mistake!)</p> <p>The simplest thing to do is</p> <pre><code>Unprotect[NonCommutativeMultiply]; NonCommutativeMultiply[a___, 1, b___] := a ** b NonCommutativeMultiply[___, 0, ___] := 0 NonCommutativeMultiply[a_] := a Protect[NonCommutativeMultiply]; </code></pre> <p>The final expression is needed so that <code>a**1</code> simplifies to <code>a</code> instead of <code>NonCommutativeMultiply[a]</code>. You might also need <code>NonCommutativeMultiply[]:=1</code> so that expressions like <code>1**1</code> simplify properly (*). The only problem with all of this, is for large expressions, the pattern is checked against everything and this gets really slow.</p> <p>The above two definitions for 0 and 1 can be combined and generalized to</p> <pre><code>NonCommutativeMultiply[a___, n_?NumericQ, b___] := n a ** b </code></pre> <p>which factors out any numerical terms inside the expression. But this slows down things even more in large expressions, since each term is checked to see if its numerical.</p> <p>To simplify your <code>a**a</code> to <code>a^2</code>, you need something like</p> <pre><code>NonCommutativeMultiply[a___, b_, b_, c___] := a ** b^2 ** c </code></pre> <p>or more generally</p> <pre><code>NonCommutativeMultiply[a___, b_^n_., b_^m_., c___] := a ** b^(n + m) ** c </code></pre> <hr> <p>(*) Note that this is only because the default order that Mathematica puts its <code>DownValues</code> in is not necessarily the best in this case. Change the order so that <code>NonCommutativeMultiply[a_]</code> comes before <code>a___ ** n_?NumericQ ** b___</code> then <code>NonCommutativeMultiply[]</code> won't be generated by the rules, and you won't need that last pattern (unless you produce <code>NonCommutativeMultiply[]</code> some other way).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload