I'm new to mathematica, so I still cannot use it properly. I want to do symbolic programming.
My question is : is there any way to define our own multiplication. Suppose $a,b$ are arbitrary variables, I want to define $ba=qab$ as the rule, with $q$ is some constant. Then if a compute $baab$, then I want the result become $baab=qabab=q^{2}aabb=q^{2}a^{2}b^{2}$ (I swap the position of the first $b$ from left to $a$ two times).
Answer
As suggested in comment by J.M., NonCommutativeMultiply
might be useful here. Using //.
and two replacement rules you can get desired results.
$ncmRules = {
(* Change b ** a to q a ** b. *)
x___ ** b^n_. ** a^m_. ** y___ :> q^(n m) x ** a^m ** b^n ** y,
(* Replace adjacent powers of same multiplicands by single power. *)
x___ ** y_^n_. ** y_^m_. ** z___ :> x ** y^(n + m) ** z
};
a ** b //. $ncmRules
(* a ** b *)
b ** a //. $ncmRules
(* q a ** b *)
b ** a ** a ** b //. $ncmRules
(* q^2 a^2 ** b^2 *)
b ** a ** b ** a ** a //. $ncmRules
(* q^5 a^3 ** b^2 *)
b ** a ** b ** b ** a //. $ncmRules
(* q^4 a^2 ** b^3 *)
Comments
Post a Comment