Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following is a bit wasteful, but it works: The idea is to simply watch for cases where the <code>Listable</code> attribute of <code>Plus</code> has put the same <code>pt</code> into all elements of a list (i.e. a raw point) -- and then pull it back out. First, define a function for adding pt objects: </p> <pre><code>SetAttributes[ptPlus, {Orderless}] ptPlus[pt[pa : {_, _}], pt[pb : {_, _}], r___] := ptPlus[pt[pa + pb], r]; ptPlus[p_pt] := p; </code></pre> <p>Then we make sure that any <code>Plus</code> which involves a <code>pt</code> is mapped to <code>ptPlus</code> (an associate the rule with pt). </p> <pre><code>Plus[h___, a_pt, t___] ^:= ptPlus[h, a, t]; </code></pre> <p>The above rules means that: <code>{x0,y0}+pt[{x1,y1}]</code> will be expanded from <code>{x0+pt[{x1,y1}],y0+pt[{x1,y1}]}</code> to <code>{ptPlus[x0,pt[{x1,y1}]],ptPlus[y0,pt[{x1,y1}]]}</code>. Now we just make a rule to transform this to <code>pt[{x0,y0}]+pt[{x1,y1}]</code> (note the deferred condition which checks that the <code>pt</code>s are equal): </p> <pre><code>{ptPlus[x__], ptPlus[y__]} ^:= Module[{ ptCases = Cases[{{x}, {y}}, _pt, {2}]}, ptCases[[1]] + pt[Plus @@@ DeleteCases[{{x}, {y}}, _pt, {2}]] /; Equal @@ ptCases] </code></pre> <p>A more opaque, but slightly more careful version which is easier to generalize to higher dimensions:</p> <pre><code>ptPlus /: p : {_ptPlus, _ptPlus} := Module[{ptCases, rest, lp = ReleaseHold@Apply[List, Hold[p], {2}]}, ptCases = Cases[lp, _pt, {2}]; rest = Plus @@@ DeleteCases[lp, _pt, {2}]; ptCases[[1]] + pt[rest] /; And[Equal @@ ptCases, VectorQ@rest]] </code></pre> <p>This whole approach will of course lead to horribly subtle bugs when <code>{a+pt[{0,0}],a+pt[{0,b}]} /. {a -&gt; pt[{0,0}]}</code> evaluates to <code>pt[{0,0}]</code> when <code>c==0</code> and <code>{pt[{0,0}],pt[{0,c}]}</code> otherwise...</p> <p>HTH -- said the guy to himself...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload