3
$\begingroup$

There is the following expression:

expr=a^3**x**Transpose[x]**y+x+y**Transpose[y]**x**a^3+x**Transpose[y]

It is necessary to apply the Transpose[] to all terms in which include variable a^3.

I tried applying sequence of expressions for given variable __x^3__

expr /. __x^3___ -> Transpose[__x__]

But it seems to me that I chose not quite the right pattern. I ask for help, I will be grateful.

EDIT:

Also I try HoldPattern:

HoldPattern[a^3 ** __] -> Transpose[a^3 ** __]
expr/.%

Desired output:

out=Transpose[a^3**x**Transpose[x]**y+x+Transpose[y**Transpose[y]**x**a^3]+x**Transpose[y]
$\endgroup$
3
  • $\begingroup$ Are you looking for something like expr /. Power[x_, 3] :> Transpose[x]? Could you include the output you would like to obtain from your expression expr? What "did not please you" in the last result specifically? $\endgroup$
    – MarcoB
    May 23, 2022 at 11:49
  • $\begingroup$ @MarcoB I need to transpose all the terms that are connected to this variable through non-commutative multiplication. See my edit please $\endgroup$
    – dtn
    May 23, 2022 at 12:34
  • 2
    $\begingroup$ The rule NonCommutativeMultiply[x___, a^3, y___] :>Transpose[NonCommutativeMultiply[x, a^3, y]] does the trick?! $\endgroup$
    – N0va
    May 23, 2022 at 12:48

1 Answer 1

3
$\begingroup$

I am assuming that out was missing a closing bracket and should look like the following:

out =
 Transpose[a^3 ** x ** Transpose[x] ** y] +
  x +
  Transpose[y ** Transpose[y] ** x ** a^3] +
  x ** Transpose[y]

Here are two pretty similar approaches to achieve that:

rule1 = expr /. 
  NonCommutativeMultiply[x___, a^3, y___] :> 
   Transpose[NonCommutativeMultiply[x, a^3, y]];

rule2 = expr /. x_NonCommutativeMultiply?(MemberQ[#, a^3] &) :> Transpose[x];

rule1 == rule2 == out
(* Out: True *)

Note that in the above I "hard-coded" the presence of $a^3$ specifically, and not any variable raised to the third power. If you wanted to apply your rule to another variable, then change the a^3 expression in those rules. If you wanted to apply it to any expression that contains a cube, independent of the name of the variable, then change a^3 to the pattern a_^3 in the left-hand sides of the expressions above.

$\endgroup$
3
  • $\begingroup$ Hello! Please help me modify your code to transpose terms in such expressions: J.a^3 + J.(-P.a^3) /. x_Dot?(MemberQ[#, a^3] &) :> Transpose[x] J.a^3 + J.(-P.a^3) /.x_Dot?(MemberQ[#, (Dot[a___, a^3])] &) :> Transpose[x] Problem: a^3 in the corresponding term can be enclosed in brackets (moreover, the level of brackets can be arbitrary). It is necessary that the program works in the case of a deep level of brackets. Otherwise, only parts of the terms that are next to $a ^ 3$ and are separated from the rest of the term by brackets are transposed $\endgroup$
    – dtn
    Sep 9, 2022 at 16:11
  • 1
    $\begingroup$ @dtn That doesn't seem related to this question, so it would be best if you started a new question with your current problem. $\endgroup$
    – MarcoB
    Sep 9, 2022 at 16:40
  • $\begingroup$ Well, let's try! mathematica.stackexchange.com/questions/273219/… $\endgroup$
    – dtn
    Sep 10, 2022 at 3:18

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.