2
$\begingroup$

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?

Thanks in advance.

$\endgroup$
1
$\begingroup$
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]]]] :> 
             scalar1*scalar2*z1.z2};

rule3 = {Dot[Times[scalar1__, z1_?(MemberQ[opheads, Head[#]] &)], 
             Times[scalar2__, z2_?(MemberQ[opheads, Head[#]] &)]] :>
          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] *)
$\endgroup$
  • $\begingroup$ @atnemip, my pleasure.. $\endgroup$ – kglr Jun 3 '14 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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