Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a set of "functions operators", say A1[u],A2[u],A3[u],and I would like to define a rule such that, for instance,




I have been trying something like:

operatorset = {A1[u1],A2[u1],A3[u1],A1[u2],A2[u2],A3[u2]};
distri = Dot -> Composition[Distribute, Dot];
rule1 = {Dot[Times[scalar1__, z1_ /; MemberQ[operatorset, z1]], 
    Times[scalar2__, z2_ /; MemberQ[operatorset, z2]]] -> 
   scalar1*scalar2 *z1.z2};

It works, however, I will need this kind of product when the operators have other arguments, like A1[u1^2], A2[u3], etc.

Is there a way to define an "operatorset" where each operator has arbitrary arguments?

Thanks in advance.

share|improve this question
up vote 1 down vote accepted
operatorset = {A1[u1], A2[u1], A3[u1], A1[u2], A2[u2], A3[u2]};
opheads = DeleteDuplicates[Head /@ operatorset]
(* {A1, A2, A3} *)

One way -- definitely not the best way -- is to match only the Heads of the operators: For example,slightly modifying your pattern

rule2 = {Dot[Times[scalar1__, z1_ /; MemberQ[opheads, Head[z1]]], 
             Times[scalar2__, z2_ /; MemberQ[opheads, Head[z2]]]] :> 

rule3 = {Dot[Times[scalar1__, z1_?(MemberQ[opheads, Head[#]] &)], 
             Times[scalar2__, z2_?(MemberQ[opheads, Head[#]] &)]] :>

(x*A1[t,r,s]).(y*A2[w]) /. rule2 (* or rule3 *)
(* x y A1[t, r, s].A2[w] *)


rule4 = Dot[Times[s1_, op1 : (Alternatives[_A1, _A2, _A3])], 
        Times[s2_, op2 : (Alternatives[_A1, _A2, _A3])]] :> s1 s2 Dot[op1, op2];

or, with pre-defined patterns:

patterns = Alternatives @@ (Blank[#] & /@ opheads);
rule5 = Dot[Times[s1_, op1 : patterns], Times[s2_, op2 : patterns]] :> s1 s2 Dot[op1, op2]

(x*A1[t,r,s]).(y*A2[w]) /. rule4 (* or rule5 *)
(* x y A1[t, r, s].A2[w] *)
share|improve this answer
Thanks a lot kguler! – atnemip Jun 3 '14 at 17:24
@atnemip, my pleasure.. – kglr Jun 3 '14 at 17:25
@atnemip Don't forget to Accept this answer if it solved your problem. – Mr.Wizard Sep 2 '14 at 9:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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