# Make own rule for multiplication

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).

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Nov 20 '15 at 13:50
• You might consider using NonCommutativeMultiply[] in your implementation. – J. M. is in limbo Nov 20 '15 at 13:56
• I'll try to look at that command and how it works then – R. Kasyfil Nov 20 '15 at 13:59
• yes, I want the general rules to be $ba=qab$, so the multiplication is not commutative. – R. Kasyfil Nov 20 '15 at 18:18
• Can all the q's move all the way to the left in any expression? $-$ that is, do they commute with a and b? – march Nov 20 '15 at 18:49

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 *)

• I haven't check this page for a week, many thanks @jkuczm , it works – R. Kasyfil Nov 30 '15 at 17:42

Define your multiplication by two rules

CircleTimes[x_, y_] := q Times[x, y]


for 2 arguments and

CircleTimes[a___] := Module[{b, c},
If[Length[{a}] > 2,
b = CircleTimes [{a}[], {a}[] ];
c = Join[{b}, {a}[[3 ;; All]] ];
Apply[CircleTimes, c]]
]


It can be written shorter, here I separated into steps for clarity.

Test:

a⊗b⊗b⊗a
(*a^2 b^2 q^3*)

a⊗b a⊗b
(*a^2 b^2 q^2*)


An approach similar to that of yarchik but non-commutative is

CircleTimes[a, b] := Times[a, b]
CircleTimes[b, a] := q Times[a, b]
CircleTimes[z_, z_] := Times[z, z]
CircleTimes[z__] := Module[{zz = {z}, tem}, tem = CircleTimes @@ zz[[-2 ;; -1]];
(CircleTimes @@ Join[zz[[1 ;; -3]], {First@tem}]) Rest@tem]


Then,

a⊗b
(* a b *)

b⊗a
(* a b q *)

a⊗b⊗b⊗a
(* a^2 b^2 q^2 *)


as specified in the Question. Note that this definition of CircleTimes works only for a and b, because those are the only symbols defined in the Question. It could be generalized to other symbols if the OP wished to provide rules for them.