forked from sammy-tri/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic_expression.cc
1150 lines (1038 loc) · 34.4 KB
/
symbolic_expression.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
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
// NOLINTNEXTLINE(build/include): Its header file is included in symbolic.h.
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <ios>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
#include <Eigen/Core>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include "drake/common/drake_assert.h"
#include "drake/common/never_destroyed.h"
#include "drake/common/symbolic.h"
#define DRAKE_COMMON_SYMBOLIC_DETAIL_HEADER
#include "drake/common/symbolic_expression_cell.h"
#undef DRAKE_COMMON_SYMBOLIC_DETAIL_HEADER
namespace drake {
namespace symbolic {
using std::logic_error;
using std::make_shared;
using std::map;
using std::numeric_limits;
using std::ostream;
using std::ostringstream;
using std::pair;
using std::runtime_error;
using std::shared_ptr;
using std::streamsize;
using std::string;
using std::vector;
bool operator<(ExpressionKind k1, ExpressionKind k2) {
return static_cast<int>(k1) < static_cast<int>(k2);
}
namespace {
// Negates an addition expression.
// - (E_1 + ... + E_n) => (-E_1 + ... + -E_n)
Expression NegateAddition(const Expression& e) {
DRAKE_ASSERT(is_addition(e));
return ExpressionAddFactory{to_addition(e)}.Negate().GetExpression();
}
// Negates a multiplication expression.
// - (c0 * E_1 * ... * E_n) => (-c0 * E_1 * ... * E_n)
Expression NegateMultiplication(const Expression& e) {
DRAKE_ASSERT(is_multiplication(e));
return ExpressionMulFactory{to_multiplication(e)}.Negate().GetExpression();
}
} // namespace
shared_ptr<ExpressionCell> Expression::make_cell(const double d) {
if (d == 0.0) {
// The objects created by `Expression(0.0)` share the unique
// `ExpressionConstant` object created in `Expression::Zero()`.
//
// See https://github.com/RobotLocomotion/drake/issues/12453 for details.
//
// Typically we should never access a ptr_ directly, but here we know it
// poses no risk because ExpressionConstant has no non-const member fields
// of any consequence (only "is_expanded_" is non-const, and it can never
// be reset to false).
return Expression::Zero().ptr_;
}
if (std::isnan(d)) {
return make_shared<ExpressionNaN>();
}
return make_shared<ExpressionConstant>(d);
}
Expression::Expression(const Variable& var)
: Expression{make_shared<ExpressionVar>(var)} {}
Expression::Expression(const double constant)
: Expression{make_cell(constant)} {}
Expression::Expression(std::shared_ptr<ExpressionCell> ptr)
: ptr_{std::move(ptr)} {
DRAKE_ASSERT(ptr_ != nullptr);
}
ExpressionKind Expression::get_kind() const {
return cell().get_kind();
}
void Expression::HashAppend(DelegatingHasher* hasher) const {
using drake::hash_append;
hash_append(*hasher, get_kind());
cell().HashAppendDetail(hasher);
}
ExpressionCell& Expression::mutable_cell() {
DRAKE_ASSERT(ptr_ != nullptr);
DRAKE_DEMAND(ptr_.use_count() == 1);
return *ptr_;
}
Expression Expression::Zero() {
static const never_destroyed<Expression> zero{
Expression{make_shared<ExpressionConstant>(0.0)}};
return zero.access();
}
Expression Expression::One() {
static const never_destroyed<Expression> one{
Expression{make_shared<ExpressionConstant>(1.0)}};
return one.access();
}
Expression Expression::Pi() {
static const never_destroyed<Expression> pi{
Expression{make_shared<ExpressionConstant>(M_PI)}};
return pi.access();
}
Expression Expression::E() {
static const never_destroyed<Expression> e{
Expression{make_shared<ExpressionConstant>(M_E)}};
return e.access();
}
Expression Expression::NaN() {
static const never_destroyed<Expression> nan{
Expression{make_shared<ExpressionNaN>()}};
return nan.access();
}
Variables Expression::GetVariables() const {
return cell().GetVariables();
}
bool Expression::EqualTo(const Expression& e) const {
const auto& this_cell = cell();
const auto& other_cell = e.cell();
if (&this_cell == &other_cell) {
return true;
}
if (get_kind() != e.get_kind()) {
return false;
}
// Check structural equality.
return cell().EqualTo(e.cell());
}
bool Expression::Less(const Expression& e) const {
const auto& this_cell = cell();
const auto& other_cell = e.cell();
if (&this_cell == &other_cell) {
return false; // this equals to e, not less-than.
}
const ExpressionKind k1{get_kind()};
const ExpressionKind k2{e.get_kind()};
if (k1 < k2) {
return true;
}
if (k2 < k1) {
return false;
}
// k1 == k2
return this_cell.Less(other_cell);
}
bool Expression::is_polynomial() const {
return cell().is_polynomial();
}
bool Expression::is_expanded() const {
return cell().is_expanded();
}
double Expression::Evaluate(const Environment& env,
RandomGenerator* const random_generator) const {
if (random_generator == nullptr) {
return cell().Evaluate(env);
} else {
return cell().Evaluate(
PopulateRandomVariables(env, GetVariables(), random_generator));
}
}
double Expression::Evaluate(RandomGenerator* const random_generator) const {
return Evaluate(Environment{}, random_generator);
}
Eigen::SparseMatrix<double> Evaluate(
const Eigen::Ref<const Eigen::SparseMatrix<Expression>>& m,
const Environment& env) {
return m.unaryExpr([&env](const Expression& e) { return e.Evaluate(env); });
}
Expression Expression::EvaluatePartial(const Environment& env) const {
if (env.empty()) {
return *this;
}
Substitution subst;
for (const pair<const Variable, double>& p : env) {
subst.emplace(p.first, p.second);
}
return Substitute(subst);
}
Expression Expression::Expand() const {
if (cell().is_expanded()) {
// If it is already expanded, return the current expression without calling
// Expand() on the cell.
return *this;
}
Expression result = cell().Expand();
if (!result.is_expanded()) {
result.mutable_cell().set_expanded();
}
return result;
}
Expression Expression::Substitute(const Variable& var,
const Expression& e) const {
return cell().Substitute({{var, e}});
}
Expression Expression::Substitute(const Substitution& s) const {
if (!s.empty()) {
return cell().Substitute(s);
}
return *this;
}
Expression Expression::Differentiate(const Variable& x) const {
return cell().Differentiate(x);
}
RowVectorX<Expression> Expression::Jacobian(
const Eigen::Ref<const VectorX<Variable>>& vars) const {
RowVectorX<Expression> J(vars.size());
for (VectorX<Variable>::Index i = 0; i < vars.size(); ++i) {
J(i) = Differentiate(vars(i));
}
return J;
}
string Expression::to_string() const {
ostringstream oss;
oss << *this;
return oss.str();
}
Expression operator+(Expression lhs, const Expression& rhs) {
lhs += rhs;
return lhs;
}
// NOLINTNEXTLINE(runtime/references) per C++ standard signature.
Expression& operator+=(Expression& lhs, const Expression& rhs) {
// Simplification: 0 + x => x
if (is_zero(lhs)) {
lhs = rhs;
return lhs;
}
// Simplification: x + 0 => x
if (is_zero(rhs)) {
return lhs;
}
// Simplification: Expression(c1) + Expression(c2) => Expression(c1 + c2)
if (is_constant(lhs) && is_constant(rhs)) {
lhs = get_constant_value(lhs) + get_constant_value(rhs);
return lhs;
}
// Simplification: flattening. To build a new expression, we use
// ExpressionAddFactory which holds intermediate terms and does
// simplifications internally.
ExpressionAddFactory add_factory{};
if (is_addition(lhs)) {
// 1. (e_1 + ... + e_n) + rhs
add_factory = to_addition(lhs);
// Note: AddExpression method takes care of the special case where `rhs` is
// of ExpressionAdd.
add_factory.AddExpression(rhs);
} else {
if (is_addition(rhs)) {
// 2. lhs + (e_1 + ... + e_n)
add_factory = to_addition(rhs);
add_factory.AddExpression(lhs);
} else {
// nothing to flatten: return lhs + rhs
add_factory.AddExpression(lhs);
add_factory.AddExpression(rhs);
}
}
// Extract an expression from factory
lhs = add_factory.GetExpression();
return lhs;
}
Expression& Expression::operator++() {
*this += Expression::One();
return *this;
}
Expression Expression::operator++(int) {
Expression copy(*this);
++*this;
return copy;
}
Expression operator+(const Expression& e) { return e; }
Expression operator-(Expression lhs, const Expression& rhs) {
lhs -= rhs;
return lhs;
}
// NOLINTNEXTLINE(runtime/references) per C++ standard signature.
Expression& operator-=(Expression& lhs, const Expression& rhs) {
// Simplification: E - E => 0
// TODO(soonho-tri): This simplification is not sound since it cancels `E`
// which might cause 0/0 during evaluation.
if (lhs.EqualTo(rhs)) {
lhs = Expression::Zero();
return lhs;
}
// Simplification: x - 0 => x
if (is_zero(rhs)) {
return lhs;
}
// Simplification: Expression(c1) - Expression(c2) => Expression(c1 - c2)
if (is_constant(lhs) && is_constant(rhs)) {
lhs = get_constant_value(lhs) - get_constant_value(rhs);
return lhs;
}
// x - y => x + (-y)
lhs += -rhs;
return lhs;
}
Expression operator-(const Expression& e) {
// Simplification: constant folding
if (is_constant(e)) {
return Expression{-get_constant_value(e)};
}
// Simplification: push '-' inside over '+'.
// -(E_1 + ... + E_n) => (-E_1 + ... + -E_n)
if (is_addition(e)) {
return NegateAddition(e);
}
// Simplification: push '-' inside over '*'.
// -(c0 * E_1 * ... * E_n) => (-c0 * E_1 * ... * E_n)
if (is_multiplication(e)) {
return NegateMultiplication(e);
}
return -1 * e;
}
Expression& Expression::operator--() {
*this -= Expression::One();
return *this;
}
Expression Expression::operator--(int) {
// Declare as non-const to allow move.
Expression copy(*this);
--*this;
return copy;
}
Expression operator*(Expression lhs, const Expression& rhs) {
lhs *= rhs;
return lhs;
}
// NOLINTNEXTLINE(runtime/references) per C++ standard signature.
Expression& operator*=(Expression& lhs, const Expression& rhs) {
// Simplification: 1 * x => x
if (is_one(lhs)) {
lhs = rhs;
return lhs;
}
// Simplification: x * 1 => x
if (is_one(rhs)) {
return lhs;
}
// Simplification: (E1 / E2) * (E3 / E4) => (E1 * E3) / (E2 * E4)
if (is_division(lhs) && is_division(rhs)) {
lhs = (get_first_argument(lhs) * get_first_argument(rhs)) /
(get_second_argument(lhs) * get_second_argument(rhs));
return lhs;
}
// Simplification: lhs * (c / E) => (c * lhs) / E
if (is_division(rhs) && is_constant(get_first_argument(rhs))) {
lhs = (get_first_argument(rhs) * lhs) / get_second_argument(rhs);
return lhs;
}
// Simplification: (c / E) * rhs => (c * rhs) / E
if (is_division(lhs) && is_constant(get_first_argument(lhs))) {
lhs = (get_first_argument(lhs) * rhs) / get_second_argument(lhs);
return lhs;
}
if (is_neg_one(lhs)) {
if (is_addition(rhs)) {
// Simplification: push '-' inside over '+'.
// -1 * (E_1 + ... + E_n) => (-E_1 + ... + -E_n)
lhs = NegateAddition(rhs);
return lhs;
}
if (is_multiplication(rhs)) {
// Simplification: push '-' inside over '*'.
// -1 * (c0 * E_1 * ... * E_n) => (-c0 * E_1 * ... * E_n)
lhs = NegateMultiplication(rhs);
return lhs;
}
}
if (is_neg_one(rhs)) {
if (is_addition(lhs)) {
// Simplification: push '-' inside over '+'.
// (E_1 + ... + E_n) * -1 => (-E_1 + ... + -E_n)
lhs = NegateAddition(lhs);
return lhs;
}
if (is_multiplication(lhs)) {
// Simplification: push '-' inside over '*'.
// (c0 * E_1 * ... * E_n) * -1 => (-c0 * E_1 * ... * E_n)
lhs = NegateMultiplication(lhs);
return lhs;
}
}
// Simplification: 0 * E => 0
// TODO(soonho-tri): This simplification is not sound since it cancels `E`
// which might cause 0/0 during evaluation.
if (is_zero(lhs)) {
return lhs;
}
// Simplification: E * 0 => 0
// TODO(soonho-tri): This simplification is not sound since it cancels `E`
// which might cause 0/0 during evaluation.
if (is_zero(rhs)) {
lhs = Expression::Zero();
return lhs;
}
// Pow-related simplifications.
if (is_pow(lhs)) {
const Expression& e1{get_first_argument(lhs)};
if (is_pow(rhs)) {
const Expression& e3{get_first_argument(rhs)};
if (e1.EqualTo(e3)) {
// Simplification: pow(e1, e2) * pow(e1, e4) => pow(e1, e2 + e4)
// TODO(soonho-tri): This simplification is not sound. For example, x^4
// * x^(-3) => x. The original expression `x^4 * x^(-3)` is evaluated to
// `nan` when x = 0 while the simplified expression `x` is evaluated to
// 0.
const Expression& e2{get_second_argument(lhs)};
const Expression& e4{get_second_argument(rhs)};
lhs = pow(e1, e2 + e4);
return lhs;
}
}
if (e1.EqualTo(rhs)) {
// Simplification: pow(e1, e2) * e1 => pow(e1, e2 + 1)
// TODO(soonho-tri): This simplification is not sound.
const Expression& e2{get_second_argument(lhs)};
lhs = pow(e1, e2 + 1);
return lhs;
}
} else {
if (is_pow(rhs)) {
const Expression& e1{get_first_argument(rhs)};
if (e1.EqualTo(lhs)) {
// Simplification: (lhs * rhs == e1 * pow(e1, e2)) => pow(e1, 1 + e2)
// TODO(soonho-tri): This simplification is not sound.
const Expression& e2{get_second_argument(rhs)};
lhs = pow(e1, 1 + e2);
return lhs;
}
}
}
if (is_constant(lhs) && is_constant(rhs)) {
// Simplification: Expression(c1) * Expression(c2) => Expression(c1 * c2)
lhs = Expression{get_constant_value(lhs) * get_constant_value(rhs)};
return lhs;
}
// Simplification: flattening
ExpressionMulFactory mul_factory{};
if (is_multiplication(lhs)) {
// (e_1 * ... * e_n) * rhs
mul_factory = to_multiplication(lhs);
// Note: AddExpression method takes care of the special case where `rhs` is
// of ExpressionMul.
mul_factory.AddExpression(rhs);
} else {
if (is_multiplication(rhs)) {
// e_1 * (e_2 * ... * e_n) -> (e_2 * ... * e_n * e_1)
//
// Note that we do not preserve the original ordering because * is
// associative.
mul_factory = to_multiplication(rhs);
mul_factory.AddExpression(lhs);
} else {
// Simplification: x * x => x^2 (=pow(x,2))
if (lhs.EqualTo(rhs)) {
lhs = pow(lhs, 2.0);
return lhs;
}
// nothing to flatten
mul_factory.AddExpression(lhs);
mul_factory.AddExpression(rhs);
}
}
lhs = mul_factory.GetExpression();
return lhs;
}
Expression operator/(Expression lhs, const Expression& rhs) {
lhs /= rhs;
return lhs;
}
// NOLINTNEXTLINE(runtime/references) per C++ standard signature.
Expression& operator/=(Expression& lhs, const Expression& rhs) {
// Simplification: x / 1 => x
if (is_one(rhs)) {
return lhs;
}
// Simplification: Expression(c1) / Expression(c2) => Expression(c1 / c2)
if (is_constant(lhs) && is_constant(rhs)) {
const double v1{get_constant_value(lhs)};
const double v2{get_constant_value(rhs)};
if (v2 == 0.0) {
ostringstream oss{};
oss << "Division by zero: " << v1 << "/" << v2;
throw runtime_error(oss.str());
}
lhs = Expression{v1 / v2};
return lhs;
}
// Simplification: E / E => 1
// TODO(soonho-tri): This simplification is not sound since it cancels `E`
// which might contain 0/0 problems.
if (lhs.EqualTo(rhs)) {
lhs = Expression::One();
return lhs;
}
lhs = Expression{make_shared<ExpressionDiv>(lhs, rhs)};
return lhs;
}
namespace {
// Changes the precision of `os` to be the `new_precision` and saves the
// original precision so that it can be reverted when an instance of this class
// is destructed. It is used in `operator<<` of symbolic expression.
class PrecisionGuard {
public:
PrecisionGuard(ostream* const os, const streamsize& new_precision)
: os_{os}, original_precision_{os->precision()} {
os_->precision(new_precision);
}
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(PrecisionGuard)
~PrecisionGuard() { os_->precision(original_precision_); }
private:
ostream* const os_;
const streamsize original_precision_;
};
} // namespace
ostream& operator<<(ostream& os, const Expression& e) {
const PrecisionGuard precision_guard{&os,
numeric_limits<double>::max_digits10};
return e.cell().Display(os);
}
Expression log(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
const double v{get_constant_value(e)};
ExpressionLog::check_domain(v);
return Expression{std::log(v)};
}
return Expression{make_shared<ExpressionLog>(e)};
}
Expression abs(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::fabs(get_constant_value(e))};
}
return Expression{make_shared<ExpressionAbs>(e)};
}
Expression exp(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::exp(get_constant_value(e))};
}
return Expression{make_shared<ExpressionExp>(e)};
}
Expression sqrt(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
const double v{get_constant_value(e)};
ExpressionSqrt::check_domain(v);
return Expression{std::sqrt(v)};
}
// Simplification: sqrt(pow(x, 2)) => abs(x)
if (is_pow(e)) {
if (is_two(get_second_argument(e))) {
return abs(get_first_argument(e));
}
}
return Expression{make_shared<ExpressionSqrt>(e)};
}
Expression pow(const Expression& e1, const Expression& e2) {
// Simplification
if (is_constant(e2)) {
const double v2{get_constant_value(e2)};
if (is_constant(e1)) {
// Constant folding
const double v1{get_constant_value(e1)};
ExpressionPow::check_domain(v1, v2);
return Expression{std::pow(v1, v2)};
}
// pow(E, 0) => 1
// TODO(soonho-tri): This simplification is not sound since it cancels `E`
// which might contain 0/0 problems.
if (v2 == 0.0) {
return Expression::One();
}
// pow(E, 1) => E
if (v2 == 1.0) {
return e1;
}
}
if (is_pow(e1)) {
// pow(base, exponent) ^ e2 => pow(base, exponent * e2)
const Expression& base{get_first_argument(e1)};
const Expression& exponent{get_second_argument(e1)};
return Expression{make_shared<ExpressionPow>(base, exponent * e2)};
}
return Expression{make_shared<ExpressionPow>(e1, e2)};
}
Expression sin(const Expression& e) {
// simplification: constant folding.
if (is_constant(e)) {
return Expression{std::sin(get_constant_value(e))};
}
return Expression{make_shared<ExpressionSin>(e)};
}
Expression cos(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::cos(get_constant_value(e))};
}
return Expression{make_shared<ExpressionCos>(e)};
}
Expression tan(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::tan(get_constant_value(e))};
}
return Expression{make_shared<ExpressionTan>(e)};
}
Expression asin(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
const double v{get_constant_value(e)};
ExpressionAsin::check_domain(v);
return Expression{std::asin(v)};
}
return Expression{make_shared<ExpressionAsin>(e)};
}
Expression acos(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
const double v{get_constant_value(e)};
ExpressionAcos::check_domain(v);
return Expression{std::acos(v)};
}
return Expression{make_shared<ExpressionAcos>(e)};
}
Expression atan(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::atan(get_constant_value(e))};
}
return Expression{make_shared<ExpressionAtan>(e)};
}
Expression atan2(const Expression& e1, const Expression& e2) {
// Simplification: constant folding.
if (is_constant(e1) && is_constant(e2)) {
return Expression{
std::atan2(get_constant_value(e1), get_constant_value(e2))};
}
return Expression{make_shared<ExpressionAtan2>(e1, e2)};
}
Expression sinh(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::sinh(get_constant_value(e))};
}
return Expression{make_shared<ExpressionSinh>(e)};
}
Expression cosh(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::cosh(get_constant_value(e))};
}
return Expression{make_shared<ExpressionCosh>(e)};
}
Expression tanh(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::tanh(get_constant_value(e))};
}
return Expression{make_shared<ExpressionTanh>(e)};
}
Expression min(const Expression& e1, const Expression& e2) {
// simplification: min(x, x) => x
if (e1.EqualTo(e2)) {
return e1;
}
// Simplification: constant folding.
if (is_constant(e1) && is_constant(e2)) {
return Expression{std::min(get_constant_value(e1), get_constant_value(e2))};
}
return Expression{make_shared<ExpressionMin>(e1, e2)};
}
Expression max(const Expression& e1, const Expression& e2) {
// Simplification: max(x, x) => x
if (e1.EqualTo(e2)) {
return e1;
}
// Simplification: constant folding
if (is_constant(e1) && is_constant(e2)) {
return Expression{std::max(get_constant_value(e1), get_constant_value(e2))};
}
return Expression{make_shared<ExpressionMax>(e1, e2)};
}
Expression ceil(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::ceil(get_constant_value(e))};
}
return Expression{make_shared<ExpressionCeiling>(e)};
}
Expression floor(const Expression& e) {
// Simplification: constant folding.
if (is_constant(e)) {
return Expression{std::floor(get_constant_value(e))};
}
return Expression{make_shared<ExpressionFloor>(e)};
}
Expression if_then_else(const Formula& f_cond, const Expression& e_then,
const Expression& e_else) {
// simplification:: if(true, e1, e2) => e1
if (f_cond.EqualTo(Formula::True())) {
return e_then;
}
// simplification:: if(false, e1, e2) => e2
if (f_cond.EqualTo(Formula::False())) {
return e_else;
}
return Expression{make_shared<ExpressionIfThenElse>(f_cond, e_then, e_else)};
}
Expression uninterpreted_function(string name, vector<Expression> arguments) {
return Expression{make_shared<ExpressionUninterpretedFunction>(
std::move(name), std::move(arguments))};
}
bool is_constant(const Expression& e) { return is_constant(e.cell()); }
bool is_constant(const Expression& e, const double v) {
return is_constant(e) && (to_constant(e).get_value() == v);
}
bool is_zero(const Expression& e) { return is_constant(e, 0.0); }
bool is_one(const Expression& e) { return is_constant(e, 1.0); }
bool is_neg_one(const Expression& e) { return is_constant(e, -1.0); }
bool is_two(const Expression& e) { return is_constant(e, 2.0); }
bool is_nan(const Expression& e) { return e.get_kind() == ExpressionKind::NaN; }
bool is_variable(const Expression& e) { return is_variable(e.cell()); }
bool is_addition(const Expression& e) { return is_addition(e.cell()); }
bool is_multiplication(const Expression& e) {
return is_multiplication(e.cell());
}
bool is_division(const Expression& e) { return is_division(e.cell()); }
bool is_log(const Expression& e) { return is_log(e.cell()); }
bool is_abs(const Expression& e) { return is_abs(e.cell()); }
bool is_exp(const Expression& e) { return is_exp(e.cell()); }
bool is_sqrt(const Expression& e) { return is_sqrt(e.cell()); }
bool is_pow(const Expression& e) { return is_pow(e.cell()); }
bool is_sin(const Expression& e) { return is_sin(e.cell()); }
bool is_cos(const Expression& e) { return is_cos(e.cell()); }
bool is_tan(const Expression& e) { return is_tan(e.cell()); }
bool is_asin(const Expression& e) { return is_asin(e.cell()); }
bool is_acos(const Expression& e) { return is_acos(e.cell()); }
bool is_atan(const Expression& e) { return is_atan(e.cell()); }
bool is_atan2(const Expression& e) { return is_atan2(e.cell()); }
bool is_sinh(const Expression& e) { return is_sinh(e.cell()); }
bool is_cosh(const Expression& e) { return is_cosh(e.cell()); }
bool is_tanh(const Expression& e) { return is_tanh(e.cell()); }
bool is_min(const Expression& e) { return is_min(e.cell()); }
bool is_max(const Expression& e) { return is_max(e.cell()); }
bool is_ceil(const Expression& e) { return is_ceil(e.cell()); }
bool is_floor(const Expression& e) { return is_floor(e.cell()); }
bool is_if_then_else(const Expression& e) { return is_if_then_else(e.cell()); }
bool is_uninterpreted_function(const Expression& e) {
return is_uninterpreted_function(e.cell());
}
double get_constant_value(const Expression& e) {
return to_constant(e).get_value();
}
const Variable& get_variable(const Expression& e) {
return to_variable(e).get_variable();
}
const Expression& get_argument(const Expression& e) {
return to_unary(e).get_argument();
}
const Expression& get_first_argument(const Expression& e) {
return to_binary(e).get_first_argument();
}
const Expression& get_second_argument(const Expression& e) {
return to_binary(e).get_second_argument();
}
double get_constant_in_addition(const Expression& e) {
return to_addition(e).get_constant();
}
const map<Expression, double>& get_expr_to_coeff_map_in_addition(
const Expression& e) {
return to_addition(e).get_expr_to_coeff_map();
}
double get_constant_in_multiplication(const Expression& e) {
return to_multiplication(e).get_constant();
}
const map<Expression, Expression>& get_base_to_exponent_map_in_multiplication(
const Expression& e) {
return to_multiplication(e).get_base_to_exponent_map();
}
const string& get_uninterpreted_function_name(const Expression& e) {
return to_uninterpreted_function(e).get_name();
}
const vector<Expression>& get_uninterpreted_function_arguments(
const Expression& e) {
return to_uninterpreted_function(e).get_arguments();
}
const Formula& get_conditional_formula(const Expression& e) {
return to_if_then_else(e).get_conditional_formula();
}
const Expression& get_then_expression(const Expression& e) {
return to_if_then_else(e).get_then_expression();
}
const Expression& get_else_expression(const Expression& e) {
return to_if_then_else(e).get_else_expression();
}
Expression operator+(const Variable& var) { return Expression{var}; }
Expression operator-(const Variable& var) { return -Expression{var}; }
VectorX<Variable> GetVariableVector(
const Eigen::Ref<const VectorX<Expression>>& evec) {
VectorX<Variable> vec(evec.size());
for (int i = 0; i < evec.size(); i++) {
const Expression e_i{evec(i)};
if (is_variable(e_i)) {
vec(i) = get_variable(e_i);
} else {
throw logic_error(fmt::format("{} is not a variable.", e_i));
}
}
return vec;
}
MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
const vector<Variable>& vars) {
DRAKE_DEMAND(!vars.empty());
const Eigen::Ref<const VectorX<Expression>>::Index n{f.size()};
const size_t m{vars.size()};
MatrixX<Expression> J(n, m);
for (int i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
J(i, j) = f[i].Differentiate(vars[j]);
}
}
return J;
}
MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
const Eigen::Ref<const VectorX<Variable>>& vars) {
return Jacobian(f, vector<Variable>(vars.data(), vars.data() + vars.size()));
}
namespace {
// Helper class for IsAffine functions below where an instance of this class
// is passed to Eigen::MatrixBase::visit() function.
class IsAffineVisitor {
public:
IsAffineVisitor() = default;
explicit IsAffineVisitor(const Variables& variables)
: variables_{&variables} {}
// Called for the first coefficient. Needed for Eigen::MatrixBase::visit()
// function.
void init(const Expression& e, const Eigen::Index i, const Eigen::Index j) {
(*this)(e, i, j);
}
// Called for all other coefficients. Needed for Eigen::MatrixBase::visit()
// function.
void operator()(const Expression& e, const Eigen::Index, const Eigen::Index) {
// Note that `IsNotAffine` is only called when we have not found a
// non-affine element yet.
found_non_affine_element_ = found_non_affine_element_ || IsNotAffine(e);
}
[[nodiscard]] bool result() const { return !found_non_affine_element_; }
private:
// Returns true if `e` is *not* affine in variables_ (if exists) or all
// variables in `e`.
[[nodiscard]] bool IsNotAffine(const Expression& e) const {
// TODO(#16393) This check is incorrect when variables_ is non-null.
if (!e.is_polynomial()) {
return true;
}
const Polynomial p{(variables_ != nullptr) ? Polynomial{e, *variables_}
: Polynomial{e}};
return p.TotalDegree() > 1;
}
bool found_non_affine_element_{false};
const Variables* const variables_{nullptr};
};
} // namespace
bool IsAffine(const Eigen::Ref<const MatrixX<Expression>>& m,
const Variables& vars) {
if (m.size() == 0) {
return true;
}
IsAffineVisitor visitor{vars};
m.visit(visitor);
return visitor.result();
}
bool IsAffine(const Eigen::Ref<const MatrixX<Expression>>& m) {
if (m.size() == 0) {
return true;
}
IsAffineVisitor visitor;
m.visit(visitor);
return visitor.result();
}
namespace {
// Helper functions for TaylorExpand.
//
// We use the multi-index notation. Please read
// https://en.wikipedia.org/wiki/Multi-index_notation for more information.
// α = (a₁, ..., aₙ) where αᵢ ∈ Z.
using MultiIndex = vector<int>;
// Generates multi-indices of order `order` whose size is `num_vars` and append
// to `vec`. It generates the indices by increasing the elements of the given
// `base`. It only changes the i-th dimension which is greater than or equal to
// `start_dim` to avoid duplicates.
void DoEnumerateMultiIndex(const int order, const int num_vars,
const int start_dim, const MultiIndex& base,
vector<MultiIndex>* const vec) {