7
$\begingroup$

I wish to use Distribute[p.(q+r)+m] to get p.q+p.r+m. However, Distribute apparently does not thread through all parts of the expression. What should I do instead?

$\endgroup$
  • 3
    $\begingroup$ Is Distribute[#] & /@ (p.(q + r) + m) acceptable ? $\endgroup$ – b.gates.you.know.what Nov 29 '12 at 9:26
  • $\begingroup$ @b.gatessucks This is not good because if there is a function like Log[a+b] inside the expression, the distribute would expand that too, yielding Log[a]+Log[b]. $\endgroup$ – QuantumDot Jan 8 '13 at 20:06
  • $\begingroup$ @QuantumDot That would be a different question, too. $\endgroup$ – b.gates.you.know.what Jan 8 '13 at 20:10
  • $\begingroup$ Oh, that's true. On second thought, never mind. $\endgroup$ – QuantumDot Jan 8 '13 at 20:24
5
$\begingroup$

You could also define special symbols that distribute themselves when they get combined with Plus under a Dot operation. Here I denote those symbols with a hat:

Clear[OverHat];
OverHat /: Dot[x___OverHat, Plus[y__OverHat], z___OverHat] := 
 Plus @@ Thread[Dot[x, {y}, z]]

Then you can enter your expression like this:

OverHat[m] + OverHat[p].(OverHat[q] + OverHat[r])

where the OverHat is more easily input using the shortcut Ctrl7+^ to get this:

overhat

| improve this answer | |
$\endgroup$
4
$\begingroup$

You don't give a lot of context about how the target expression is generated (interactively or via code). The documentation for Distribute indicates that it won't automatically map down through an expression. But, one way to do this comes to mind.

Via code, using ReplaceAll and a replacement rule:

expr=p.(q+r)+m
expr /. d : HoldPattern[Dot[_, Plus[_, _]]] :> Distribute[d]
(* m + p.q + p.r *)
| improve this answer | |
$\endgroup$
4
$\begingroup$
  p.(q + r) + m /. Dot -> Composition[Distribute, Dot]
  (* m + p.q + p.r *)

  Log[p.(q + Log[r + s])] + Log[a + b] + m /.  Dot -> Composition[Distribute, Dot]
  (* m + Log[a + b] + Log[p.q + p.Log[r + s]] *)
| improve this answer | |
$\endgroup$

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.