tinyopt
700
Tinyopt, a lightweight, header only optimization library
Toggle main menu visibility
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
9
namespace
tinyopt::traits {
10
11
template
<
typename
T,
int
N>
12
struct
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
17
template
<
typename
T>
18
struct
is_jet_type
{
19
static
constexpr
bool
value =
false
;
20
};
21
22
template
<
typename
T,
int
N>
23
struct
is_jet_type
<diff::Jet<T, N>> {
24
static
constexpr
bool
value =
true
;
25
};
26
27
template
<
typename
T>
28
inline
constexpr
bool
is_jet_type_v = is_jet_type<T>::value;
29
30
// Check whether a type 'T' or '&T' is a Jet type
31
template
<
typename
T>
32
struct
jet_details
{
33
using
Scalar = double;
34
static
constexpr
Index Dims = 0;
35
};
36
37
template
<
typename
T,
int
N>
38
struct
jet_details
<diff::Jet<T, N>> {
39
using
Scalar = T;
40
static
constexpr
Index Dims = N;
41
};
42
43
// Trait specialization for Jet
44
template
<
typename
_Scalar,
int
N>
45
struct
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
tinyopt::traits::is_jet_type
Definition
jet_traits.h:18
tinyopt::traits::is_scalar
Definition
type_traits.h:29
tinyopt::traits::jet_details
Definition
jet_traits.h:32
tinyopt::traits::params_trait
Definition
parameter_traits.h:15
include
tinyopt
diff
jet_traits.h
Generated by
1.18.0