Sleipnir C++ API
Loading...
Searching...
No Matches
variable.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <concepts>
7#include <initializer_list>
8#include <optional>
9#include <source_location>
10#include <type_traits>
11#include <utility>
12#include <vector>
13
14#include <Eigen/Core>
15
16#include "sleipnir/autodiff/expression.hpp"
17#include "sleipnir/autodiff/expression_graph.hpp"
18#include "sleipnir/util/assert.hpp"
19#include "sleipnir/util/concepts.hpp"
20#include "sleipnir/util/small_vector.hpp"
21#include "sleipnir/util/symbol_exports.hpp"
22
23#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
24#include "sleipnir/util/print.hpp"
25#endif
26
27namespace slp {
28
29// Forward declarations for friend declarations in Variable
30namespace detail {
31class AdjointExpressionGraph;
32} // namespace detail
33template <int UpLo = Eigen::Lower | Eigen::Upper>
34 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
35class SLEIPNIR_DLLEXPORT Hessian;
36class SLEIPNIR_DLLEXPORT Jacobian;
37
41class SLEIPNIR_DLLEXPORT Variable {
42 public:
46 Variable() = default;
47
51 explicit constexpr Variable(std::nullptr_t) : expr{nullptr} {}
52
58 Variable(std::floating_point auto value) // NOLINT
59 : expr{detail::make_expression_ptr<detail::ConstExpression>(value)} {}
60
66 Variable(std::integral auto value) // NOLINT
67 : expr{detail::make_expression_ptr<detail::ConstExpression>(value)} {}
68
74 explicit Variable(const detail::ExpressionPtr& expr) : expr{expr} {}
75
81 explicit constexpr Variable(detail::ExpressionPtr&& expr)
82 : expr{std::move(expr)} {}
83
90 Variable& operator=(double value) {
91 expr = detail::make_expression_ptr<detail::ConstExpression>(value);
92
93 return *this;
94 }
95
101 void set_value(double value) {
102 if (expr->is_constant(0.0)) {
103 expr = detail::make_expression_ptr<detail::ConstExpression>(value);
104 } else {
105#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
106 // We only need to check the first argument since unary and binary
107 // operators both use it
108 if (expr->args[0] != nullptr) {
109 auto location = std::source_location::current();
110 slp::println(
111 stderr,
112 "WARNING: {}:{}: {}: Modified the value of a dependent variable",
113 location.file_name(), location.line(), location.function_name());
114 }
115#endif
116 expr->val = value;
117 }
118 }
119
127 friend SLEIPNIR_DLLEXPORT Variable operator*(const Variable& lhs,
128 const Variable& rhs) {
129 return Variable{lhs.expr * rhs.expr};
130 }
131
139 *this = *this * rhs;
140 return *this;
141 }
142
150 friend SLEIPNIR_DLLEXPORT Variable operator/(const Variable& lhs,
151 const Variable& rhs) {
152 return Variable{lhs.expr / rhs.expr};
153 }
154
162 *this = *this / rhs;
163 return *this;
164 }
165
173 friend SLEIPNIR_DLLEXPORT Variable operator+(const Variable& lhs,
174 const Variable& rhs) {
175 return Variable{lhs.expr + rhs.expr};
176 }
177
185 *this = *this + rhs;
186 return *this;
187 }
188
196 friend SLEIPNIR_DLLEXPORT Variable operator-(const Variable& lhs,
197 const Variable& rhs) {
198 return Variable{lhs.expr - rhs.expr};
199 }
200
208 *this = *this - rhs;
209 return *this;
210 }
211
217 friend SLEIPNIR_DLLEXPORT Variable operator-(const Variable& lhs) {
218 return Variable{-lhs.expr};
219 }
220
226 friend SLEIPNIR_DLLEXPORT Variable operator+(const Variable& lhs) {
227 return Variable{+lhs.expr};
228 }
229
235 double value() {
236 if (!m_graph) {
237 m_graph = detail::topological_sort(expr);
238 }
239 detail::update_values(m_graph.value());
240
241 return expr->val;
242 }
243
250 ExpressionType type() const { return expr->type(); }
251
252 private:
255 detail::make_expression_ptr<detail::DecisionVariableExpression>();
256
259 std::optional<small_vector<detail::Expression*>> m_graph;
260
261 friend SLEIPNIR_DLLEXPORT Variable abs(const Variable& x);
262 friend SLEIPNIR_DLLEXPORT Variable acos(const Variable& x);
263 friend SLEIPNIR_DLLEXPORT Variable asin(const Variable& x);
264 friend SLEIPNIR_DLLEXPORT Variable atan(const Variable& x);
265 friend SLEIPNIR_DLLEXPORT Variable atan2(const Variable& y,
266 const Variable& x);
267 friend SLEIPNIR_DLLEXPORT Variable cos(const Variable& x);
268 friend SLEIPNIR_DLLEXPORT Variable cosh(const Variable& x);
269 friend SLEIPNIR_DLLEXPORT Variable erf(const Variable& x);
270 friend SLEIPNIR_DLLEXPORT Variable exp(const Variable& x);
271 friend SLEIPNIR_DLLEXPORT Variable hypot(const Variable& x,
272 const Variable& y);
273 friend SLEIPNIR_DLLEXPORT Variable log(const Variable& x);
274 friend SLEIPNIR_DLLEXPORT Variable log10(const Variable& x);
275 friend SLEIPNIR_DLLEXPORT Variable pow(const Variable& base,
276 const Variable& power);
277 friend SLEIPNIR_DLLEXPORT Variable sign(const Variable& x);
278 friend SLEIPNIR_DLLEXPORT Variable sin(const Variable& x);
279 friend SLEIPNIR_DLLEXPORT Variable sinh(const Variable& x);
280 friend SLEIPNIR_DLLEXPORT Variable sqrt(const Variable& x);
281 friend SLEIPNIR_DLLEXPORT Variable tan(const Variable& x);
282 friend SLEIPNIR_DLLEXPORT Variable tanh(const Variable& x);
283 friend SLEIPNIR_DLLEXPORT Variable hypot(const Variable& x, const Variable& y,
284 const Variable& z);
285
287 template <int UpLo>
288 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
289 friend class SLEIPNIR_DLLEXPORT Hessian;
290 friend class SLEIPNIR_DLLEXPORT Jacobian;
291};
292
298SLEIPNIR_DLLEXPORT inline Variable abs(const Variable& x) {
299 return Variable{detail::abs(x.expr)};
300}
301
307SLEIPNIR_DLLEXPORT inline Variable acos(const Variable& x) {
308 return Variable{detail::acos(x.expr)};
309}
310
316SLEIPNIR_DLLEXPORT inline Variable asin(const Variable& x) {
317 return Variable{detail::asin(x.expr)};
318}
319
325SLEIPNIR_DLLEXPORT inline Variable atan(const Variable& x) {
326 return Variable{detail::atan(x.expr)};
327}
328
335SLEIPNIR_DLLEXPORT inline Variable atan2(const Variable& y, const Variable& x) {
336 return Variable{detail::atan2(y.expr, x.expr)};
337}
338
344SLEIPNIR_DLLEXPORT inline Variable cos(const Variable& x) {
345 return Variable{detail::cos(x.expr)};
346}
347
353SLEIPNIR_DLLEXPORT inline Variable cosh(const Variable& x) {
354 return Variable{detail::cosh(x.expr)};
355}
356
362SLEIPNIR_DLLEXPORT inline Variable erf(const Variable& x) {
363 return Variable{detail::erf(x.expr)};
364}
365
371SLEIPNIR_DLLEXPORT inline Variable exp(const Variable& x) {
372 return Variable{detail::exp(x.expr)};
373}
374
381SLEIPNIR_DLLEXPORT inline Variable hypot(const Variable& x, const Variable& y) {
382 return Variable{detail::hypot(x.expr, y.expr)};
383}
384
391SLEIPNIR_DLLEXPORT inline Variable pow(const Variable& base,
392 const Variable& power) {
393 return Variable{detail::pow(base.expr, power.expr)};
394}
395
401SLEIPNIR_DLLEXPORT inline Variable log(const Variable& x) {
402 return Variable{detail::log(x.expr)};
403}
404
410SLEIPNIR_DLLEXPORT inline Variable log10(const Variable& x) {
411 return Variable{detail::log10(x.expr)};
412}
413
419SLEIPNIR_DLLEXPORT inline Variable sign(const Variable& x) {
420 return Variable{detail::sign(x.expr)};
421}
422
428SLEIPNIR_DLLEXPORT inline Variable sin(const Variable& x) {
429 return Variable{detail::sin(x.expr)};
430}
431
437SLEIPNIR_DLLEXPORT inline Variable sinh(const Variable& x) {
438 return Variable{detail::sinh(x.expr)};
439}
440
446SLEIPNIR_DLLEXPORT inline Variable sqrt(const Variable& x) {
447 return Variable{detail::sqrt(x.expr)};
448}
449
455SLEIPNIR_DLLEXPORT inline Variable tan(const Variable& x) {
456 return Variable{detail::tan(x.expr)};
457}
458
464SLEIPNIR_DLLEXPORT inline Variable tanh(const Variable& x) {
465 return Variable{detail::tanh(x.expr)};
466}
467
475SLEIPNIR_DLLEXPORT inline Variable hypot(const Variable& x, const Variable& y,
476 const Variable& z) {
477 return Variable{slp::sqrt(slp::pow(x, 2) + slp::pow(y, 2) + slp::pow(z, 2))};
478}
479
491template <typename LHS, typename RHS>
492 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
494 (!std::same_as<std::decay_t<LHS>, double> ||
495 !std::same_as<std::decay_t<RHS>, double>)
496small_vector<Variable> make_constraints(LHS&& lhs, RHS&& rhs) {
497 small_vector<Variable> constraints;
498
499 if constexpr (ScalarLike<LHS> && ScalarLike<RHS>) {
500 constraints.emplace_back(lhs - rhs);
501 } else if constexpr (ScalarLike<LHS> && MatrixLike<RHS>) {
502 constraints.reserve(rhs.rows() * rhs.cols());
503
504 for (int row = 0; row < rhs.rows(); ++row) {
505 for (int col = 0; col < rhs.cols(); ++col) {
506 // Make right-hand side zero
507 constraints.emplace_back(lhs - rhs(row, col));
508 }
509 }
510 } else if constexpr (MatrixLike<LHS> && ScalarLike<RHS>) {
511 constraints.reserve(lhs.rows() * lhs.cols());
512
513 for (int row = 0; row < lhs.rows(); ++row) {
514 for (int col = 0; col < lhs.cols(); ++col) {
515 // Make right-hand side zero
516 constraints.emplace_back(lhs(row, col) - rhs);
517 }
518 }
519 } else if constexpr (MatrixLike<LHS> && MatrixLike<RHS>) {
520 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
521 constraints.reserve(lhs.rows() * lhs.cols());
522
523 for (int row = 0; row < lhs.rows(); ++row) {
524 for (int col = 0; col < lhs.cols(); ++col) {
525 // Make right-hand side zero
526 constraints.emplace_back(lhs(row, col) - rhs(row, col));
527 }
528 }
529 }
530
531 return constraints;
532}
533
537struct SLEIPNIR_DLLEXPORT EqualityConstraints {
539 small_vector<Variable> constraints;
540
547 std::initializer_list<EqualityConstraints> equality_constraints) {
548 for (const auto& elem : equality_constraints) {
549 constraints.insert(constraints.end(), elem.constraints.begin(),
550 elem.constraints.end());
551 }
552 }
553
562 const std::vector<EqualityConstraints>& equality_constraints) {
563 for (const auto& elem : equality_constraints) {
564 constraints.insert(constraints.end(), elem.constraints.begin(),
565 elem.constraints.end());
566 }
567 }
568
578 template <typename LHS, typename RHS>
579 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
581 (!std::same_as<std::decay_t<LHS>, double> ||
582 !std::same_as<std::decay_t<RHS>, double>)
583 EqualityConstraints(LHS&& lhs, RHS&& rhs)
584 : constraints{make_constraints(lhs, rhs)} {}
585
589 operator bool() { // NOLINT
590 return std::ranges::all_of(constraints, [](auto& constraint) {
591 return constraint.value() == 0.0;
592 });
593 }
594};
595
599struct SLEIPNIR_DLLEXPORT InequalityConstraints {
601 small_vector<Variable> constraints;
602
610 std::initializer_list<InequalityConstraints> inequality_constraints) {
611 for (const auto& elem : inequality_constraints) {
612 constraints.insert(constraints.end(), elem.constraints.begin(),
613 elem.constraints.end());
614 }
615 }
616
626 const std::vector<InequalityConstraints>& inequality_constraints) {
627 for (const auto& elem : inequality_constraints) {
628 constraints.insert(constraints.end(), elem.constraints.begin(),
629 elem.constraints.end());
630 }
631 }
632
642 template <typename LHS, typename RHS>
643 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
645 (!std::same_as<std::decay_t<LHS>, double> ||
646 !std::same_as<std::decay_t<RHS>, double>)
647 InequalityConstraints(LHS&& lhs, RHS&& rhs)
648 : constraints{make_constraints(lhs, rhs)} {}
649
653 operator bool() { // NOLINT
654 return std::ranges::all_of(constraints, [](auto& constraint) {
655 return constraint.value() >= 0.0;
656 });
657 }
658};
659
666template <typename LHS, typename RHS>
667 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
668 (ScalarLike<RHS> || MatrixLike<RHS>) &&
669 (!std::same_as<std::decay_t<LHS>, double> ||
670 !std::same_as<std::decay_t<RHS>, double>)
671EqualityConstraints operator==(LHS&& lhs, RHS&& rhs) {
672 return EqualityConstraints{lhs, rhs};
673}
674
682template <typename LHS, typename RHS>
683 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
684 (ScalarLike<RHS> || MatrixLike<RHS>) &&
685 (!std::same_as<std::decay_t<LHS>, double> ||
686 !std::same_as<std::decay_t<RHS>, double>)
687InequalityConstraints operator<(LHS&& lhs, RHS&& rhs) {
688 return rhs >= lhs;
689}
690
698template <typename LHS, typename RHS>
699 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
700 (ScalarLike<RHS> || MatrixLike<RHS>) &&
701 (!std::same_as<std::decay_t<LHS>, double> ||
702 !std::same_as<std::decay_t<RHS>, double>)
703InequalityConstraints operator<=(LHS&& lhs, RHS&& rhs) {
704 return rhs >= lhs;
705}
706
714template <typename LHS, typename RHS>
715 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
716 (ScalarLike<RHS> || MatrixLike<RHS>) &&
717 (!std::same_as<std::decay_t<LHS>, double> ||
718 !std::same_as<std::decay_t<RHS>, double>)
719InequalityConstraints operator>(LHS&& lhs, RHS&& rhs) {
720 return lhs >= rhs;
721}
722
730template <typename LHS, typename RHS>
731 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
732 (ScalarLike<RHS> || MatrixLike<RHS>) &&
733 (!std::same_as<std::decay_t<LHS>, double> ||
734 !std::same_as<std::decay_t<RHS>, double>)
735InequalityConstraints operator>=(LHS&& lhs, RHS&& rhs) {
736 return InequalityConstraints{lhs, rhs};
737}
738
739} // namespace slp
740
741namespace Eigen {
742
746template <>
747struct NumTraits<slp::Variable> : NumTraits<double> {
754
756 static constexpr int IsComplex = 0;
758 static constexpr int IsInteger = 0;
760 static constexpr int IsSigned = 1;
762 static constexpr int RequireInitialization = 1;
764 static constexpr int ReadCost = 1;
766 static constexpr int AddCost = 3;
768 static constexpr int MulCost = 3;
769};
770
771} // namespace Eigen
Definition hessian.hpp:31
Definition jacobian.hpp:27
Definition variable.hpp:41
friend SLEIPNIR_DLLEXPORT Variable operator+(const Variable &lhs)
Definition variable.hpp:226
constexpr Variable(detail::ExpressionPtr &&expr)
Definition variable.hpp:81
friend SLEIPNIR_DLLEXPORT Variable operator*(const Variable &lhs, const Variable &rhs)
Definition variable.hpp:127
Variable & operator=(double value)
Definition variable.hpp:90
Variable & operator-=(const Variable &rhs)
Definition variable.hpp:207
friend SLEIPNIR_DLLEXPORT Variable operator-(const Variable &lhs)
Definition variable.hpp:217
friend SLEIPNIR_DLLEXPORT Variable operator+(const Variable &lhs, const Variable &rhs)
Definition variable.hpp:173
Variable & operator+=(const Variable &rhs)
Definition variable.hpp:184
friend SLEIPNIR_DLLEXPORT Variable operator-(const Variable &lhs, const Variable &rhs)
Definition variable.hpp:196
Variable & operator*=(const Variable &rhs)
Definition variable.hpp:138
Variable(std::floating_point auto value)
Definition variable.hpp:58
constexpr Variable(std::nullptr_t)
Definition variable.hpp:51
friend SLEIPNIR_DLLEXPORT Variable operator/(const Variable &lhs, const Variable &rhs)
Definition variable.hpp:150
Variable()=default
Variable(std::integral auto value)
Definition variable.hpp:66
void set_value(double value)
Definition variable.hpp:101
Variable & operator/=(const Variable &rhs)
Definition variable.hpp:161
ExpressionType type() const
Definition variable.hpp:250
double value()
Definition variable.hpp:235
Variable(const detail::ExpressionPtr &expr)
Definition variable.hpp:74
Definition adjoint_expression_graph.hpp:21
Definition concepts.hpp:29
Definition concepts.hpp:13
Definition variable.hpp:537
EqualityConstraints(std::initializer_list< EqualityConstraints > equality_constraints)
Definition variable.hpp:546
EqualityConstraints(const std::vector< EqualityConstraints > &equality_constraints)
Definition variable.hpp:561
EqualityConstraints(LHS &&lhs, RHS &&rhs)
Definition variable.hpp:583
small_vector< Variable > constraints
A vector of scalar equality constraints.
Definition variable.hpp:539
Definition variable.hpp:599
InequalityConstraints(const std::vector< InequalityConstraints > &inequality_constraints)
Definition variable.hpp:625
InequalityConstraints(std::initializer_list< InequalityConstraints > inequality_constraints)
Definition variable.hpp:609
small_vector< Variable > constraints
A vector of scalar inequality constraints.
Definition variable.hpp:601
InequalityConstraints(LHS &&lhs, RHS &&rhs)
Definition variable.hpp:647