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

Example of forward-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/forward/variable_math.h" // IWYU pragma: keep
#include "xexp.h"
auto main() -> int {
const variable<double> var = create_diff_variable(1.234);
const variable<double> val = xexp(var);
constexpr int precision = 15;
std::cout << std::setprecision(precision);
std::cout << "Value: " << val.value() << std::endl;
std::cout << "Derivative: " << val.diff() << std::endl;
std::cout << "Reference: "
<< std::exp(var.value()) + var.value() * std::exp(var.value())
<< std::endl;
return 0;
}
Class of variables in forward-mode automatic differentiation kubota1998.
Definition variable.h:43
Definition of create_diff_variable function.
Definition of variable class.
Definition of mathematical functions for variable class.
auto create_diff_variable(const Value &value) -> variable< Value >
Create a variable by which functions will be differentiated (for scalar differential coefficients).