Source

pynac-patches / trac_9880-pynac_order_jp_new-p2.take2.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
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# HG changeset patch
# User Jean-Pierre Flori <flori@enst.fr>
# Date 1299143384 -3600
# Node ID 67bdc3eefa160482ccbef431db334ee49159764f
# Parent cc1cb9baa5690cc0d6318340289ff7525325ae87
Fixes to printing order.

diff --git a/ginac/basic.cpp b/ginac/basic.cpp
--- a/ginac/basic.cpp
+++ b/ginac/basic.cpp
@@ -829,7 +829,7 @@
  *  1 greater. */
 int basic::compare(const basic & other) const
 {
- #ifdef GINAC_COMPARE_STATISTICS
+#ifdef GINAC_COMPARE_STATISTICS
         compare_statistics.total_basic_compares++;
 #endif
         const unsigned hash_this = gethash();
diff --git a/ginac/basic.h b/ginac/basic.h
--- a/ginac/basic.h
+++ b/ginac/basic.h
@@ -109,7 +109,7 @@
 	
 	friend class ex;
 	friend struct ex_is_greater_degrevlex;
-	
+	friend struct expair_is_greater_degrevlex;
 	// default constructor, destructor, copy constructor and assignment operator
 protected:
 	basic() : tinfo_key(&tinfo_static), flags(0) {}
diff --git a/ginac/container.h b/ginac/container.h
--- a/ginac/container.h
+++ b/ginac/container.h
@@ -129,6 +129,8 @@
 class container : public basic, public container_storage<C> {
 	GINAC_DECLARE_REGISTERED_CLASS(container, basic)
 
+	friend struct ex_is_greater_degrevlex;
+
 protected:
 	typedef typename container_storage<C>::STLT STLT;
 
diff --git a/ginac/ex.h b/ginac/ex.h
--- a/ginac/ex.h
+++ b/ginac/ex.h
@@ -84,6 +84,7 @@
 	template<class T> friend inline bool is_exactly_a(const ex &);
 	
 	friend struct ex_is_greater_degrevlex;
+	friend struct expair_is_greater_degrevlex;
 	// default constructor, copy constructor and assignment operator
 public:
 	ex() throw();
diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp
--- a/ginac/expairseq.cpp
+++ b/ginac/expairseq.cpp
@@ -1177,7 +1177,7 @@
 		seq_sorted = new epvector(seq.size());
 		partial_sort_copy(seq.begin(), seq.end(),
 				seq_sorted->begin(), seq_sorted->end(),
-				expair_rest_is_greater_degrevlex());
+				  expair_is_greater_degrevlex::in_type(tinfo()));
 	}
 	return seq_sorted;
 }
diff --git a/ginac/expairseq.h b/ginac/expairseq.h
--- a/ginac/expairseq.h
+++ b/ginac/expairseq.h
@@ -1,3 +1,4 @@
+
 /** @file expairseq.h
  *
  *  Interface to sequences of expression pairs. */
@@ -38,7 +39,7 @@
  *  combining n terms into one large sum (or n terms into one large product)
  *  from O(n*log(n)) to about O(n).  There are, however, several drawbacks.
  *  The constant in front of O(n) is quite large, when copying such an object
- *  one also has to copy the has table, comparison is quite expensive because
+ *  one also has to copy the hash table, comparison is quite expensive because
  *  there is no ordering any more, it doesn't help at all when combining two
  *  expairseqs because due to the presorted nature the behaviour would be
  *  O(n) anyways, the code is quite messy, etc, etc.  The code is here as
@@ -132,6 +133,7 @@
 	void make_flat(const epvector & v, bool do_index_renaming = false);
 	void canonicalize();
 	void combine_same_terms_sorted_seq();
+	epvector* get_sorted_seq() const;
 #if EXPAIRSEQ_USE_HASHTAB
 	void combine_same_terms();
 	unsigned calc_hashtabsize(unsigned sz) const;
@@ -158,7 +160,6 @@
 	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:
