Source

pynac-patches / trac9880_pynac_order_burcin_original.patch

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# HG changeset patch
# User Burcin Erocal <burcin@erocal.org>
# Date 1284298206 -7200
# Node ID a8d8d201814186823ebc1204f5acf55e0b8b5b91
# Parent 5f14c0386822d8c7448bae2577f3966d3dd35902
Revert the changes to the original comparison functions of GiNaC and use the
new comparison functions only in the printing of add and mul objects.

diff --git a/ginac/Makefile.am b/ginac/Makefile.am
--- a/ginac/Makefile.am
+++ b/ginac/Makefile.am
@@ -9,7 +9,7 @@
   operators.cpp power.cpp registrar.cpp relational.cpp remember.cpp \
   pseries.cpp print.cpp symbol.cpp symmetry.cpp tensor.cpp \
   utils.cpp wildcard.cpp \
-  remember.h tostring.h utils.h compiler.h
+  remember.h tostring.h utils.h compiler.h order.cpp
 
 #The -no-undefined breaks Pynac on OS X 10.4.  See #9135
 if CYGWIN
@@ -26,5 +26,5 @@
   exprseq.h fail.h fderivative.h flags.h function.h hash_map.h idx.h indexed.h \
   inifcns.h integral.h lst.h matrix.h mul.h ncmul.h normal.h numeric.h operators.h \
   power.h print.h pseries.h ptr.h registrar.h relational.h structure.h \
-  symbol.h symmetry.h tensor.h version.h wildcard.h
+  symbol.h symmetry.h tensor.h version.h wildcard.h order.h
 EXTRA_DIST = version.h.in
diff --git a/ginac/add.cpp b/ginac/add.cpp
--- a/ginac/add.cpp
+++ b/ginac/add.cpp
@@ -127,8 +127,9 @@
 	numeric coeff;
 	bool first = true;
 
+	epvector* sorted_seq = get_sorted_seq();
 	// Then proceed with the remaining factors
