Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p> I've mentioned <a href="https://stackoverflow.com/questions/2695182/splitting-a-list-based-on-another-list-values-in-mathematica/2698832#2698832">this</a> before, but the tool I find most useful is an application of <code>Reap</code> and <code>Sow</code> which mimics/extends the behavior of <code>GatherBy</code>:</p> <pre class="lang-scala prettyprint-override"><code>SelectEquivalents[x_List,f_:Identity, g_:Identity, h_:(#2&amp;)]:= Reap[Sow[g[#],{f[#]}]&amp;/@x, _, h][[2]]; </code></pre> <p>This allows me to group lists by any criteria and transform them in the process. The way it works is that a criteria function (<code>f</code>) tags each item in the list, each item is then transformed by a second supplied function (<code>g</code>), and the specific output is controlled by a third function (<code>h</code>). The function <code>h</code> accepts two arguments: a tag and a list of the collected items that have that tag. The items retain their original order, so if you set <code>h = #1&amp;</code> then you get an unsorted <code>Union</code>, like in the <a href="http://reference.wolfram.com/mathematica/ref/Reap.html?q=Reap&amp;lang=en" rel="nofollow noreferrer">examples</a> for <code>Reap</code>. But, it can be used for secondary processing.</p> <p>As an example of its utility, I've been working with <a href="http://www.wannier.org/" rel="nofollow noreferrer">Wannier90</a> which outputs the spatially dependent Hamiltonian into a file where each line is a different element in the matrix, as follows</p> <pre class="lang-scala prettyprint-override"><code>rx ry rz i j Re[Hij] Im[Hij] </code></pre> <p>To turn that list into a set of matrices, I gathered up all sublists that contain the same coordinate, turned the element information into a rule (i.e. {i,j}-> Re[Hij]+I Im[Hij]), and then turned the collected rules into a <code>SparseArray</code> all with the one liner:</p> <pre class="lang-scala prettyprint-override"><code>SelectEquivalents[hamlst, #[[;; 3]] &amp;, #[[{4, 5}]] -&gt; (Complex @@ #[[6 ;;]]) &amp;, {#1, SparseArray[#2]} &amp;] </code></pre> <p>Honestly, this is my Swiss Army Knife, and it makes complex things very simple. Most of my other tools are somewhat domain specific, so I'll probably not post them. However, most, if not all, of them reference <code>SelectEquivalents</code>.</p> <p><strong>Edit</strong>: it doesn't completely mimic <a href="http://reference.wolfram.com/mathematica/ref/GatherBy.html?q=GatherBy&amp;lang=en" rel="nofollow noreferrer"><code>GatherBy</code></a> in that it cannot group multiple levels of the expression as simply as <code>GatherBy</code> can. However, <code>Map</code> works just fine for most of what I need.</p> <p><strong>Example</strong>: @Yaroslav Bulatov has asked for a self-contained example. Here's one from my research that has been greatly simplified. So, let's say we have a set of points in a plane</p> <pre class="lang-scala prettyprint-override"><code>In[1] := pts = {{-1, -1, 0}, {-1, 0, 0}, {-1, 1, 0}, {0, -1, 0}, {0, 0, 0}, {0, 1, 0}, {1, -1, 0}, {1, 0, 0}, {1, 1, 0}} </code></pre> <p>and we'd like to reduce the number of points by a set of symmetry operations. (For the curious, we are generating the <a href="http://en.wikipedia.org/wiki/Little_group#Orbits_and_stabilizers" rel="nofollow noreferrer">little group</a> of each point.) For this example, let's use a four fold rotation axis about the z-axis</p> <pre class="lang-scala prettyprint-override"><code>In[2] := rots = RotationTransform[#, {0, 0, 1}] &amp; /@ (Pi/2 Range[0, 3]); </code></pre> <p>Using <code>SelectEquivalents</code> we can group the points that produce the same set of images under these operations, i.e. they're equivalent, using the following</p> <pre class="lang-scala prettyprint-override"><code>In[3] := SelectEquivalents[ pts, Union[Through[rots[#] ] ]&amp; ] (*&lt;-- Note Union*) Out[3]:= {{{-1, -1, 0}, {-1, 1, 0}, {1, -1, 0}, {1, 1, 0}}, {{-1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {1, 0, 0}}, {{0,0,0}}} </code></pre> <p>which produces 3 sublists containing the equivalent points. (Note, <code>Union</code> is absolutely vital here as it ensures that the same image is produced by each point. Originally, I used <code>Sort</code>, but if a point lies on a symmetry axis, it is invariant under the rotation about that axis giving an extra image of itself. So, <code>Union</code> eliminates these extra images. Also, <code>GatherBy</code> would produce the same result.) In this case, the points are already in a form that I will use, but I only need a representative point from each grouping and I'd like a count of the equivalent points. Since, I don't need to transform each point, I use the <code>Identity</code> function in the second position. For the third function, we need to be careful. The first argument passed to it will be the images of the points under the rotations which for the point <code>{0,0,0}</code> is a list of four identical elements, and using it would throw off the count. However, the second argument is just a list of all the elements that have that tag, so it will only contain <code>{0,0,0}</code>. In code, </p> <pre class="lang-scala prettyprint-override"><code>In[4] := SelectEquivalents[pts, Union[Through[rots[#]]]&amp;, #&amp;, {#2[[1]], Length[#2]}&amp; ] Out[4]:= {{{-1, -1, 0}, 4}, {{-1, 0, 0}, 4}, {{0, 0, 0}, 1}} </code></pre> <p>Note, this last step can just as easily be accomplished by </p> <pre class="lang-scala prettyprint-override"><code>In[5] := {#[[1]], Length[#]}&amp; /@ Out[3] </code></pre> <p>But, it is easy with this and the less complete example above to see how very complex transformations are possible with a minimum of code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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