Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new autodiff type based on tape representation #89

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
2a2566e
[autodiff/mixed] Implement base expression class for new mixed mode type
supersega Nov 30, 2019
88114bb
[autodiff/mixed] Add expression tree node class which hold topology a…
supersega Nov 30, 2019
9663649
[autodiff/mixed] Create tape based storage for expression tree nodes
supersega Nov 30, 2019
7912624
[autodiff/mixed] Add variable which supports mixed type of automatic …
supersega Nov 30, 2019
1f669b2
[autodiff/mixed] Implement meta function which help to select derivat…
supersega Nov 30, 2019
1d2d962
[autodiff/mixed] Add algorithm which compute all first order derivatives
supersega Nov 30, 2019
613707f
[autodiff/mixed] Implement parameter expression to be able to create …
supersega Nov 30, 2019
2df418b
[autodiff/mixed] Implement binary expressions classes for differentia…
supersega Nov 30, 2019
6c346a9
[autodiff/mixed] Implement unary expressions classes for differentiab…
supersega Nov 30, 2019
e4c2a20
[autodiff/mixed] Overload binary operators for mixed differentiable v…
supersega Nov 30, 2019
f4ae890
[autodiff/mixed] Create sinus function for mixed differentiable variable
supersega Nov 30, 2019
7589f8d
[autodiff/mixed] Add Eigen support for first order mixed differentiab…
supersega Nov 30, 2019
f25f3d1
[autodiff/mixed] Calculate gradient w.r.t. Eigen vector
supersega Nov 30, 2019
3e3a45d
[autodiff/mixed] Add alias headers for all folders in mixed
supersega Nov 30, 2019
a5fa2aa
[autodiff/mixed] Add mixed header file for autodiff
supersega Nov 30, 2019
152d5a5
[autodiff/mixed] Add stream operator for mixed type variable
supersega Nov 30, 2019
d0af7aa
[autodiff/mixed] Add first example for autodiff variable
supersega Nov 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions autodiff/mixed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// _ _
// _ _|_ _ _|o_|__|_
// (_||_||_(_)(_|| | |
//
// automatic differentiation made easier in C++
// https://github.com/autodiff/autodiff
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Copyright (c) 2018-2019 Serhii Malyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

// autodiff includes
#include <autodiff/mixed/algorithms.hpp>
#include <autodiff/mixed/core.hpp>
#include <autodiff/mixed/expressions.hpp>
#include <autodiff/mixed/functions.hpp>
#include <autodiff/mixed/operators.hpp>
30 changes: 30 additions & 0 deletions autodiff/mixed/algorithms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// _ _
// _ _|_ _ _|o_|__|_
// (_||_||_(_)(_|| | |
//
// automatic differentiation made easier in C++
// https://github.com/autodiff/autodiff
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Copyright (c) 2018-2019 Serhii Malyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <autodiff/mixed/algorithms/first_order.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For file names, so far we have adopted a no-underline, no-caps convention, meaning, we would use firstorder.hpp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that _ will help to reed file name, but if we prefer such naming as you mentioned I will change.

82 changes: 82 additions & 0 deletions autodiff/mixed/algorithms/first_order.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// _ _
// _ _|_ _ _|o_|__|_
// (_||_||_(_)(_|| | |
//
// automatic differentiation made easier in C++
// https://github.com/autodiff/autodiff
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Copyright (c) 2018-2019 Serhii Malyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <vector>

#include <autodiff/mixed/core/node.hpp>
#include <autodiff/mixed/core/variable.hpp>

namespace autodiff::taperep
{
namespace detail
{
/// @brief Compute all first derivatives derivatives using reverse loop.
/// @param y target variable which derivatives we want to compute.
/// @return all derivatives of target variable in <code>std::vector</code>
template<typename Var>
auto all_first_derivatives(const Var& x) -> std::vector<double>
{
const auto& nodes = x.tape().nodes();
const auto length = nodes.size();

std::vector<double> derivatives(length, 0.0);

derivatives[x.index()] = 1.0;

for (auto i = length; i > 0; i--)
{
const auto& node = nodes[i - 1];

for (std::size_t j = 0; j < node.parents_count(); j++)
derivatives[node.parent(j)] += node. template partial_derivative<first_order>(j) * derivatives[i - 1];
}

return derivatives;
}
}

/// @brief Compute all first derivatives derivatives using reverse loop.
/// @param y Target variable which derivatives we want to compute.
/// @return Function due to we can get derivatives by operator <code>()</code>.
template<typename Var>
auto first_derivatives(const Var& y)
{
static_assert(traits::is_var_v<Var>, "Please, use 'first_derivatives' function only for var");

return [derivatives = detail::all_first_derivatives(y)](auto x) -> double
{
static_assert(traits::is_var_v<decltype(x)>,
"Looks like you want to get derivative for non var type, it is prevented");

return derivatives[x.index()];
};
}
} // namespace autodiff::taperep
35 changes: 35 additions & 0 deletions autodiff/mixed/core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// _ _
// _ _|_ _ _|o_|__|_
// (_||_||_(_)(_|| | |
//
// automatic differentiation made easier in C++
// https://github.com/autodiff/autodiff
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Copyright (c) 2018-2019 Serhii Malyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <autodiff/mixed/core/meta.hpp>
#include <autodiff/mixed/core/node.hpp>
#include <autodiff/mixed/core/tape.hpp>
#include <autodiff/mixed/core/variable.hpp>
69 changes: 69 additions & 0 deletions autodiff/mixed/core/meta.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// _ _
// _ _|_ _ _|o_|__|_
// (_||_||_(_)(_|| | |
//
// automatic differentiation made easier in C++
// https://github.com/autodiff/autodiff
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Copyright (c) 2018-2019 Serhii Malyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <array>
#include <tuple>

