tinyopt 700
Tinyopt, a lightweight, header only optimization library
Loading...
Searching...
No Matches
jet_traits.h
1// Copyright 2026 Julien Michot.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <tinyopt/diff/jet.h>
7#include <tinyopt/traits.h>
8
9namespace tinyopt::traits {
10
11template <typename T, int N>
12struct is_scalar<diff::Jet<T, N>> {
13 static constexpr bool value = true;
14};
15
16// Check whether a type 'T' or '&T' is a Jet type
17template <typename T>
19 static constexpr bool value = false;
20};
21
22template <typename T, int N>
23struct is_jet_type<diff::Jet<T, N>> {
24 static constexpr bool value = true;
25};
26
27template <typename T>
28inline constexpr bool is_jet_type_v = is_jet_type<T>::value;
29
30// Check whether a type 'T' or '&T' is a Jet type
31template <typename T>
33 using Scalar = double;
34 static constexpr Index Dims = 0;
35};
36
37template <typename T, int N>
38struct jet_details<diff::Jet<T, N>> {
39 using Scalar = T;
40 static constexpr Index Dims = N;
41};
42
43// Trait specialization for Jet
44template <typename _Scalar, int N>
45struct params_trait<diff::Jet<_Scalar, N>> {
46 using T = diff::Jet<_Scalar, N>;
47 using Scalar = _Scalar; // The scalar type
48 static constexpr Index Dims =
49 params_trait<Scalar>::Dims == Dynamic ? Dynamic : 1; // Compile-time parameters dimensions
50 // Execution-time parameters dimensions
51 static auto dims(const T& v) { return params_trait<Scalar>::dims(v); }
52 // Cast to a new type, only needed when using automatic differentiation
53 template <typename T2>
54 static auto cast(const T& v) {
55 if constexpr (has_static_cast_v<T>)
56 return T::template cast<T2>(v);
57 else if constexpr (has_cast_v<T>)
58 return v.template cast<T2>();
59 else
60 return T2(v); // use casting operator by default
61 }
62 // Define update / manifold
63 static void PlusEq(T& v, const auto& delta) { v += delta; }
64};
65
66} // namespace tinyopt::traits
Definition jet_traits.h:18
Definition type_traits.h:29
Definition jet_traits.h:32
Definition parameter_traits.h:15