-	epvector::const_iterator it = seq.begin(), itend = seq.end();
+	epvector::const_iterator it = sorted_seq->begin(), itend = sorted_seq->end();
 	while (it != itend) {
 		std::stringstream tstream;
 		print_context *tcontext_p;
diff --git a/ginac/basic.cpp b/ginac/basic.cpp
--- a/ginac/basic.cpp
+++ b/ginac/basic.cpp
@@ -829,58 +829,44 @@
  *  1 greater. */
 int basic::compare(const basic & other) const
 {
+ #ifdef GINAC_COMPARE_STATISTICS
+        compare_statistics.total_basic_compares++;
+#endif
+        const unsigned hash_this = gethash();
+        const unsigned hash_other = other.gethash();
+        if (hash_this<hash_other) return -1;
+        if (hash_this>hash_other) return 1;
 #ifdef GINAC_COMPARE_STATISTICS
-	compare_statistics.total_basic_compares++;
-#endif
-	static const tinfo_t function_id = find_tinfo_key("function");
-	static const tinfo_t fderivative_id = find_tinfo_key("fderivative");
-	const tinfo_t typeid_this = tinfo();
-	const tinfo_t typeid_other = other.tinfo();
-
-	// SAGE: If we want orderings to make any sense, it only
-	// makes sense to compare using the hashes when the types
-	// are the same.  WARNING -- doing this *will* presumably
-	// have negative performance implications.
-	/*	if (typeid_this==typeid_other) {
-	  const unsigned hash_this = gethash();
-	  const unsigned hash_other = other.gethash();
-	  if (hash_this<hash_other) return -1;
-	  if (hash_this>hash_other) return 1;
-	}
-	*/
-
-#ifdef GINAC_COMPARE_STATISTICS
-	compare_statistics.compare_same_hashvalue++;
+        compare_statistics.compare_same_hashvalue++;
 #endif
 
-	if (typeid_this==typeid_other) {
-		GINAC_ASSERT(typeid(*this)==typeid(other));
-// 		int cmpval = compare_same_type(other);
-// 		if (cmpval!=0) {
-// 			std::cout << "hash collision, same type: " 
-// 			          << *this << " and " << other << std::endl;
-// 			this->print(print_tree(std::cout));
-// 			std::cout << " and ";
-// 			other.print(print_tree(std::cout));
-// 			std::cout << std::endl;
-// 		}
-// 		return cmpval;
+        const tinfo_t& typeid_this = tinfo();//typeid(*this);
+        const tinfo_t& typeid_other = other.tinfo();//typeid(other);
+        if (typeid_this == typeid_other) {
+//              int cmpval = compare_same_type(other);
+//              if (cmpval!=0) {
+//                      std::cout << "hash collision, same type: " 
+//                                << *this << " and " << other << std::endl;
+//                      this->print(print_tree(std::cout));
+//                      std::cout << " and ";
+//                      other.print(print_tree(std::cout));
+//                      std::cout << std::endl;
+//              }
+//              return cmpval;
 #ifdef GINAC_COMPARE_STATISTICS
-		compare_statistics.compare_same_type++;
+                compare_statistics.compare_same_type++;
 #endif
-		return compare_same_type(other);
-	} else if (typeid_other == function_id ||
-			typeid_other == fderivative_id) {
-		return -1;
-	} else {
-// 		std::cout << "hash collision, different types: " 
-// 		          << *this << " and " << other << std::endl;
-// 		this->print(print_tree(std::cout));
-// 		std::cout << " and ";
-// 		other.print(print_tree(std::cout));
-// 		std::cout << std::endl;
+                return compare_same_type(other);
+        } else {
+//              std::cout << "hash collision, different types: " 
+//                        << *this << " and " << other << std::endl;
+//              this->print(print_tree(std::cout));
+//              std::cout << " and ";
+//              other.print(print_tree(std::cout));
+//              std::cout << std::endl;
+                //return (typeid_this.before(typeid_other) ? -1 : 1);
 		return (typeid_this<typeid_other ? -1 : 1);
-	}
+        }
 }
 
 /** Test for syntactic equality.
diff --git a/ginac/basic.h b/ginac/basic.h
--- a/ginac/basic.h
+++ b/ginac/basic.h
@@ -108,6 +108,7 @@
 	GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
 	
 	friend class ex;
+	friend struct ex_is_greater_degrevlex;
 	
 	// default constructor, destructor, copy constructor and assignment operator
 protected:
diff --git a/ginac/ex.cpp b/ginac/ex.cpp
--- a/ginac/ex.cpp
+++ b/ginac/ex.cpp
@@ -597,5 +597,5 @@
 
 // none
 
-
+//
 } // namespace GiNaC
diff --git a/ginac/ex.h b/ginac/ex.h
--- a/ginac/ex.h
+++ b/ginac/ex.h
@@ -83,6 +83,7 @@
 	template<class T> friend inline bool is_a(const ex &);
 	template<class T> friend inline bool is_exactly_a(const ex &);
 	
+	friend struct ex_is_greater_degrevlex;
 	// default constructor, copy constructor and assignment operator
 public:
 	ex() throw();
@@ -706,6 +707,7 @@
 	void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
 };
 
+
 // Make it possible to print exvectors and exmaps
 std::ostream & operator<<(std::ostream & os, const exvector & e);
 std::ostream & operator<<(std::ostream & os, const exset & e);
diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp
--- a/ginac/expairseq.cpp
+++ b/ginac/expairseq.cpp
@@ -33,6 +33,7 @@
 #include "indexed.h"
 #include "infinity.h"
 #include "compiler.h"
+#include "order.h"
 
 #include <iostream>
 #include <algorithm>
@@ -71,7 +72,7 @@
 
 // public
 
-expairseq::expairseq() : inherited(&expairseq::tinfo_static)
+expairseq::expairseq() : inherited(&expairseq::tinfo_static), seq_sorted(NULL)
 #if EXPAIRSEQ_USE_HASHTAB
                                                    , hashtabsize(0)
 #endif // EXPAIRSEQ_USE_HASHTAB
@@ -110,20 +111,20 @@
 // other constructors
 //////////
 
-expairseq::expairseq(const ex &lh, const ex &rh) : inherited(&expairseq::tinfo_static)
+expairseq::expairseq(const ex &lh, const ex &rh) : inherited(&expairseq::tinfo_static), seq_sorted(NULL)
 {
 	construct_from_2_ex(lh,rh);
 	GINAC_ASSERT(is_canonical());
 }
 
-expairseq::expairseq(const exvector &v) : inherited(&expairseq::tinfo_static)
+expairseq::expairseq(const exvector &v) : inherited(&expairseq::tinfo_static), seq_sorted(NULL)
 {
 	construct_from_exvector(v);
 	GINAC_ASSERT(is_canonical());
 }
 
 expairseq::expairseq(const epvector &v, const ex &oc, bool do_index_renaming)
-  : inherited(&expairseq::tinfo_static), overall_coeff(oc)
+  : inherited(&expairseq::tinfo_static), overall_coeff(oc), seq_sorted(NULL)
 {
 	GINAC_ASSERT(is_a<numeric>(oc));
 	construct_from_epvector(v, do_index_renaming);
@@ -131,7 +132,7 @@
 }
 
 expairseq::expairseq(std::auto_ptr<epvector> vp, const ex &oc, bool do_index_renaming)
-  : inherited(&expairseq::tinfo_static), overall_coeff(oc)
+  : inherited(&expairseq::tinfo_static), overall_coeff(oc), seq_sorted(NULL)
 {
 	GINAC_ASSERT(vp.get()!=0);
 	GINAC_ASSERT(is_a<numeric>(oc));
@@ -143,7 +144,7 @@
 // archiving
 //////////
 
-expairseq::expairseq(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
+expairseq::expairseq(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst), seq_sorted(NULL)
 #if EXPAIRSEQ_USE_HASHTAB
 	, hashtabsize(0)
 #endif
@@ -492,46 +493,48 @@
 {
 	GINAC_ASSERT(is_a<expairseq>(other));
 	const expairseq &o = static_cast<const expairseq &>(other);
-	
+
 	int cmpval;
-	
+
 	// compare number of elements
 	if (seq.size() != o.seq.size())
 		return (seq.size()<o.seq.size()) ? -1 : 1;
 
+	// compare overall_coeff
+	cmpval = overall_coeff.compare(o.overall_coeff);
+	if (cmpval!=0)
+		return cmpval;
+
 #if EXPAIRSEQ_USE_HASHTAB
 	GINAC_ASSERT(hashtabsize==o.hashtabsize);
 	if (hashtabsize==0) {
 #endif // EXPAIRSEQ_USE_HASHTAB
+
 		epvector::const_iterator cit1 = seq.begin();
 		epvector::const_iterator cit2 = o.seq.begin();
 		epvector::const_iterator last1 = seq.end();
 		epvector::const_iterator last2 = o.seq.end();
 
 		for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
-			cmpval = (*cit2).compare(*cit1);
+			cmpval = (*cit1).compare(*cit2);
 			if (cmpval!=0) return cmpval;
 		}
 
 		GINAC_ASSERT(cit1==last1);
 		GINAC_ASSERT(cit2==last2);
-	
-		// compare overall_coeff
-		cmpval = overall_coeff.compare(o.overall_coeff);
-		if (cmpval!=0)
-			return cmpval;
-	
+
 		return 0;
+
 #if EXPAIRSEQ_USE_HASHTAB
 	}
-	
+
 	// compare number of elements in each hashtab entry
 	for (unsigned i=0; i<hashtabsize; ++i) {
 		unsigned cursize=hashtab[i].size();
 		if (cursize != o.hashtab[i].size())
 			return (cursize < o.hashtab[i].size()) ? -1 : 1;
 	}
-	
+
 	// compare individual (sorted) hashtab entries
 	for (unsigned i=0; i<hashtabsize; ++i) {
 		unsigned sz = hashtab[i].size();
@@ -549,7 +552,6 @@
 			}
 		}
 	}
-	
 	return 0; // equal
 #endif // EXPAIRSEQ_USE_HASHTAB
 }
@@ -1185,6 +1187,17 @@
 	std::sort(seq.begin(), seq.end(), expair_rest_is_less());
 }
 
+epvector* expairseq::get_sorted_seq() const
+{
+	if (!this->seq_sorted) {
+		seq_sorted = new epvector(seq.size());
+		partial_sort_copy(seq.begin(), seq.end(),
+				seq_sorted->begin(), seq_sorted->end(),
+				expair_rest_is_greater_degrevlex());
+	}
+	return seq_sorted;
+}
+
 
 /** Compact a presorted expairseq by combining all matching expairs to one
  *  each.  On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
diff --git a/ginac/expairseq.h b/ginac/expairseq.h
--- a/ginac/expairseq.h
+++ b/ginac/expairseq.h
@@ -67,6 +67,7 @@
 {
 	GINAC_DECLARE_REGISTERED_CLASS(expairseq, basic)
 
+	friend struct ex_is_greater_degrevlex;
 	// other constructors
 public:
 	expairseq(const ex & lh, const ex & rh);
@@ -157,10 +158,12 @@
 	std::auto_ptr<epvector> evalchildren(int level) const;
 	std::auto_ptr<epvector> subschildren(const exmap & m, unsigned options = 0) const;
 	
+	epvector* get_sorted_seq() const;
 // member variables
 	
 protected:
 	epvector seq;
+	mutable epvector *seq_sorted;
 	ex overall_coeff;
 #if EXPAIRSEQ_USE_HASHTAB
 	epplistvector hashtab;
diff --git a/ginac/function.cpp b/ginac/function.cpp
--- a/ginac/function.cpp
+++ b/ginac/function.cpp
@@ -1196,6 +1196,7 @@
 	return result;
 }
 
+/*
 int function::compare(const basic& other) const
 {
 	static const tinfo_t function_id = find_tinfo_key("function");
@@ -1213,6 +1214,7 @@
 		return 1;
 	}
 }
+*/
 
 int function::compare_same_type(const basic & other) const
 {
@@ -1225,6 +1227,7 @@
 		return exprseq::compare_same_type(o);
 }
 
+
 bool function::is_equal_same_type(const basic & other) const
 {
 	GINAC_ASSERT(is_a<function>(other));
diff --git a/ginac/function.h b/ginac/function.h
--- a/ginac/function.h
+++ b/ginac/function.h
@@ -347,7 +347,7 @@
 	ex conjugate() const;
 	ex real_part() const;
 	ex imag_part() const;
-	int compare(const basic &other) const;
+	//int compare(const basic &other) const;
 protected:
 	ex derivative(const symbol & s) const;
 	bool is_equal_same_type(const basic & other) const;
diff --git a/ginac/mul.cpp b/ginac/mul.cpp
--- a/ginac/mul.cpp
+++ b/ginac/mul.cpp
@@ -37,9 +37,9 @@
 
 #include <iostream>
 #include <vector>
+#include <cmath>
 #include <stdexcept>
 #include <limits>
-#include <cmath>
 #include <sstream>
 #ifdef DO_GINAC_ASSERT
 #  include <typeinfo>
@@ -261,7 +261,9 @@
 
 	// Separate factors into those with negative numeric exponent
 	// and all others
-	epvector::const_iterator it = seq.begin(), itend = seq.end();
+	epvector* sorted_seq = get_sorted_seq();
+	epvector::const_iterator it = sorted_seq->begin(), itend = sorted_seq->end();
+	//epvector::const_iterator it = seq.begin(), itend = seq.end();
 	exvector neg_powers, others;
 	while (it != itend) {
 		GINAC_ASSERT(is_exactly_a<numeric>(it->coeff));
@@ -1142,6 +1144,7 @@
 	return tdegree;
 }
 
+/*
 int mul::compare(const basic& other) const
 {
 	static const tinfo_t function_id = find_tinfo_key("function");
@@ -1164,96 +1167,12 @@
 		return (typeid_this<typeid_other ? -1 : 1);
 	}
 }
-
-int mul::compare_symbol(const symbol &other) const
-{
-	int cmpval;
-	double tdeg;
-	tdeg = total_degree();
-	if (tdeg == 1) {
-		cmpval = seq[0].rest.compare(other);
-		if (cmpval != 0) {
-			return cmpval;
-		}
-		cmpval = _ex1.compare(seq[0].coeff);
-		if (cmpval != 0) {
-			return cmpval;
-		}
-		return -1;
-	}
-	return tdeg > 1 ? -1 : 1;
-}
-
-// compare this to a pow object
-// first we compare degrees
-// if equal we compare the first item in the sequence to the base in other
-int mul::compare_pow(const power &other) const
-{
-	double my_deg = total_degree();
-	double odeg;
-	numeric oexp;
-	int cmpval = 0;
-	if (is_a<numeric>(other.exponent)) {
-		numeric oexp = ex_to<numeric>(other.exponent);
-		if (oexp.is_real()) {
-			odeg = oexp.to_double();
-		} else {
-			odeg = std::sqrt(std::pow(oexp.real().to_double(), 2) + 
-					std::pow(oexp.imag().to_double(), 2));
-		}
-		if (odeg != my_deg)
-			return my_deg < odeg ? 1 : -1;
-	} else {
-		cmpval = seq[0].coeff.compare(other.exponent);
-		if (cmpval != 0)
-			return cmpval;
-	}
-	cmpval = seq[0].rest.compare(other.basis);
-	if (cmpval != 0) {
-		return cmpval;
-	}
-	if (seq.size() == 1 && overall_coeff.is_equal(_ex1))
-		return 0;
-	return 1;
-}
+*/
 
 
 int mul::compare_same_type(const basic & other) const
 {
-	int cmpval;
-
-	const mul &o = static_cast<const mul &>(other);
-
-	// compare total degrees
-	double deg1 = total_degree();
-	double deg2 = o.total_degree();
-	if (deg1 != deg2)
-		return deg1 < deg2 ? 1 : -1;
-
-	// compare each item in this product to correnponding element in other
-	epvector::const_iterator cit1 = seq.begin();
-	epvector::const_iterator cit2 = o.seq.begin();
-	epvector::const_iterator last1 = seq.end();
-	epvector::const_iterator last2 = o.seq.end();
-
-	for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
-		cmpval = (*cit1).compare(*cit2);
-		if (cmpval != 0)
-			return cmpval;
-	}
-
-	// compare sizes
-	if (cit1 != last1) 
-		return 1;
-	else if (cit2 != last2)
-		return -1;
-
-	// compare overall_coeff
-	cmpval = overall_coeff.compare(o.overall_coeff);
-	if (cmpval!=0)
-		return cmpval;
-
-	return 0;
+	return inherited::compare_same_type(other);
 }
 
 unsigned mul::return_type() const
diff --git a/ginac/mul.h b/ginac/mul.h
--- a/ginac/mul.h
+++ b/ginac/mul.h
@@ -85,7 +85,7 @@
 	bool can_make_flat(const expair & p) const;
 	ex expand(unsigned options=0) const;
 	void find_real_imag(ex&, ex&) const;
-	int compare(const basic& other) const;
+	//int compare(const basic& other) const;
 	
 	ex eval_infinity(epvector::const_iterator infinity_iter) const;
 	ex eval_exponentials() const;
@@ -97,8 +97,8 @@
 public:
 	ex algebraic_subs_mul(const exmap & m, unsigned options) const;
 	double total_degree() const;
-	int compare_symbol(const symbol &other) const;
-	int compare_pow(const power &other) const;
+	//int compare_symbol(const symbol &other) const;
+	//int compare_pow(const power &other) const;
 protected:
 	void print_overall_coeff(const ex coeff_ex, const print_context & c,
 			const char *mul_sym, bool latex=false) const;
diff --git a/ginac/order.cpp b/ginac/order.cpp
new file mode 100644
--- /dev/null
+++ b/ginac/order.cpp
@@ -0,0 +1,259 @@
+
+#include "order.h"
+#include "registrar.h"
+#include "utils.h"
+
+#include <cmath>
+#include <iostream>
+
+namespace GiNaC {
+
+
+bool ex_is_greater_degrevlex::operator() (const ex &lhex, const ex &rhex) const {
+	//std::cout<<"in ex_is_greater_degrevlex::operator()"<<std::endl;
+	//std::cout<<"lh: ";
+	//lhex.dbgprint();
+	//std::cout<<std::endl<<"rh: ";
+	//rhex.dbgprint();
+	//std::cout<<std::endl;
+	const basic *lh = get_pointer(lhex.bp); 
+	const basic *rh = get_pointer(rhex.bp);
+	return compare(lh, rh) == 1;
+}
+
+int ex_is_greater_degrevlex::compare(const basic *lh, const basic *rh) const {
+	const tinfo_t typeid_lh = lh->tinfo();
+	const tinfo_t typeid_rh = rh->tinfo();
+
+	if (typeid_rh==typeid_lh) {
+		if (typeid_rh == mul_id) {
+			return compare_same_type_mul(
+					static_cast<const mul*>(lh),
+					static_cast<const mul*>(rh));
+		} else if (typeid_rh == add_id) {
+			return compare_same_type_add(
+					static_cast<const add*>(lh),
+					static_cast<const add*>(rh));
+		} else if (typeid_rh == symbol_id) {
+			return compare_same_type_symbol(
+					static_cast<const symbol*>(lh),
+					static_cast<const symbol*>(rh));
+		} else if (typeid_rh == power_id) {
+			return compare_same_type_power(
+					static_cast<const power*>(lh),
+					static_cast<const power*>(rh));
+		} else {
+			return lh->compare_same_type(*rh);
+		}
+	} else if (typeid_lh == fderivative_id) {
+		//print fderivatives after everything
+		return -1;
+	} else if (typeid_lh == function_id) {
+		//print functions before fderivatives, after anything else
+		return typeid_rh == fderivative_id ? 1 : -1;
+	} else if (typeid_lh == mul_id) {
+		if (typeid_rh == power_id) {
+			return compare_mul_power(
+					static_cast<const mul*>(lh),
+					static_cast<const power*>(rh));
+		} else if (typeid_rh == symbol_id) {
+			return compare_mul_symbol(
+					static_cast<const mul*>(lh),
+					static_cast<const symbol*>(rh));
+		}
+	} else if (typeid_lh == power_id) {
+		if (typeid_rh == mul_id) {
+			return -compare_mul_power(
+					static_cast<const mul*>(rh),
+					static_cast<const power*>(lh));
+		} else if (typeid_rh == symbol_id) {
+			return compare_power_symbol(
+					static_cast<const power*>(lh),
+					static_cast<const symbol*>(rh));
+		}
+	} else if (typeid_lh == symbol_id) {
+		if (typeid_rh == mul_id) {
+			return -compare_mul_symbol(
+					static_cast<const mul*>(rh),
+					static_cast<const symbol*>(lh));
+		} else if (typeid_rh == power_id) {
+			return -compare_power_symbol(
+					static_cast<const power*>(rh),
+					static_cast<const symbol*>(lh));
+		}
+	}
+	//std::cout<<"comparing typeid's"<<std::endl;
+	return (typeid_lh<typeid_rh ? 1 : -1);
+}
+
+int ex_is_greater_degrevlex::compare_mul_symbol(const mul *lh,
+		const symbol *rh) const
+{
+	int cmpval;
+	double tdeg;
+	tdeg = lh->total_degree();
+	if (tdeg == 1) {
+		cmpval = compare(get_pointer(lh->seq[0].rest.bp), rh);
+		if (cmpval != 0) {
+			return cmpval;
+		}
+		cmpval = compare(_num1_p,
+				get_pointer(lh->seq[0].coeff.bp));
+		if (cmpval != 0) {
+			return cmpval;
+		}
+		return -1;
+	}
+	return tdeg > 1 ? 1 : -1;
+}
+
+// compare this to a pow object
+// first we compare degrees
+// if equal we compare the first item in the sequence to the base in other
+int ex_is_greater_degrevlex::compare_mul_power(const mul *lh,
+		const power *rh) const
+{
+	double lh_deg = lh->total_degree();
+	double rh_deg;
+	numeric rh_exp;
+	int cmpval = 0;
+	if (is_a<numeric>(rh->exponent)) {
+		rh_exp = ex_to<numeric>(rh->exponent);
+		if (rh_exp.is_real()) {
+			rh_deg = rh_exp.to_double();
+		} else {
+			rh_deg = std::sqrt(std::pow(rh_exp.real().to_double(), 2) + 
+					std::pow(rh_exp.imag().to_double(), 2));
+		}
+		if (rh_deg != lh_deg)
+			return lh_deg < rh_deg ? -1 : 1;
+	} else {
+		cmpval = compare(get_pointer(lh->seq[0].coeff.bp),
+				get_pointer(rh->exponent.bp));
+		if (cmpval != 0)
+			return cmpval;
+	}
+	cmpval = compare(get_pointer(lh->seq[0].rest.bp),
+			get_pointer(rh->basis.bp));
+	if (cmpval != 0) {
+		return cmpval;
+	}
+	if (lh->seq.size() == 1 && lh->overall_coeff.is_equal(_ex1))
+		return 0;
+	return 1;
+}
+
+int ex_is_greater_degrevlex::compare_same_type_mul(const mul *lh,
+		const mul *rh) const
+{
+	int cmpval;
+
+	// compare total degrees
+	double lh_deg = lh->total_degree();
+	double rh_deg = rh->total_degree();
+	if (lh_deg != rh_deg)
+		return lh_deg < rh_deg ? -1 : 1;
+
+	// compare each item in lh to corresponding element in rh
+	epvector::const_iterator cit1 = lh->seq.begin();
+	epvector::const_iterator cit2 = rh->seq.begin();
+	epvector::const_iterator last1 = lh->seq.end();
+	epvector::const_iterator last2 = rh->seq.end();
+
+	for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
+		cmpval = ( cit1->compare(*cit2));
+		if (cmpval != 0)
+			return cmpval;
+	}
+
+	// compare sizes
+	if (cit1 != last1) 
+		return 1;
+	else if (cit2 != last2)
+		return -1;
+
+	// compare overall_coeff
+	cmpval = compare(get_pointer(lh->overall_coeff.bp),
+			get_pointer(rh->overall_coeff.bp));
+	if (cmpval!=0)
+		return cmpval;
+
+	return 0;
+}
+
+int ex_is_greater_degrevlex::compare_same_type_add(const add *lh,
+		const add *rh) const
+{
+	int cmpval;
+
+	// compare number of elements
+	if (lh->seq.size() != rh->seq.size())
+		return (lh->seq.size()<rh->seq.size()) ? -1 : 1;
+
+	epvector::const_iterator cit1 = lh->seq.begin();
+	epvector::const_iterator cit2 = rh->seq.begin();
+	epvector::const_iterator last1 = lh->seq.end();
+	epvector::const_iterator last2 = rh->seq.end();
+
+	for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
+		cmpval = compare(get_pointer(cit2->rest.bp),
+				get_pointer(cit1->rest.bp));
+		if (cmpval!=0) return cmpval;
+	}
+
+	GINAC_ASSERT(cit1==last1);
+	GINAC_ASSERT(cit2==last2);
+
+	// compare overall_coeff
+	cmpval = compare(get_pointer(lh->overall_coeff.bp),
+			get_pointer(rh->overall_coeff.bp));
+	if (cmpval!=0)
+		return cmpval;
+
+	return 0;
+}
+
+int ex_is_greater_degrevlex::compare_power_symbol(const power *lh,
+		const symbol *rh) const
+{
+	//std::cout<<"in compare_power_symbol"<<std::endl;
+	int cmpval;
+	cmpval = compare(get_pointer(lh->exponent.bp), _num1_p);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+	return compare(get_pointer(lh->basis.bp), rh);
+}
+
+int ex_is_greater_degrevlex::compare_same_type_power(const power *lh,
+		const power *rh) const
+{
+	int cmpval = compare(get_pointer(lh->exponent.bp),
+				get_pointer(rh->exponent.bp));
+	if (cmpval)
+		return cmpval;
+	else
+		return compare(get_pointer(lh->basis.bp),
+			get_pointer(rh->basis.bp));
+}
+
+int ex_is_greater_degrevlex::compare_same_type_symbol(const symbol *lh,
+		const symbol *rh) const
+{
+	//std::cout<<"in compare_same symbol"<<std::endl;
+	//std::cout<<"lh: ";
+	//lh->dbgprint();
+	//std::cout<<"rh: ";
+	//rh->dbgprint();
+	//std::cout<<std::endl;
+	if (lh->serial==rh->serial) return 0;
+	//std::cout<<"after if"<<std::endl;
+
+// SAGE/Pynac: Sorting based on creation order doesn't work for Sage.
+// instead we sort on variable name. -- William Stein
+
+	//std::cout<<"comparing names: "<<(lh->name < rh->name)<<std::endl;
+	return lh->name < (rh->name) ? 1 : -1;
+}
+
+} // namespace GiNaC
diff --git a/ginac/order.h b/ginac/order.h
new file mode 100644
--- /dev/null
+++ b/ginac/order.h
@@ -0,0 +1,40 @@
+#include "ex.h"
+#include "basic.h"
+#include "mul.h"
+#include "power.h"
+#include "add.h"
+#include "symbol.h"
+
+namespace GiNaC {
+	
+	struct ex_is_greater_degrevlex : public std::binary_function<ex, ex, bool> {
+		const tinfo_t function_id;// = find_tinfo_key("function");
+		const tinfo_t fderivative_id;// = find_tinfo_key("fderivative");
+		const tinfo_t power_id;// = find_tinfo_key("power");
+		const tinfo_t symbol_id;// = find_tinfo_key("symbol");
+		const tinfo_t mul_id;// = find_tinfo_key("mul");
+		const tinfo_t add_id;// = find_tinfo_key("add");
+
+		ex_is_greater_degrevlex():
+			function_id(find_tinfo_key("function")),
+			fderivative_id(find_tinfo_key("fderivative")),
+			power_id(find_tinfo_key("power")),
+			symbol_id(find_tinfo_key("symbol")),
+			mul_id(find_tinfo_key("mul")),
+			add_id(find_tinfo_key("add")) {};
+		bool operator() (const ex &lh, const ex &rh) const;
+		int compare(const basic *lh, const basic *rh) const;
+		int compare_mul_symbol(const mul *lh, const symbol *rh) const;
+		int compare_mul_power(const mul *lh, const power *rh) const;
+		int compare_same_type_mul(const mul *lh, const mul *rh) const;
+		int compare_same_type_add(const add *lh, const add *rh) const;
+		int compare_power_symbol(const power *lh, const symbol *rh) const;
+		int compare_same_type_power(const power *lh, const power *rh) const;
+		int compare_same_type_symbol(const symbol *lh, const symbol *rh) const;
+	};
+
+struct expair_rest_is_greater_degrevlex : public std::binary_function<expair, expair, bool> {
+	bool operator()(const expair &lh, const expair &rh) const { return ex_is_greater_degrevlex()(lh.rest, rh.rest); }
+};
+
+}
diff --git a/ginac/power.cpp b/ginac/power.cpp
--- a/ginac/power.cpp
+++ b/ginac/power.cpp
@@ -900,39 +900,6 @@
 	}
 }
 