namespace autodiff::meta
{
namespace detail
{
/// @brief Meta function to generate tuple which contains derivatives array.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setting JAVADOC_AUTOBRIEF to YES, so @brief is not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just arrange about documentation style, and I will do ;)

/// @return Tuple which contain array of derivatives weights.
constexpr auto generate_tuple_of_derivatives_weights(std::index_sequence<0>)
{
return std::tuple(std::array<double, 2>{ });
}

/// @brief Meta function to generate tuple which contains derivatives arrays.
/// @return Tuple which contain arrays of derivatives weights.
template<std::size_t I, std::size_t... N>
constexpr auto generate_tuple_of_derivatives_weights(std::index_sequence<I, N...>)
{
constexpr auto cnt = sizeof...(N);
return std::tuple_cat(generate_tuple_of_derivatives_weights(std::make_index_sequence<cnt>{}),
std::tuple(std::array<double, cnt + 2>{}));
}

/// @brief Meta function to generate type of weights container
/// @return Type of container.
template<std::size_t N>
struct tuple_of_derivatives_weights
{
using type = decltype(generate_tuple_of_derivatives_weights(std::make_index_sequence<N>{}));
};
}

/// @brief Meta function to generate type of weights container
/// @return Type of container
template<std::size_t N>
using tuple_of_derivatives_weights_t = typename detail::tuple_of_derivatives_weights<N>::type;
}
94 changes: 94 additions & 0 deletions autodiff/mixed/core/node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#pragma once

// _ _
// _ _|_ _ _|o_|__|_
// (_||_||_(_)(_|| | |
//
// automatic differentiation made easier in C++
// https://github.com/autodiff/autodiff
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Copyright (c) 2018-2019 Serhii Malyshev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <array>
#include <vector>
#include <memory>

#include <autodiff/mixed/expressions/expression.hpp>

namespace autodiff::taperep
{
/// @brief Struct which represent node of expression graph.
/// @details Give access to topology of node. Makes enable to update children etc.
/// Node holds topology(parent indices) and differential properties (expression) in separate members.
/// @tparam N order of node.
template<std::size_t N>
struct node final
{
static_assert(N > 0 && "bad idea to create node with zero order derivatives variable, use double or other trivial type");
static_assert(N <= 2 && "we support only 1st and 2nd order vars");

/// @brief Construct node from parents indices and underlying expression.
/// @param parents parent indices (topological info).
/// @param expr storage for values and derivatives.
node(std::array<std::size_t, 2> parents, expression_ptr<N> expr) :
m_parents { parents }, m_expression { expr } { }

/// @brief access to class which holds differential props.
/// @return node differential props.
auto expression() const -> const expression_ptr<N>&
{
return m_expression;
}

/// @brief Partial derivative at position i.
/// @details For example if we work with first order derivatives
/// i = 0 represent dnode / dleft, i = 1 represent dnode / dright.
/// @tparam Order order of derivatives weights.
/// @param i index.
/// @return partial derivative of given order.
template<std::size_t Order>
auto partial_derivative(std::size_t i) const -> double
{
return m_expression->template partial_derivatives<Order>()[i];
}

/// @brief Parent index at position i.
/// @param i index.
/// @return Parent node index for given i.
auto parent(std::size_t i) const -> std::size_t
{
return m_parents[i];
}

/// @brief Count of parents.
/// @details For now always two.
/// @return Number of parent nodes.
auto parents_count() const -> std::size_t
{
return 2;
}
private:
std::array<std::size_t, 2> m_parents; ///< parents nodes indices
expression_ptr<N> m_expression; ///< pointer to expression of node
};
}
Loading