7#include <initializer_list>
8#include <source_location>
21#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
31template <
typename Scalar>
36template <
typename Scalar,
int UpLo = Eigen::Lower | Eigen::Upper>
37 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
40template <
typename Scalar>
46template <
typename Scalar_>
56 explicit Variable(std::nullptr_t) : expr{nullptr} {}
100 : expr{
std::move(expr)} {}
109 m_graph_initialized =
false;
118#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
121 if (expr->args[0] !=
nullptr) {
122 auto location = std::source_location::current();
125 "WARNING: {}:{}: {}: Modified the value of a dependent variable",
126 location.file_name(), location.line(), location.function_name());
136 if (!m_graph_initialized) {
138 m_graph_initialized =
true;
156 template <ScalarLike LHS, SleipnirScalarLike<Scalar> RHS>
166 template <SleipnirScalarLike<Scalar> LHS, ScalarLike RHS>
178 return Variable{lhs.expr * rhs.expr};
197 return Variable{lhs.expr / rhs.expr};
216 return Variable{lhs.expr + rhs.expr};
235 return Variable{lhs.expr - rhs.expr};
271 bool m_graph_initialized =
false;
273 template <
typename Scalar>
275 template <
typename Scalar>
277 template <
typename Scalar>
279 template <
typename Scalar>
281 template <
typename Scalar>
284 template <
typename Scalar>
287 template <
typename Scalar>
290 template <
typename Scalar>
292 template <
typename Scalar>
294 template <
typename Scalar>
296 template <
typename Scalar>
298 template <
typename Scalar>
300 template <
typename Scalar>
303 template <
typename Scalar>
306 template <
typename Scalar>
309 template <
typename Scalar>
311 template <
typename Scalar>
313 template <
typename Scalar>
316 template <
typename Scalar>
319 template <
typename Scalar>
322 template <
typename Scalar>
325 template <
typename Scalar>
328 template <
typename Scalar>
331 template <
typename Scalar>
334 template <
typename Scalar>
337 template <
typename Scalar>
340 template <
typename Scalar>
342 template <
typename Scalar>
344 template <
typename Scalar>
346 template <
typename Scalar>
348 template <
typename Scalar>
350 template <
typename Scalar>
352 template <
typename Scalar>
358 template <
typename Scalar,
int UpLo>
359 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
361 template <typename
Scalar>
365template <template <typename> typename T, typename
Scalar>
369template <
std::floating_point T>
372template <
std::integral T>
388template <
typename Scalar>
397template <
typename Scalar>
406template <
typename Scalar>
416template <
typename Scalar>
426template <
typename Scalar>
436template <
typename Scalar>
445template <
typename Scalar>
454template <
typename Scalar>
463template <
typename Scalar>
472template <
typename Scalar>
481template <
typename Scalar>
491template <
typename Scalar>
501template <
typename Scalar>
511template <
typename Scalar>
520template <
typename Scalar>
529template <
typename Scalar>
541template <
typename Scalar>
553template <
typename Scalar>
565template <
typename Scalar>
577template <
typename Scalar>
589template <
typename Scalar>
601template <
typename Scalar>
611template <
typename Scalar>
622template <
typename Scalar>
633template <
typename Scalar>
643template <
typename Scalar>
652template <
typename Scalar>
661template <
typename Scalar>
670template <
typename Scalar>
679template <
typename Scalar>
688template <
typename Scalar>
699template <
typename Scalar>
710template <
typename Scalar, ScalarLike LHS, ScalarLike RHS>
714 constraints.emplace_back(lhs - rhs);
719template <
typename Scalar, ScalarLike LHS, MatrixLike RHS>
720 requires SleipnirScalarLike<LHS, Scalar> || SleipnirMatrixLike<RHS, Scalar>
723 constraints.reserve(rhs.rows() * rhs.cols());
725 for (
int row = 0; row < rhs.rows(); ++row) {
726 for (
int col = 0; col < rhs.cols(); ++col) {
728 constraints.emplace_back(lhs - rhs(row, col));
735template <
typename Scalar, MatrixLike LHS, ScalarLike RHS>
736 requires SleipnirMatrixLike<LHS, Scalar> || SleipnirScalarLike<RHS, Scalar>
739 constraints.reserve(lhs.rows() * lhs.cols());
741 for (
int row = 0; row < lhs.rows(); ++row) {
742 for (
int col = 0; col < lhs.cols(); ++col) {
744 constraints.emplace_back(lhs(row, col) - rhs);
751template <
typename Scalar, MatrixLike LHS, MatrixLike RHS>
752 requires SleipnirMatrixLike<LHS, Scalar> || SleipnirMatrixLike<RHS, Scalar>
754 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
757 constraints.reserve(lhs.rows() * lhs.cols());
759 for (
int row = 0; row < lhs.rows(); ++row) {
760 for (
int col = 0; col < lhs.cols(); ++col) {
762 constraints.emplace_back(lhs(row, col) - rhs(row, col));
772template <
typename Scalar>
782 std::initializer_list<EqualityConstraints> equality_constraints) {
783 for (
const auto& elem : equality_constraints) {
785 elem.constraints.end());
796 const std::vector<EqualityConstraints>& equality_constraints) {
797 for (
const auto& elem : equality_constraints) {
799 elem.constraints.end());
810 template <
typename LHS,
typename RHS>
820 return std::ranges::all_of(
constraints, [](
auto& constraint) {
821 return constraint.value() == Scalar(0);
829template <
typename Scalar>
839 std::initializer_list<InequalityConstraints> inequality_constraints) {
840 for (
const auto& elem : inequality_constraints) {
842 elem.constraints.end());
853 const std::vector<InequalityConstraints>& inequality_constraints) {
854 for (
const auto& elem : inequality_constraints) {
856 elem.constraints.end());
868 template <
typename LHS,
typename RHS>
878 return std::ranges::all_of(
constraints, [](
auto& constraint) {
879 return constraint.value() >= Scalar(0);
888template <
typename LHS,
typename RHS>
889 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
890 (ScalarLike<RHS> || MatrixLike<RHS>) && (!SleipnirType<RHS>)
891auto operator==(LHS&& lhs, RHS&& rhs) {
899template <
typename LHS,
typename RHS>
900 requires(ScalarLike<LHS> || MatrixLike<LHS>) && (!SleipnirType<LHS>) &&
901 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
902auto operator==(LHS&& lhs, RHS&& rhs) {
910template <
typename LHS,
typename RHS>
911 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
912 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
922template <
typename LHS,
typename RHS>
923 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
924 (ScalarLike<RHS> || MatrixLike<RHS>) &&
925 (SleipnirType<LHS> || SleipnirType<RHS>)
926auto operator<(LHS&& lhs, RHS&& rhs) {
935template <
typename LHS,
typename RHS>
936 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
937 (ScalarLike<RHS> || MatrixLike<RHS>) &&
938 (SleipnirType<LHS> || SleipnirType<RHS>)
939auto operator<=(LHS&& lhs, RHS&& rhs) {
948template <
typename LHS,
typename RHS>
949 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
950 (ScalarLike<RHS> || MatrixLike<RHS>) &&
951 (SleipnirType<LHS> || SleipnirType<RHS>)
952auto operator>(LHS&& lhs, RHS&& rhs) {
961template <
typename LHS,
typename RHS>
962 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
963 (ScalarLike<RHS> || MatrixLike<RHS>) && (!SleipnirType<RHS>)
964auto operator>=(LHS&& lhs, RHS&& rhs) {
973template <
typename LHS,
typename RHS>
974 requires(ScalarLike<LHS> || MatrixLike<LHS>) && (!SleipnirType<LHS>) &&
975 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
976auto operator>=(LHS&& lhs, RHS&& rhs) {
985template <
typename LHS,
typename RHS>
986 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
987 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
997template <
typename L,
typename X,
typename U>
998 requires(ScalarLike<L> || MatrixLike<L>) && SleipnirType<X> &&
999 (ScalarLike<U> || MatrixLike<U>)
1012template <
typename Scalar>
1013struct NumTraits<
slp::Variable<Scalar>> : NumTraits<Scalar> {
#define slp_assert(condition)
Aborts in C++.
Definition assert.hpp:25
sign
Definition base.h:689
This class calculates the Hessian of a variable with respect to a vector of variables.
Definition hessian.hpp:28
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition jacobian.hpp:28
Marker interface for concepts to determine whether a given scalar or matrix type belongs to Sleipnir.
Definition sleipnir_base.hpp:9
An autodiff variable pointing to an expression node.
Definition variable.hpp:47
ExpressionType type() const
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition variable.hpp:149
friend Variable< Scalar > operator*(const LHS &lhs, const RHS &rhs)
Variable-scalar multiplication operator.
Definition variable.hpp:157
Variable(const detail::ExpressionPtr< Scalar > &expr)
Constructs a Variable pointing to the specified expression.
Definition variable.hpp:94
friend Variable< Scalar > pow(const ScalarLike auto &base, const Variable< Scalar > &power)
friend Variable< Scalar > abs(const Variable< Scalar > &x)
friend class Jacobian
Definition variable.hpp:362
friend Variable< Scalar > hypot(const Variable< Scalar > &x, const ScalarLike auto &y)
Variable< Scalar > & operator/=(const Variable< Scalar > &rhs)
Variable-Variable compound division operator.
Definition variable.hpp:204
friend Variable< Scalar > log(const Variable< Scalar > &x)
friend Variable< Scalar > erf(const Variable< Scalar > &x)
friend Variable< Scalar > atan(const Variable< Scalar > &x)
friend Variable< Scalar > min(const ScalarLike auto &a, const Variable< Scalar > &b)
friend Variable< Scalar > max(const ScalarLike auto &a, const Variable< Scalar > &b)
Variable< Scalar > & operator*=(const Variable< Scalar > &rhs)
Variable-Variable compound multiplication operator.
Definition variable.hpp:185
friend Variable< Scalar > asin(const Variable< Scalar > &x)
friend Variable< Scalar > exp(const Variable< Scalar > &x)
friend Variable< Scalar > hypot(const ScalarLike auto &x, const Variable< Scalar > &y)
friend Variable< Scalar > sin(const Variable< Scalar > &x)
friend Variable< Scalar > operator+(const Variable< Scalar > &lhs, const Variable< Scalar > &rhs)
Variable-Variable addition operator.
Definition variable.hpp:214
friend Variable< Scalar > atan2(const Variable< Scalar > &y, const Variable< Scalar > &x)
friend class Hessian
Definition variable.hpp:360
void set_value(Scalar value)
Sets Variable's internal value.
Definition variable.hpp:117
friend Variable< Scalar > operator/(const Variable< Scalar > &lhs, const Variable< Scalar > &rhs)
Variable-Variable division operator.
Definition variable.hpp:195
friend Variable< Scalar > log10(const Variable< Scalar > &x)
friend Variable< Scalar > atan2(const Variable< Scalar > &y, const ScalarLike auto &x)
Variable(std::floating_point auto value)
Constructs a Variable from a floating-point type.
Definition variable.hpp:79
friend Variable< Scalar > pow(const Variable< Scalar > &base, const ScalarLike auto &power)
friend Variable< Scalar > cos(const Variable< Scalar > &x)
friend Variable< Scalar > sign(const Variable< Scalar > &x)
friend Variable< Scalar > max(const Variable< Scalar > &a, const ScalarLike auto &b)
friend Variable< Scalar > operator+(const Variable< Scalar > &lhs)
Unary plus operator.
Definition variable.hpp:257
friend Variable< Scalar > tanh(const Variable< Scalar > &x)
Variable(Scalar value)
Constructs a Variable from a scalar type.
Definition variable.hpp:62
friend Variable< Scalar > sinh(const Variable< Scalar > &x)
friend Variable< Scalar > pow(const Variable< Scalar > &base, const Variable< Scalar > &power)
Variable< Scalar > & operator=(ScalarLike auto value)
Assignment operator for scalar.
Definition variable.hpp:106
friend Variable< Scalar > cbrt(const Variable< Scalar > &x)
friend Variable< Scalar > sqrt(const Variable< Scalar > &x)
friend Variable< Scalar > atan2(const ScalarLike auto &y, const Variable< Scalar > &x)
Variable()=default
Constructs a linear Variable with a value of zero.
friend Variable< Scalar > operator-(const Variable< Scalar > &lhs)
Unary minus operator.
Definition variable.hpp:250
friend Variable< Scalar > operator*(const Variable< Scalar > &lhs, const Variable< Scalar > &rhs)
Variable-scalar multiplication operator.
Definition variable.hpp:176
friend Variable< Scalar > acos(const Variable< Scalar > &x)
friend Variable< Scalar > cosh(const Variable< Scalar > &x)
Variable(std::integral auto value)
Constructs a Variable from an integral type.
Definition variable.hpp:87
friend Variable< Scalar > hypot(const Variable< Scalar > &x, const Variable< Scalar > &y)
Variable(detail::ExpressionPtr< Scalar > &&expr)
Constructs a Variable pointing to the specified expression.
Definition variable.hpp:99
Variable(SleipnirMatrixLike< Scalar > auto value)
Constructs a Variable from a scalar type.
Definition variable.hpp:71
Variable< Scalar > & operator+=(const Variable< Scalar > &rhs)
Variable-Variable compound addition operator.
Definition variable.hpp:223
Scalar Scalar
Definition variable.hpp:50
friend Variable< Scalar > tan(const Variable< Scalar > &x)
Scalar value()
Definition variable.hpp:135
Variable< Scalar > & operator-=(const Variable< Scalar > &rhs)
Variable-Variable compound subtraction operator.
Definition variable.hpp:242
friend Variable< Scalar > operator-(const Variable< Scalar > &lhs, const Variable< Scalar > &rhs)
Variable-Variable subtraction operator.
Definition variable.hpp:233
friend Variable< Scalar > min(const Variable< Scalar > &a, const Variable< Scalar > &b)
friend Variable< Scalar > max(const Variable< Scalar > &a, const Variable< Scalar > &b)
friend Variable< Scalar > min(const Variable< Scalar > &a, const ScalarLike auto &b)
Variable(std::nullptr_t)
Constructs an empty Variable.
Definition variable.hpp:56
This class is an adapter type that performs value updates of an expression graph, generates a gradien...
Definition gradient_expression_graph.hpp:25
Definition concepts.hpp:18
Definition concepts.hpp:24
Definition concepts.hpp:33
Definition concepts.hpp:38
Definition concepts.hpp:15
Definition variable.hpp:1006
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
@ sign
Definition base.h:1467
wpi::util::SmallVector< T > small_vector
Definition small_vector.hpp:10
Definition expression_graph.hpp:11
ExpressionPtr< Scalar > cosh(const ExpressionPtr< Scalar > &x)
cosh() for Expressions.
Definition expression.hpp:1164
ExpressionPtr< Scalar > cos(const ExpressionPtr< Scalar > &x)
cos() for Expressions.
Definition expression.hpp:1110
ExpressionPtr< Scalar > cbrt(const ExpressionPtr< Scalar > &x)
cbrt() for Expressions.
Definition expression.hpp:554
ExpressionPtr< Scalar > sin(const ExpressionPtr< Scalar > &x)
sin() for Expressions.
Definition expression.hpp:1825
ExpressionPtr< Scalar > hypot(const ExpressionPtr< Scalar > &x, const ExpressionPtr< Scalar > &y)
hypot() for Expressions.
Definition expression.hpp:1347
ExpressionPtr< Scalar > pow(const ExpressionPtr< Scalar > &base, const ExpressionPtr< Scalar > &power)
pow() for Expressions.
Definition expression.hpp:1704
ExpressionPtr< Scalar > sqrt(const ExpressionPtr< Scalar > &x)
sqrt() for Expressions.
Definition expression.hpp:1935
ExpressionPtr< Scalar > sinh(const ExpressionPtr< Scalar > &x)
sinh() for Expressions.
Definition expression.hpp:1880
void update_values(const gch::small_vector< Expression< Scalar > * > &list)
Updates the values of all nodes in this graph based on the values of their dependent nodes.
Definition expression_graph.hpp:77
ExpressionPtr< Scalar > min(const ExpressionPtr< Scalar > &a, const ExpressionPtr< Scalar > &b)
min() for Expressions.
Definition expression.hpp:1621
ExpressionPtr< Scalar > erf(const ExpressionPtr< Scalar > &x)
erf() for Expressions.
Definition expression.hpp:1220
ExpressionPtr< Scalar > tan(const ExpressionPtr< Scalar > &x)
tan() for Expressions.
Definition expression.hpp:1994
ExpressionPtr< Scalar > abs(const ExpressionPtr< Scalar > &x)
abs() for Expressions.
Definition expression.hpp:822
ExpressionPtr< Scalar > tanh(const ExpressionPtr< Scalar > &x)
tanh() for Expressions.
Definition expression.hpp:2052
ExpressionPtr< Scalar > exp(const ExpressionPtr< Scalar > &x)
exp() for Expressions.
Definition expression.hpp:1275
ExpressionPtr< Scalar > asin(const ExpressionPtr< Scalar > &x)
asin() for Expressions.
Definition expression.hpp:931
ExpressionPtr< Scalar > log(const ExpressionPtr< Scalar > &x)
log() for Expressions.
Definition expression.hpp:1403
ExpressionPtr< Scalar > acos(const ExpressionPtr< Scalar > &x)
acos() for Expressions.
Definition expression.hpp:877
gch::small_vector< Expression< Scalar > * > topological_sort(const ExpressionPtr< Scalar > &root)
Generates a topological sort of an expression graph from parent to child.
Definition expression_graph.hpp:20
ExpressionPtr< Scalar > max(const ExpressionPtr< Scalar > &a, const ExpressionPtr< Scalar > &b)
max() for Expressions.
Definition expression.hpp:1541
ExpressionPtr< Scalar > atan(const ExpressionPtr< Scalar > &x)
atan() for Expressions.
Definition expression.hpp:985
IntrusiveSharedPtr< Expression< Scalar > > ExpressionPtr
Typedef for intrusive shared pointer to Expression.
Definition expression.hpp:43
ExpressionPtr< Scalar > atan2(const ExpressionPtr< Scalar > &y, const ExpressionPtr< Scalar > &x)
atan2() for Expressions.
Definition expression.hpp:1052
static ExpressionPtr< typename T::Scalar > make_expression_ptr(Args &&... args)
Creates an intrusive shared pointer to an expression from the global pool allocator.
Definition expression.hpp:51
ExpressionPtr< Scalar > log10(const ExpressionPtr< Scalar > &x)
log10() for Expressions.
Definition expression.hpp:1457
Definition to_underlying.hpp:7
ExpressionType
Expression type.
Definition expression_type.hpp:16
auto operator==(LHS &&lhs, RHS &&rhs)
Equality operator that returns an equality constraint for two Variables.
Definition variable.hpp:891
auto make_constraints(LHS &&lhs, RHS &&rhs)
Definition variable.hpp:712
auto bounds(L &&l, X &&x, U &&u)
Helper function for creating bound constraints.
Definition variable.hpp:1000
auto operator>=(LHS &&lhs, RHS &&rhs)
Greater-than-or-equal-to comparison operator that returns an inequality constraint for two Variables.
Definition variable.hpp:964
void println(fmt::format_string< T... > fmt, T &&... args)
Wrapper around fmt::println() that squelches write failure exceptions.
Definition print.hpp:37
Definition StringMap.hpp:773
static constexpr int IsInteger
Is integer.
Definition variable.hpp:1024
static constexpr int AddCost
Add cost.
Definition variable.hpp:1032
static constexpr int ReadCost
Read cost.
Definition variable.hpp:1030
slp::Variable< Scalar > Real
Real type.
Definition variable.hpp:1015
static constexpr int RequireInitialization
Require initialization.
Definition variable.hpp:1028
static constexpr int MulCost
Multiply cost.
Definition variable.hpp:1034
static constexpr int IsComplex
Is complex.
Definition variable.hpp:1022
static constexpr int IsSigned
Is signed.
Definition variable.hpp:1026
slp::Variable< Scalar > Nested
Nested type.
Definition variable.hpp:1019
slp::Variable< Scalar > NonInteger
Non-integer type.
Definition variable.hpp:1017
A vector of equality constraints of the form cₑ(x) = 0.
Definition variable.hpp:773
EqualityConstraints(LHS &&lhs, RHS &&rhs)
Constructs an equality constraint from a left and right side.
Definition variable.hpp:814
EqualityConstraints(std::initializer_list< EqualityConstraints > equality_constraints)
Concatenates multiple equality constraints.
Definition variable.hpp:781
gch::small_vector< Variable< Scalar > > constraints
A vector of scalar equality constraints.
Definition variable.hpp:775
EqualityConstraints(const std::vector< EqualityConstraints > &equality_constraints)
Concatenates multiple equality constraints.
Definition variable.hpp:795
A vector of inequality constraints of the form cᵢ(x) ≥ 0.
Definition variable.hpp:830
gch::small_vector< Variable< Scalar > > constraints
A vector of scalar inequality constraints.
Definition variable.hpp:832
InequalityConstraints(LHS &&lhs, RHS &&rhs)
Constructs an inequality constraint from a left and right side.
Definition variable.hpp:872
InequalityConstraints(const std::vector< InequalityConstraints > &inequality_constraints)
Concatenates multiple inequality constraints.
Definition variable.hpp:852
InequalityConstraints(std::initializer_list< InequalityConstraints > inequality_constraints)
Concatenates multiple inequality constraints.
Definition variable.hpp:838