summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/math/example/autodiff_fourth_power.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/math/example/autodiff_fourth_power.cpp
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/math/example/autodiff_fourth_power.cpp')
-rw-r--r--src/boost/libs/math/example/autodiff_fourth_power.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/boost/libs/math/example/autodiff_fourth_power.cpp b/src/boost/libs/math/example/autodiff_fourth_power.cpp
new file mode 100644
index 00000000..50f280b6
--- /dev/null
+++ b/src/boost/libs/math/example/autodiff_fourth_power.cpp
@@ -0,0 +1,34 @@
+// Copyright Matthew Pulver 2018 - 2019.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// https://www.boost.org/LICENSE_1_0.txt)
+
+#include <boost/math/differentiation/autodiff.hpp>
+#include <iostream>
+
+template <typename T>
+T fourth_power(T const& x) {
+ T x4 = x * x; // retval in operator*() uses x4's memory via NRVO.
+ x4 *= x4; // No copies of x4 are made within operator*=() even when squaring.
+ return x4; // x4 uses y's memory in main() via NRVO.
+}
+
+int main() {
+ using namespace boost::math::differentiation;
+
+ constexpr unsigned Order = 5; // Highest order derivative to be calculated.
+ auto const x = make_fvar<double, Order>(2.0); // Find derivatives at x=2.
+ auto const y = fourth_power(x);
+ for (unsigned i = 0; i <= Order; ++i)
+ std::cout << "y.derivative(" << i << ") = " << y.derivative(i) << std::endl;
+ return 0;
+}
+/*
+Output:
+y.derivative(0) = 16
+y.derivative(1) = 32
+y.derivative(2) = 48
+y.derivative(3) = 48
+y.derivative(4) = 24
+y.derivative(5) = 0
+**/