7#include <initializer_list>
65 expr = detail::MakeExpressionPtr(value);
76 if (expr->IsConstant(0.0)) {
77 expr = detail::MakeExpressionPtr(value);
81 if (expr->args[0] !=
nullptr && !expr->args[0]->IsConstant(0.0)) {
84 "WARNING: {}:{}: Modified the value of a dependent variable",
99 return Variable{lhs.expr * rhs.expr};
120 return Variable{lhs.expr / rhs.expr};
141 return Variable{lhs.expr + rhs.expr};
162 return Variable{lhs.expr - rhs.expr};
213 detail::MakeExpressionPtr(0.0, ExpressionType::kLinear);
443template <
typename LHS,
typename RHS>
446 (!std::same_as<std::decay_t<LHS>,
double> ||
447 !std::same_as<std::decay_t<RHS>,
double>)
466 constraints.
reserve(rows * cols);
468 for (
int row = 0; row < rows; ++row) {
469 for (
int col = 0; col < cols; ++col) {
486 constraints.
reserve(rows * cols);
488 for (
int row = 0; row < rows; ++row) {
489 for (
int col = 0; col < cols; ++col) {
499 lhsRows = lhs.rows();
500 lhsCols = lhs.cols();
502 lhsRows = lhs.Rows();
503 lhsCols = lhs.Cols();
511 rhsRows = rhs.rows();
512 rhsCols = rhs.cols();
514 rhsRows = rhs.Rows();
515 rhsCols = rhs.Cols();
518 Assert(lhsRows == rhsRows && lhsCols == rhsCols);
519 constraints.
reserve(lhsRows * lhsCols);
521 for (
int row = 0; row < lhsRows; ++row) {
522 for (
int col = 0; col < lhsCols; ++col) {
524 constraints.
emplace_back(lhs(row, col) - rhs(row, col));
545 std::initializer_list<EqualityConstraints> equalityConstraints) {
546 for (
const auto& elem : equalityConstraints) {
547 constraints.
insert(constraints.
end(), elem.constraints.begin(),
548 elem.constraints.end());
560 const std::vector<EqualityConstraints>& equalityConstraints) {
561 for (
const auto& elem : equalityConstraints) {
562 constraints.
insert(constraints.
end(), elem.constraints.begin(),
563 elem.constraints.end());
576 template <
typename LHS,
typename RHS>
579 (!std::same_as<std::decay_t<LHS>,
double> ||
580 !std::same_as<std::decay_t<RHS>,
double>)
589 constraints.
begin(), constraints.
end(),
590 [](
auto& constraint) { return constraint.Value() == 0.0; });
608 std::initializer_list<InequalityConstraints> inequalityConstraints) {
609 for (
const auto& elem : inequalityConstraints) {
610 constraints.
insert(constraints.
end(), elem.constraints.begin(),
611 elem.constraints.end());
624 const std::vector<InequalityConstraints>& inequalityConstraints) {
625 for (
const auto& elem : inequalityConstraints) {
626 constraints.
insert(constraints.
end(), elem.constraints.begin(),
627 elem.constraints.end());
640 template <
typename LHS,
typename RHS>
643 (!std::same_as<std::decay_t<LHS>,
double> ||
644 !std::same_as<std::decay_t<RHS>,
double>)
653 constraints.
begin(), constraints.
end(),
654 [](
auto& constraint) { return constraint.Value() >= 0.0; });
664template <
typename LHS,
typename RHS>
665 requires(ScalarLike<std::decay_t<LHS>> || MatrixLike<std::decay_t<LHS>>) &&
666 (ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
667 (!std::same_as<std::decay_t<LHS>,
double> ||
668 !std::same_as<std::decay_t<RHS>,
double>)
680template <
typename LHS,
typename RHS>
681 requires(ScalarLike<std::decay_t<LHS>> || MatrixLike<std::decay_t<LHS>>) &&
682 (ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
683 (!std::same_as<std::decay_t<LHS>,
double> ||
684 !std::same_as<std::decay_t<RHS>,
double>)
696template <
typename LHS,
typename RHS>
697 requires(ScalarLike<std::decay_t<LHS>> || MatrixLike<std::decay_t<LHS>>) &&
698 (ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
699 (!std::same_as<std::decay_t<LHS>,
double> ||
700 !std::same_as<std::decay_t<RHS>,
double>)
712template <
typename LHS,
typename RHS>
713 requires(ScalarLike<std::decay_t<LHS>> || MatrixLike<std::decay_t<LHS>>) &&
714 (ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
715 (!std::same_as<std::decay_t<LHS>,
double> ||
716 !std::same_as<std::decay_t<RHS>,
double>)
728template <
typename LHS,
typename RHS>
729 requires(ScalarLike<std::decay_t<LHS>> || MatrixLike<std::decay_t<LHS>>) &&
730 (ScalarLike<std::decay_t<RHS>> || MatrixLike<std::decay_t<RHS>>) &&
731 (!std::same_as<std::decay_t<LHS>,
double> ||
732 !std::same_as<std::decay_t<RHS>,
double>)
745struct NumTraits<
sleipnir::Variable> : NumTraits<double> {
754 RequireInitialization = 1,
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class calculates the Hessian of a variable with respect to a vector of variables.
Definition Hessian.hpp:27
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition Jacobian.hpp:25
An autodiff variable pointing to an expression node.
Definition Variable.hpp:31
Variable(detail::ExpressionPtr &&expr)
Constructs a Variable pointing to the specified expression.
Definition Variable.hpp:57
friend SLEIPNIR_DLLEXPORT Variable operator+(const Variable &lhs)
Unary plus operator.
Definition Variable.hpp:189
friend SLEIPNIR_DLLEXPORT Variable operator*(const Variable &lhs, const Variable &rhs)
Variable-Variable multiplication operator.
Definition Variable.hpp:97
Variable & operator*=(const Variable &rhs)
Variable-Variable compound multiplication operator.
Definition Variable.hpp:107
friend SLEIPNIR_DLLEXPORT Variable operator-(const Variable &lhs)
Unary minus operator.
Definition Variable.hpp:180
friend SLEIPNIR_DLLEXPORT Variable operator+(const Variable &lhs, const Variable &rhs)
Variable-Variable addition operator.
Definition Variable.hpp:139
friend SLEIPNIR_DLLEXPORT Variable operator-(const Variable &lhs, const Variable &rhs)
Variable-Variable subtraction operator.
Definition Variable.hpp:160
ExpressionType Type() const
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition Variable.hpp:208
Variable & operator=(double value)
Assignment operator for double.
Definition Variable.hpp:64
Variable & operator-=(const Variable &rhs)
Variable-Variable compound subtraction operator.
Definition Variable.hpp:170
Variable(const detail::ExpressionPtr &expr)
Constructs a Variable pointing to the specified expression.
Definition Variable.hpp:50
friend SLEIPNIR_DLLEXPORT Variable operator/(const Variable &lhs, const Variable &rhs)
Variable-Variable division operator.
Definition Variable.hpp:118
double Value()
Returns the value of this variable.
Definition Variable.hpp:196
Variable & operator+=(const Variable &rhs)
Variable-Variable compound addition operator.
Definition Variable.hpp:149
Variable(double value)
Constructs a Variable from a double.
Definition Variable.hpp:43
Variable & operator/=(const Variable &rhs)
Variable-Variable compound division operator.
Definition Variable.hpp:128
void SetValue(double value)
Sets Variable's internal value.
Definition Variable.hpp:75
Variable()=default
Constructs a linear Variable with a value of zero.
This class is an adaptor type that performs value updates of an expression's computational graph in a...
Definition ExpressionGraph.hpp:19
void Update()
Update the values of all nodes in this computational tree based on the values of their dependent node...
Definition ExpressionGraph.hpp:99
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:953
iterator insert(iterator I, T &&Elt)
Definition SmallVector.h:821
void reserve(size_type N)
Definition SmallVector.h:679
iterator begin()
Definition SmallVector.h:283
iterator end()
Definition SmallVector.h:285
Definition Concepts.hpp:25
Definition Concepts.hpp:28
Definition Concepts.hpp:12
Definition Variable.hpp:739
detail namespace with internal helper functions
Definition input_adapters.h:32
SLEIPNIR_DLLEXPORT ExpressionPtr sin(const ExpressionPtr &x)
std::sin() for Expressions.
Definition Expression.hpp:987
SLEIPNIR_DLLEXPORT ExpressionPtr tan(const ExpressionPtr &x)
std::tan() for Expressions.
Definition Expression.hpp:1084
SLEIPNIR_DLLEXPORT ExpressionPtr abs(const ExpressionPtr &x)
std::abs() for Expressions.
Definition Expression.hpp:471
SLEIPNIR_DLLEXPORT ExpressionPtr log10(const ExpressionPtr &x)
std::log10() for Expressions.
Definition Expression.hpp:852
SLEIPNIR_DLLEXPORT ExpressionPtr asin(const ExpressionPtr &x)
std::asin() for Expressions.
Definition Expression.hpp:548
SLEIPNIR_DLLEXPORT ExpressionPtr tanh(const ExpressionPtr &x)
std::tanh() for Expressions.
Definition Expression.hpp:1117
SLEIPNIR_DLLEXPORT ExpressionPtr exp(const ExpressionPtr &x)
std::exp() for Expressions.
Definition Expression.hpp:752
SLEIPNIR_DLLEXPORT ExpressionPtr sinh(const ExpressionPtr &x)
std::sinh() for Expressions.
Definition Expression.hpp:1019
SLEIPNIR_DLLEXPORT ExpressionPtr log(const ExpressionPtr &x)
std::log() for Expressions.
Definition Expression.hpp:824
SLEIPNIR_DLLEXPORT ExpressionPtr hypot(const ExpressionPtr &x, const ExpressionPtr &y)
std::hypot() for Expressions.
Definition Expression.hpp:784
SLEIPNIR_DLLEXPORT ExpressionPtr cosh(const ExpressionPtr &x)
std::cosh() for Expressions.
Definition Expression.hpp:686
SLEIPNIR_DLLEXPORT ExpressionPtr acos(const ExpressionPtr &x)
std::acos() for Expressions.
Definition Expression.hpp:516
SLEIPNIR_DLLEXPORT ExpressionPtr pow(const ExpressionPtr &base, const ExpressionPtr &power)
std::pow() for Expressions.
Definition Expression.hpp:885
SLEIPNIR_DLLEXPORT ExpressionPtr atan2(const ExpressionPtr &y, const ExpressionPtr &x)
std::atan2() for Expressions.
Definition Expression.hpp:614
SLEIPNIR_DLLEXPORT ExpressionPtr atan(const ExpressionPtr &x)
std::atan() for Expressions.
Definition Expression.hpp:581
SLEIPNIR_DLLEXPORT ExpressionPtr cos(const ExpressionPtr &x)
std::cos() for Expressions.
Definition Expression.hpp:655
SLEIPNIR_DLLEXPORT ExpressionPtr sqrt(const ExpressionPtr &x)
std::sqrt() for Expressions.
Definition Expression.hpp:1050
SLEIPNIR_DLLEXPORT ExpressionPtr erf(const ExpressionPtr &x)
std::erf() for Expressions.
Definition Expression.hpp:717
Definition Hessian.hpp:18
void println(fmt::format_string< T... > fmt, T &&... args)
Wrapper around fmt::println() that squelches write failure exceptions.
Definition Print.hpp:43
SLEIPNIR_DLLEXPORT Variable sin(const Variable &x)
std::sin() for Variables.
Definition Variable.hpp:379
SLEIPNIR_DLLEXPORT Variable tanh(const Variable &x)
std::tanh() for Variables.
Definition Variable.hpp:415
SLEIPNIR_DLLEXPORT Variable asin(const Variable &x)
std::asin() for Variables.
Definition Variable.hpp:267
SLEIPNIR_DLLEXPORT Variable cosh(const Variable &x)
std::cosh() for Variables.
Definition Variable.hpp:304
ExpressionType
Expression type.
Definition ExpressionType.hpp:14
SLEIPNIR_DLLEXPORT Variable atan2(const Variable &y, const Variable &x)
std::atan2() for Variables.
Definition Variable.hpp:286
SLEIPNIR_DLLEXPORT Variable erf(const Variable &x)
std::erf() for Variables.
Definition Variable.hpp:313
SLEIPNIR_DLLEXPORT Variable sinh(const Variable &x)
std::sinh() for Variables.
Definition Variable.hpp:388
SLEIPNIR_DLLEXPORT Variable abs(const Variable &x)
std::abs() for Variables.
Definition Variable.hpp:249
SLEIPNIR_DLLEXPORT Variable log(const Variable &x)
std::log() for Variables.
Definition Variable.hpp:352
SLEIPNIR_DLLEXPORT Variable pow(const Variable &base, const Variable &power)
std::pow() for Variables.
Definition Variable.hpp:342
SLEIPNIR_DLLEXPORT Variable hypot(const Variable &x, const Variable &y)
std::hypot() for Variables.
Definition Variable.hpp:332
SLEIPNIR_DLLEXPORT Variable atan(const Variable &x)
std::atan() for Variables.
Definition Variable.hpp:276
SLEIPNIR_DLLEXPORT Variable cos(const Variable &x)
std::cos() for Variables.
Definition Variable.hpp:295
SLEIPNIR_DLLEXPORT Variable sqrt(const Variable &x)
std::sqrt() for Variables.
Definition Variable.hpp:397
SLEIPNIR_DLLEXPORT Variable log10(const Variable &x)
std::log10() for Variables.
Definition Variable.hpp:361
SLEIPNIR_DLLEXPORT Variable acos(const Variable &x)
std::acos() for Variables.
Definition Variable.hpp:258
wpi::SmallVector< Variable > MakeConstraints(LHS &&lhs, RHS &&rhs)
Make a list of constraints.
Definition Variable.hpp:448
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
A vector of equality constraints of the form cₑ(x) = 0.
Definition Variable.hpp:535
EqualityConstraints(std::initializer_list< EqualityConstraints > equalityConstraints)
Concatenates multiple equality constraints.
Definition Variable.hpp:544
EqualityConstraints(LHS &&lhs, RHS &&rhs)
Constructs an equality constraint from a left and right side.
Definition Variable.hpp:581
EqualityConstraints(const std::vector< EqualityConstraints > &equalityConstraints)
Concatenates multiple equality constraints.
Definition Variable.hpp:559
wpi::SmallVector< Variable > constraints
A vector of scalar equality constraints.
Definition Variable.hpp:537
A vector of inequality constraints of the form cᵢ(x) ≥ 0.
Definition Variable.hpp:597
wpi::SmallVector< Variable > constraints
A vector of scalar inequality constraints.
Definition Variable.hpp:599
InequalityConstraints(std::initializer_list< InequalityConstraints > inequalityConstraints)
Concatenates multiple inequality constraints.
Definition Variable.hpp:607
InequalityConstraints(LHS &&lhs, RHS &&rhs)
Constructs an inequality constraint from a left and right side.
Definition Variable.hpp:645
InequalityConstraints(const std::vector< InequalityConstraints > &inequalityConstraints)
Concatenates multiple inequality constraints.
Definition Variable.hpp:623
#define Assert(condition)
Abort in C++.
Definition Assert.hpp:24
sign
Definition base.h:685