numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
create_diff_variable.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 <Eigen/Core>
23
32
34
42template <base::concepts::real_scalar Scalar>
43[[nodiscard]] inline auto create_diff_variable(const Scalar& value)
45 return variable<Scalar>(value, variable_tag());
46}
47
53template <base::concepts::real_scalar_dense_vector ValueVector>
55 Eigen::Matrix<variable<typename ValueVector::Scalar>,
56 ValueVector::RowsAtCompileTime, 1, Eigen::ColMajor,
57 ValueVector::MaxRowsAtCompileTime, 1>;
58
59namespace impl {
60
68template <typename ValueVector>
70public:
72 using scalar_type = typename ValueVector::Scalar;
73
76
79 Eigen::Matrix<variable_type, ValueVector::RowsAtCompileTime, 1,
80 Eigen::ColMajor, ValueVector::MaxRowsAtCompileTime, 1>;
81
87 explicit create_diff_variable_vector_functor(const ValueVector& value_vec)
88 : value_vec_(value_vec) {}
89
97 [[nodiscard]] auto operator()(index_type row, index_type col) const
98 -> variable_type {
101 }
102
103private:
105 const ValueVector& value_vec_;
106};
107
108} // namespace impl
109
117template <base::concepts::real_scalar_dense_vector ValueVector>
118[[nodiscard]] inline auto create_diff_variable_vector(
119 const Eigen::MatrixBase<ValueVector>& value_vec)
120 -> Eigen::CwiseNullaryOp<
123 ValueVector>::result_type> {
124 NUM_COLLECT_PRECONDITION(value_vec.cols() == 1,
125 "create_diff_variable_vector function requires a vector as the "
126 "argument.");
127
128 using result_type = typename impl::create_diff_variable_vector_functor<
129 ValueVector>::result_type;
130 return result_type::NullaryExpr(value_vec.rows(), 1,
132 value_vec.derived()));
133}
134
135} // namespace num_collect::auto_diff::backward
Definition of assertion macros.
#define NUM_COLLECT_DEBUG_ASSERT(CONDITION)
Macro to check whether a condition is satisfied in debug build only.
Definition assert.h:75
Definition of node class.
Class of functor to create a vector of variables by which functions will be differentiated.
auto operator()(index_type row, index_type col) const -> variable_type
Get an element of the vector.
Eigen::Matrix< variable_type, ValueVector::RowsAtCompileTime, 1, Eigen::ColMajor, ValueVector::MaxRowsAtCompileTime, 1 > result_type
Type of resulting differential coefficients.
Class of variables in backward-mode automatic differentiation kubota1998.
Definition variable.h:48
Definition of exceptions.
Definition of index_type type.
Definition of macros for logging.
Namespace of backward-mode automatic differentiation.
auto create_diff_variable(const Scalar &value) -> variable< Scalar >
Create a variable by which functions will be differentiated.
auto create_diff_variable_vector(const Eigen::MatrixBase< ValueVector > &value_vec) -> Eigen::CwiseNullaryOp< impl::create_diff_variable_vector_functor< ValueVector >, typename impl::create_diff_variable_vector_functor< ValueVector >::result_type >
Create a vector of variables.
Eigen::Matrix< variable< typename ValueVector::Scalar >, ValueVector::RowsAtCompileTime, 1, Eigen::ColMajor, ValueVector::MaxRowsAtCompileTime, 1 > variable_vector_type
Get type of vectors 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.
Definition of real_scalar concept.
Definition of real_scalar_dense_vector concept.
Tag class to specify variables.
Definition variable.h:39