Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The key to doing this is to realize that Mathematica represents <code>a-b</code> as <code>a+((-1)*b)</code>, as you can see from </p> <pre><code>In[1]= FullForm[a-b] Out[2]= Plus[a,Times[-1,b]] </code></pre> <p>For the first part of your question, all you have to do is add this rule:</p> <pre><code>NonCommutativeMultiply[Times[-1, a_], b__] := - a ** b </code></pre> <p>or you can even catch the sign from any position:</p> <pre><code>NonCommutativeMultiply[a___, Times[-1, b_], c___] := - a ** b ** c </code></pre> <p><strong>Update -- part 2.</strong> The general problem with getting scalars to front is that the pattern <code>_Integer</code> in your current rule will only spot things that are manifestly integers. It wont even spot that <code>q</code> is an integer in a construction like <code>Assuming[{Element[q, Integers]}, a**q**b]</code>.<br> To achieve this, you need to examine assumptions, a process that is probably to expensive to be put in the global transformation table. Instead I would write a transformation function that I could apply manually (and maybe remove the current rule form the global table). Something like this might work:</p> <pre><code>NCMScalarReduce[e_] := e //. { NonCommutativeMultiply[a___, i_ /; Simplify@Element[i, Reals],b___] :&gt; i a ** b } </code></pre> <p>The rule used above uses <code>Simplify</code> to explicitly query assumptions, which you can set globally by assigning to <code>$Assumptions</code> or locally by using <code>Assuming</code>:</p> <pre><code>Assuming[{q \[Element] Reals}, NCMScalarReduce[c ** (-q) ** c]] </code></pre> <p>returns <code>-q c**c</code>. </p> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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