Drake
cond.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <type_traits>
5 
7 
8 namespace drake {
9 /** @name cond
10  Constructs conditional expression (similar to Lisp's cond).
11 
12  @verbatim
13  cond(cond_1, expr_1,
14  cond_2, expr_2,
15  ..., ...,
16  cond_n, expr_n,
17  expr_{n+1})
18  @endverbatim
19 
20  The value returned by the above cond expression is @c expr_1 if @c cond_1 is
21  true; else if @c cond_2 is true then @c expr_2; ... ; else if @c cond_n is
22  true then @c expr_n. If none of the conditions are true, it returns @c
23  expr_{n+1}.
24 
25  @note This functions assumes that @p ScalarType provides @c operator< and the
26  type of @c f_cond is the type of the return type of <tt>operator<(ScalarType,
27  ScalarType)</tt>. For example, @c symbolic::Expression can be used as a @p
28  ScalarType because it provides <tt>symbolic::Formula
29  operator<(symbolic::Expression, symbolic::Expression)</tt>.
30 
31  */
32 ///@{
33 template <typename ScalarType>
34 ScalarType cond(const ScalarType& e) {
35  return e;
36 }
37 template <typename ScalarType, typename... Rest>
38 ScalarType cond(const decltype(ScalarType() < ScalarType()) & f_cond,
39  const ScalarType& e_then, Rest... rest) {
40  return if_then_else(f_cond, e_then, cond(rest...));
41 }
42 ///@}
43 } // namespace drake
Definition: automotive_demo.cc:88
Eigen::AutoDiffScalar< typename Eigen::internal::remove_all< DerType1 >::type::PlainObject > if_then_else(bool f_cond, const Eigen::AutoDiffScalar< DerType1 > &x, const Eigen::AutoDiffScalar< DerType2 > &y)
Provides if-then-else expression for Eigen::AutoDiffScalar type.
Definition: autodiff_overloads.h:168
Provides necessary operations on double to have it as a ScalarType in drake.
Eigen::AutoDiffScalar< typename Eigen::internal::remove_all< DerType >::type::PlainObject > cond(bool f_cond, const Eigen::AutoDiffScalar< DerType > &e_then, Rest...rest)
Provides special case of cond expression for Eigen::AutoDiffScalar type.
Definition: autodiff_overloads.h:185