forked from sammy-tri/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolic_expression_test.cc
2668 lines (2327 loc) · 101 KB
/
symbolic_expression_test.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
#include <algorithm>
#include <cmath>
#include <functional>
#include <map>
#include <memory>
#include <random>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
#include "drake/common/hash.h"
#include "drake/common/polynomial.h"
#include "drake/common/symbolic.h"
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/common/test_utilities/is_memcpy_movable.h"
#include "drake/common/test_utilities/symbolic_test_util.h"
using std::count_if;
using std::domain_error;
using std::equal_to;
using std::map;
using std::ostringstream;
using std::pair;
using std::runtime_error;
using std::set;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::vector;
namespace drake {
using test::IsMemcpyMovable;
namespace symbolic {
namespace {
using test::ExprEqual;
using test::ExprLess;
using test::ExprNotEqual;
using test::ExprNotLess;
using test::FormulaEqual;
template <typename T>
size_t get_std_hash(const T& item) {
return std::hash<T>{}(item);
}
// Checks if a given 'expressions' is ordered by Expression::Less.
void CheckOrdering(const vector<Expression>& expressions) {
for (size_t i{0}; i < expressions.size(); ++i) {
for (size_t j{0}; j < expressions.size(); ++j) {
if (i < j) {
EXPECT_PRED2(ExprLess, expressions[i], expressions[j])
<< "(Expressions[" << i << "] = " << expressions[i] << ")"
<< " is not less than "
<< "(Expressions[" << j << "] = " << expressions[j] << ")";
EXPECT_PRED2(ExprNotLess, expressions[j], expressions[i])
<< "(Expressions[" << j << "] = " << expressions[j] << ")"
<< " is less than "
<< "(Expressions[" << i << "] = " << expressions[i] << ")";
} else if (i > j) {
EXPECT_PRED2(ExprLess, expressions[j], expressions[i])
<< "(Expressions[" << j << "] = " << expressions[j] << ")"
<< " is not less than "
<< "(Expressions[" << i << "] = " << expressions[i] << ")";
EXPECT_PRED2(ExprNotLess, expressions[i], expressions[j])
<< "(Expressions[" << i << "] = " << expressions[i] << ")"
<< " is less than "
<< "(Expressions[" << j << "] = " << expressions[j] << ")";
} else {
EXPECT_PRED2(ExprNotLess, expressions[i], expressions[j])
<< "(Expressions[" << i << "] = " << expressions[i] << ")"
<< " is less than "
<< "(Expressions[" << j << "] = " << expressions[j] << ")";
EXPECT_PRED2(ExprNotLess, expressions[j], expressions[i])
<< "(Expressions[" << j << "] = " << expressions[j] << ")"
<< " is less than "
<< "(Expressions[" << i << "] = " << expressions[i] << ")";
}
}
}
}
// Provides common variables that are used by the following tests.
class SymbolicExpressionTest : public ::testing::Test {
protected:
const Variable var_a_{"a"};
const Variable var_x_{"x"};
const Variable var_y_{"y"};
const Variable var_z_{"z"};
const Expression a_{var_a_};
const Expression x_{var_x_};
const Expression y_{var_y_};
const Expression z_{var_z_};
const Expression x_plus_y_{x_ + y_};
const Expression x_plus_z_{x_ + z_};
const Expression zero_{0.0};
const Expression one_{1.0};
const Expression two_{2.0};
const Expression neg_one_{-1.0};
const Expression pi_{M_PI};
const Expression neg_pi_{-M_PI};
const Expression e_{M_E};
const Expression c1_{-10.0};
const Expression c2_{1.0};
const Expression c3_{3.14159};
const Expression c4_{-2.718};
const Expression e_constant_{1.0};
const Expression e_var_{var_x_};
const Expression e_add_{x_ + y_};
const Expression e_neg_{-x_}; // -1 * x_
const Expression e_mul_{x_ * y_};
const Expression e_div_{x_ / y_};
const Expression e_log_{log(x_)};
const Expression e_abs_{abs(x_)};
const Expression e_exp_{exp(x_)};
const Expression e_sqrt_{sqrt(x_)};
const Expression e_pow_{pow(x_, y_)};
const Expression e_sin_{sin(x_)};
const Expression e_cos_{cos(x_)};
const Expression e_tan_{tan(x_)};
const Expression e_asin_{asin(x_)};
const Expression e_acos_{acos(x_)};
const Expression e_atan_{atan(x_)};
const Expression e_atan2_{atan2(x_, y_)};
const Expression e_sinh_{sinh(x_)};
const Expression e_cosh_{cosh(x_)};
const Expression e_tanh_{tanh(x_)};
const Expression e_min_{min(x_, y_)};
const Expression e_max_{max(x_, y_)};
const Expression e_ceil_{ceil(x_)};
const Expression e_floor_{floor(x_)};
const Expression e_ite_{if_then_else(x_ < y_, x_, y_)};
const Expression e_nan_{Expression::NaN()};
const Expression e_uf_{uninterpreted_function("uf", {var_x_, var_y_})};
const vector<Expression> collection_{
e_constant_, e_var_, e_add_, e_neg_, e_mul_, e_div_, e_log_,
e_abs_, e_exp_, e_sqrt_, e_pow_, e_sin_, e_cos_, e_tan_,
e_asin_, e_acos_, e_atan_, e_atan2_, e_sinh_, e_cosh_, e_tanh_,
e_min_, e_max_, e_ceil_, e_floor_, e_ite_, e_nan_, e_uf_};
};
TEST_F(SymbolicExpressionTest, Dummy) {
EXPECT_TRUE(is_nan(dummy_value<Expression>::get()));
}
TEST_F(SymbolicExpressionTest, IsConstant1) {
EXPECT_TRUE(is_constant(e_constant_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_constant(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsConstant2) {
EXPECT_TRUE(is_constant(Expression{}, 0.0));
EXPECT_TRUE(is_constant(Expression::Zero(), 0.0));
EXPECT_TRUE(is_constant(Expression::One(), 1.0));
EXPECT_TRUE(is_constant(Expression::Pi(), M_PI));
EXPECT_TRUE(is_constant(Expression::E(), M_E));
}
TEST_F(SymbolicExpressionTest, IsZero) {
EXPECT_TRUE(is_zero(Expression{}));
EXPECT_TRUE(is_zero(Expression::Zero()));
}
TEST_F(SymbolicExpressionTest, IsOne) {
EXPECT_TRUE(is_one(Expression::One()));
}
TEST_F(SymbolicExpressionTest, IsNegOne) { EXPECT_TRUE(is_neg_one(neg_one_)); }
TEST_F(SymbolicExpressionTest, IsTwo) { EXPECT_TRUE(is_two(two_)); }
TEST_F(SymbolicExpressionTest, NaN) {
// It's OK to have NaN expression.
const Expression nan{NAN};
EXPECT_TRUE(is_nan(nan));
EXPECT_TRUE(nan.EqualTo(Expression::NaN()));
// It's OK to have an expression including NaN inside.
const Expression e1{1.0 + nan};
// It's OK to display an expression including NaN inside.
EXPECT_EQ(e1.to_string(), "(1 + NaN)");
// It throws when we evaluate an expression including NaN.
EXPECT_THROW(e1.Evaluate(), runtime_error);
}
TEST_F(SymbolicExpressionTest, IsVariable) {
EXPECT_TRUE(is_variable(e_var_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_variable(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsAddition) {
EXPECT_TRUE(is_addition(e_add_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_addition(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsMultiplication) {
EXPECT_TRUE(is_multiplication(e_mul_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_multiplication(e); })};
EXPECT_EQ(cnt, 2);
}
TEST_F(SymbolicExpressionTest, IsDivision) {
EXPECT_TRUE(is_division(e_div_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_division(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsLog) {
EXPECT_TRUE(is_log(e_log_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_log(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsAbs) {
EXPECT_TRUE(is_abs(e_abs_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_abs(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsExp) {
EXPECT_TRUE(is_exp(e_exp_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_exp(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsSqrt) {
EXPECT_TRUE(is_sqrt(e_sqrt_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_sqrt(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsPow) {
EXPECT_TRUE(is_pow(e_pow_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_pow(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsSin) {
EXPECT_TRUE(is_sin(e_sin_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_sin(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsCos) {
EXPECT_TRUE(is_cos(e_cos_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_cos(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsTan) {
EXPECT_TRUE(is_tan(e_tan_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_tan(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsAsin) {
EXPECT_TRUE(is_asin(e_asin_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_asin(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsAcos) {
EXPECT_TRUE(is_acos(e_acos_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_acos(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsAtan) {
EXPECT_TRUE(is_atan(e_atan_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_atan(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsAtan2) {
EXPECT_TRUE(is_atan2(e_atan2_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_atan2(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsSinh) {
EXPECT_TRUE(is_sinh(e_sinh_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_sinh(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsCosh) {
EXPECT_TRUE(is_cosh(e_cosh_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_cosh(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsTanh) {
EXPECT_TRUE(is_tanh(e_tanh_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_tanh(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsMin) {
EXPECT_TRUE(is_min(e_min_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_min(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsMax) {
EXPECT_TRUE(is_max(e_max_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_max(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsCeil) {
EXPECT_TRUE(is_ceil(e_ceil_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_ceil(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsFloor) {
EXPECT_TRUE(is_floor(e_floor_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_floor(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsIfThenElse) {
EXPECT_TRUE(is_if_then_else(e_ite_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_if_then_else(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsNaN) {
EXPECT_TRUE(is_nan(e_nan_));
const vector<Expression>::difference_type cnt{
count_if(collection_.begin(), collection_.end(),
[](const Expression& e) { return is_nan(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, IsUninterpretedFunction) {
EXPECT_TRUE(is_uninterpreted_function(e_uf_));
const vector<Expression>::difference_type cnt{count_if(
collection_.begin(), collection_.end(),
[](const Expression& e) { return is_uninterpreted_function(e); })};
EXPECT_EQ(cnt, 1);
}
TEST_F(SymbolicExpressionTest, GetConstantValue) {
EXPECT_EQ(get_constant_value(c1_), -10.0);
EXPECT_EQ(get_constant_value(c2_), 1.0);
EXPECT_EQ(get_constant_value(c3_), 3.14159);
EXPECT_EQ(get_constant_value(c4_), -2.718);
}
TEST_F(SymbolicExpressionTest, GetVariable) {
EXPECT_EQ(get_variable(x_), var_x_);
EXPECT_EQ(get_variable(y_), var_y_);
EXPECT_EQ(get_variable(z_), var_z_);
}
TEST_F(SymbolicExpressionTest, GetArgument) {
EXPECT_PRED2(ExprEqual, get_argument(e_log_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_abs_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_exp_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_sqrt_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_sin_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_cos_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_tan_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_asin_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_acos_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_atan_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_sinh_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_cosh_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_tanh_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_ceil_), x_);
EXPECT_PRED2(ExprEqual, get_argument(e_floor_), x_);
}
TEST_F(SymbolicExpressionTest, GetFirstArgument) {
EXPECT_PRED2(ExprEqual, get_first_argument(e_div_), x_);
EXPECT_PRED2(ExprEqual, get_first_argument(e_pow_), x_);
EXPECT_PRED2(ExprEqual, get_first_argument(e_atan2_), x_);
EXPECT_PRED2(ExprEqual, get_first_argument(e_min_), x_);
EXPECT_PRED2(ExprEqual, get_first_argument(e_max_), x_);
}
TEST_F(SymbolicExpressionTest, GetSecondArgument) {
EXPECT_PRED2(ExprEqual, get_second_argument(e_div_), y_);
EXPECT_PRED2(ExprEqual, get_second_argument(e_pow_), y_);
EXPECT_PRED2(ExprEqual, get_second_argument(e_atan2_), y_);
EXPECT_PRED2(ExprEqual, get_second_argument(e_min_), y_);
EXPECT_PRED2(ExprEqual, get_second_argument(e_max_), y_);
}
TEST_F(SymbolicExpressionTest, GetConstantTermInAddition) {
EXPECT_PRED2(ExprEqual, get_constant_in_addition(2 * x_ + 3 * y_), 0.0);
EXPECT_PRED2(ExprEqual, get_constant_in_addition(3 + 2 * x_ + 3 * y_), 3);
EXPECT_PRED2(ExprEqual, get_constant_in_addition(-2 + 2 * x_ + 3 * y_), -2);
}
TEST_F(SymbolicExpressionTest, GetTermsInAddition) {
const Expression e{3 + 2 * x_ + 3 * y_};
const map<Expression, double> terms{get_expr_to_coeff_map_in_addition(e)};
EXPECT_EQ(terms.at(x_), 2.0);
EXPECT_EQ(terms.at(y_), 3.0);
}
TEST_F(SymbolicExpressionTest, GetConstantFactorInMultiplication) {
EXPECT_PRED2(ExprEqual, get_constant_in_multiplication(e_neg_), -1.0);
EXPECT_PRED2(ExprEqual, get_constant_in_multiplication(x_ * y_ * y_), 1.0);
EXPECT_PRED2(ExprEqual, get_constant_in_multiplication(2 * x_ * y_ * y_),
2.0);
EXPECT_PRED2(ExprEqual, get_constant_in_multiplication(-3 * x_ * y_ * y_),
-3.0);
}
TEST_F(SymbolicExpressionTest, GetProductsInMultiplication) {
const Expression e{2 * x_ * y_ * y_ * pow(z_, y_)};
const map<Expression, Expression> products{
get_base_to_exponent_map_in_multiplication(e)};
EXPECT_PRED2(ExprEqual, products.at(x_), 1.0);
EXPECT_PRED2(ExprEqual, products.at(y_), 2.0);
EXPECT_PRED2(ExprEqual, products.at(z_), y_);
}
TEST_F(SymbolicExpressionTest, GetIfThenElse) {
const Formula conditional{x_ > y_};
const Expression e1{x_ + y_};
const Expression e2{x_ - y_};
const Expression e{if_then_else(conditional, e1, e2)};
EXPECT_PRED2(FormulaEqual, get_conditional_formula(e), conditional);
EXPECT_PRED2(ExprEqual, get_then_expression(e), e1);
EXPECT_PRED2(ExprEqual, get_else_expression(e), e2);
}
TEST_F(SymbolicExpressionTest, IsPolynomial) {
const vector<pair<Expression, bool>> test_vec{
{e_constant_, true}, {e_var_, true}, {e_neg_, true},
{e_add_, true}, {e_mul_, true}, {e_div_, false},
{e_log_, false}, {e_abs_, false}, {e_exp_, false},
{e_sqrt_, false}, {e_pow_, false}, {e_sin_, false},
{e_cos_, false}, {e_tan_, false}, {e_asin_, false},
{e_acos_, false}, {e_atan_, false}, {e_atan2_, false},
{e_sinh_, false}, {e_cosh_, false}, {e_tanh_, false},
{e_min_, false}, {e_max_, false}, {e_ceil_, false},
{e_floor_, false}, {e_ite_, false}, {e_nan_, false},
{e_uf_, false}};
for (const pair<Expression, bool>& p : test_vec) {
EXPECT_EQ(p.first.is_polynomial(), p.second);
}
// x^2 -> polynomial
EXPECT_TRUE(pow(x_, 2).is_polynomial());
// 3 + x + y + z -> polynomial
EXPECT_TRUE((3 + x_ + y_ + z_).is_polynomial());
// 1 + x^2 + y^2 -> polynomial
EXPECT_TRUE((1 + pow(x_, 2) + pow(y_, 2)).is_polynomial());
// x^2 * y^2 -> polynomial
EXPECT_TRUE((pow(x_, 2) * pow(y_, 2)).is_polynomial());
// (x + y + z)^3 -> polynomial
EXPECT_TRUE(pow(x_ + y_ + z_, 3).is_polynomial());
// (x + y + z)^3 / 10 -> polynomial
EXPECT_TRUE((pow(x_ + y_ + z_, 3) / 10).is_polynomial());
// (x^3)^(1/3) -> x -> polynomial
EXPECT_TRUE(pow(pow(x_, 3), 1 / 3).is_polynomial());
// x^-1 -> not polynomial
EXPECT_FALSE(pow(x_, -1).is_polynomial());
// x^2.1 -> not polynomial
EXPECT_FALSE(pow(x_, 2.1).is_polynomial());
// x^y -> not polynomial
EXPECT_FALSE(pow(x_, y_).is_polynomial());
// 3 + x^y -> not polynomial
EXPECT_FALSE((3 + pow(x_, y_)).is_polynomial());
// 3 + x^2.1 -> not polynomial
EXPECT_FALSE((3 + pow(x_, 2.1)).is_polynomial());
// x^y / 10 -> not polynomial
EXPECT_FALSE((pow(x_, y_) / 10).is_polynomial());
// x^2 * y^ 2.1 -> not polynomial
EXPECT_FALSE((pow(x_, 2) * pow(y_, 2.1)).is_polynomial());
// x^2 * y^ -1 -> not polynomial
EXPECT_FALSE((pow(x_, 2) * pow(y_, -1)).is_polynomial());
// x^2 * y^ 2 * x^y / 10 -> not polynomial
EXPECT_FALSE((pow(x_, 2) * pow(y_, 2) * pow(x_, y_) / 10).is_polynomial());
// (x + y + z)^3 / x -> not polynomial
EXPECT_FALSE((pow(x_ + y_ + z_, 3) / x_).is_polynomial());
// sqrt(x^2) -> |x| -> not polynomial
EXPECT_FALSE(sqrt(pow(x_, 2)).is_polynomial());
}
TEST_F(SymbolicExpressionTest, LessKind) {
CheckOrdering({e_constant_, e_var_, e_add_, e_neg_, e_mul_, e_div_,
e_log_, e_abs_, e_exp_, e_sqrt_, e_pow_, e_sin_,
e_cos_, e_tan_, e_asin_, e_acos_, e_atan_, e_atan2_,
e_sinh_, e_cosh_, e_tanh_, e_min_, e_max_, e_ceil_,
e_floor_, e_ite_, e_nan_, e_uf_});
}
TEST_F(SymbolicExpressionTest, LessConstant) { CheckOrdering({c1_, c2_, c3_}); }
TEST_F(SymbolicExpressionTest, LessVariable) { CheckOrdering({x_, y_, z_}); }
TEST_F(SymbolicExpressionTest, LessNeg) {
// Defined in the ascending order.
const Expression neg1{-c3_};
const Expression neg2{-c1_};
const Expression neg3{-x_}; // note: Constant kind < Variable kind
CheckOrdering({neg1, neg2, neg3});
}
TEST_F(SymbolicExpressionTest, LessAdd) {
const Expression add1{c1_ + x_ + y_};
const Expression add2{c1_ + 2 * x_ + y_};
const Expression add3{c1_ - 2 * y_ + z_};
const Expression add4{c1_ + y_ + z_};
const Expression add5{c1_ + 5 * y_ + z_};
const Expression add6{c3_ - 2 * x_ + y_};
const Expression add7{c3_ + x_ + y_};
const Expression add8{c3_ + y_ + 2 * z_};
const Expression add9{c3_ + y_ + 3 * z_};
CheckOrdering({add1, add2, add3, add4, add5, add6, add7, add8, add9});
}
TEST_F(SymbolicExpressionTest, LessSub) {
const Expression sub1{c1_ - x_ - y_};
const Expression sub2{c1_ - y_ - z_};
const Expression sub3{c3_ - x_ - y_};
const Expression sub4{c3_ - y_ - z_};
CheckOrdering({sub1, sub2, sub3, sub4});
}
TEST_F(SymbolicExpressionTest, LessMul) {
const Expression mul1{c1_ * x_ * y_};
const Expression mul2{c1_ * y_ * z_};
const Expression mul3{c3_ * x_ * y_};
const Expression mul4{c3_ * y_ * z_};
CheckOrdering({mul1, mul2, mul3, mul4});
}
TEST_F(SymbolicExpressionTest, LessDiv) {
const Expression div1{x_ / y_};
const Expression div2{x_ / z_};
const Expression div3{y_ / z_};
CheckOrdering({div1, div2, div3});
}
TEST_F(SymbolicExpressionTest, LessLog) {
const Expression log1{log(x_)};
const Expression log2{log(y_)};
const Expression log3{log(x_plus_y_)};
const Expression log4{log(x_plus_z_)};
CheckOrdering({log1, log2, log3, log4});
}
TEST_F(SymbolicExpressionTest, LessAbs) {
const Expression abs1{abs(x_)};
const Expression abs2{abs(y_)};
const Expression abs3{abs(x_plus_y_)};
const Expression abs4{abs(x_plus_z_)};
CheckOrdering({abs1, abs2, abs3, abs4});
}
TEST_F(SymbolicExpressionTest, LessExp) {
const Expression exp1{exp(x_)};
const Expression exp2{exp(y_)};
const Expression exp3{exp(x_plus_y_)};
const Expression exp4{exp(x_plus_z_)};
CheckOrdering({exp1, exp2, exp3, exp4});
}
TEST_F(SymbolicExpressionTest, LessSqrt) {
const Expression sqrt1{sqrt(x_)};
const Expression sqrt2{sqrt(y_)};
const Expression sqrt3{sqrt(x_plus_y_)};
const Expression sqrt4{sqrt(x_plus_z_)};
CheckOrdering({sqrt1, sqrt2, sqrt3, sqrt4});
}
TEST_F(SymbolicExpressionTest, LessSin) {
const Expression sin1{sin(x_)};
const Expression sin2{sin(y_)};
const Expression sin3{sin(x_plus_y_)};
const Expression sin4{sin(x_plus_z_)};
CheckOrdering({sin1, sin2, sin3, sin4});
}
TEST_F(SymbolicExpressionTest, LessCos) {
const Expression cos1{cos(x_)};
const Expression cos2{cos(y_)};
const Expression cos3{cos(x_plus_y_)};
const Expression cos4{cos(x_plus_z_)};
CheckOrdering({cos1, cos2, cos3, cos4});
}
TEST_F(SymbolicExpressionTest, LessTan) {
const Expression tan1{tan(x_)};
const Expression tan2{tan(y_)};
const Expression tan3{tan(x_plus_y_)};
const Expression tan4{tan(x_plus_z_)};
CheckOrdering({tan1, tan2, tan3, tan4});
}
TEST_F(SymbolicExpressionTest, LessAsin) {
const Expression asin1{asin(x_)};
const Expression asin2{asin(y_)};
const Expression asin3{asin(x_plus_y_)};
const Expression asin4{asin(x_plus_z_)};
CheckOrdering({asin1, asin2, asin3, asin4});
}
TEST_F(SymbolicExpressionTest, LessAcos) {
const Expression acos1{acos(x_)};
const Expression acos2{acos(y_)};
const Expression acos3{acos(x_plus_y_)};
const Expression acos4{acos(x_plus_z_)};
CheckOrdering({acos1, acos2, acos3, acos4});
}
TEST_F(SymbolicExpressionTest, LessAtan) {
const Expression atan1{atan(x_)};
const Expression atan2{atan(y_)};
const Expression atan3{atan(x_plus_y_)};
const Expression atan4{atan(x_plus_z_)};
CheckOrdering({atan1, atan2, atan3, atan4});
}
TEST_F(SymbolicExpressionTest, LessSinh) {
const Expression sinh1{sinh(x_)};
const Expression sinh2{sinh(y_)};
const Expression sinh3{sinh(x_plus_y_)};
const Expression sinh4{sinh(x_plus_z_)};
CheckOrdering({sinh1, sinh2, sinh3, sinh4});
}
TEST_F(SymbolicExpressionTest, LessCosh) {
const Expression cosh1{cosh(x_)};
const Expression cosh2{cosh(y_)};
const Expression cosh3{cosh(x_plus_y_)};
const Expression cosh4{cosh(x_plus_z_)};
CheckOrdering({cosh1, cosh2, cosh3, cosh4});
}
TEST_F(SymbolicExpressionTest, LessTanh) {
const Expression tanh1{tanh(x_)};
const Expression tanh2{tanh(y_)};
const Expression tanh3{tanh(x_plus_y_)};
const Expression tanh4{tanh(x_plus_z_)};
CheckOrdering({tanh1, tanh2, tanh3, tanh4});
}
TEST_F(SymbolicExpressionTest, LessPow) {
const Expression pow1{pow(x_, y_)};
const Expression pow2{pow(x_, z_)};
const Expression pow3{pow(y_, z_)};
CheckOrdering({pow1, pow2, pow3});
}
TEST_F(SymbolicExpressionTest, LessAtan2) {
const Expression atan2_1{atan2(x_, y_)};
const Expression atan2_2{atan2(x_, z_)};
const Expression atan2_3{atan2(y_, z_)};
CheckOrdering({atan2_1, atan2_2, atan2_3});
}
TEST_F(SymbolicExpressionTest, LessMin) {
const Expression min1{min(x_, y_)};
const Expression min2{min(x_, z_)};
const Expression min3{min(y_, z_)};
CheckOrdering({min1, min2, min3});
}
TEST_F(SymbolicExpressionTest, LessMax) {
const Expression max1{max(x_, y_)};
const Expression max2{max(x_, z_)};
const Expression max3{max(y_, z_)};
CheckOrdering({max1, max2, max3});
}
TEST_F(SymbolicExpressionTest, LessCeil) {
const Expression ceil1{ceil(x_)};
const Expression ceil2{ceil(y_)};
const Expression ceil3{ceil(x_plus_y_)};
const Expression ceil4{ceil(x_plus_z_)};
CheckOrdering({ceil1, ceil2, ceil3, ceil4});
}
TEST_F(SymbolicExpressionTest, LessFloor) {
const Expression floor1{floor(x_)};
const Expression floor2{floor(y_)};
const Expression floor3{floor(x_plus_y_)};
const Expression floor4{floor(x_plus_z_)};
CheckOrdering({floor1, floor2, floor3, floor4});
}
TEST_F(SymbolicExpressionTest, LessIfThenElse) {
const Formula f1{x_ < y_};
const Formula f2{y_ < z_};
const Expression ite1{if_then_else(f1, x_, y_)};
const Expression ite2{if_then_else(f1, x_, z_)};
const Expression ite3{if_then_else(f1, y_, z_)};
const Expression ite4{if_then_else(f2, y_, z_)};
const Expression ite5{if_then_else(f2, z_, x_)};
CheckOrdering({ite1, ite2, ite3, ite4, ite5});
}
TEST_F(SymbolicExpressionTest, LessUninterpretedFunction) {
const Expression uf1{uninterpreted_function("name1", {x_, y_ + z_})};
const Expression uf2{uninterpreted_function("name1", {x_, y_ * z_})};
const Expression uf3{uninterpreted_function("name1", {x_, y_ * z_, 3.0})};
const Expression uf4{uninterpreted_function("name2", {})};
const Expression uf5{uninterpreted_function("name2", {0.0, -1.0})};
const Expression uf6{uninterpreted_function("name2", {1.0, 0.0})};
CheckOrdering({uf1, uf2, uf3, uf4, uf5, uf6});
}
TEST_F(SymbolicExpressionTest, Variable) {
EXPECT_EQ(x_.to_string(), var_x_.get_name());
EXPECT_EQ(y_.to_string(), var_y_.get_name());
EXPECT_EQ(z_.to_string(), var_z_.get_name());
EXPECT_PRED2(ExprEqual, x_, x_);
EXPECT_PRED2(ExprNotEqual, x_, y_);
EXPECT_PRED2(ExprNotEqual, x_, z_);
EXPECT_PRED2(ExprNotEqual, y_, x_);
EXPECT_PRED2(ExprEqual, y_, y_);
EXPECT_PRED2(ExprNotEqual, y_, z_);
EXPECT_PRED2(ExprNotEqual, z_, x_);
EXPECT_PRED2(ExprNotEqual, z_, y_);
EXPECT_PRED2(ExprEqual, z_, z_);
}
TEST_F(SymbolicExpressionTest, Evaluate) {
EXPECT_THROW(x_plus_y_.Evaluate(), std::runtime_error);
}
TEST_F(SymbolicExpressionTest, Constant) {
EXPECT_EQ(c1_.Evaluate(), -10);
EXPECT_EQ(c2_.Evaluate(), 1);
EXPECT_EQ(c3_.Evaluate(), 3.14159);
EXPECT_EQ(c4_.Evaluate(), -2.718);
EXPECT_THROW(Expression{NAN}.Evaluate(), runtime_error);
}
TEST_F(SymbolicExpressionTest, StaticConstant) {
EXPECT_DOUBLE_EQ(Expression::Zero().Evaluate(), 0.0);
EXPECT_DOUBLE_EQ(Expression::One().Evaluate(), 1.0);
EXPECT_NEAR(Expression::Pi().Evaluate(), M_PI, 0.000001);
EXPECT_NEAR(Expression::E().Evaluate(), M_E, 0.000000001);
}
TEST_F(SymbolicExpressionTest, Hash) {
Expression x{var_x_};
const Expression x_prime(x);
EXPECT_EQ(get_std_hash(x), get_std_hash(x_prime));
x++;
EXPECT_NE(get_std_hash(x), get_std_hash(x_prime));
}
TEST_F(SymbolicExpressionTest, HashBinary) {
const Expression e1{x_plus_y_ + x_plus_z_};
const Expression e2{x_plus_y_ - x_plus_z_};
const Expression e3{x_plus_y_ * x_plus_z_};
const Expression e4{x_plus_y_ / x_plus_z_};
const Expression e5{pow(x_plus_y_, x_plus_z_)};
const Expression e6{atan2(x_plus_y_, x_plus_z_)};
const Expression e7{min(x_plus_y_, x_plus_z_)};
const Expression e8{max(x_plus_y_, x_plus_z_)};
// e1, ..., e8 share the same sub-expressions, but their hash values should be
// distinct.
unordered_set<size_t> hash_set;
const vector<Expression> exprs{e1, e2, e3, e4, e5, e6, e7, e8};
for (auto const& e : exprs) {
hash_set.insert(get_std_hash(e));
}
EXPECT_EQ(hash_set.size(), exprs.size());
}
TEST_F(SymbolicExpressionTest, HashUnary) {
const Expression e0{log(x_plus_y_)};
const Expression e1{abs(x_plus_y_)};
const Expression e2{exp(x_plus_y_)};
const Expression e3{sqrt(x_plus_y_)};
const Expression e4{sin(x_plus_y_)};
const Expression e5{cos(x_plus_y_)};
const Expression e6{tan(x_plus_y_)};
const Expression e7{asin(x_plus_y_)};
const Expression e8{acos(x_plus_y_)};
const Expression e9{atan(x_plus_y_)};
const Expression e10{sinh(x_plus_y_)};
const Expression e11{cosh(x_plus_y_)};
const Expression e12{tanh(x_plus_y_)};
const Expression e13{ceil(x_plus_y_)};
const Expression e14{floor(x_plus_y_)};
// e0, ..., e14 share the same sub-expression, but their hash values should be
// distinct.
unordered_set<size_t> hash_set;
const vector<Expression> exprs{e0, e1, e2, e3, e4, e5, e6, e7,
e8, e9, e10, e11, e12, e13, e14};
for (auto const& e : exprs) {
hash_set.insert(get_std_hash(e));
}
EXPECT_EQ(hash_set.size(), exprs.size());
}
// Confirm that numeric_limits is appropriately specialized for Expression.
// We'll just spot-test a few values, since our implementation is trivially
// forwarding to numeric_limits<double>.
TEST_F(SymbolicExpressionTest, NumericLimits) {
using std::numeric_limits;
using Limits = numeric_limits<Expression>;
const Expression num_eps = Limits::epsilon();
ASSERT_TRUE(is_constant(num_eps));
EXPECT_EQ(get_constant_value(num_eps), numeric_limits<double>::epsilon());
const Expression num_min = Limits::min();
ASSERT_TRUE(is_constant(num_min));
EXPECT_EQ(get_constant_value(num_min), numeric_limits<double>::min());
const Expression num_infinity = Limits::infinity();
EXPECT_EQ(num_infinity.to_string(), "inf");
}
TEST_F(SymbolicExpressionTest, UnaryPlus) {
EXPECT_PRED2(ExprEqual, c3_, +c3_);
EXPECT_PRED2(ExprEqual, Expression(var_x_), +var_x_);
}
// TODO(jwnimmer-tri) These tests should probably live in symbolic_formula_test.
//
// Confirm that Eigen::numext::{not_,}equal_strict are appropriately
// specialized for Expression.
// We only need a limited set of cases because if the specialization doesn't
// exist, this would result in a compile error.
// This function was only introduced in eigen 3.3.5. Therefore, we only want to
// test if the eigen version is at least that.
#if EIGEN_VERSION_AT_LEAST(3, 3, 5)
TEST_F(SymbolicExpressionTest, EigenEqualStrict) {
EXPECT_TRUE(Eigen::numext::equal_strict(c3_, c3_));
EXPECT_FALSE(Eigen::numext::equal_strict(c3_, c4_));
// Check our special-case zero handling.
EXPECT_TRUE(Eigen::numext::equal_strict(zero_, zero_));
EXPECT_FALSE(Eigen::numext::equal_strict(zero_, one_));
EXPECT_FALSE(Eigen::numext::equal_strict(one_, zero_));
EXPECT_FALSE(Eigen::numext::equal_strict(zero_, x_));
EXPECT_FALSE(Eigen::numext::equal_strict(x_, zero_));
EXPECT_THROW(Eigen::numext::equal_strict(x_, y_), std::exception);
}
TEST_F(SymbolicExpressionTest, EigenNotEqualStrict) {
EXPECT_TRUE(Eigen::numext::not_equal_strict(c3_, c4_));
EXPECT_FALSE(Eigen::numext::not_equal_strict(c3_, c3_));
// Check our special-case zero handling.
EXPECT_FALSE(Eigen::numext::not_equal_strict(zero_, zero_));
EXPECT_TRUE(Eigen::numext::not_equal_strict(zero_, one_));
EXPECT_TRUE(Eigen::numext::not_equal_strict(one_, zero_));
EXPECT_TRUE(Eigen::numext::not_equal_strict(zero_, x_));
EXPECT_TRUE(Eigen::numext::not_equal_strict(x_, zero_));
EXPECT_THROW(Eigen::numext::not_equal_strict(x_, y_), std::exception);
}
#endif
// Confirm the other Eigen::numext specializations:
// - isfinite
// - isnan
// - isinf
// They all trivially forward to our own functions.
TEST_F(SymbolicExpressionTest, EigenNumext) {
// isnan is only valid for non-NaN Expressions. Trying to evaluate
// a NaN expression will throw an exception. So we can't check that.
EXPECT_FALSE(Eigen::numext::isnan(one_));
const Expression num_infinity = std::numeric_limits<Expression>::infinity();
EXPECT_FALSE(Eigen::numext::isinf(one_));
EXPECT_TRUE(Eigen::numext::isinf(num_infinity));
EXPECT_TRUE(Eigen::numext::isfinite(one_));
EXPECT_FALSE(Eigen::numext::isfinite(num_infinity));
}
TEST_F(SymbolicExpressionTest, UnaryMinus) {
EXPECT_PRED2(ExprEqual, -Expression(var_x_), -var_x_);
EXPECT_PRED2(ExprNotEqual, c3_, -c3_);
EXPECT_DOUBLE_EQ(c3_.Evaluate(), -(-c3_).Evaluate());
EXPECT_PRED2(ExprEqual, c3_, -(-c3_));
EXPECT_DOUBLE_EQ(c3_.Evaluate(), (-(-c3_)).Evaluate());
const Expression e{x_ + y_};
const Environment env{{var_x_, 1.0}, {var_y_, 2.0}};
EXPECT_EQ((-x_).Evaluate(env), -1.0);
EXPECT_PRED2(ExprEqual, x_, -(-x_));
// (x + y) and -(-(x + y)) are structurally equal (after simplification)
EXPECT_PRED2(ExprEqual, e, -(-e));
// and their evaluations should be the same.
EXPECT_DOUBLE_EQ(e.Evaluate(env), (-(-e)).Evaluate(env));
EXPECT_PRED2(ExprEqual, -(x_plus_y_ + x_plus_z_ + pi_),
-x_plus_y_ + (-x_plus_z_) + (-pi_));
EXPECT_EQ((-(x_)).to_string(), "(-1 * x)");
}
TEST_F(SymbolicExpressionTest, Add1) {
EXPECT_PRED2(ExprEqual, c3_ + zero_, c3_);
EXPECT_EQ((c3_ + zero_).to_string(), c3_.to_string());
EXPECT_PRED2(ExprEqual, zero_ + c3_, c3_);
EXPECT_EQ((zero_ + c3_).to_string(), c3_.to_string());
EXPECT_PRED2(ExprEqual, 0.0 + c3_, c3_);
EXPECT_EQ((0.0 + c3_).to_string(), c3_.to_string());
EXPECT_PRED2(ExprEqual, c3_ + 0.0, c3_);
EXPECT_EQ((c3_ + 0.0).to_string(), c3_.to_string());
EXPECT_PRED2(ExprEqual, c3_ + c4_, 3.14159 + -2.718);
EXPECT_EQ((c3_ + c4_).to_string(), Expression{3.14159 + -2.718}.to_string());
EXPECT_PRED2(ExprEqual, c3_ + x_, 3.14159 + x_);
EXPECT_EQ((c3_ + x_).to_string(), (3.14159 + x_).to_string());
EXPECT_PRED2(ExprEqual, x_ + c3_, x_ + 3.14159);
EXPECT_EQ((x_ + c3_).to_string(), (x_ + 3.14159).to_string());
}
TEST_F(SymbolicExpressionTest, Add2) {
Expression e1{x_ + y_};
Expression e2{e1 + e1};
const auto str_rep_e2(e2.to_string());
EXPECT_EQ(str_rep_e2, "(2 * x + 2 * y)");
EXPECT_PRED2(ExprEqual, e2, 2 * x_ + 2 * y_);
e1 += z_;
EXPECT_PRED2(ExprEqual, e1, x_ + y_ + z_);
EXPECT_EQ(e2.to_string(), str_rep_e2); // e2 doesn't change.
}
TEST_F(SymbolicExpressionTest, Add3) {
const Expression e1{2 + x_ + y_};
const Expression e2{3 + x_ + y_};
EXPECT_PRED2(ExprNotEqual, e1, e2);
}
TEST_F(SymbolicExpressionTest, Add4) {
const Expression e1{-2 - x_ + -3 * y_};
const Expression e2{-2 - x_ - 3 * y_};
EXPECT_EQ(e1.to_string(), "(-2 - x - 3 * y)");
EXPECT_EQ(e2.to_string(), "(-2 - x - 3 * y)");
}