tinyopt 700
Tinyopt, a lightweight, header only optimization library
Loading...
Searching...
No Matches
matrix_traits.h
1// Copyright 2026 Julien Michot.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <tinyopt/types.h>
7#include <tinyopt/traits/type_traits.h>
8
9namespace tinyopt::traits {
10
11// Trait to check if a type is a Sparse Matrix.
12template <typename T>
13struct is_sparse_matrix : std::false_type {};
14template <typename T>
15struct is_sparse_matrix<SparseMatrix<T>> : std::true_type {};
16
17template <typename T>
18inline constexpr bool is_sparse_matrix_v = is_sparse_matrix<std::decay_t<T>>::value;
19
20// Trait to check if a type is a Matrix/Vector.
21template <typename T, typename = void>
22struct is_matrix_or_array : std::false_type {};
23
24template <typename T>
26 T,
27 std::void_t<decltype(std::declval<const std::remove_cvref_t<T>&>().rows()),
28 decltype(std::declval<const std::remove_cvref_t<T>&>().cols()),
29 typename std::remove_cvref_t<T>::Scalar>>
30 : std::bool_constant<!is_sparse_matrix<std::remove_cvref_t<T>>::value> {};
31
32template <typename T>
33inline constexpr bool is_matrix_or_array_v = is_matrix_or_array<std::remove_cvref_t<T>>::value;
34
35template <typename T>
36inline constexpr bool is_matrix_or_scalar_v =
37 (std::is_scalar_v<T> && !std::is_same_v<T, bool>) || is_sparse_matrix_v<T> ||
38 is_matrix_or_array_v<T>;
39
40} // namespace tinyopt::traits
Definition matrix_traits.h:22
Definition matrix_traits.h:13