# Simplifying expressions with dot or non-commutative product (without the help of NCAlgebra)

I have two expressions (with dot and non-commutative product):

$$f=J^T.x+J.x$$

$$g=J^T**x+J**x$$

I want to simplify them and get the following expressions:

$$f=(J^T+J).x$$

$$g=(J^T+J)**x$$

In an attempt to solve this problem, I used some tricks from: Symbolic Tensors

\$Assumptions = {x \[Element] Arrays[{3, 1}, Reals], J \[Element] Matrices[{3, 3}, Reals]};

expr = Transpose[J].x + J.x // TensorReduce


But the expression doesn't change. What needs to be done so that Mathematica can simplify expressions with scalar and non-commutative products?

Some EDIT:

Clear["Derivative"]; ClearAll["Global*"];

Unprotect[Dot, Plus];

(*Dot rules*)
Dot[A_, x_] + Dot[B_, x_] := Dot[A + B, x];
Protect[Dot, Plus];

(***)

Clear["Derivative"]; ClearAll["Global*"];

Unprotect[NonCommutativeMultiply, Plus];

(*NonCommutativeMultiply rules*)
NonCommutativeMultiply[A_, x_] + NonCommutativeMultiply[B_, x_] :=
NonCommutativeMultiply[A + B, x];
Protect[NonCommutativeMultiply, Plus];

(***)

J ** x + Transpose[J] ** x // TraditionalForm;


Try:

f = Transpose[J] . x + J . x
f /. c1_ . x + c2_ . x -> (c1 + c2) . x

(* J + Transpose[J]) . x *)


Note: take care to put a space between "" and "." because "." represents an argument that can be omitted and the above does not work:

f /. c1_. x + c2_. x -> (c1 + c2) . x
(* J . x + Transpose[J] . x *)

• Hi Daniel! Thank you for your answer! Literally an hour ago I was visited by a similar thought, only I implemented it in the form of a general rule. Check out my edit. Your version is simpler and more compact. Let each user who reads the topic choose the option that is convenient for him.
– dtn
Sep 7, 2022 at 11:03
• In general, I was interested in the following subtlety in this problem. Let's say we have a very complex multi-component vector-matrix expression. There is a problem in the priority of simplification options created by a set of simplification rules and their possible contradictions, as well as the search for an optimal simplified variant. But these are just thoughts...
– dtn
Sep 7, 2022 at 11:03
• Your solution is also o.k. Priority problems may be solved by forceing an order on rules like e.g.: ... /. ruleset1 /. ruleset2 ... Or maybe you need ReplaceRepeated : "//." Sep 7, 2022 at 11:09