numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
node.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 <memory>
23#include <utility>
24#include <vector>
25
28
30
31template <base::concepts::real_scalar Scalar>
32class node;
33
39template <base::concepts::real_scalar Scalar>
40using node_ptr = std::shared_ptr<const node<Scalar>>;
41
47template <base::concepts::real_scalar Scalar>
49public:
51 using scalar_type = Scalar;
52
64
70 [[nodiscard]] auto node() const noexcept -> const node_ptr<scalar_type>& {
71 return node_;
72 }
73
81 [[nodiscard]] auto sensitivity() const noexcept -> const scalar_type& {
82 return sensitivity_;
83 }
84
85private:
88
91};
92
98template <base::concepts::real_scalar Scalar>
99class node {
100public:
102 using scalar_type = Scalar;
103
109 explicit node(std::vector<child_node<scalar_type>> children =
110 std::vector<child_node<scalar_type>>())
111 : children_(std::move(children)) {}
112
118 [[nodiscard]] auto children() const noexcept
119 -> const std::vector<child_node<scalar_type>>& {
120 return children_;
121 }
122
123private:
125 std::vector<child_node<scalar_type>> children_;
126};
127
128namespace impl {
129
137template <base::concepts::real_scalar Scalar>
138[[nodiscard]] inline auto create_node_impl(
139 std::vector<child_node<Scalar>>& children) -> node_ptr<Scalar> {
140 return std::make_shared<node<Scalar>>(std::move(children));
141}
142
155template <base::concepts::real_scalar Scalar, typename... Args>
156[[nodiscard]] inline auto create_node_impl(
157 std::vector<child_node<Scalar>>& children, node_ptr<Scalar> node,
158 const Scalar& sensitivity, Args&&... args) -> node_ptr<Scalar> {
159 children.emplace_back(std::move(node), sensitivity);
160 return create_node_impl(children, std::forward<Args>(args)...);
161}
162
163} // namespace impl
164
173template <base::concepts::real_scalar Scalar, typename... Args>
174[[nodiscard]] inline auto create_node(Args&&... args) -> node_ptr<Scalar> {
175 std::vector<child_node<Scalar>> children;
176 return impl::create_node_impl(children, std::forward<Args>(args)...);
177}
178
179} // namespace num_collect::auto_diff::backward::graph
Definition of assertion macros.
#define NUM_COLLECT_ASSERT(CONDITION)
Macro to check whether a condition is satisfied.
Definition assert.h:66
class to save information of child nodes.
Definition node.h:48
child_node(node_ptr< scalar_type > node, const scalar_type &sensitivity)
Constructor.
Definition node.h:60
scalar_type sensitivity_
Partial differential coefficient of the parent node by the child node.
Definition node.h:90
auto node() const noexcept -> const node_ptr< scalar_type > &
Get the child node.
Definition node.h:70
auto sensitivity() const noexcept -> const scalar_type &
Get the partial differential coefficient of the parent node by the child node.
Definition node.h:81
node_ptr< scalar_type > node_
Child node.
Definition node.h:87
Class of nodes in graphs of automatic differentiation.
Definition node.h:99
std::vector< child_node< scalar_type > > children_
Child nodes.
Definition node.h:125
auto children() const noexcept -> const std::vector< child_node< scalar_type > > &
Get the child nodes.
Definition node.h:118
node(std::vector< child_node< scalar_type > > children=std::vector< child_node< scalar_type > >())
Constructor.
Definition node.h:109
auto create_node_impl(std::vector< child_node< Scalar > > &children) -> node_ptr< Scalar >
Create a node.
Definition node.h:138
Namespace of graphs in backward-mode automatic differentiation.
Definition node.h:29
std::shared_ptr< const node< Scalar > > node_ptr
Type of pointers of nodes.
Definition node.h:40
auto create_node(Args &&... args) -> node_ptr< Scalar >
Create a node.
Definition node.h:174
STL namespace.
Definition of real_scalar concept.