-int power::compare(const basic& other) const
-{
-	static const tinfo_t mul_id = find_tinfo_key("mul");
-	static const tinfo_t symbol_id = find_tinfo_key("symbol");
-	static const tinfo_t function_id = find_tinfo_key("function");
-	static const tinfo_t fderivative_id = find_tinfo_key("fderivative");
-	const tinfo_t typeid_this = tinfo();
-	const tinfo_t typeid_other = other.tinfo();
-	if (typeid_this==typeid_other) {
-		GINAC_ASSERT(typeid(*this)==typeid(other));
-		return compare_same_type(other);
-	} else if (typeid_other == mul_id) {
-		return -static_cast<const mul&>(other).compare_pow(*this);
-	} else if (typeid_other == symbol_id) {
-		return compare_symbol(static_cast<const symbol&>(other));
-	} else if (typeid_other == function_id ||
-			typeid_other == fderivative_id) {
-		return -1;
-	} else {
-		return (typeid_this<typeid_other ? -1 : 1);
-	}
-}
-
-int power::compare_symbol(const symbol & other) const
-{
-	int cmpval;
-	cmpval = _ex1.compare(exponent);
-	if (cmpval != 0) {
-		return cmpval;
-	}
-	return basis.compare(other);
-}
-
 int power::compare_same_type(const basic & other) const
 {
 	GINAC_ASSERT(is_exactly_a<power>(other));
@@ -942,11 +909,7 @@
 	if (cmpval)
 		return cmpval;
 	else
-	  // SAGE -- I changed the sign below for consistency with standard
-	  // mathematics and all other math software (except mathematica).
-	  // This makes it so x^5 + x^2 prints correctly instead of 
-	  // as x^2 + x^5.   -- William Stein
-		return -exponent.compare(o.exponent);
+		return exponent.compare(o.exponent);
 }
 
 unsigned power::return_type() const
