]> www.ginac.de Git - ginac.git/blob - ginac/expair.h
- switched to automake build environment
[ginac.git] / ginac / expair.h
1 /** @file expair.h */
2
3 /** A pair of expressions.
4  *  This similar to, but slightly extended STL's pair<> but we need to account
5  *  for methods like .compare() */
6 class expair
7 {
8 public:
9     expair() {}
10     ~expair() {}
11     expair(expair const & other) : rest(other.rest), coeff(other.coeff)
12     {
13         ASSERT(is_ex_exactly_of_type(coeff,numeric));
14     }
15     expair const & operator=(expair const & other)
16     {
17         if (this != &other) {
18             rest=other.rest;
19             coeff=other.coeff;
20         }
21         return *this;
22     }
23     expair(ex const & r, ex const & c) : rest(r), coeff(c)
24     {
25         ASSERT(is_ex_exactly_of_type(coeff,numeric));
26     }
27     
28     bool is_numeric_with_coeff_1(void) const
29     {
30         ASSERT(is_ex_exactly_of_type(coeff,numeric));
31         return is_ex_exactly_of_type(rest,numeric) &&
32                (ex_to_numeric(coeff).compare(numONE())==0);
33     }
34
35     bool is_equal(expair const & other) const
36     {
37         return (rest.is_equal(other.rest) && coeff.is_equal(other.coeff));
38     }
39     bool is_less(expair const & other) const 
40     {
41         return (rest.compare(other.rest)<0) ||
42                (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
43     }
44     int compare(expair const & other) const
45     {
46         int cmpval=rest.compare(other.rest);
47         if (cmpval!=0) return cmpval;
48         cmpval=coeff.compare(other.coeff);
49         return cmpval;
50     }
51
52     bool is_less_old2(expair const & other) const 
53     {
54         /*
55         bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
56         bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
57         if (this_numeric_with_coeff_1) {
58             if (other_numeric_with_coeff_1) {
59                 // both have coeff 1: compare rests
60                 return rest.compare(other.rest)<0;
61             }
62             // only this has coeff 1: >
63             return false;
64         } else if (other_numeric_with_coeff_1) {
65             // only other has coeff 1: <
66             return true;
67         }
68         return (rest.compare(other.rest)<0) ||
69                (!(other.rest.compare(rest)<0) &&
70                  (coeff.compare(other.coeff)<0));
71         */
72         if (is_ex_exactly_of_type(rest,numeric) &&
73             is_ex_exactly_of_type(other.rest,numeric)) {
74             if (ex_to_numeric(coeff).compare(numONE())==0) {
75                 if (ex_to_numeric(other.coeff).compare(numONE())==0) {
76                     // both have coeff 1: compare rests
77                     return rest.compare(other.rest)<0;
78                 }
79                 // only this has coeff 1: >
80                 return false;
81             } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
82                 // only other has coeff 1: <
83                 return true;
84             }
85             // neither has coeff 1: usual compare        
86         }
87         return (rest.compare(other.rest)<0) ||
88                (!(other.rest.compare(rest)<0) &&
89                  (coeff.compare(other.coeff)<0));
90     }
91     int compare_old2(expair const & other) const
92     {
93         if (is_ex_exactly_of_type(rest,numeric) &&
94             is_ex_exactly_of_type(other.rest,numeric)) {
95             if (ex_to_numeric(coeff).compare(numONE())==0) {
96                 if (ex_to_numeric(other.coeff).compare(numONE())==0) {
97                     // both have coeff 1: compare rests
98                     return rest.compare(other.rest);
99                 }
100                 // only this has coeff 1: >
101                 return 1;
102             } else if (ex_to_numeric(other.coeff).compare(numONE())==0) {
103                 // only other has coeff 1: <
104                 return -1;
105             }
106             // neither has coeff 1: usual compare        
107         }
108         /*
109         bool this_numeric_with_coeff_1=is_numeric_with_coeff_1();
110         bool other_numeric_with_coeff_1=other.is_numeric_with_coeff_1();
111         if (this_numeric_with_coeff_1) {
112             if (other_numeric_with_coeff_1) {
113                 // both have coeff 1: compare rests
114                 return rest.compare(other.rest);
115             }
116             // only this has coeff 1: >
117             return 1;
118         } else if (other_numeric_with_coeff_1) {
119             // only other has coeff 1: <
120             return -1;
121             // neither has coeff 1: usual compare        
122         }
123         */
124         int cmpval=rest.compare(other.rest);
125         if (cmpval!=0) return cmpval;
126         return coeff.compare(other.coeff);
127     }
128     bool is_less_old(expair const & other) const 
129     {
130         return (rest.compare(other.rest)<0) ||
131                (!(other.rest.compare(rest)<0) && (coeff.compare(other.coeff)<0));
132     }
133     int compare_old(expair const & other) const
134     {
135         int cmpval=rest.compare(other.rest);
136         if (cmpval!=0) return cmpval;
137         cmpval=coeff.compare(other.coeff);
138         return cmpval;
139     }
140
141     void printraw(ostream & os) const
142     {
143         os << "expair(";
144         rest.printraw(os);
145         os << ",";
146         coeff.printraw(os);
147         os << ")";
148     }
149
150     ex rest;
151     ex coeff;
152 };
153
154 class expair_is_less
155 {
156 public:
157     bool operator()(expair const & lh, expair const & rh) const
158     {
159         return lh.is_less(rh);
160     }
161 };
162
163 class expair_is_less_old
164 {
165 public:
166     bool operator()(expair const & lh, expair const & rh) const
167     {
168         return lh.is_less_old(rh);
169     }
170 };
171
172
173