diff --git a/ginac/fderivative.h b/ginac/fderivative.h
--- a/ginac/fderivative.h
+++ b/ginac/fderivative.h
@@ -40,6 +40,7 @@
 {
 	GINAC_DECLARE_REGISTERED_CLASS(fderivative, function)
 
+	friend struct ex_is_greater_degrevlex;
 	// other constructors
 public:
 	/** Construct derivative with respect to one parameter.
diff --git a/ginac/function.h b/ginac/function.h
--- a/ginac/function.h
+++ b/ginac/function.h
@@ -310,6 +310,7 @@
 {
 	GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
 
+	friend struct ex_is_greater_degrevlex;
 	// CINT has a linking problem
 #ifndef __MAKECINT__
 	friend void ginsh_get_ginac_functions();
diff --git a/ginac/mul.cpp b/ginac/mul.cpp
--- a/ginac/mul.cpp
+++ b/ginac/mul.cpp
@@ -33,6 +33,7 @@
 #include "constant.h"
 #include "function.h"
 #include "inifcns.h"
+#include "order.h"
 
 #include <iostream>
 #include <vector>
@@ -1232,6 +1233,8 @@
 	return (new add(addseq))->setflag(status_flags::dynallocated);
 }
 
+/** Total degree of a mul object.
+ Beware that symbolic exponents are wrapped inside pow objects with 1 as coeff. */
 double mul::total_degree() const
 {
 	if (flags & status_flags::tdegree_calculated) {
diff --git a/ginac/mul.h b/ginac/mul.h
--- a/ginac/mul.h
+++ b/ginac/mul.h
@@ -33,6 +33,7 @@
 {
 	GINAC_DECLARE_REGISTERED_CLASS(mul, expairseq)
 	
+	friend struct ex_is_greater_degrevlex;
 	friend class add;
 	friend class ncmul;
 	friend class power;
diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp
--- a/ginac/numeric.cpp
+++ b/ginac/numeric.cpp
@@ -236,6 +236,7 @@
         return x.v._pyobject;
 
       default:
+	std::cout << x.t <<std::endl;
 	stub("Number_T_to_pyobject -- not able to do conversion to pyobject; everything else will be nonsense");
 	return 0;
       }
diff --git a/ginac/numeric.h b/ginac/numeric.h
--- a/ginac/numeric.h
+++ b/ginac/numeric.h
@@ -207,7 +207,7 @@
   class numeric : public basic
   {
     GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
-	
+    //friend struct expair_is_greater_degrevlex;
     // member functions
 	
     // other constructors
diff --git a/ginac/order.cpp b/ginac/order.cpp
--- a/ginac/order.cpp
+++ b/ginac/order.cpp
@@ -1,3 +1,25 @@
+/** @file order.cpp
+ *
+ *  Definitions of order used for printing. */
+
+/*
+ *   Copyright (C) 2011 Burcin Erocal <burcin@erocal.org>
+ *   Copyright (C) 2011 Jean-Pierre Flori <flori@enst.fr>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
 
 #include "order.h"
 #include "registrar.h"
@@ -8,19 +30,88 @@
 
 namespace GiNaC {
 
+/** What SAGE does for printing:
+ To print multivariate polynomials, SAGE uses "reversed" (bigger terms first) 'degrevlex' order
+ i.e. "reversed" graded reversed lexicographic order, the lexicographic order is defined below.
+ The variables are ordered according to their "creation" order
+ i.e PR.<x,a,t> = PolynomialRing(QQ) gives x > a > t
+ and x+t+a is printed x + a + t
+*/
 
+/** Returns true if lhex > rhex for 'degrevlex' of SAGE
+ i.e. graded reversed lexicographic order
+*/
 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<<"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 *lh = get_pointer(lhex.bp);
 	const basic *rh = get_pointer(rhex.bp);
 	return compare(lh, rh) == 1;
 }
 
+int ex_is_greater_degrevlex::compare (const ex &lhex, const ex &rhex) const {
+	const basic *lh = get_pointer(lhex.bp);
+	const basic *rh = get_pointer(rhex.bp);
+	return compare(lh, rh);
+}
+
+bool expair_is_greater_degrevlex::operator() (const expair &lhex, const expair &rhex) const
+{
+	const basic *lh = get_pointer(lhex.rest.bp);
+	const basic *rh = get_pointer(rhex.rest.bp);
+
+	// compare rests
+        int cmpval = ex_is_greater_degrevlex::in_type(in_type_id).compare(lh, rh);
+	if (cmpval != 0) {
+	        return cmpval == 1;
+	}
+
+	// compare coeffs which are numerics
+	//std::cout << "comparing coeffs" << std::endl;
+	//lhex.coeff.bp->dbgprinttree();
+	//rhex.coeff.bp->dbgprinttree();
+	numeric lh_coeff = ex_to<numeric>(lhex.coeff);
+	numeric rh_coeff = ex_to<numeric>(rhex.coeff);
+	// strange...
+	//std::cout<<lh->compare_same_type(rh)<<std::endl;
+	//return lh->compare_same_type(rh) == 1;
+	double lh_deg = 1;
+	double rh_deg = 1;
+		if (lh_coeff.is_real()) {
+			lh_deg = lh_coeff.to_double();
+		} else {
+			lh_deg = std::sqrt(std::pow(lh_coeff.real().to_double(), 2) + 
+					std::pow(lh_coeff.imag().to_double(), 2));
+		}
+		if (rh_coeff.is_real()) {
+			rh_deg = rh_coeff.to_double();
+		} else {
+			rh_deg = std::sqrt(std::pow(rh_coeff.real().to_double(), 2) + 
+					std::pow(rh_coeff.imag().to_double(), 2));
+		}
+
+	return lh_deg > rh_deg;	
+}
+
+/** Comparison functions:
+ They should implement 'degrevlex' of SAGE
+ i.e. graded reversed lexicographic order
+ The lexicographic order should depend on the "creation" order of variables ?
+ Or should it be a "natural" one on strings : a > b > ... > x > y > ... ?
+*/
+
+/** Return values for comparison functions :
+ compare(a,b) should return :
+ -1 if a < b
+ 0 if a == b
+ 1 if a > b
+ as <=> in Perl and GiNaC internal functions
+*/
+
 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();
@@ -42,15 +133,44 @@
 			return compare_same_type_power(
 					static_cast<const power*>(lh),
 					static_cast<const power*>(rh));
+		} else if (typeid_rh == function_id) {
+			return compare_same_type_function(
+					static_cast<const function*>(lh),
+					static_cast<const function*>(rh));
+		} else if (typeid_rh == fderivative_id) {
+			return compare_same_type_fderivative(
+					static_cast<const fderivative*>(lh),
+					static_cast<const fderivative*>(rh));
 		} else {
+		        // using GiNaC functions by default
 			return lh->compare_same_type(*rh);
 		}
+	// at present numerics are combined into overall_coefficient
+	// even when hold parameter is used
+	} else if (typeid_lh == numeric_id) {
+	 	//print numerics after anything else
+	        return -1;
+	} else if (typeid_rh == numeric_id) {
+	 	//print numerics after anything else
+	        return 1;
+	} else if (typeid_lh == constant_id) {
+	 	//print constants after anything else
+	        return -1;
+	} else if (typeid_rh == constant_id) {
+	 	//print constants after anything else
+	        return 1;
 	} else if (typeid_lh == fderivative_id) {
-		//print fderivatives after everything
+		//print fderivatives after everything else
 		return -1;
+	} else if (typeid_rh == fderivative_id) {
+		//print fderivatives after everything esle
+		return 1;
 	} else if (typeid_lh == function_id) {
 		//print functions before fderivatives, after anything else
-		return typeid_rh == fderivative_id ? 1 : -1;
+	        return -1;
+	} else if (typeid_rh == function_id) {
+		//print functions before fderivatives, after anything else
+	        return 1;
 	} else if (typeid_lh == mul_id) {
 		if (typeid_rh == power_id) {
 			return compare_mul_power(
@@ -60,12 +180,34 @@
 			return compare_mul_symbol(
 					static_cast<const mul*>(lh),
 					static_cast<const symbol*>(rh));
+		} else if (typeid_rh == add_id) {
+			return -compare_add_mul(
+					static_cast<const add*>(rh),
+					static_cast<const mul*>(lh));
+		}
+	} else if (typeid_lh == add_id) {
+		if (typeid_rh == power_id) {
+			return compare_add_power(
+					static_cast<const add*>(lh),
+					static_cast<const power*>(rh));
+		} else if (typeid_rh == symbol_id) {
+			return compare_add_symbol(
+					static_cast<const add*>(lh),
+					static_cast<const symbol*>(rh));
+		} else if (typeid_rh == mul_id) {
+			return compare_add_mul(
+					static_cast<const add*>(lh),
+					static_cast<const mul*>(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 == add_id) {
+			return -compare_add_power(
+					static_cast<const add*>(rh),
+					static_cast<const power*>(lh));
 		} else if (typeid_rh == symbol_id) {
 			return compare_power_symbol(
 					static_cast<const power*>(lh),
@@ -76,47 +218,73 @@
 			return -compare_mul_symbol(
 					static_cast<const mul*>(rh),
 					static_cast<const symbol*>(lh));
+		} else if (typeid_rh == add_id) {
+			return -compare_add_symbol(
+					static_cast<const add*>(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);
+        }
+	std::cout<<"comparing typeid's"<<std::endl;
+	return (typeid_lh<typeid_rh ? -1 : 1);
 }
 
+// compare a mul and a symbol objects
+// same behavior within mul and add objects:
+// the total degree of the symbol is 1
+// check total degree of mul then compare smallest item to symbol
 int ex_is_greater_degrevlex::compare_mul_symbol(const mul *lh,
 		const symbol *rh) const
 {
+  //std::cout<<"comparing mul and symbol"<<std::endl;
+  //lh->dbgprint();
+  //rh->dbgprint();
 	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;
+
+	if (tdeg != 1)
+	        return tdeg > 1 ? 1 : -1;
+	
+	const expair smallest_item = lh->get_sorted_seq()->back();
+
+	// compare bases
+	cmpval = compare(get_pointer(smallest_item.rest.bp), rh);
+	if (cmpval != 0) {
+		return cmpval;
 	}
-	return tdeg > 1 ? 1 : -1;
+
+	// compare exponents
+	cmpval = -compare(get_pointer(smallest_item.coeff.bp),_num1_p);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	if (lh->seq.size() == 1 && lh->overall_coeff.is_equal(_ex1))
+		return 0;
+
+	// there is something of total degree 0 in front of the mul object
+	return 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
+
+// compare a mul and a pow objects
+// same behavior wihtin mul and add objects:
+// first we compare total degrees
+// if equal we compare the smallest basis in the sequence to the basis in other
+// then their exponents
 int ex_is_greater_degrevlex::compare_mul_power(const mul *lh,
 		const power *rh) const
 {
+	int cmpval;
+
 	double lh_deg = lh->total_degree();
-	double rh_deg;
+	double rh_deg = 1;
 	numeric rh_exp;
-	int cmpval = 0;
 	if (is_a<numeric>(rh->exponent)) {
 		rh_exp = ex_to<numeric>(rh->exponent);
 		if (rh_exp.is_real()) {
@@ -125,45 +293,76 @@
 			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),
+	if (rh_deg != lh_deg)
+		return lh_deg < rh_deg ? -1 : 1;
+	// same total degree
+
+	// smallest item is at the end of the sorted sequence
+	const expair smallest_item = lh->get_sorted_seq()->back();
+	
+	// compare bases
+	cmpval = compare(get_pointer(smallest_item.rest.bp),
 			get_pointer(rh->basis.bp));
 	if (cmpval != 0) {
 		return cmpval;
 	}
+
+	// compare exponents
+	cmpval = -compare(get_pointer(smallest_item.coeff.bp),
+		        get_pointer(rh->exponent.bp));
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
 	if (lh->seq.size() == 1 && lh->overall_coeff.is_equal(_ex1))
 		return 0;
+
+	// there is something of total degree 0 in front of the mul object
 	return 1;
 }
 
+// compare two mul objects
+// same behavior wihtin mul and add objects:
+// first we compare total degrees
+// if equal we compare the basis of the smallest items
+// then their exponents
+// and so on
 int ex_is_greater_degrevlex::compare_same_type_mul(const mul *lh,
 		const mul *rh) const
 {
+  //std::cout<<"comparing mul and mul"<<std::endl;
+  //lh->dbgprint();
+  //rh->dbgprint();
 	int cmpval;
-
+	
 	// compare total degrees
 	double lh_deg = lh->total_degree();
 	double rh_deg = rh->total_degree();
+        //std::cout<<"degree "<<lh_deg<<" and "<<rh_deg<<std::endl;
 	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();
+	const epvector *sorted_seq1 = lh->get_sorted_seq();
+	const epvector *sorted_seq2 = rh->get_sorted_seq();
+	epvector::const_reverse_iterator cit1 = sorted_seq1->rbegin();
+	epvector::const_reverse_iterator cit2 = sorted_seq2->rbegin();
+	epvector::const_reverse_iterator last1 = sorted_seq1->rend();
+	epvector::const_reverse_iterator last2 = sorted_seq2->rend();
 
 	for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
-		cmpval = ( cit1->compare(*cit2));
-		if (cmpval != 0)
+		// compare bases
+		cmpval = compare(get_pointer(cit1->rest.bp),get_pointer(cit2->rest.bp));
+		if (cmpval != 0) {
 			return cmpval;
+		}
+
+		// compare exponents
+	        cmpval = -compare(get_pointer(cit1->coeff.bp),get_pointer(cit2->coeff.bp));
+		if (cmpval != 0) {
+			return cmpval;
+		}
 	}
 
 	// compare sizes
@@ -175,85 +374,316 @@
 	// compare overall_coeff
 	cmpval = compare(get_pointer(lh->overall_coeff.bp),
 			get_pointer(rh->overall_coeff.bp));
-	if (cmpval!=0)
-		return cmpval;
 
-	return 0;
+	return cmpval;
 }
 
+// compare an add and a symbol objects
+// same behavior wihtin mul and add objects:
+// the coefficient of the symbol is 1
+int ex_is_greater_degrevlex::compare_add_symbol(const add *lh,
+		const symbol *rh) const
+{
+  //std::cout<<"comparing add and symbol"<<std::endl;
+  //lh->dbgprint();
+  //rh->dbgprint();
+	int cmpval;
+
+	const expair biggest_item = lh->get_sorted_seq()->front();
+
+	// compare bases
+	cmpval = ex_is_greater_degrevlex::in_type(&add::tinfo_static).compare(get_pointer(biggest_item.rest.bp), rh);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	// compare coefficients
+	cmpval = compare(get_pointer(biggest_item.coeff.bp),_num1_p);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	if (lh->seq.size() == 1 && lh->overall_coeff.is_equal(_ex0))
+		return 0;
+
+	// there is something at the end of the add object
+	return 1;
+}
+
+// compare an add and a mul objects
+// same behavior within mul and add objects:
+// the coefficient of the mul object is 1
+int ex_is_greater_degrevlex::compare_add_mul(const add *lh,
+		const mul *rh) const
+{
+        int cmpval;
+	const expair biggest_item = lh->get_sorted_seq()->front();
+
+	// compare bases
+	cmpval = ex_is_greater_degrevlex::in_type(&add::tinfo_static).compare(get_pointer(biggest_item.rest.bp), rh);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	// compare coefficients
+	cmpval = compare(get_pointer(biggest_item.coeff.bp),_num1_p);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	if (lh->seq.size() == 1 && lh->overall_coeff.is_equal(_ex0))
+		return 0;
+
+	// there is something at the end of the object
+	return 1;
+}
+
+// compare an add and a pow objects
+// same behavior wihtin mul and add objects:
+// the coefficient of the power object is 1
+int ex_is_greater_degrevlex::compare_add_power(const add *lh,
+		const power *rh) const
+{
+        int cmpval;
+	const expair biggest_item = lh->get_sorted_seq()->front();
+
+	// compare bases
+	cmpval = ex_is_greater_degrevlex::in_type(&add::tinfo_static).compare(get_pointer(biggest_item.rest.bp), rh);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	// compare coefficients
+	cmpval = compare(get_pointer(biggest_item.coeff.bp),_num1_p);
+	if (cmpval != 0) {
+		return cmpval;
+	}
+
+	if (lh->seq.size() == 1 && lh->overall_coeff.is_equal(_ex0))
+		return 0;
+
+	// there is something at the end of the object
+	return 1;
+}
+
+// compare two add objects
+// same behavior wihtin mul and add objects:
+// first we compare the basis of the biggest items
+// then their coefficients
+// and so on
 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();
+	const epvector *sorted_seq1 = lh->get_sorted_seq();
+	const epvector *sorted_seq2 = rh->get_sorted_seq();
+	epvector::const_iterator cit1 = sorted_seq1->begin();
+	epvector::const_iterator cit2 = sorted_seq2->begin();
+	epvector::const_iterator last1 = sorted_seq1->end();
+	epvector::const_iterator last2 = sorted_seq2->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;
+		// compare bases
+		cmpval = ex_is_greater_degrevlex::in_type(&add::tinfo_static).compare(get_pointer(cit1->rest.bp),get_pointer(cit2->rest.bp));
+		if (cmpval != 0) {
+			return cmpval;
+		}
+
+		// compare coefficients
+	        cmpval = compare(get_pointer(cit1->coeff.bp),get_pointer(cit2->coeff.bp));
+		if (cmpval != 0) {
+			return cmpval;
+		}
 	}
 
-	GINAC_ASSERT(cit1==last1);
-	GINAC_ASSERT(cit2==last2);
+	// 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;
+	
+	return cmpval;
 }
 
+// compare a power and a symbol objects
+// does not behave the same way inside add or mul
+// in mul object:
+// first we compare bases
+// then exponents
+// in add object:
+// first exponents
+// then bases
 int ex_is_greater_degrevlex::compare_power_symbol(const power *lh,
 		const symbol *rh) const
 {
+        int cmpval;
+
 	//std::cout<<"in compare_power_symbol"<<std::endl;
-	int cmpval;
-	cmpval = compare(get_pointer(lh->exponent.bp), _num1_p);
-	if (cmpval != 0) {
+        if (in_type_id == &mul::tinfo_static) {
+	        cmpval = compare(get_pointer(lh->basis.bp), rh);
+		if (cmpval != 0)
+        		  return cmpval;
+		
+		cmpval = compare(get_pointer(lh->exponent.bp), _num1_p);
+		
 		return cmpval;
 	}
-	return compare(get_pointer(lh->basis.bp), rh);
+
+	// We are in an add object
+	if (is_a<numeric>(lh->exponent)) {
+		numeric lh_exp = ex_to<numeric>(lh->exponent);
+		double lh_deg;
+		if (lh_exp.is_real()) {
+			lh_deg = lh_exp.to_double();
+		} else {
+			lh_deg = std::sqrt(std::pow(lh_exp.real().to_double(), 2) + 
+					std::pow(lh_exp.imag().to_double(), 2));
+		}
+		if (lh_deg != 1)
+			return lh_deg < 1 ? -1 : 1;
+	}
+
+	cmpval = compare(get_pointer(lh->basis.bp), rh);
+
+	return cmpval;
 }
 
+// compare two power objects
+// does not behave the same way inside add or mul
+// in mul object:
+// fist we compare bases
+// then exponents
+// in add object:
+// first exponents
+// then bases
 int ex_is_greater_degrevlex::compare_same_type_power(const power *lh,
-		const power *rh) const
+	        const power *rh) const
 {
-	int cmpval = compare(get_pointer(lh->exponent.bp),
-				get_pointer(rh->exponent.bp));
-	if (cmpval)
+        int cmpval;
+        if (in_type_id == &mul::tinfo_static) {
+	        cmpval = compare(get_pointer(lh->basis.bp), get_pointer(rh->basis.bp));
+		if (cmpval != 0)
+        		  return cmpval;
+		cmpval = compare(get_pointer(lh->exponent.bp), get_pointer(rh->exponent.bp));
+		
 		return cmpval;
-	else
-		return compare(get_pointer(lh->basis.bp),
-			get_pointer(rh->basis.bp));
+	}
+
+	double lh_deg = 1;
+	double rh_deg = 1;
+	if (is_a<numeric>(lh->exponent)) {
+		numeric lh_exp = ex_to<numeric>(lh->exponent);
+		if (lh_exp.is_real()) {
+			lh_deg = lh_exp.to_double();
+		} else {
+			lh_deg = std::sqrt(std::pow(lh_exp.real().to_double(), 2) + 
+					std::pow(lh_exp.imag().to_double(), 2));
+		}
+	}
+	if (is_a<numeric>(rh->exponent)) {
+		numeric 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;
+
+	cmpval = compare(get_pointer(lh->basis.bp),
+			 get_pointer(rh->basis.bp));
+	if (cmpval != 0)
+        	return cmpval;
+
+	if (is_a<numeric>(lh->exponent) && is_a<numeric>(rh->exponent))
+	        return 0;
+	return compare(get_pointer(lh->exponent.bp), get_pointer(rh->exponent.bp));
 }
 
+// compare two symbol objects
+// same behavior wihtin mul and add objects:
+// we compare names
 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<<"in compare_same symbol"<<std::endl;
 	//std::cout<<"lh: ";
 	//lh->dbgprint();
 	//std::cout<<"rh: ";
 	//rh->dbgprint();
 	//std::cout<<std::endl;
+
+        // Weird because Sage sorts based on creation order.
+        // 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;
+	/* Reversed ordering on char encoding (i.e. "a" < "b") then length (i.e. "abc" < "abcd")
+	   i.e. "x" > "y" and "xyz" > "xyzt" */
 	if (lh->serial==rh->serial) return 0;
 	//std::cout<<"after if"<<std::endl;
+	return lh->name < rh->name ? 1 : -1;
 
-// SAGE/Pynac: Sorting based on creation order doesn't work for Sage.
-// instead we sort on variable name. -- William Stein
+	//return lh->serial < rh->serial ? 1 : -1;
+	//return -lh->compare_same_type(*rh);
+}
 
-	//std::cout<<"comparing names: "<<(lh->name < rh->name)<<std::endl;
-	return lh->name < (rh->name) ? 1 : -1;
+// compare two containers of the same type
+template <template <class T, class = std::allocator<T> > class C>
+int ex_is_greater_degrevlex::compare_same_type_container(const container<C> *lh,
+							 const container<C> *rh) const
+{
+        typename C<ex>::const_iterator it1 = lh->seq.begin(), it1end = lh->seq.end(),
+	                      it2 = rh->seq.begin(), it2end = rh->seq.end();
+
+	while (it1 != it1end && it2 != it2end) {
+	        int cmpval = compare(get_pointer(it1->bp), get_pointer(it2->bp));
+		if (cmpval)
+			return cmpval;
+		++it1; ++it2;
+	}
+
+	return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
+}
+
+// compare two function objects
+// same behavior wihtin mul and add objects:
+// we compare names
+int ex_is_greater_degrevlex::compare_same_type_function(const function *lh,
+		const function *rh) const
+{
+
+	//std::cout<<"comparing names: "<<(lh->get_name() < rh->get_name())<<std::endl;
+	/* Reversed ordering on char encoding (i.e. "a" < "b") then length (i.e. "abc" < "abcd")
+	   i.e. "x" > "y" and "xyz" > "xyzt" */
+        if (lh->serial==rh->serial) //return 0;
+	        return compare_same_type_container(lh,rh);
+	//std::cout<<"after if"<<std::endl;
+	return lh->get_name() < rh->get_name() ? 1 : -1;	
+
+	//return lh->serial < rh->serial ? 1 : -1;
+	//return -lh->compare_same_type(*rh);
+}
+
+// compare two fderivative objects
+// same behavior wihtin mul and add objects:
+// we compare names
+int ex_is_greater_degrevlex::compare_same_type_fderivative(const fderivative *lh,
+		const fderivative *rh) const
+{
+        int cmpval = compare_same_type_function(lh, rh);
+	if (cmpval != 0)
+	        return cmpval;
+	if (lh->parameter_set != rh->parameter_set)
+	        return lh->parameter_set < rh->parameter_set ? -1 : 1;
+	return 0;
 }
 
 } // namespace GiNaC
diff --git a/ginac/order.h b/ginac/order.h
--- a/ginac/order.h
+++ b/ginac/order.h
@@ -1,40 +1,131 @@
+/** @file order.h
+ *
+ *  Definitions of order used for printing. */
+
+/*
+ *   Copyright (C) 2011 Burcin Erocal <burcin@erocal.org>
+ *   Copyright (C) 2011 Jean-Pierre Flori <flori@enst.fr>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GINAC_ORDER_H__
+#define __GINAC_ORDER_H__
+
 #include "ex.h"
 #include "basic.h"
 #include "mul.h"
 #include "power.h"
 #include "add.h"
 #include "symbol.h"
+#include "function.h"
+#include "fderivative.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 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");
+	const tinfo_t numeric_id;// = find_tinfo_key("numeric");
+	const tinfo_t constant_id;// = find_tinfo_key("constant");
+	const tinfo_t in_type_id;
 
-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); }
+	ex_is_greater_degrevlex(const tinfo_t type_id):
+		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")),
+		numeric_id(find_tinfo_key("numeric")),
+		constant_id(find_tinfo_key("constant")),
+		in_type_id(type_id) {};
+
+	bool operator() (const ex &lh, const ex &rh) const;
+	int compare(const ex &lh, const ex &rh) const;
+	int compare(const basic *lh, const basic *rh) const;
+	// mul objects
+	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;
+	// add objects
+	int compare_add_symbol(const add *lh, const symbol *rh) const;
+	int compare_add_power(const add *lh, const power *rh) const;
+	int compare_add_mul(const add *lh, const mul *rh) const;
+	int compare_same_type_add(const add *lh, const add *rh) const;
+	// power objects
+	int compare_power_symbol(const power *lh, const symbol *rh) const;
+	int compare_same_type_power(const power *lh, const power *rh) const;
+	// symbol objects
+	int compare_same_type_symbol(const symbol *lh, const symbol *rh) const;
+	// container objects
+	template <template <class T, class = std::allocator<T> > class C>
+	int compare_same_type_container(const container<C> *lh,const container<C> *rh) const;
+	// function objects
+	int compare_same_type_function(const function *lh, const function *rh) const;
+	// fderivative objects
+	int compare_same_type_fderivative(const fderivative *lh, const fderivative *rh) const;
+
+	static const ex_is_greater_degrevlex& in_type(tinfo_t in_type_id) {
+	  static ex_is_greater_degrevlex in_type[2] = {ex_is_greater_degrevlex(&add::tinfo_static),
+						       ex_is_greater_degrevlex(&mul::tinfo_static)};
+	        if (in_type_id == &mul::tinfo_static)
+		        return in_type[1];
+        	return in_type[0];
+	}
 };
 
-}
+// We have to define the following class to sort held expressions
+// E.g. 3*x+2*x which does not get simplified to 5*x.
+struct expair_is_greater_degrevlex : public std::binary_function<expair, expair, bool>
+{
+        const tinfo_t in_type_id;
+        expair_is_greater_degrevlex(tinfo_t in_type):
+	        in_type_id(in_type) {};
+	bool operator() (const expair &lh, const expair &rh) const;
+
+	static const expair_is_greater_degrevlex& in_type(tinfo_t in_type_id) {
+        	static expair_is_greater_degrevlex in_type[2] = {expair_is_greater_degrevlex(&add::tinfo_static),expair_is_greater_degrevlex(&add::tinfo_static)};
+		if (in_type_id == &mul::tinfo_static)
+		        return in_type[1];
+        	return in_type[0];
+	}
+};
+
+struct expair_rest_is_greater_degrevlex : public std::binary_function<expair, expair, bool>
+{
+        const tinfo_t in_type_id;
+        expair_rest_is_greater_degrevlex(tinfo_t in_type):
+	        in_type_id(in_type) {};
+	bool operator() (const expair &lh, const expair &rh) const {
+	        return ex_is_greater_degrevlex::in_type(in_type_id)(lh.rest, rh.rest);
+	}
+
+	static const expair_rest_is_greater_degrevlex& in_type(tinfo_t in_type_id) {
+        	static expair_rest_is_greater_degrevlex in_type[2] = {expair_rest_is_greater_degrevlex(&add::tinfo_static),
+					expair_rest_is_greater_degrevlex(&mul::tinfo_static)};
+		if (in_type_id == &mul::tinfo_static)
+		        return in_type[1];
+        	return in_type[0];
+	}
+};
+
+} // namespace GiNaC
+
+#endif // ndef __GINAC_ORDER_H__