# Defining a rule with functions of arbitrary arguments

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,

(x*A1[u1]).(y*A2[u2])


returns

x*y*A1[u1].A2[u2]


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?

-

operatorset = {A1[u1], A2[u1], A3[u1], A1[u2], A2[u2], A3[u2]};
(* {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]]],
scalar1*scalar2*z1.z2};

scalar1*scalar2*z1.z2}

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


Similarly,

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

-
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