[GiNaC-list] Ordering of expressions

Burcin Erocal burcin at erocal.org
Mon Aug 10 10:44:01 CEST 2009

Hi Luigi,

On Sun, 9 Aug 2009 17:19:52 +0200 (CEST)
Luigi Capozza <capozza at kph.uni-mainz.de> wrote:

> On Sun, 9 Aug 2009, Alexei Sheplyakov wrote:
> >> I have the following issue with the canonicalization of
> >> expressions. If I run many times the same program without changing
> >> anything, I get the output for the same expression ordered in
> >> different ways.
> >> Is this normal or am I doing something wrong? I saw that the hash
> >> value is calculated in a fairly sophisticated manner but is it
> >> eventually possible to set it by hand?
> > 
> > No, this is an implementation detail, and users are not supposed to
> > fiddle with it (the only exception is implementing calchash()
> > method in user defined classes).
> > 
> I was considering doing that. For the given simple example I found the
> easy solution of implementing a class inheriting from symbol and
> setting the hash value already in the constructor. Alas, it does not
> seem to scale to more elaborated expressions. As you suggest: I'll
> just stop fiddling about with the hash.

In order to use ginac as the symbolics backend in Sage [1], we also had
to work around this random printing problem. For this purpose, I changed
the comparison functions to enforce something similar to a degree
lexicographic ordering, where the variables are ordered alphabetically.

[1] http://sagemath.org/

You can find the bulk of the changes here:


and the bug fixes that came afterwards:


In retrospect, this was not the right way to fix the printing problem.
I should have done as the ginac developers suggested when I first asked
about this [2]. Unfortunately, I didn't know much about the design of
the ginac library then, and I wanted to get things done quickly.

[2] http://www.ginac.de/pipermail/ginac-list/2008-August/001406.html

When I find the time, I plan to move the new comparison functions out
of the way and bring back the old ones to make things fast again. Then,
add some code in expairseq to sort its arguments with the new
comparison functions before printing, and cache the sorted sequence
somewhere. I don't expect to have the time to do this in the near
future however.

I hope some of this will be useful if you try to stabilize printing
order in ginac.


More information about the GiNaC-list mailing list