WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
variable.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <concepts>
7#include <initializer_list>
8#include <source_location>
9#include <utility>
10#include <vector>
11
12#include <Eigen/Core>
13#include <gch/small_vector.hpp>
14
20
21#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
23#endif
24
25namespace slp {
26
27// Forward declarations for friend declarations in Variable
28
29namespace detail {
30
31template <typename Scalar>
33
34} // namespace detail
35
36template <typename Scalar, int UpLo = Eigen::Lower | Eigen::Upper>
37 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
38class Hessian;
39
40template <typename Scalar>
41class Jacobian;
42
43/// An autodiff variable pointing to an expression node.
44///
45/// @tparam Scalar_ Scalar type.
46template <typename Scalar_>
47class Variable : public SleipnirBase {
48 public:
49 /// Scalar type alias.
50 using Scalar = Scalar_;
51
52 /// Constructs a linear Variable with a value of zero.
53 Variable() = default;
54
55 /// Constructs an empty Variable.
56 explicit Variable(std::nullptr_t) : expr{nullptr} {}
57
58 /// Constructs a Variable from a scalar type.
59 ///
60 /// @param value The value of the Variable.
61 // NOLINTNEXTLINE (google-explicit-constructor)
63 requires(!MatrixLike<Scalar>)
64 : expr{detail::make_expression_ptr<detail::ConstantExpression<Scalar>>(
65 value)} {}
66
67 /// Constructs a Variable from a scalar type.
68 ///
69 /// @param value The value of the Variable.
70 // NOLINTNEXTLINE (google-explicit-constructor)
72 slp_assert(value.rows() == 1 && value.cols() == 1);
73 }
74
75 /// Constructs a Variable from a floating-point type.
76 ///
77 /// @param value The value of the Variable.
78 // NOLINTNEXTLINE (google-explicit-constructor)
79 Variable(std::floating_point auto value)
80 : expr{detail::make_expression_ptr<detail::ConstantExpression<Scalar>>(
81 Scalar(value))} {}
82
83 /// Constructs a Variable from an integral type.
84 ///
85 /// @param value The value of the Variable.
86 // NOLINTNEXTLINE (google-explicit-constructor)
87 Variable(std::integral auto value)
88 : expr{detail::make_expression_ptr<detail::ConstantExpression<Scalar>>(
89 Scalar(value))} {}
90
91 /// Constructs a Variable pointing to the specified expression.
92 ///
93 /// @param expr The autodiff variable.
94 explicit Variable(const detail::ExpressionPtr<Scalar>& expr) : expr{expr} {}
95
96 /// Constructs a Variable pointing to the specified expression.
97 ///
98 /// @param expr The autodiff variable.
100 : expr{std::move(expr)} {}
101
102 /// Assignment operator for scalar.
103 ///
104 /// @param value The value of the Variable.
105 /// @return This variable.
107 expr =
109 m_graph_initialized = false;
110
111 return *this;
112 }
113
114 /// Sets Variable's internal value.
115 ///
116 /// @param value The value of the Variable.
118#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
119 // We only need to check the first argument since unary and binary operators
120 // both use it
121 if (expr->args[0] != nullptr) {
122 auto location = std::source_location::current();
124 stderr,
125 "WARNING: {}:{}: {}: Modified the value of a dependent variable",
126 location.file_name(), location.line(), location.function_name());
127 }
128#endif
129 expr->val = Scalar(value);
130 }
131
132 /// Returns the value of this variable.
133 ///
134 /// @return The value of this variable.
136 if (!m_graph_initialized) {
137 m_graph = detail::topological_sort(expr);
138 m_graph_initialized = true;
139 }
140 detail::update_values(m_graph);
141
142 return Scalar(expr->val);
143 }
144
145 /// Returns the type of this expression (constant, linear, quadratic, or
146 /// nonlinear).
147 ///
148 /// @return The type of this expression.
149 ExpressionType type() const { return expr->type(); }
150
151 /// Variable-scalar multiplication operator.
152 ///
153 /// @param lhs Operator left-hand side.
154 /// @param rhs Operator right-hand side.
155 /// @return Result of multiplication.
156 template <ScalarLike LHS, SleipnirScalarLike<Scalar> RHS>
157 friend Variable<Scalar> operator*(const LHS& lhs, const RHS& rhs) {
158 return Variable{Variable<Scalar>{lhs}.expr * rhs.expr};
159 }
160
161 /// Variable-scalar multiplication operator.
162 ///
163 /// @param lhs Operator left-hand side.
164 /// @param rhs Operator right-hand side.
165 /// @return Result of multiplication.
166 template <SleipnirScalarLike<Scalar> LHS, ScalarLike RHS>
167 friend Variable<Scalar> operator*(const LHS& lhs, const RHS& rhs) {
168 return Variable{lhs.expr * Variable<Scalar>{rhs}.expr};
169 }
170
171 /// Variable-scalar multiplication operator.
172 ///
173 /// @param lhs Operator left-hand side.
174 /// @param rhs Operator right-hand side.
175 /// @return Result of multiplication.
177 const Variable<Scalar>& rhs) {
178 return Variable{lhs.expr * rhs.expr};
179 }
180
181 /// Variable-Variable compound multiplication operator.
182 ///
183 /// @param rhs Operator right-hand side.
184 /// @return Result of multiplication.
186 *this = *this * rhs;
187 return *this;
188 }
189
190 /// Variable-Variable division operator.
191 ///
192 /// @param lhs Operator left-hand side.
193 /// @param rhs Operator right-hand side.
194 /// @return Result of division.
196 const Variable<Scalar>& rhs) {
197 return Variable{lhs.expr / rhs.expr};
198 }
199
200 /// Variable-Variable compound division operator.
201 ///
202 /// @param rhs Operator right-hand side.
203 /// @return Result of division.
205 *this = *this / rhs;
206 return *this;
207 }
208
209 /// Variable-Variable addition operator.
210 ///
211 /// @param lhs Operator left-hand side.
212 /// @param rhs Operator right-hand side.
213 /// @return Result of addition.
215 const Variable<Scalar>& rhs) {
216 return Variable{lhs.expr + rhs.expr};
217 }
218
219 /// Variable-Variable compound addition operator.
220 ///
221 /// @param rhs Operator right-hand side.
222 /// @return Result of addition.
224 *this = *this + rhs;
225 return *this;
226 }
227
228 /// Variable-Variable subtraction operator.
229 ///
230 /// @param lhs Operator left-hand side.
231 /// @param rhs Operator right-hand side.
232 /// @return Result of subtraction.
234 const Variable<Scalar>& rhs) {
235 return Variable{lhs.expr - rhs.expr};
236 }
237
238 /// Variable-Variable compound subtraction operator.
239 ///
240 /// @param rhs Operator right-hand side.
241 /// @return Result of subtraction.
243 *this = *this - rhs;
244 return *this;
245 }
246
247 /// Unary minus operator.
248 ///
249 /// @param lhs Operand for unary minus.
251 return Variable{-lhs.expr};
252 }
253
254 /// Unary plus operator.
255 ///
256 /// @param lhs Operand for unary plus.
258 return Variable{+lhs.expr};
259 }
260
261 private:
262 /// The expression node
265
266 /// Used to update the value of this variable based on the values of its
267 /// dependent variables
269
270 /// Used for lazy initialization of m_graph
271 bool m_graph_initialized = false;
272
273 template <typename Scalar>
275 template <typename Scalar>
277 template <typename Scalar>
279 template <typename Scalar>
281 template <typename Scalar>
282 friend Variable<Scalar> atan2(const ScalarLike auto& y,
283 const Variable<Scalar>& x);
284 template <typename Scalar>
286 const ScalarLike auto& x);
287 template <typename Scalar>
289 const Variable<Scalar>& x);
290 template <typename Scalar>
292 template <typename Scalar>
294 template <typename Scalar>
296 template <typename Scalar>
298 template <typename Scalar>
300 template <typename Scalar>
301 friend Variable<Scalar> hypot(const ScalarLike auto& x,
302 const Variable<Scalar>& y);
303 template <typename Scalar>
305 const ScalarLike auto& y);
306 template <typename Scalar>
308 const Variable<Scalar>& y);
309 template <typename Scalar>
311 template <typename Scalar>
313 template <typename Scalar>
314 friend Variable<Scalar> pow(const ScalarLike auto& base,
315 const Variable<Scalar>& power);
316 template <typename Scalar>
318 const ScalarLike auto& power);
319 template <typename Scalar>
321 const Variable<Scalar>& power);
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>
336 const Variable<Scalar>& y,
337 const Variable<Scalar>& z);
338
340 template <typename Scalar, int UpLo>
341 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
342 friend class Hessian;
343 template <typename Scalar>
344 friend class Jacobian;
345};
346
347template <template <typename> typename T, typename Scalar>
348 requires SleipnirMatrixLike<T<Scalar>, Scalar>
350
351template <std::floating_point T>
353
354template <std::integral T>
356
357/// abs() for Variables.
358///
359/// @tparam Scalar Scalar type.
360/// @param x The argument.
361template <typename Scalar>
363 return Variable{detail::abs(x.expr)};
364}
365
366/// acos() for Variables.
367///
368/// @tparam Scalar Scalar type.
369/// @param x The argument.
370template <typename Scalar>
372 return Variable{detail::acos(x.expr)};
373}
374
375/// asin() for Variables.
376///
377/// @tparam Scalar Scalar type.
378/// @param x The argument.
379template <typename Scalar>
381 return Variable{detail::asin(x.expr)};
382}
383
384/// atan() for Variables.
385///
386/// @tparam Scalar Scalar type.
387/// @param x The argument.
388template <typename Scalar>
390 return Variable{detail::atan(x.expr)};
391}
392
393/// atan2() for Variables.
394///
395/// @tparam Scalar Scalar type.
396/// @param y The y argument.
397/// @param x The x argument.
398template <typename Scalar>
400 return Variable{detail::atan2(Variable<Scalar>(y).expr, x.expr)};
401}
402
403/// atan2() for Variables.
404///
405/// @tparam Scalar Scalar type.
406/// @param y The y argument.
407/// @param x The x argument.
408template <typename Scalar>
410 return Variable{detail::atan2(y.expr, Variable<Scalar>(x).expr)};
411}
412
413/// atan2() for Variables.
414///
415/// @tparam Scalar Scalar type.
416/// @param y The y argument.
417/// @param x The x argument.
418template <typename Scalar>
420 return Variable{detail::atan2(y.expr, x.expr)};
421}
422
423/// cbrt() for Variables.
424///
425/// @tparam Scalar Scalar type.
426/// @param x The argument.
427template <typename Scalar>
429 return Variable{detail::cbrt(x.expr)};
430}
431
432/// cos() for Variables.
433///
434/// @tparam Scalar Scalar type.
435/// @param x The argument.
436template <typename Scalar>
438 return Variable{detail::cos(x.expr)};
439}
440
441/// cosh() for Variables.
442///
443/// @tparam Scalar Scalar type.
444/// @param x The argument.
445template <typename Scalar>
447 return Variable{detail::cosh(x.expr)};
448}
449
450/// erf() for Variables.
451///
452/// @tparam Scalar Scalar type.
453/// @param x The argument.
454template <typename Scalar>
456 return Variable{detail::erf(x.expr)};
457}
458
459/// exp() for Variables.
460///
461/// @tparam Scalar Scalar type.
462/// @param x The argument.
463template <typename Scalar>
465 return Variable{detail::exp(x.expr)};
466}
467
468/// hypot() for Variables.
469///
470/// @tparam Scalar Scalar type.
471/// @param x The x argument.
472/// @param y The y argument.
473template <typename Scalar>
475 return Variable{detail::hypot(Variable<Scalar>(x).expr, y.expr)};
476}
477
478/// hypot() for Variables.
479///
480/// @tparam Scalar Scalar type.
481/// @param x The x argument.
482/// @param y The y argument.
483template <typename Scalar>
485 return Variable{detail::hypot(x.expr, Variable<Scalar>(y).expr)};
486}
487
488/// hypot() for Variables.
489///
490/// @tparam Scalar Scalar type.
491/// @param x The x argument.
492/// @param y The y argument.
493template <typename Scalar>
495 return Variable{detail::hypot(x.expr, y.expr)};
496}
497
498/// log() for Variables.
499///
500/// @tparam Scalar Scalar type.
501/// @param x The argument.
502template <typename Scalar>
504 return Variable{detail::log(x.expr)};
505}
506
507/// log10() for Variables.
508///
509/// @tparam Scalar Scalar type.
510/// @param x The argument.
511template <typename Scalar>
513 return Variable{detail::log10(x.expr)};
514}
515
516/// pow() for Variables.
517///
518/// @tparam Scalar Scalar type.
519/// @param base The base.
520/// @param power The power.
521template <typename Scalar>
523 const Variable<Scalar>& power) {
524 return Variable{detail::pow(Variable<Scalar>(base).expr, power.expr)};
525}
526
527/// pow() for Variables.
528///
529/// @tparam Scalar Scalar type.
530/// @param base The base.
531/// @param power The power.
532template <typename Scalar>
534 const ScalarLike auto& power) {
535 return Variable{detail::pow(base.expr, Variable<Scalar>(power).expr)};
536}
537
538/// pow() for Variables.
539///
540/// @tparam Scalar Scalar type.
541/// @param base The base.
542/// @param power The power.
543template <typename Scalar>
545 const Variable<Scalar>& power) {
546 return Variable{detail::pow(base.expr, power.expr)};
547}
548
549/// sign() for Variables.
550///
551/// @tparam Scalar Scalar type.
552/// @param x The argument.
553template <typename Scalar>
555 return Variable{detail::sign(x.expr)};
556}
557
558/// sin() for Variables.
559///
560/// @tparam Scalar Scalar type.
561/// @param x The argument.
562template <typename Scalar>
564 return Variable{detail::sin(x.expr)};
565}
566
567/// sinh() for Variables.
568///
569/// @tparam Scalar Scalar type.
570/// @param x The argument.
571template <typename Scalar>
573 return Variable{detail::sinh(x.expr)};
574}
575
576/// sqrt() for Variables.
577///
578/// @tparam Scalar Scalar type.
579/// @param x The argument.
580template <typename Scalar>
582 return Variable{detail::sqrt(x.expr)};
583}
584
585/// tan() for Variables.
586///
587/// @tparam Scalar Scalar type.
588/// @param x The argument.
589template <typename Scalar>
591 return Variable{detail::tan(x.expr)};
592}
593
594/// tanh() for Variables.
595///
596/// @tparam Scalar Scalar type.
597/// @param x The argument.
598template <typename Scalar>
600 return Variable{detail::tanh(x.expr)};
601}
602
603/// hypot() for Variables.
604///
605/// @tparam Scalar Scalar type.
606/// @param x The x argument.
607/// @param y The y argument.
608/// @param z The z argument.
609template <typename Scalar>
611 const Variable<Scalar>& z) {
612 return Variable{sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))};
613}
614
615// The standard form for equality constraints is c(x) = 0, and the standard form
616// for inequality constraints is c(x) ≥ 0. make_constraints() takes constraints
617// of the form lhs = rhs or lhs ≥ rhs and converts them to lhs - rhs = 0 or
618// lhs - rhs ≥ 0.
619
620template <typename Scalar, ScalarLike LHS, ScalarLike RHS>
622auto make_constraints(LHS&& lhs, RHS&& rhs) {
624 constraints.emplace_back(lhs - rhs);
625
626 return constraints;
627}
628
629template <typename Scalar, ScalarLike LHS, MatrixLike RHS>
630 requires SleipnirScalarLike<LHS, Scalar> || SleipnirMatrixLike<RHS, Scalar>
631auto make_constraints(LHS&& lhs, RHS&& rhs) {
633 constraints.reserve(rhs.rows() * rhs.cols());
634
635 for (int row = 0; row < rhs.rows(); ++row) {
636 for (int col = 0; col < rhs.cols(); ++col) {
637 // Make right-hand side zero
638 constraints.emplace_back(lhs - rhs(row, col));
639 }
640 }
641
642 return constraints;
643}
644
645template <typename Scalar, MatrixLike LHS, ScalarLike RHS>
646 requires SleipnirMatrixLike<LHS, Scalar> || SleipnirScalarLike<RHS, Scalar>
647auto make_constraints(LHS&& lhs, RHS&& rhs) {
649 constraints.reserve(lhs.rows() * lhs.cols());
650
651 for (int row = 0; row < lhs.rows(); ++row) {
652 for (int col = 0; col < lhs.cols(); ++col) {
653 // Make right-hand side zero
654 constraints.emplace_back(lhs(row, col) - rhs);
655 }
656 }
657
658 return constraints;
659}
660
661template <typename Scalar, MatrixLike LHS, MatrixLike RHS>
662 requires SleipnirMatrixLike<LHS, Scalar> || SleipnirMatrixLike<RHS, Scalar>
663auto make_constraints(LHS&& lhs, RHS&& rhs) {
664 slp_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
665
667 constraints.reserve(lhs.rows() * lhs.cols());
668
669 for (int row = 0; row < lhs.rows(); ++row) {
670 for (int col = 0; col < lhs.cols(); ++col) {
671 // Make right-hand side zero
672 constraints.emplace_back(lhs(row, col) - rhs(row, col));
673 }
674 }
675
676 return constraints;
677}
678
679/// A vector of equality constraints of the form cₑ(x) = 0.
680///
681/// @tparam Scalar Scalar type.
682template <typename Scalar>
684 /// A vector of scalar equality constraints.
686
687 /// Concatenates multiple equality constraints.
688 ///
689 /// @param equality_constraints The list of EqualityConstraints to
690 /// concatenate.
692 std::initializer_list<EqualityConstraints> equality_constraints) {
693 for (const auto& elem : equality_constraints) {
694 constraints.insert(constraints.end(), elem.constraints.begin(),
695 elem.constraints.end());
696 }
697 }
698
699 /// Concatenates multiple equality constraints.
700 ///
701 /// This overload is for Python bindings only.
702 ///
703 /// @param equality_constraints The list of EqualityConstraints to
704 /// concatenate.
706 const std::vector<EqualityConstraints>& equality_constraints) {
707 for (const auto& elem : equality_constraints) {
708 constraints.insert(constraints.end(), elem.constraints.begin(),
709 elem.constraints.end());
710 }
711 }
712
713 /// Constructs an equality constraint from a left and right side.
714 ///
715 /// The standard form for equality constraints is c(x) = 0. This function
716 /// takes a constraint of the form lhs = rhs and converts it to lhs - rhs = 0.
717 ///
718 /// @param lhs Left-hand side.
719 /// @param rhs Right-hand side.
720 template <typename LHS, typename RHS>
721 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
724 EqualityConstraints(LHS&& lhs, RHS&& rhs)
726
727 /// Implicit conversion operator to bool.
728 // NOLINTNEXTLINE (google-explicit-constructor)
729 operator bool() {
730 return std::ranges::all_of(constraints, [](auto& constraint) {
731 return constraint.value() == Scalar(0);
732 });
733 }
734};
735
736/// A vector of inequality constraints of the form cᵢ(x) ≥ 0.
737///
738/// @tparam Scalar Scalar type.
739template <typename Scalar>
741 /// A vector of scalar inequality constraints.
743
744 /// Concatenates multiple inequality constraints.
745 ///
746 /// @param inequality_constraints The list of InequalityConstraints to
747 /// concatenate.
749 std::initializer_list<InequalityConstraints> inequality_constraints) {
750 for (const auto& elem : inequality_constraints) {
751 constraints.insert(constraints.end(), elem.constraints.begin(),
752 elem.constraints.end());
753 }
754 }
755
756 /// Concatenates multiple inequality constraints.
757 ///
758 /// This overload is for Python bindings only.
759 ///
760 /// @param inequality_constraints The list of InequalityConstraints to
761 /// concatenate.
763 const std::vector<InequalityConstraints>& inequality_constraints) {
764 for (const auto& elem : inequality_constraints) {
765 constraints.insert(constraints.end(), elem.constraints.begin(),
766 elem.constraints.end());
767 }
768 }
769
770 /// Constructs an inequality constraint from a left and right side.
771 ///
772 /// The standard form for inequality constraints is c(x) ≥ 0. This function
773 /// takes a constraints of the form lhs ≥ rhs and converts it to lhs - rhs ≥
774 /// 0.
775 ///
776 /// @param lhs Left-hand side.
777 /// @param rhs Right-hand side.
778 template <typename LHS, typename RHS>
779 requires(ScalarLike<LHS> || MatrixLike<LHS>) &&
782 InequalityConstraints(LHS&& lhs, RHS&& rhs)
784
785 /// Implicit conversion operator to bool.
786 // NOLINTNEXTLINE (google-explicit-constructor)
787 operator bool() {
788 return std::ranges::all_of(constraints, [](auto& constraint) {
789 return constraint.value() >= Scalar(0);
790 });
791 }
792};
793
794/// Equality operator that returns an equality constraint for two Variables.
795///
796/// @param lhs Left-hand side.
797/// @param rhs Left-hand side.
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) {
803}
804
805/// Equality operator that returns an equality constraint for two Variables.
806///
807/// @param lhs Left-hand side.
808/// @param rhs Left-hand side.
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) {
814}
815
816/// Equality operator that returns an equality constraint for two Variables.
817///
818/// @param lhs Left-hand side.
819/// @param rhs Left-hand side.
820template <typename LHS, typename RHS>
821 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
822 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
823auto operator==(LHS&& lhs, RHS&& rhs) {
825}
826
827/// Less-than comparison operator that returns an inequality constraint for two
828/// Variables.
829///
830/// @param lhs Left-hand side.
831/// @param rhs Left-hand side.
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) {
837 return rhs >= lhs;
838}
839
840/// Less-than-or-equal-to comparison operator that returns an inequality
841/// constraint for two Variables.
842///
843/// @param lhs Left-hand side.
844/// @param rhs Left-hand side.
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) {
850 return rhs >= lhs;
851}
852
853/// Greater-than comparison operator that returns an inequality constraint for
854/// two Variables.
855///
856/// @param lhs Left-hand side.
857/// @param rhs Left-hand side.
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) {
863 return lhs >= rhs;
864}
865
866/// Greater-than-or-equal-to comparison operator that returns an inequality
867/// constraint for two Variables.
868///
869/// @param lhs Left-hand side.
870/// @param rhs Left-hand side.
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) {
876}
877
878/// Greater-than-or-equal-to comparison operator that returns an inequality
879/// constraint for two Variables.
880///
881/// @param lhs Left-hand side.
882/// @param rhs Left-hand side.
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) {
888}
889
890/// Greater-than-or-equal-to comparison operator that returns an inequality
891/// constraint for two Variables.
892///
893/// @param lhs Left-hand side.
894/// @param rhs Left-hand side.
895template <typename LHS, typename RHS>
896 requires(ScalarLike<LHS> || MatrixLike<LHS>) && SleipnirType<LHS> &&
897 (ScalarLike<RHS> || MatrixLike<RHS>) && SleipnirType<RHS>
898auto operator>=(LHS&& lhs, RHS&& rhs) {
900}
901
902/// Helper function for creating bound constraints.
903///
904/// @param l Lower bound.
905/// @param x Variable to bound.
906/// @param u Upper bound.
907template <typename L, typename X, typename U>
908 requires(ScalarLike<L> || MatrixLike<L>) && SleipnirType<X> &&
909 (ScalarLike<U> || MatrixLike<U>)
910auto bounds(L&& l, X&& x, U&& u) {
911 return InequalityConstraints{l <= x, x <= u};
912}
913
914} // namespace slp
915
916namespace Eigen {
917
918/// NumTraits specialization that allows instantiating Eigen types with
919/// Variable.
920///
921/// @tparam Scalar Scalar type.
922template <typename Scalar>
923struct NumTraits<slp::Variable<Scalar>> : NumTraits<Scalar> {
924 /// Real type.
926 /// Non-integer type.
928 /// Nested type.
930
931 /// Is complex.
932 static constexpr int IsComplex = 0;
933 /// Is integer.
934 static constexpr int IsInteger = 0;
935 /// Is signed.
936 static constexpr int IsSigned = 1;
937 /// Require initialization.
938 static constexpr int RequireInitialization = 1;
939 /// Read cost.
940 static constexpr int ReadCost = 1;
941 /// Add cost.
942 static constexpr int AddCost = 3;
943 /// Multiply cost.
944 static constexpr int MulCost = 3;
945};
946
947} // namespace Eigen
#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