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
46template <base::concepts::real_scalar Value>
47[[nodiscard]] inline auto create_diff_variable(const Value& value)
49 return variable<Value>(value, static_cast<Value>(1));
50}
51
63template <base::concepts::real_scalar Value,
65[[nodiscard]] inline auto create_diff_variable(const Value& value,
67 return variable<Value, Diff>(value, Diff::Unit(size, index));
68}
69
75template <base::concepts::real_scalar_dense_vector ValueVector>
77 Eigen::Matrix<variable<typename ValueVector::Scalar, ValueVector>,
78 ValueVector::RowsAtCompileTime, 1, Eigen::ColMajor,
79 ValueVector::MaxRowsAtCompileTime, 1>;
80
81namespace impl {
82
90template <base::concepts::real_scalar_dense_vector ValueVector>
92public:
94 using value_type = typename ValueVector::Scalar;
95
97 using diff_type = typename ValueVector::PlainMatrix;
98
101
104 Eigen::Matrix<variable_type, ValueVector::RowsAtCompileTime, 1,
105 Eigen::ColMajor, ValueVector::MaxRowsAtCompileTime, 1>;
106
112 explicit create_diff_variable_vector_functor(const ValueVector& value_vec)
113 : value_vec_(value_vec) {}
114
122 [[nodiscard]] auto operator()(index_type row, index_type col) const
123 -> variable_type {
124 NUM_COLLECT_DEBUG_ASSERT(col == 0);
126 value_vec_(row, col), value_vec_.size(), row);
127 }
128
129private:
131 const ValueVector& value_vec_;
132};
133
134} // namespace impl
135
143template <base::concepts::real_scalar_dense_vector ValueVector>
144[[nodiscard]] inline auto create_diff_variable_vector(
145 const Eigen::MatrixBase<ValueVector>& value_vec)
146 -> Eigen::CwiseNullaryOp<
149 ValueVector>::result_type> {
150 NUM_COLLECT_PRECONDITION(value_vec.cols() == 1,
151 "create_diff_variable_vector function requires a vector as the "
152 "argument.");
153
154 using result_type = typename impl::create_diff_variable_vector_functor<
155 ValueVector>::result_type;
156 return result_type::NullaryExpr(value_vec.rows(), 1,
158 value_vec.derived()));
159}
160
161} // namespace num_collect::auto_diff::forward
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
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.
typename ValueVector::PlainMatrix diff_type
Type of differential coefficients.
Eigen::Matrix< variable_type, ValueVector::RowsAtCompileTime, 1, Eigen::ColMajor, ValueVector::MaxRowsAtCompileTime, 1 > result_type
Type of resulting differential coefficients.
Class of variables in forward-mode automatic differentiation kubota1998.
Definition variable.h:43
Concept of Eigen's dense vectors with real scalars.
Definition of exceptions.
Definition of variable class.
Definition of index_type type.
Definition of macros for logging.
Namespace of forward-mode automatic differentiation.
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.
auto create_diff_variable(const Value &value) -> variable< Value >
Create a variable by which functions will be differentiated (for scalar differential coefficients).
Eigen::Matrix< variable< typename ValueVector::Scalar, ValueVector >, 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.