numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
make_jacobian.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <type_traits> // IWYU pragma: keep
23
24#include <Eigen/Core>
25
32
34
35namespace impl {
36
42template <base::concepts::dense_vector VectorType>
44public:
46 using variable_vector_type = VectorType;
47
49 using variable_type = typename variable_vector_type::Scalar;
50
52 using value_type = typename variable_type::value_type;
53
55 using diff_type = typename variable_type::diff_type;
56
57 static_assert(std::is_same_v<typename diff_type::Scalar, value_type>);
58
60 using result_type = Eigen::Matrix<value_type,
61 variable_vector_type::RowsAtCompileTime, diff_type::RowsAtCompileTime,
62 Eigen::ColMajor, variable_vector_type::MaxRowsAtCompileTime,
63 diff_type::MaxRowsAtCompileTime>;
64
71 : vector_(vector) {}
72
80 [[nodiscard]] auto operator()(index_type row, index_type col) const
81 -> value_type {
82 return vector_(row).diff()(col);
83 }
84
85private:
88};
89
90} // namespace impl
91
99template <base::concepts::dense_vector VectorType>
100[[nodiscard]] inline auto make_jacobian(
101 const Eigen::MatrixBase<VectorType>& vector)
102 -> Eigen::CwiseNullaryOp<impl::make_jacobian_functor<VectorType>,
104 using result_type =
106
107 NUM_COLLECT_PRECONDITION(vector.cols() == 1,
108 "differentiate function requires a vector as the argument.");
109 NUM_COLLECT_PRECONDITION(vector.rows() >= 2,
110 "differentiate function requires a vector with at least two "
111 "elements.");
112
113 for (index_type i = 0; i < vector.rows(); ++i) {
114 NUM_COLLECT_ASSERT(vector(i).diff().cols() == 1);
115 }
116 const auto result_rows = vector.rows();
117 const auto result_cols = vector(0).diff().rows();
118
119 return result_type::NullaryExpr(result_rows, result_cols,
121}
122
123} // namespace num_collect::auto_diff::forward
Definition of assertion macros.
#define NUM_COLLECT_ASSERT(CONDITION)
Macro to check whether a condition is satisfied.
Definition assert.h:66
typename variable_vector_type::Scalar variable_type
Type of variables.
make_jacobian_functor(const variable_vector_type &vector)
Constructor.
Eigen::Matrix< value_type, variable_vector_type::RowsAtCompileTime, diff_type::RowsAtCompileTime, Eigen::ColMajor, variable_vector_type::MaxRowsAtCompileTime, diff_type::MaxRowsAtCompileTime > result_type
Type of resulting matrix.
VectorType variable_vector_type
Type of vectors of variables.
typename variable_type::diff_type diff_type
Type of differential coefficients.
const variable_vector_type & vector_
Vector of variables.
auto operator()(index_type row, index_type col) const -> value_type
Get an element of Jacobian.
typename variable_type::value_type value_type
Type of values.
Definition of dense_vector concept.
Definition of exceptions.
Definition of index_type type.
Definition of macros for logging.
Namespace of forward-mode automatic differentiation.
auto make_jacobian(const Eigen::MatrixBase< VectorType > &vector) -> Eigen::CwiseNullaryOp< impl::make_jacobian_functor< VectorType >, typename impl::make_jacobian_functor< VectorType >::result_type >
Make Jacobian from a vector of variables.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.