Re: Apart, Factor on different operations
- To: mathgroup at smc.vnet.net
- Subject: [mg41323] Re: [mg41293] Apart, Factor on different operations
- From: Bobby Treat <drmajorbob at mailblocks.com>
- Date: Wed, 14 May 2003 08:18:37 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
This comes close: op[x_, y_ + z_] := op[x, y] + op[x, z] op[x_ + y_, z_] := op[x, z] + op[y, z] term = x + y op[term, z] Factor[% /. {op -> Times}] % /. Plus -> plus % /. Times -> op % /. plus[a__] :> HoldForm[Plus[a]] But Times is commutative and op is not, so this won't always work the way you want. Perhaps this will do: reversal = { op[a_, b_] + op[a_, c_] :> HoldForm@op[a, b + c], op[a_, b_] + op[c_, b_] :> HoldForm@op[a + c, b] }; op[term, z] % /. reversal In both cases, HoldForm is required to prevent undoing the factorization you just did. Keep in mind that defining op[x_, y_ + z_] := op[x, y] + op[x, z] op[x_ + y_, z_] := op[x, z] + op[y, z] means telling Mathematica to try the same rules as in "expand", but to do so every time op is encountered. It may be better to control when that happens: ClearAll@op expand = { op[a_, b_ + c_] :> op[a, b] + op[a, c], op[b_ + c_, a_] :> op[b, a] + op[c, a] }; factor = { op[a_, b_] + op[a_, c_] :> op[a, b + c], op[a_, b_] + op[c_, b_] :> op[a + c, b] }; op[x + y, z] % /. expand % /. factor op[x, y + z] % /. expand % /. factor You can even use Factor and Expand: ClearAll@op expand = { op[a_, b_ + c_] :> op[a, b] + op[a, c], op[b_ + c_, a_] :> op[b, a] + op[c, a] }; factor = { op[a_, b_] + op[a_, c_] :> op[a, b + c], op[a_, b_] + op[c_, b_] :> op[a + c, b] }; Unprotect@Factor; Factor[op[a__] + op[b__]] := op[a] + op[b] /. factor Protect@Factor; op /: Expand[op[a__]] := op[a] /. expand op[x + y, z] % // Expand % // Factor op[x, y + z] % // Expand % // Factor That may slow down use of Factor for other purposes, however. It also doesn't handle (as is) things like op[x, w + y + z] Bobby -----Original Message----- From: Paolo Bientinesi <pauldj at cs.utexas.edu> To: mathgroup at smc.vnet.net Subject: [mg41323] [mg41293] Apart, Factor on different operations Hello, here is what I am trying to do: let op[x,y] be a binary associative operation but not commutative, say Dot: op[x_, y_ + z_] := op[x,y] + op[x,z] op[x_ + y_, z_] := op[x,z] + op[y,z] op[x_, y_] := x.y and let term = x + y; now, exp = op[term, z] is x.z + y.z So far so good. What I am trying to do is to go back from x.y + y.z to op[term, z] by means of pattern matching: exp /. term -> X is the operation I have to perform, expecting X.z but of course it doesn't work. If I were using the common Times operation instead of Dot I could achieve the result using Apart or Factor: Apart[ x z + y z ] /. term -> X returns X z Does anyone have an idea? thanks a lot, -- Paolo pauldj at cs.utexas.edu paolo.bientinesi at iit.cnr.it