forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic_rational_function.cc
222 lines (178 loc) · 6.62 KB
/
symbolic_rational_function.cc
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
// NOLINTNEXTLINE(build/include): Its header file is included in symbolic.h.
#include "drake/common/symbolic.h"
namespace drake {
namespace symbolic {
RationalFunction::RationalFunction()
: numerator_{} /* zero polynomial */, denominator_{1} {}
RationalFunction::RationalFunction(const Polynomial& numerator,
const Polynomial& denominator)
: numerator_{numerator}, denominator_{denominator} {
if (denominator_.EqualTo(Polynomial() /* zero polynomial */)) {
throw std::invalid_argument(
"RationalFunction: the denominator should not be 0.");
}
DRAKE_ASSERT_VOID(CheckIndeterminates());
}
RationalFunction::RationalFunction(const Polynomial& p)
: RationalFunction(p, Polynomial(1)) {}
RationalFunction::RationalFunction(double c)
: RationalFunction(Polynomial(c), Polynomial(1)) {}
bool RationalFunction::EqualTo(const RationalFunction& f) const {
return numerator_.EqualTo(f.numerator()) &&
denominator_.EqualTo(f.denominator());
}
Formula RationalFunction::operator==(const RationalFunction& f) const {
return denominator_ * f.numerator() == numerator_ * f.denominator();
}
Formula RationalFunction::operator!=(const RationalFunction& f) const {
return !(*this == f);
}
std::ostream& operator<<(std::ostream& os, const RationalFunction& f) {
os << "(" << f.numerator() << ") / (" << f.denominator() << ")";
return os;
}
void RationalFunction::CheckIndeterminates() const {
const Variables vars1{intersect(numerator_.indeterminates(),
denominator_.decision_variables())};
const Variables vars2{intersect(numerator_.decision_variables(),
denominator_.indeterminates())};
if (!vars1.empty() || !vars2.empty()) {
std::ostringstream oss;
oss << "RationalFunction " << *this << " is invalid.\n";
if (!vars1.empty()) {
oss << "The following variable(s) "
"are used as indeterminates in the numerator and decision "
"variables in the denominator at the same time:\n"
<< vars1 << ".\n";
}
if (!vars2.empty()) {
oss << "The following variable(s) "
"are used as decision variables in the numerator and "
"indeterminates variables in the denominator at the same time:\n"
<< vars2 << ".\n";
}
throw std::logic_error(oss.str());
}
}
RationalFunction& RationalFunction::operator+=(const RationalFunction& f) {
numerator_ = numerator_ * f.denominator() + denominator_ * f.numerator();
denominator_ *= f.denominator();
return *this;
}
RationalFunction& RationalFunction::operator+=(const Polynomial& p) {
numerator_ = p * denominator_ + numerator_;
return *this;
}
RationalFunction& RationalFunction::operator+=(double c) {
numerator_ = c * denominator_ + numerator_;
return *this;
}
RationalFunction& RationalFunction::operator-=(const RationalFunction& f) {
*this += -f;
return *this;
}
RationalFunction& RationalFunction::operator-=(const Polynomial& p) {
*this += -p;
return *this;
}
RationalFunction& RationalFunction::operator-=(double c) { return *this += -c; }
RationalFunction& RationalFunction::operator*=(const RationalFunction& f) {
numerator_ *= f.numerator();
denominator_ *= f.denominator();
DRAKE_ASSERT_VOID(CheckIndeterminates());
return *this;
}
RationalFunction& RationalFunction::operator*=(const Polynomial& p) {
numerator_ *= p;
DRAKE_ASSERT_VOID(CheckIndeterminates());
return *this;
}
RationalFunction& RationalFunction::operator*=(double c) {
numerator_ *= c;
return *this;
}
RationalFunction& RationalFunction::operator/=(const RationalFunction& f) {
if (f.numerator().EqualTo(Polynomial())) {
throw std::logic_error("RationalFunction: operator/=: The divider is 0.");
}
numerator_ *= f.denominator();
denominator_ *= f.numerator();
DRAKE_ASSERT_VOID(CheckIndeterminates());
return *this;
}
RationalFunction& RationalFunction::operator/=(const Polynomial& p) {
if (p.EqualTo(Polynomial())) {
throw std::logic_error("RationalFunction: operator/=: The divider is 0.");
}
denominator_ *= p;
DRAKE_ASSERT_VOID(CheckIndeterminates());
return *this;
}
RationalFunction& RationalFunction::operator/=(double c) {
if (c == 0) {
throw std::logic_error("RationalFunction: operator/=: The divider is 0.");
}
denominator_ *= c;
return *this;
}
RationalFunction operator-(RationalFunction f) {
return RationalFunction(-f.numerator(), f.denominator());
}
RationalFunction operator+(RationalFunction f1, const RationalFunction& f2) {
return f1 += f2;
}
RationalFunction operator+(RationalFunction f, const Polynomial& p) {
return f += p;
}
RationalFunction operator+(const Polynomial& p, RationalFunction f) {
return f += p;
}
RationalFunction operator+(RationalFunction f, double c) { return f += c; }
RationalFunction operator+(double c, RationalFunction f) { return f += c; }
RationalFunction operator-(RationalFunction f1, const RationalFunction& f2) {
return f1 -= f2;
}
RationalFunction operator-(RationalFunction f, const Polynomial& p) {
return f -= p;
}
RationalFunction operator-(const Polynomial& p, RationalFunction f) {
return f = -f + p;
}
RationalFunction operator-(RationalFunction f, double c) { return f -= c; }
RationalFunction operator-(double c, RationalFunction f) { return f = -f + c; }
RationalFunction operator*(RationalFunction f1, const RationalFunction& f2) {
return f1 *= f2;
}
RationalFunction operator*(RationalFunction f, const Polynomial& p) {
return f *= p;
}
RationalFunction operator*(const Polynomial& p, RationalFunction f) {
return f *= p;
}
RationalFunction operator*(RationalFunction f, double c) { return f *= c; }
RationalFunction operator*(double c, RationalFunction f) { return f *= c; }
RationalFunction operator/(RationalFunction f1, const RationalFunction& f2) {
return f1 /= f2;
}
RationalFunction operator/(RationalFunction f, const Polynomial& p) {
return f /= p;
}
RationalFunction operator/(const Polynomial& p, const RationalFunction& f) {
return RationalFunction(p * f.denominator(), f.numerator());
}
RationalFunction operator/(RationalFunction f, double c) { return f /= c; }
RationalFunction operator/(double c, const RationalFunction& f) {
return RationalFunction(c * f.denominator(), f.numerator());
}
RationalFunction pow(const RationalFunction& f, int n) {
if (n == 0) {
return RationalFunction(Polynomial(1), Polynomial(1));
} else if (n >= 1) {
return RationalFunction(pow(f.numerator(), n), pow(f.denominator(), n));
} else {
// n < 0
return RationalFunction(pow(f.denominator(), -n), pow(f.numerator(), -n));
}
}
} // namespace symbolic
} // namespace drake