forked from sammy-tri/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic_formula.h
1405 lines (1297 loc) · 58.8 KB
/
symbolic_formula.h
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
#pragma once
#ifndef DRAKE_COMMON_SYMBOLIC_HEADER
#error Do not directly include this file. Include "drake/common/symbolic.h".
#endif
#include <functional>
#include <memory>
#include <ostream>
#include <set>
#include <string>
#include <utility>
#include <Eigen/Core>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_bool.h"
#include "drake/common/drake_copyable.h"
#include "drake/common/eigen_types.h"
#include "drake/common/hash.h"
#include "drake/common/random.h"
#include "drake/common/symbolic.h"
namespace drake {
namespace symbolic {
/** Kinds of symbolic formulas. */
enum class FormulaKind {
False, ///< ⊥
True, ///< ⊤
Var, ///< Boolean Variable
Eq, ///< =
Neq, ///< !=
Gt, ///< >
Geq, ///< >=
Lt, ///< <
Leq, ///< <=
And, ///< Conjunction (∧)
Or, ///< Disjunction (∨)
Not, ///< Negation (¬)
Forall, ///< Universal quantification (∀)
Isnan, ///< NaN check predicate
PositiveSemidefinite, ///< Positive semidefinite matrix
};
// Total ordering between FormulaKinds
bool operator<(FormulaKind k1, FormulaKind k2);
class FormulaCell; // In drake/common/symbolic_formula_cell.h
class FormulaFalse; // In drake/common/symbolic_formula_cell.h
class FormulaTrue; // In drake/common/symbolic_formula_cell.h
class FormulaVar; // In drake/common/symbolic_formula_cell.h
class RelationalFormulaCell; // In drake/common/symbolic_formula_cell.h
class FormulaEq; // In drake/common/symbolic_formula_cell.h
class FormulaNeq; // In drake/common/symbolic_formula_cell.h
class FormulaGt; // In drake/common/symbolic_formula_cell.h
class FormulaGeq; // In drake/common/symbolic_formula_cell.h
class FormulaLt; // In drake/common/symbolic_formula_cell.h
class FormulaLeq; // In drake/common/symbolic_formula_cell.h
class NaryFormulaCell; // In drake/common/symbolic_formula_cell.h
class FormulaNot; // In drake/common/symbolic_formula_cell.h
class FormulaAnd; // In drake/common/symbolic_formula_cell.h
class FormulaOr; // In drake/common/symbolic_formula_cell.h
class FormulaForall; // In drake/common/symbolic_formula_cell.h
class FormulaIsnan; // In drake/common/symbolic_formula_cell.h
class FormulaPositiveSemidefinite; // In drake/common/symbolic_formula_cell.h
/** Represents a symbolic form of a first-order logic formula.
It has the following grammar:
\verbatim
F := ⊥ | ⊤ | Var | E = E | E ≠ E | E > E | E ≥ E | E < E | E ≤ E
| E ∧ ... ∧ E | E ∨ ... ∨ E | ¬F | ∀ x₁, ..., xn. F
\endverbatim
In the implementation, Formula is a simple wrapper including a shared
pointer to FormulaCell class which is a super-class of different kinds
of symbolic formulas (i.e. FormulaAnd, FormulaOr, FormulaEq). Note
that it includes a shared pointer, not a unique pointer, to allow
sharing sub-expressions.
\note The sharing of sub-expressions is not yet implemented.
The following simple simplifications are implemented:
\verbatim
E1 = E2 -> True (if E1 and E2 are structurally equal)
E1 ≠ E2 -> False (if E1 and E2 are structurally equal)
E1 > E2 -> False (if E1 and E2 are structurally equal)
E1 ≥ E2 -> True (if E1 and E2 are structurally equal)
E1 < E2 -> False (if E1 and E2 are structurally equal)
E1 ≤ E2 -> True (if E1 and E2 are structurally equal)
F1 ∧ F2 -> False (if either F1 or F2 is False)
F1 ∨ F2 -> True (if either F1 or F2 is True)
¬(¬(F)) -> F
\endverbatim
We flatten nested conjunctions (or disjunctions) at the construction. A
conjunction (resp. disjunction) takes a set of conjuncts (resp. disjuncts). Note
that any duplicated conjunct/disjunct is removed. For example, both of `f1 &&
(f2 && f1)` and `(f1 && f2) && f1` are flattened to `f1 && f2 && f1` and
simplified into `f1 && f2`. As a result, the two are identified as the same
formula.
\note Formula class has an explicit conversion operator to bool. It evaluates a
symbolic formula under an empty environment. If a symbolic formula includes
variables, the conversion operator throws an exception. This operator is only
intended for third-party code doing things like `(imag(SymbolicExpression(0))
== SymbolicExpression(0)) { ... };` that we found in Eigen3 codebase. In
general, a user of this class should explicitly call `Evaluate` from within
Drake for readability.
*/
class Formula {
public:
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Formula)
/** Default constructor. Sets the value to Formula::False, to be consistent
* with value-initialized `bool`s.
*/
Formula() : Formula(False()) {}
/** Constructs from a `bool`. This overload is also used by Eigen when
* EIGEN_INITIALIZE_MATRICES_BY_ZERO is enabled.
*/
explicit Formula(bool value) : Formula(value ? True() : False()) {}
explicit Formula(std::shared_ptr<const FormulaCell> ptr);
/** Constructs a formula from @p var.
* @pre @p var is of BOOLEAN type and not a dummy variable.
*/
explicit Formula(const Variable& var);
[[nodiscard]] FormulaKind get_kind() const;
/** Gets free variables (unquantified variables). */
[[nodiscard]] Variables GetFreeVariables() const;
/** Checks structural equality. */
[[nodiscard]] bool EqualTo(const Formula& f) const;
/** Checks lexicographical ordering between this and @p e.
*
* If the two formulas f1 and f2 have different kinds k1 and k2 respectively,
* f1.Less(f2) is equal to k1 < k2. If f1 and f2 are expressions of the same
* kind, we check the ordering between f1 and f2 by comparing their elements
* lexicographically.
*
* For example, in case of And, let f1 and f2 be
*
* f1 = f_1,1 ∧ ... ∧ f_1,n
* f2 = f_2,1 ∧ ... ∧ f_2,m
*
* f1.Less(f2) is true if there exists an index i (<= n, m) such that
* for all j < i, we have
*
* ¬(f_1_j.Less(f_2_j)) ∧ ¬(f_2_j.Less(f_1_j))
*
* and f_1_i.Less(f_2_i) holds.
*
* This function is used as a compare function in
* std::map<symbolic::Formula> and std::set<symbolic::Formula> via
* std::less<symbolic::Formula>. */
[[nodiscard]] bool Less(const Formula& f) const;
/** Evaluates using a given environment (by default, an empty environment) and
* a random number generator. If there is a random variable in this formula
* which is unassigned in @p env, it uses @p random_generator to sample a
* value and use it to substitute all occurrences of the random variable in
* this formula.
*
* @throws std::exception if a variable `v` is needed for an evaluation
* but not provided by @p env.
* @throws std::exception if an unassigned random variable is detected
* while @p random_generator is `nullptr`.
*/
bool Evaluate(const Environment& env = Environment{},
RandomGenerator* random_generator = nullptr) const;
/** Evaluates using an empty environment and a random number generator.
*
* See the above overload for the exceptions that it might throw.
*/
bool Evaluate(RandomGenerator* random_generator) const;
/** Returns a copy of this formula replacing all occurrences of @p var
* with @p e.
* @throws std::exception if NaN is detected during substitution.
*/
[[nodiscard]] Formula Substitute(const Variable& var,
const Expression& e) const;
/** Returns a copy of this formula replacing all occurrences of the
* variables in @p s with corresponding expressions in @p s. Note that the
* substitutions occur simultaneously. For example, (x / y >
* 0).Substitute({{x, y}, {y, x}}) gets (y / x > 0).
* @throws std::exception if NaN is detected during substitution.
*/
[[nodiscard]] Formula Substitute(const Substitution& s) const;
/** Returns string representation of Formula. */
[[nodiscard]] std::string to_string() const;
static Formula True();
static Formula False();
/** Conversion to bool. */
explicit operator bool() const { return Evaluate(); }
/** Implements the @ref hash_append concept. */
template <class HashAlgorithm>
friend void hash_append(HashAlgorithm& hasher, const Formula& item) noexcept {
DelegatingHasher delegating_hasher(
[&hasher](const void* data, const size_t length) {
return hasher(data, length);
});
item.HashAppend(&delegating_hasher);
}
friend std::ostream& operator<<(std::ostream& os, const Formula& f);
friend void swap(Formula& a, Formula& b) { std::swap(a.ptr_, b.ptr_); }
friend bool is_false(const Formula& f);
friend bool is_true(const Formula& f);
friend bool is_variable(const Formula& f);
friend bool is_equal_to(const Formula& f);
friend bool is_not_equal_to(const Formula& f);
friend bool is_greater_than(const Formula& f);
friend bool is_greater_than_or_equal_to(const Formula& f);
friend bool is_less_than(const Formula& f);
friend bool is_less_than_or_equal_to(const Formula& f);
friend bool is_relational(const Formula& f);
friend bool is_conjunction(const Formula& f);
friend bool is_disjunction(const Formula& f);
friend bool is_negation(const Formula& f);
friend bool is_forall(const Formula& f);
friend bool is_isnan(const Formula& f);
friend bool is_positive_semidefinite(const Formula& f);
// Note that the following cast functions are only for low-level operations
// and not exposed to the user of symbolic_formula.h. These functions are
// declared in symbolic_formula_cell.h header.
friend std::shared_ptr<const FormulaFalse> to_false(const Formula& f);
friend std::shared_ptr<const FormulaTrue> to_true(const Formula& f);
friend std::shared_ptr<const FormulaVar> to_variable(const Formula& f);
friend std::shared_ptr<const RelationalFormulaCell> to_relational(
const Formula& f);
friend std::shared_ptr<const FormulaEq> to_equal_to(const Formula& f);
friend std::shared_ptr<const FormulaNeq> to_not_equal_to(const Formula& f);
friend std::shared_ptr<const FormulaGt> to_greater_than(const Formula& f);
friend std::shared_ptr<const FormulaGeq> to_greater_than_or_equal_to(
const Formula& f);
friend std::shared_ptr<const FormulaLt> to_less_than(const Formula& f);
friend std::shared_ptr<const FormulaLeq> to_less_than_or_equal_to(
const Formula& f);
friend std::shared_ptr<const NaryFormulaCell> to_nary(const Formula& f);
friend std::shared_ptr<const FormulaAnd> to_conjunction(const Formula& f);
friend std::shared_ptr<const FormulaOr> to_disjunction(const Formula& f);
friend std::shared_ptr<const FormulaNot> to_negation(const Formula& f);
friend std::shared_ptr<const FormulaForall> to_forall(const Formula& f);
friend std::shared_ptr<const FormulaIsnan> to_isnan(const Formula& f);
friend std::shared_ptr<const FormulaPositiveSemidefinite>
to_positive_semidefinite(const Formula& f);
private:
void HashAppend(DelegatingHasher* hasher) const;
// Note: We use "const" FormulaCell type here because a FormulaCell object can
// be shared by multiple formulas, a formula should _not_ be able to change
// the cell that it points to.
std::shared_ptr<const FormulaCell> ptr_;
};
/** Returns a formula @p f, universally quantified by variables @p vars. */
Formula forall(const Variables& vars, const Formula& f);
/** Returns a conjunction of @p formulas. It performs the following
* simplification:
*
* - make_conjunction({}) returns True.
* - make_conjunction({f₁}) returns f₁.
* - If False ∈ @p formulas, it returns False.
* - If True ∈ @p formulas, it will not appear in the return value.
* - Nested conjunctions will be flattened. For example, make_conjunction({f₁,
* f₂ ∧ f₃}) returns f₁ ∧ f₂ ∧ f₃.
*/
Formula make_conjunction(const std::set<Formula>& formulas);
Formula operator&&(const Formula& f1, const Formula& f2);
Formula operator&&(const Variable& v, const Formula& f);
Formula operator&&(const Formula& f, const Variable& v);
Formula operator&&(const Variable& v1, const Variable& v2);
/** Returns a disjunction of @p formulas. It performs the following
* simplification:
*
* - make_disjunction({}) returns False.
* - make_disjunction({f₁}) returns f₁.
* - If True ∈ @p formulas, it returns True.
* - If False ∈ @p formulas, it will not appear in the return value.
* - Nested disjunctions will be flattened. For example, make_disjunction({f₁,
* f₂ ∨ f₃}) returns f₁ ∨ f₂ ∨ f₃.
*/
Formula make_disjunction(const std::set<Formula>& formulas);
Formula operator||(const Formula& f1, const Formula& f2);
Formula operator||(const Variable& v, const Formula& f);
Formula operator||(const Formula& f, const Variable& v);
Formula operator||(const Variable& v1, const Variable& v2);
Formula operator!(const Formula& f);
Formula operator!(const Variable& v);
Formula operator==(const Expression& e1, const Expression& e2);
Formula operator!=(const Expression& e1, const Expression& e2);
Formula operator<(const Expression& e1, const Expression& e2);
Formula operator<=(const Expression& e1, const Expression& e2);
Formula operator>(const Expression& e1, const Expression& e2);
Formula operator>=(const Expression& e1, const Expression& e2);
/** Returns a Formula for the predicate isnan(e) to the given expression. This
* serves as the argument-dependent lookup related to std::isnan(double).
*
* When this formula is evaluated, there are two possible outcomes:
* - Returns false if the e.Evaluate() is not NaN.
* - Throws std::exception if NaN is detected during evaluation.
* Note that the evaluation of `isnan(e)` never returns true.
*/
Formula isnan(const Expression& e);
/** Returns a Formula determining if the given expression @p e is a
* positive or negative infinity.
* @throws std::exception if NaN is detected during evaluation.
*/
Formula isinf(const Expression& e);
/** Returns a Formula determining if the given expression @p e has a finite
* value.
* @throws std::exception if NaN is detected during evaluation.
*/
Formula isfinite(const Expression& e);
/** Returns a symbolic formula constraining @p m to be a positive-semidefinite
* matrix. By definition, a symmetric matrix @p m is positive-semidefinte if xᵀ
* m x ≥ 0 for all vector x ∈ ℝⁿ.
*
* @throws std::exception if @p m is not symmetric.
*
* @note This method checks if @p m is symmetric, which can be costly. If you
* want to avoid it, please consider using
* `positive_semidefinite(m.triangularView<Eigen::Lower>())` or
* `positive_semidefinite(m.triangularView<Eigen::Upper>())` instead of
* `positive_semidefinite(m)`.
*
* @pydrake_mkdoc_identifier{1args_m}
*/
Formula positive_semidefinite(const Eigen::Ref<const MatrixX<Expression>>& m);
/** Constructs and returns a symbolic positive-semidefinite formula from @p
* m. If @p mode is Eigen::Lower, it's using the lower-triangular part of @p m
* to construct a positive-semidefinite formula. If @p mode is Eigen::Upper, the
* upper-triangular part of @p m is used. It throws std::exception if @p has
* other values. See the following code snippet.
*
* @code
* Eigen::Matrix<Expression, 2, 2> m;
* m << 1.0, 2.0,
* 3.0, 4.0;
*
* const Formula psd_l{positive_semidefinite(m, Eigen::Lower)};
* // psd_l includes [1.0 3.0]
* // [3.0 4.0].
*
* const Formula psd_u{positive_semidefinite(m, Eigen::Upper)};
* // psd_u includes [1.0 2.0]
* // [2.0 4.0].
* @endcode
*
* @exclude_from_pydrake_mkdoc{This overload is not bound.}
*/
Formula positive_semidefinite(const MatrixX<Expression>& m,
Eigen::UpLoType mode);
/** Constructs and returns a symbolic positive-semidefinite formula from a lower
* triangular-view @p l. See the following code snippet.
*
* @code
* Eigen::Matrix<Expression, 2, 2> m;
* m << 1.0, 2.0,
* 3.0, 4.0;
*
* Formula psd{positive_semidefinite(m.triangularView<Eigen::Lower>())};
* // psd includes [1.0 3.0]
* // [3.0 4.0].
* @endcode
*
* @exclude_from_pydrake_mkdoc{This overload is not bound.}
*/
template <typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::MatrixXpr> &&
std::is_same_v<typename Derived::Scalar, Expression>,
Formula>
positive_semidefinite(const Eigen::TriangularView<Derived, Eigen::Lower>& l) {
return positive_semidefinite(l, Eigen::Lower);
}
/** Constructs and returns a symbolic positive-semidefinite formula from an
* upper triangular-view @p u. See the following code snippet.
*
* @code
* Eigen::Matrix<Expression, 2, 2> m;
* m << 1.0, 2.0,
* 3.0, 4.0;
*
* Formula psd{positive_semidefinite(m.triangularView<Eigen::Upper>())};
* // psd includes [1.0 2.0]
* // [2.0 4.0].
* @endcode
*
* @exclude_from_pydrake_mkdoc{This overload is not bound.}
*/
template <typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::MatrixXpr> &&
std::is_same_v<typename Derived::Scalar, Expression>,
Formula>
positive_semidefinite(const Eigen::TriangularView<Derived, Eigen::Upper>& u) {
return positive_semidefinite(u, Eigen::Upper);
}
std::ostream& operator<<(std::ostream& os, const Formula& f);
/** Checks if @p f is structurally equal to False formula. */
bool is_false(const Formula& f);
/** Checks if @p f is structurally equal to True formula. */
bool is_true(const Formula& f);
/** Checks if @p f is a variable formula. */
bool is_variable(const Formula& f);
/** Checks if @p f is a formula representing equality (==). */
bool is_equal_to(const Formula& f);
/** Checks if @p f is a formula representing disequality (!=). */
bool is_not_equal_to(const Formula& f);
/** Checks if @p f is a formula representing greater-than (>). */
bool is_greater_than(const Formula& f);
/** Checks if @p f is a formula representing greater-than-or-equal-to (>=). */
bool is_greater_than_or_equal_to(const Formula& f);
/** Checks if @p f is a formula representing less-than (<). */
bool is_less_than(const Formula& f);
/** Checks if @p f is a formula representing less-than-or-equal-to (<=). */
bool is_less_than_or_equal_to(const Formula& f);
/** Checks if @p f is a relational formula ({==, !=, >, >=, <, <=}). */
bool is_relational(const Formula& f);
/** Checks if @p f is a conjunction (∧). */
bool is_conjunction(const Formula& f);
/** Checks if @p f is a disjunction (∨). */
bool is_disjunction(const Formula& f);
/** Checks if @p f is a n-ary formula ({∧, ∨}). */
bool is_nary(const Formula& f);
/** Checks if @p f is a negation (¬). */
bool is_negation(const Formula& f);
/** Checks if @p f is a Forall formula (∀). */
bool is_forall(const Formula& f);
/** Checks if @p f is an isnan formula. */
bool is_isnan(const Formula& f);
/** Checks if @p f is a positive-semidefinite formula. */
bool is_positive_semidefinite(const Formula& f);
/** Returns the embedded variable in the variable formula @p f.
* @pre @p f is a variable formula.
*/
const Variable& get_variable(const Formula& f);
/** Returns the lhs-argument of a relational formula @p f.
* \pre{@p f is a relational formula.}
*/
const Expression& get_lhs_expression(const Formula& f);
/** Returns the rhs-argument of a relational formula @p f.
* \pre{@p f is a relational formula.}
*/
const Expression& get_rhs_expression(const Formula& f);
/** Returns the expression in a unary expression formula @p f.
* Currently, an isnan() formula is the only kind of unary expression formula.
* \pre{@p f is a unary expression formula.}
*/
const Expression& get_unary_expression(const Formula& f);
/** Returns the set of formulas in a n-ary formula @p f.
* \pre{@p f is a n-ary formula.}
*/
const std::set<Formula>& get_operands(const Formula& f);
/** Returns the formula in a negation formula @p f.
* \pre{@p f is a negation formula.}
*/
const Formula& get_operand(const Formula& f);
/** Returns the quantified variables in a forall formula @p f.
* \pre{@p f is a forall formula.}
*/
const Variables& get_quantified_variables(const Formula& f);
/** Returns the quantified formula in a forall formula @p f.
* \pre{@p f is a forall formula.}
*/
const Formula& get_quantified_formula(const Formula& f);
/** Returns the matrix in a positive-semidefinite formula @p f.
* \pre{@p f is a positive-semidefinite formula.}
*/
const MatrixX<Expression>& get_matrix_in_positive_semidefinite(
const Formula& f);
namespace internal {
/// Provides a return type of relational operations (=, ≠, ≤, <, ≥, >) between
/// `Eigen::Array`s.
///
/// @tparam DerivedA A derived type of Eigen::ArrayBase.
/// @tparam DerivedB A derived type of Eigen::ArrayBase.
/// @pre The type of (DerivedA::Scalar() == DerivedB::Scalar()) is symbolic
/// formula.
template <
typename DerivedA,
typename DerivedB,
typename = std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() ==
typename DerivedB::Scalar()),
Formula>>>
struct RelationalOpTraits {
using ReturnType =
Eigen::Array<Formula,
EigenSizeMinPreferFixed<DerivedA::RowsAtCompileTime,
DerivedB::RowsAtCompileTime>::value,
EigenSizeMinPreferFixed<DerivedA::ColsAtCompileTime,
DerivedB::ColsAtCompileTime>::value>;
};
/// Returns @p f1 ∧ @p f2.
/// Note that this function returns a `Formula` while
/// `std::logical_and<Formula>{}` returns a bool.
inline Formula logic_and(const Formula& f1, const Formula& f2) {
return f1 && f2;
}
/// Returns @p f1 ∨ @p f2.
/// Note that this function returns a `Formula` while
/// `std::logical_or<Formula>{}` returns a bool.
inline Formula logic_or(const Formula& f1, const Formula& f2) {
return f1 || f2;
}
} // namespace internal
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise symbolic-equality of two arrays @p m1 and @p m2.
///
/// The following table describes the return type of @p m1 == @p m2.
///
/// LHS \ RHS | EA<Expression> | EA<Variable> | EA<double>
/// ----------------|----------------|--------------|--------------
/// EA<Expression> | EA<Formula> | EA<Formula> | EA<Formula>
/// EA<Variable> | EA<Formula> | EA<Formula> | EA<Formula>
/// EA<double> | EA<Formula> | EA<Formula> | EA<bool>
///
/// In the table, `EA` is a short-hand of `Eigen::Array`.
///
/// Note that this function does *not* provide operator overloading for the
/// following case. It returns `Eigen::Array<bool>` and is provided by Eigen.
///
/// - Eigen::Array<double> == Eigen::Array<double>
///
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() ==
typename DerivedB::Scalar()),
Formula>,
typename internal::RelationalOpTraits<DerivedA, DerivedB>::ReturnType>
operator==(const DerivedA& a1, const DerivedB& a2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(a1.rows() == a2.rows() && a1.cols() == a2.cols());
return a1.binaryExpr(a2, std::equal_to<void>());
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between an array @p a and a scalar @p v using
/// equal-to operator (==). That is, for all i and j, the (i, j)-th entry of `(a
/// == v)` has a symbolic formula `a(i, j) == v`.
///
/// Here is an example using this operator overloading.
/// @code
/// Eigen::Array<Variable, 2, 2> a;
/// a << Variable{"x"}, Variable{"y"},
/// Variable{"z"}, Variable{"w"};
/// Eigen::Array<Formula, 2, 2> f = (a == 3.5);
/// // Here f = |(x == 3.5) (y == 3.5)|
/// // |(z == 3.5) (w == 3.5)|.
/// @endcode
template <typename Derived, typename ScalarType>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename Derived::Scalar() == ScalarType()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator==(const Derived& a, const ScalarType& v) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return x == v; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between a scalar @p v and an array @p using equal-to
/// operator (==). That is, for all i and j, the (i, j)-th entry of `(v == a)`
/// has a symbolic formula `v == a(i, j)`.
///
/// Here is an example using this operator overloading.
/// @code
/// Eigen::Array<Variable, 2, 2> a;
/// a << Variable{"x"}, Variable{"y"},
/// Variable{"z"}, Variable{"w"};
/// Eigen::Array<Formula, 2, 2> f = (3.5 == a);
/// // Here f = |(3.5 == x) (3.5 == y)|
/// // |(3.5 == z) (3.5 == w)|.
/// @endcode
template <typename ScalarType, typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(ScalarType() == typename Derived::Scalar()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator==(const ScalarType& v, const Derived& a) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return v == x; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison of two arrays @p a1 and @p a2 using
/// less-than-or-equal operator (<=).
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() <=
typename DerivedB::Scalar()),
Formula>,
typename internal::RelationalOpTraits<DerivedA, DerivedB>::ReturnType>
operator<=(const DerivedA& a1, const DerivedB& a2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(a1.rows() == a2.rows() && a1.cols() == a2.cols());
return a1.binaryExpr(a2, std::less_equal<void>());
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between an array @p a and a scalar @p v using
/// less-than-or-equal operator (<=). That is, for all i and j, the (i, j)-th
/// entry of `(a <= v)` has a symbolic formula `a(i, j) <= v`.
template <typename Derived, typename ScalarType>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename Derived::Scalar() <= ScalarType()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator<=(const Derived& a, const ScalarType& v) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return x <= v; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between a scalar @p v and an array @p using
/// less-than-or-equal operator (<=). That is, for all i and j, the (i, j)-th
/// entry of `(v <= a)` has a symbolic formula `v <= a(i, j)`.
template <typename ScalarType, typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(ScalarType() <= typename Derived::Scalar()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator<=(const ScalarType& v, const Derived& a) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return v <= x; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison of two arrays @p a1 and @p a2 using less-than
/// operator (<).
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() <
typename DerivedB::Scalar()),
Formula>,
typename internal::RelationalOpTraits<DerivedA, DerivedB>::ReturnType>
operator<(const DerivedA& a1, const DerivedB& a2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(a1.rows() == a2.rows() && a1.cols() == a2.cols());
return a1.binaryExpr(a2, std::less<void>());
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between an array @p a and a scalar @p v using
/// less-than operator (<). That is, for all i and j, the (i, j)-th
/// entry of `(a < v)` has a symbolic formula `a(i, j) < v`.
template <typename Derived, typename ScalarType>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename Derived::Scalar() < ScalarType()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator<(const Derived& a, const ScalarType& v) {
return a.unaryExpr([&v](const typename Derived::Scalar& x) { return x < v; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between a scalar @p v and an array @p using
/// less-than operator (<). That is, for all i and j, the (i, j)-th
/// entry of `(v < a)` has a symbolic formula `v < a(i, j)`.
template <typename ScalarType, typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(ScalarType() < typename Derived::Scalar()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator<(const ScalarType& v, const Derived& a) {
return a.unaryExpr([&v](const typename Derived::Scalar& x) { return v < x; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison of two arrays @p a1 and @p a2 using
/// greater-than-or-equal operator (>=).
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() >=
typename DerivedB::Scalar()),
Formula>,
typename internal::RelationalOpTraits<DerivedA, DerivedB>::ReturnType>
operator>=(const DerivedA& a1, const DerivedB& a2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(a1.rows() == a2.rows() && a1.cols() == a2.cols());
return a1.binaryExpr(a2, std::greater_equal<void>());
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between an array @p a and a scalar @p v using
/// greater-than-or-equal operator (>=). That is, for all i and j, the (i, j)-th
/// entry of `(a >= v)` has a symbolic formula `a(i, j) >= v`.
template <typename Derived, typename ScalarType>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename Derived::Scalar() >= ScalarType()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator>=(const Derived& a, const ScalarType& v) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return x >= v; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between a scalar @p v and an array @p using
/// less-than-or-equal operator (<=) instead of greater-than-or-equal operator
/// (>=). That is, for all i and j, the (i, j)-th entry of `(v >= a)` has a
/// symbolic formula `a(i, j) <= v`.
///
/// Note that given `v >= a`, this methods returns the result of `a <= v`. First
/// of all, this formulation is mathematically equivalent to the original
/// formulation. We implement this method in this way to be consistent with
/// Eigen's semantics. See the definition of `EIGEN_MAKE_CWISE_COMP_R_OP` in
/// ArrayCwiseBinaryOps.h file in Eigen.
template <typename ScalarType, typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(ScalarType() >= typename Derived::Scalar()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator>=(const ScalarType& v, const Derived& a) {
return a <= v;
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison of two arrays @p a1 and @p a2 using greater-than
/// operator (>).
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() >
typename DerivedB::Scalar()),
Formula>,
typename internal::RelationalOpTraits<DerivedA, DerivedB>::ReturnType>
operator>(const DerivedA& a1, const DerivedB& a2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(a1.rows() == a2.rows() && a1.cols() == a2.cols());
return a1.binaryExpr(a2, std::greater<void>());
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between an array @p a and a scalar @p v using
/// greater-than operator (>). That is, for all i and j, the (i, j)-th
/// entry of `(a > v)` has a symbolic formula `a(i, j) > v`.
template <typename Derived, typename ScalarType>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename Derived::Scalar() > ScalarType()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator>(const Derived& a, const ScalarType& v) {
return a.unaryExpr([&v](const typename Derived::Scalar& x) { return x > v; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between a scalar @p v and an array @p using
/// less-than operator (<) instead of greater-than operator (>). That is, for
/// all i and j, the (i, j)-th entry of `(v > a)` has a symbolic formula `a(i,
/// j) < v`.
///
/// Note that given `v > a`, this methods returns the result of `a < v`. First
/// of all, this formulation is mathematically equivalent to the original
/// formulation. We implement this method in this way to be consistent with
/// Eigen's semantics. See the definition of `EIGEN_MAKE_CWISE_COMP_R_OP` in
/// ArrayCwiseBinaryOps.h file in Eigen.
template <typename ScalarType, typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(ScalarType() > typename Derived::Scalar()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator>(const ScalarType& v, const Derived& a) {
return a < v;
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison of two arrays @p a1 and @p a2 using not-equal
/// operator (!=).
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() !=
typename DerivedB::Scalar()),
Formula>,
typename internal::RelationalOpTraits<DerivedA, DerivedB>::ReturnType>
operator!=(const DerivedA& a1, const DerivedB& a2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(a1.rows() == a2.rows() && a1.cols() == a2.cols());
return a1.binaryExpr(a2, std::not_equal_to<void>());
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between an array @p a and a scalar @p v using
/// not-equal operator (!=). That is, for all i and j, the (i, j)-th
/// entry of `(a != v)` has a symbolic formula `a(i, j) != v`.
template <typename Derived, typename ScalarType>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(typename Derived::Scalar() != ScalarType()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator!=(const Derived& a, const ScalarType& v) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return x != v; });
}
/// Returns an Eigen array of symbolic formulas where each element includes
/// element-wise comparison between a scalar @p v and an array @p using
/// not-equal operator (!=). That is, for all i and j, the (i, j)-th
/// entry of `(v != a)` has a symbolic formula `v != a(i, j)`.
template <typename ScalarType, typename Derived>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<Derived>::XprKind,
Eigen::ArrayXpr> &&
std::is_same_v<decltype(ScalarType() != typename Derived::Scalar()),
Formula>,
Eigen::Array<Formula, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime>>
operator!=(const ScalarType& v, const Derived& a) {
return a.unaryExpr(
[&v](const typename Derived::Scalar& x) { return v != x; });
}
/// Returns a symbolic formula checking if two matrices @p m1 and @p m2 are
/// equal.
///
/// The following table describes the return type of @p m1 == @p m2.
///
/// LHS \ RHS | EM<Expression> | EM<Variable> | EM<double>
/// ----------------|----------------|--------------|------------
/// EM<Expression> | Formula | Formula | Formula
/// EM<Variable> | Formula | Formula | Formula
/// EM<double> | Formula | Formula | bool
///
/// In the table, `EM` is a short-hand of `Eigen::Matrix`.
///
/// Note that this function does *not* provide operator overloading for the
/// following case. It returns `bool` and is provided by Eigen.
///
/// - Eigen::Matrix<double> == Eigen::Matrix<double>
///
/// Note that this method returns a conjunctive formula which keeps its
/// conjuncts as `std::set<Formula>` internally. This set is ordered by
/// `Formula::Less` and this ordering can be *different* from the one in
/// inputs. Also, any duplicated formulas are removed in construction. Please
/// check the following example.
///
/// @code
/// // set up v1 = [y x y] and v2 = [1 2 1]
/// VectorX<Expression> v1{3};
/// VectorX<Expression> v2{3};
/// const Variable x{"x"};
/// const Variable y{"y"};
/// v1 << y, x, y;
/// v2 << 1, 2, 1;
/// // Here v1_eq_v2 = ((x = 2) ∧ (y = 1))
/// const Formula v1_eq_v2{v1 == v2};
/// const std::set<Formula> conjuncts{get_operands(v1_eq_v2)};
/// for (const Formula& f : conjuncts) {
/// std::cerr << f << std::endl;
/// }
/// // The outcome of the above loop is:
/// (x = 2)
/// (y = 1)
/// @endcode
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::MatrixXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::MatrixXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() ==
typename DerivedB::Scalar()),
Formula>,
Formula>
operator==(const DerivedA& m1, const DerivedB& m2) {
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
DRAKE_DEMAND(m1.rows() == m2.rows() && m1.cols() == m2.cols());
return m1.binaryExpr(m2, std::equal_to<void>()).redux(internal::logic_and);
}
/// Returns a symbolic formula representing the condition whether @p m1 and @p
/// m2 are not the same.
///
/// The following table describes the return type of @p m1 != @p m2.
///
/// LHS \ RHS | EM<Expression> | EM<Variable> | EM<double>
/// ----------------|----------------|--------------|------------
/// EM<Expression> | Formula | Formula | Formula
/// EM<Variable> | Formula | Formula | Formula
/// EM<double> | Formula | Formula | bool
///
/// In the table, `EM` is a short-hand of `Eigen::Matrix`.
///
/// Note that this function does *not* provide operator overloading for the
/// following case. It returns `bool` and is provided by Eigen.
///
/// - Eigen::Matrix<double> != Eigen::Matrix<double>
template <typename DerivedA, typename DerivedB>
typename std::enable_if_t<
std::is_same_v<typename Eigen::internal::traits<DerivedA>::XprKind,
Eigen::MatrixXpr> &&
std::is_same_v<typename Eigen::internal::traits<DerivedB>::XprKind,
Eigen::MatrixXpr> &&
std::is_same_v<decltype(typename DerivedA::Scalar() !=
typename DerivedB::Scalar()),
Formula>,
Formula>