diff --git a/ginac/power.h b/ginac/power.h
--- a/ginac/power.h
+++ b/ginac/power.h
@@ -40,6 +40,7 @@
 {
 	GINAC_DECLARE_REGISTERED_CLASS(power, basic)
 	
+	friend struct ex_is_greater_degrevlex;
 	friend class mul;
 	
 // member functions
@@ -72,8 +73,8 @@
 	ex conjugate() const;
 	ex real_part() const;
 	ex imag_part() const;
-	int compare(const basic& other) const;
-	int compare_symbol(const symbol& other) const;
+	//int compare(const basic& other) const;
+	//int compare_symbol(const symbol& other) const;
 protected:
 	ex derivative(const symbol & s) const;
 	ex eval_ncmul(const exvector & v) const;
diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp
--- a/ginac/symbol.cpp
+++ b/ginac/symbol.cpp
@@ -283,6 +283,7 @@
 		return _ex1;
 }
 
+/*
 int symbol::compare(const basic& other) const
 {
 	static const tinfo_t pow_id = find_tinfo_key("power");
@@ -305,26 +306,14 @@
 		return (typeid_this<typeid_other ? -1 : 1);
 	}
 }
+*/
 
 int symbol::compare_same_type(const basic & other) const
 {
 	GINAC_ASSERT(is_a<symbol>(other));
 	const symbol *o = static_cast<const symbol *>(&other);
 	if (serial==o->serial) return 0;
-	
-	// SAGE/Pynac: Sorting based on creation order doesn't work for Sage.
-	// instead we sort on variable name. -- William Stein
-
-	return name < (o->name) ? -1 : 1;
-	
-	// This is what Ginac used to return.  It fits with their
-	// philosophy that symbols names have no intrinsic meaning,
-	// only the order of creation of symbols matters.  Also, they
-	// allow multiple symbols with the same name.  Our Pynac
-	// wrapper does not allow for this, and we find the behavior
-	// way too confusing and ad hoc with this convention. 
-
-	// return serial < o->serial ? -1 : 1;
+        return serial < o->serial ? -1 : 1;
 }
 
 bool symbol::is_equal_same_type(const basic & other) const
diff --git a/ginac/symbol.h b/ginac/symbol.h
--- a/ginac/symbol.h
+++ b/ginac/symbol.h
@@ -43,6 +43,7 @@
 
 	friend class realsymbol;
 	friend class possymbol;
+	friend struct ex_is_greater_degrevlex;
 
 // types
 	
@@ -58,8 +59,8 @@
 	
 	// functions overriding virtual functions from base classes
 public:
-	//	int compare_same_type(const basic & other) const;
-	int compare(const basic &other) const;
+	//int compare_same_type(const basic & other) const;
+	//int compare(const basic &other) const;
 	bool info(unsigned inf) const;
 	ex eval(int level = 0) const;
 	ex evalf(int level = 0) const { return *this; } // overwrites basic::evalf() for performance reasons