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>
324 template <
typename Scalar>
326 template <
typename Scalar>
328 template <
typename Scalar>
330 template <
typename Scalar>
332 template <
typename Scalar>
334 template <
typename Scalar>
340 template <
typename Scalar,
int UpLo>
341 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
343 template <typename
Scalar>
347template <template <typename> typename T, typename
Scalar>
351template <
std::floating_point T>
354template <
std::integral T>
370template <
typename Scalar>
379template <
typename Scalar>
388template <
typename Scalar>
398template <
typename Scalar>
408template <
typename Scalar>
418template <
typename Scalar>
427template <
typename Scalar>
436template <
typename Scalar>
445template <
typename Scalar>
454template <
typename Scalar>
463template <
typename Scalar>
473template <
typename Scalar>
483template <
typename Scalar>
493template <
typename Scalar>
502template <
typename Scalar>
511template <
typename Scalar>
521template <
typename Scalar>
532template <
typename Scalar>
543template <
typename Scalar>
553template <
typename Scalar>
562template <
typename Scalar>
571template <
typename Scalar>
580template <
typename Scalar>
589template <
typename Scalar>
598template <
typename Scalar>
609template <
typename Scalar>
620template <
typename Scalar, ScalarLike LHS, ScalarLike RHS>
624 constraints.emplace_back(lhs - rhs);
629template <
typename Scalar, ScalarLike LHS, MatrixLike RHS>
630 requires SleipnirScalarLike<LHS, Scalar> || SleipnirMatrixLike<RHS, Scalar>
633 constraints.reserve(rhs.rows() * rhs.cols());
635 for (
int row = 0; row < rhs.rows(); ++row) {
636 for (
int col = 0; col < rhs.cols(); ++col) {
638 constraints.emplace_back(lhs - rhs(row, col));
645template <
typename Scalar, MatrixLike LHS, ScalarLike RHS>
646 requires SleipnirMatrixLike<LHS, Scalar> || SleipnirScalarLike<RHS, Scalar>
649 constraints.reserve(lhs.rows() * lhs.cols());
651 for (
int row = 0; row < lhs.rows(); ++row) {
652 for (
int col = 0; col < lhs.cols(); ++col) {
654 constraints.emplace_back(lhs(row, col) - rhs);
661template <
typename Scalar, MatrixLike LHS, MatrixLike RHS>
662 requires SleipnirMatrixLike<LHS, Scalar> || SleipnirMatrixLike<RHS, Scalar>
664 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
667 constraints.reserve(lhs.rows() * lhs.cols());
669 for (
int row = 0; row < lhs.rows(); ++row) {
670 for (
int col = 0; col < lhs.cols(); ++col) {
672 constraints.emplace_back(lhs(row, col) - rhs(row, col));
682template <
typename Scalar>
692 std::initializer_list<EqualityConstraints> equality_constraints) {
693 for (
const auto& elem : equality_constraints) {
695 elem.constraints.end());
706 const std::vector<EqualityConstraints>& equality_constraints) {
707 for (
const auto& elem : equality_constraints) {
709 elem.constraints.end());
720 template <
typename LHS,
typename RHS>
730 return std::ranges::all_of(
constraints, [](
auto& constraint) {
731 return constraint.value() == Scalar(0);
739template <
typename Scalar>
749 std::initializer_list<InequalityConstraints> inequality_constraints) {
750 for (
const auto& elem : inequality_constraints) {
752 elem.constraints.end());
763 const std::vector<InequalityConstraints>& inequality_constraints) {
764 for (
const auto& elem : inequality_constraints) {
766 elem.constraints.end());
778 template <
typename LHS,
typename RHS>
788 return std::ranges::all_of(
constraints, [](
auto& constraint) {
789 return constraint.value() >= Scalar(0);
798template <
typename LHS,
typename RHS>
799 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
800 (ScalarLike<RHS> || MatrixLike<RHS>) && (!SleipnirType<RHS>)
801auto operator==(LHS&& lhs, RHS&& rhs) {
809template <
typename LHS,
typename RHS>
810 requires(ScalarLike<LHS> || MatrixLike<LHS>) && (!SleipnirType<LHS>) &&
811 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
812auto operator==(LHS&& lhs, RHS&& rhs) {
820template <
typename LHS,
typename RHS>
821 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
822 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
832template <
typename LHS,
typename RHS>
833 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
834 (ScalarLike<RHS> || MatrixLike<RHS>) &&
835 (SleipnirType<LHS> || SleipnirType<RHS>)
836auto operator<(LHS&& lhs, RHS&& rhs) {
845template <
typename LHS,
typename RHS>
846 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
847 (ScalarLike<RHS> || MatrixLike<RHS>) &&
848 (SleipnirType<LHS> || SleipnirType<RHS>)
849auto operator<=(LHS&& lhs, RHS&& rhs) {
858template <
typename LHS,
typename RHS>
859 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
860 (ScalarLike<RHS> || MatrixLike<RHS>) &&
861 (SleipnirType<LHS> || SleipnirType<RHS>)
862auto operator>(LHS&& lhs, RHS&& rhs) {
871template <
typename LHS,
typename RHS>
872 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
873 (ScalarLike<RHS> || MatrixLike<RHS>) && (!SleipnirType<RHS>)
874auto operator>=(LHS&& lhs, RHS&& rhs) {
883template <
typename LHS,
typename RHS>
884 requires(ScalarLike<LHS> || MatrixLike<LHS>) && (!SleipnirType<LHS>) &&
885 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
886auto operator>=(LHS&& lhs, RHS&& rhs) {
895template <
typename LHS,
typename RHS>
896 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
897 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
907template <
typename L,
typename X,
typename U>
908 requires(ScalarLike<L> || MatrixLike<L>) && SleipnirType<X> &&
909 (ScalarLike<U> || MatrixLike<U>)
922template <
typename Scalar>
923struct NumTraits<
slp::Variable<Scalar>> : NumTraits<Scalar> {
#define slp_assert(condition)
Abort 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:344
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)
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:342
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 > 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
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:916
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:1666
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:1545
ExpressionPtr< Scalar > sqrt(const ExpressionPtr< Scalar > &x)
sqrt() for Expressions.
Definition expression.hpp:1776
ExpressionPtr< Scalar > sinh(const ExpressionPtr< Scalar > &x)
sinh() for Expressions.
Definition expression.hpp:1721
void update_values(const gch::small_vector< Expression< Scalar > * > &list)
Update the values of all nodes in this graph based on the values of their dependent nodes.
Definition expression_graph.hpp:77
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:1835
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:1893
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)
Generate a topological sort of an expression graph from parent to child.
Definition expression_graph.hpp:20
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 expression_graph.hpp:11
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:801
auto make_constraints(LHS &&lhs, RHS &&rhs)
Definition variable.hpp:622
auto bounds(L &&l, X &&x, U &&u)
Helper function for creating bound constraints.
Definition variable.hpp:910
auto operator>=(LHS &&lhs, RHS &&rhs)
Greater-than-or-equal-to comparison operator that returns an inequality constraint for two Variables.
Definition variable.hpp:874
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:934
static constexpr int AddCost
Add cost.
Definition variable.hpp:942
static constexpr int ReadCost
Read cost.
Definition variable.hpp:940
slp::Variable< Scalar > Real
Real type.
Definition variable.hpp:925
static constexpr int RequireInitialization
Require initialization.
Definition variable.hpp:938
static constexpr int MulCost
Multiply cost.
Definition variable.hpp:944
static constexpr int IsComplex
Is complex.
Definition variable.hpp:932
static constexpr int IsSigned
Is signed.
Definition variable.hpp:936
slp::Variable< Scalar > Nested
Nested type.
Definition variable.hpp:929
slp::Variable< Scalar > NonInteger
Non-integer type.
Definition variable.hpp:927
A vector of equality constraints of the form cₑ(x) = 0.
Definition variable.hpp:683
EqualityConstraints(LHS &&lhs, RHS &&rhs)
Constructs an equality constraint from a left and right side.
Definition variable.hpp:724
EqualityConstraints(std::initializer_list< EqualityConstraints > equality_constraints)
Concatenates multiple equality constraints.
Definition variable.hpp:691
gch::small_vector< Variable< Scalar > > constraints
A vector of scalar equality constraints.
Definition variable.hpp:685
EqualityConstraints(const std::vector< EqualityConstraints > &equality_constraints)
Concatenates multiple equality constraints.
Definition variable.hpp:705
A vector of inequality constraints of the form cᵢ(x) ≥ 0.
Definition variable.hpp:740
gch::small_vector< Variable< Scalar > > constraints
A vector of scalar inequality constraints.
Definition variable.hpp:742
InequalityConstraints(LHS &&lhs, RHS &&rhs)
Constructs an inequality constraint from a left and right side.
Definition variable.hpp:782
InequalityConstraints(const std::vector< InequalityConstraints > &inequality_constraints)
Concatenates multiple inequality constraints.
Definition variable.hpp:762
InequalityConstraints(std::initializer_list< InequalityConstraints > inequality_constraints)
Concatenates multiple inequality constraints.
Definition variable.hpp:748