2
$\begingroup$

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];

(***)

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

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;
$\endgroup$

1 Answer 1

2
$\begingroup$

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 *)
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – dtn
    Sep 7, 2022 at 11:03
  • $\begingroup$ 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... $\endgroup$
    – dtn
    Sep 7, 2022 at 11:03
  • 1
    $\begingroup$ 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 : "//." $\endgroup$ Sep 7, 2022 at 11:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.