0
$\begingroup$

I am interested in creating a table with positive Gaussian integers listed in order based on their norms, with ties broken by lex order.

So far I have Sort[Flatten[Table[{a, b, a^2 + b^2}, {a, 0, 10}, {b, 0, 10}], 1], #1[[3]] < #2[[3]] &], 1], which gives me some Gaussian integers sorted by norm, but the ties are broken in revlex.

For example, the code gives {1,0,1},{0,1,1} as the first nonzero entries, but I want those to be reversed. A second application of Sort destroys the first Sort, so I need to be more clever, and alas I am not.

Any help would be appreciated.

$\endgroup$
4
  • $\begingroup$ I believe this is a duplicate of: (9840) but I shall wait for confirmation from Trevor before closing. $\endgroup$
    – Mr.Wizard
    Jan 15, 2015 at 19:45
  • $\begingroup$ That is a general situation that would solve my problem. I was searching for things like "term order", so I didn't come up with that post. $\endgroup$
    – Trevor
    Jan 15, 2015 at 19:46
  • $\begingroup$ So the output below is correct, and the matter is solved to your satisfaction? $\endgroup$
    – Mr.Wizard
    Jan 15, 2015 at 19:47
  • $\begingroup$ Related: (2934) $\endgroup$
    – Mr.Wizard
    Jan 15, 2015 at 19:49

1 Answer 1

1
$\begingroup$

Your code has syntax errors but I think this is what you want:

list = Flatten[Table[{a, b, a^2 + b^2}, {a, 0, 10}, {b, 0, 10}], 1];

SortBy[list, #[[3]] &]

{{0, 0, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 2}, {0, 2, 4}, {2, 0, 4}, {1, 2, 5}, {2, 1, 5}, {2, 2, 8}, {0, 3, 9}, {3, 0, 9}, {1, 3, 10}, {3, 1, 10}, {2, 3, 13}, {3, 2, 13}, {0, 4, 16}, {4, 0, 16}, {1, 4, 17}, {4, 1, 17}, {3, 3, 18}, {2, 4, 20}, {4, 2, 20}, {0, 5, 25}, {3, 4, 25}, {4, 3, 25}, {5, 0, 25}, {1, 5, 26}, . . .}

With this syntax ties are (automatically) broken using the default ordering function.

$\endgroup$
2
  • $\begingroup$ That certainly does what I was hoping for. I just wish I understood the inner workings. I use Mathematica so rarely that I lose anything I learn during the few times a year I use it. Thanks. $\endgroup$
    – Trevor
    Jan 15, 2015 at 19:47
  • $\begingroup$ @Trevor Please read the documentation for SortBy. If you still have questions post them here as comments and I shall try to answer them later today. $\endgroup$
    – Mr.Wizard
    Jan 15, 2015 at 19:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.