numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
auto_diff/backward.cpp

Example of backward-mode automatic differentiation.

/*
* Copyright 2021 MusicScience37 (Kenta Kabashima)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cmath>
#include <iomanip>
#include <iostream>
#include "num_collect/auto_diff/backward/variable_math.h" // IWYU pragma: keep
#include "xexp.h"
auto main() -> int {
const auto var = variable<double>(1.234, variable_tag());
const variable<double> val = xexp(var);
const double derivative = differentiate(val, var);
constexpr int precision = 15;
std::cout << std::setprecision(precision);
std::cout << "Value: " << val.value() << std::endl;
std::cout << "Derivative: " << derivative << std::endl;
std::cout << "Reference: "
<< std::exp(var.value()) + var.value() * std::exp(var.value())
<< std::endl;
return 0;
}
Definition of node class.
Definition of mathematical functions for variable class.
Class of variables in backward-mode automatic differentiation kubota1998.
Definition variable.h:48
Definition of differentiate function.
auto differentiate(const variable< Scalar > &func_value, const variable< Scalar > &arg) -> Scalar
Compute a differential coefficient.
Tag class to specify variables.
Definition variable.h:39