<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <title>MaplePrimes - comments on Post, Sorting with attributes</title>
    <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes</link>
    <language>en-us</language>
    <copyright>2026 Maplesoft, A Division of Waterloo Maple Inc.</copyright>
    <generator>Maplesoft Document System</generator>
    <lastBuildDate>Tue, 09 Jun 2026 20:59:27 GMT</lastBuildDate>
    <pubDate>Tue, 09 Jun 2026 20:59:27 GMT</pubDate>
    <itunes:subtitle />
    <itunes:summary />
    <description>The latest comments added to the Post, Sorting with attributes</description>
    <image>
      <url>http://www.mapleprimes.com/images/mapleprimeswhite.jpg</url>
      <title>MaplePrimes - comments on Post, Sorting with attributes</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes</link>
    </image>
    <item>
      <title>Magnitude</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes?ref=Feed:MaplePrimes:Sorting with attributes:Comments#comment80497</link>
      <itunes:summary>Joe, 

It is a great idea to use attributes. 

Replacing &lt;code&gt;abs(x)&lt;/code&gt; with &lt;code&gt;op(1,x)^2+op(2,x)^2&lt;/code&gt; seems to give some time improvement in all examples. It can't be applied to real x though. </itunes:summary>
      <description>The latest comments added to the Post, Sorting with attributes</description>
      <guid>80497</guid>
      <pubDate>Sat, 15 Oct 2005 01:30:18 Z</pubDate>
      <itunes:author>alec</itunes:author>
      <author>alec</author>
    </item>
    <item>
      <title>faster magnitude comparison</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes?ref=Feed:MaplePrimes:Sorting with attributes:Comments#comment86782</link>
      <itunes:summary>Another possibility, which works with pure reals and imaginaries, is  (Re^2+Im^2)(z), or a variant.  Here is a comparison of several methods for generating the magnitude (or square of magnitude).
&lt;pre&gt;
restart;
use RandomTools in
    zs := [seq](Generate(complex(float(range=0..1,method=uniform))),i=1..10^3);
end use:

n := 2;
time(proc() to 10^n do map(z -&gt; z*conjugate(z),zs) od end());
time(proc() to 10^n do map(Re^2+Im^2, zs) od end());
time(proc() to 10^n do map(z -&gt; Re(z)^2+Im(z)^2, zs) od end());
time(proc() to 10^n do map(abs,zs) od end());
time(proc() to 10^n do map(z-&gt;op(1,z)^2+op(2,z)^2,zs) od end());

time(proc() to 10^n do [seq(Re(z)^2+Im(z)^2, z=zs)] od end());
time(proc() to 10^n do [seq(op(1,z)^2+op(2,z)^2,z=zs)] od end());

                                     7.570
                                     6.259
                                     6.879
                                    12.050
                                     7.079
                                     5.410
                                     5.610
&lt;/pre&gt;
Using seq is somewhat faster than map.  The fastest here, Re(z)^2+Im(z)^2, is also robust, it works for pure reals and imaginaries.
</itunes:summary>
      <description>The latest comments added to the Post, Sorting with attributes</description>
      <guid>86782</guid>
      <pubDate>Sat, 15 Oct 2005 01:48:15 Z</pubDate>
      <itunes:author>Joe
 Riel
</itunes:author>
      <author>Joe
 Riel
</author>
    </item>
    <item>
      <title>Re: Magnitude</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes?ref=Feed:MaplePrimes:Sorting with attributes:Comments#comment86781</link>
      <itunes:summary>&lt;i&gt;It can't be applied to real x though.&lt;/i&gt;

Nor to purely imaginary x either.</itunes:summary>
      <description>The latest comments added to the Post, Sorting with attributes</description>
      <guid>86781</guid>
      <pubDate>Sat, 15 Oct 2005 05:01:31 Z</pubDate>
      <itunes:author>Stephen Forrest</itunes:author>
      <author>Stephen Forrest</author>
    </item>
    <item>
      <title>seq vs. map</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes?ref=Feed:MaplePrimes:Sorting with attributes:Comments#comment90356</link>
      <itunes:summary>One seq seems to be faster than map. For 2 seqs, the situation may be different - I am not sure. For example, multiple timings of the following 2 sorting procedures didn't show a significant preference of one of them, &lt;pre&gt;sort5 := L-&gt;map(attributes,
sort([seq(setattribute(Re(z)^2+Im(z)^2,z), z=L)]));

sort6 := L-&gt;[seq(attributes(i),
i=sort([seq(setattribute(Re(z)^2+Im(z)^2,z), z=L)]))];&lt;/pre&gt;</itunes:summary>
      <description>The latest comments added to the Post, Sorting with attributes</description>
      <guid>90356</guid>
      <pubDate>Sat, 15 Oct 2005 18:51:51 Z</pubDate>
      <itunes:author>Alec Mihailovs</itunes:author>
      <author>Alec Mihailovs</author>
    </item>
    <item>
      <title>This trick is awesome.</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes?ref=Feed:MaplePrimes:Sorting with attributes:Comments#comment80467</link>
      <itunes:summary>It took me a while to realize just how useful this trick really is.  If you want to sort integers you can write an integer n as SFloat(n,0).  I think that has pretty low overhead.  Needless to say, I am now using it to sort monomials by degree.  It is substantially faster than accessing an element of a list.  Thanks for the great technique.</itunes:summary>
      <description>The latest comments added to the Post, Sorting with attributes</description>
      <guid>80467</guid>
      <pubDate>Mon, 24 Oct 2005 10:55:48 Z</pubDate>
      <itunes:author>roman_pearce</itunes:author>
      <author>roman_pearce</author>
    </item>
    <item>
      <title>Nice extension</title>
      <link>http://www.mapleprimes.com/posts/43529-Sorting-With-Attributes?ref=Feed:MaplePrimes:Sorting with attributes:Comments#comment86767</link>
      <itunes:summary>Using SFloat to convert an integer into an "equivalent" software float, so that it can take an attribute, is a nice extension.  Thanks for sharing it.  For those wondering what Roman is doing, here is the idea (I assume),
&lt;pre&gt;
sortpolys := proc(polys,vars)
local p;
    return map(attributes
               ,sort([seq](setattribute(SFloat(degree(p,vars),0),p)
                           ,p = polys)));
end proc:
sortpolys([x^3+x, 1, x^2+x+1],x);
                                 2           3
                            [1, x  + x + 1, x  + x]

&lt;/pre&gt;
However, I wouldn't think that there would be much speed advantage of this compared to my method 2 (where the keys are precomputed but pairs are used for the comparison) except for fairly long lists.  How many monomials do you have to sort?

Is there a similar method for making any numeric quantity attributable while permitting it to be directly compared with other such numbers?  SFloat can handle floats but not fractions:
&lt;pre&gt;
SFloat(2.3, 0);
                 2.3
SFloat(2/3, 0);
Error, invalid arguments for Float constructor
&lt;/pre&gt;
I'd be reluctant to use evalf because of the possibility of round-off.</itunes:summary>
      <description>The latest comments added to the Post, Sorting with attributes</description>
      <guid>86767</guid>
      <pubDate>Mon, 24 Oct 2005 19:11:03 Z</pubDate>
      <itunes:author>Joe
 Riel
</itunes:author>
      <author>Joe
 Riel
</author>
    </item>
  </channel>
</rss>