C++ Library Extensions 2022.12.09
To help learn modern C++ programming
directional_parametric_derivative.cpp
Go to the documentation of this file.
1/*
2A. Reference Videos
3
4Linear Approximation of Surface, Partial Derivatives and Chain Rule
5 https://youtu.be/jTMtBoWGHGo?t=3566
6
7Adaptive Quadrature | Lecture 41 | Vector Calculus for Engineers
8 https://youtu.be/U4NUXAwwR8E?t=41
9
10B . Intel Arc A-Series Graphics
11
12Intel® Arc™ A-Series Graphics – Desktop Quick Start Guide
13 https://www.intel.com/content/www/us/en/support/articles/000091128/graphics.html
14
15Resizable BAR Support On!
16 https://www.gigabyte.com/WebPage/785/NVIDIA_resizable_bar.html
17
18GeForce RTX 30 Series Performance Accelerates With Resizable BAR Support
19 https://www.nvidia.com/en-us/geforce/news/geforce-rtx-30-series-resizable-bar-support/
20
21C. Intel® oneAPI Reference Materials
22
231. Data Parallel C++
24 The PDF book
25 https://tinyurl.com/yfrxytac
26
27 Data Parallel C++ Book Source Samples
28 https://github.com/Apress/data-parallel-CPP
29
302. Intel® oneAPI Programming Guide
31 https://tinyurl.com/54h8z3vz
32
333. Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference
34 https://tinyurl.com/mrxz3up2
35
364. oneAPI GPU Optimization Guide
37 https://tinyurl.com/mrd9a2ns
38
395. The Compute Architecture of Intel® Processor Graphics Gen8
40 https://tinyurl.com/37hfp8nh
41
42 The Compute Architecture of Intel® Processor Graphics Gen9
43 https://tinyurl.com/527kxt84
44
45D. Must-Watch Previous Videos
46
47152- How to Implement Numerical Partial Derivatives
48 https://www.youtube.com/watch?v=V1MpmXGkc2c&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=155
49
50151- How to Implement Numerical Derivatives - Seven-Point Stencil
51 https://www.youtube.com/watch?v=uyToBr0ViG0&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=154
52
53150- Mathematical Theory Behind Numerical Derivatives, Mathematica, Solve System of Linear Equations
54 https://www.youtube.com/watch?v=tbngy72ePyU&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=153
55
56149- Intel DPC++, SYCL Ahead of Time Compilation (AOT) and Response Files
57 https://www.youtube.com/watch?v=ttAZRUmmagA&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=152
58
59148- Implement Gradient, Curl, Divergence with SYCL For the First Time
60 https://www.youtube.com/watch?v=WjVpbLmPJJg&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=151
61
62147- Gradient, Curl, Divergence, Random Parallel Fill, cast_ref, Multidimensional Arrays in SYCL
63 https://www.youtube.com/watch?v=aYGTBfmsZfo&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=150
64
65144- (SETUP) Setup Intel oneAPI - Parallel Partial Derivatives with TBB and SYCL 1
66 https://www.youtube.com/watch?v=5Y5KcrhG6mk&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=149
67
68*/
69
70// clang++ -std=c++20 directional_parametric_derivative.cpp -ltbb12 -o c.exe
71// g++ -std=c++20 directional_parametric_derivative.cpp -ltbb12 -o g.exe
72// cl /std:c++20 /EHsc directional_parametric_derivative.cpp /Fe: m.exe
73// dpcpp -Qstd=c++20 /EHsc -fno-sycl directional_parametric_derivative.cpp -o d.exe
74
76#include <cpg/cpg_calculus.hpp>
77
79
81{
83 // 0 1
84 auto z = [](auto x, auto y)
85 {
86 return x * x * y + 3.0 * x * std::pow(y, 4);
87 };
88
89 auto x = [](auto t)
90 {
91 return std::sin(2 * t);
92 };
93
94 auto y = [](auto t)
95 {
96 return std::cos(t);
97 };
98
99 /*
100 How can we evaluate dz/dt?
101
102 1. dz/dt = dz/dx * dx/dt + dz/dy * dy/dt - Chain Rule
103
104 2. We use mathematical definition, that is, dz/dt can be computed directly
105
106 Most of us are familiar with Algebraic Approach to our mathematical problems,
107 such as Chain Rules, or some other stupid mathematical theorems.
108
109 These are useful for algrebraic approach, but for Numerical Solutions,
110 Mathematical Definitions play the critical role.
111
112 For example, I = Int f(x) dx, if we apply mathematical definition,
113
114 We can easily implement multiple integrals very compactly, simply, and easily.
115
116 inner_integral(y) = { Int f(x, y) dx, y should be fixed, F(x) with y fixed, => Int F(x) dx }
117 I = Int { Int f(x, y) dx } dy => Int inner_integral(y) dy
118 */
119
120 auto dz_dt_chain_rule = [&](auto t)
121 {
122 auto dx_dt = cna::seven_point_stencil<1>(x, t); // 1st order derivative of x at t = 0
123 auto dy_dt = cna::seven_point_stencil<1>(y, t); // 1st order derivative of y at t = 0
124
125 auto cmd_dx = cna::create_command(d_0_1);
126 auto cmd_dy = cna::create_command(d_1_1);
127
128 auto dz_dx = cna::partial_derivative(cmd_dx, z, x(t), y(t));
129 auto dz_dy = cna::partial_derivative(cmd_dy, z, x(t), y(t));
130
131 return dz_dx * dx_dt + dz_dy * dy_dt; // chain rule
132 };
133
134 auto n_dz_dt_chain_rule = dz_dt_chain_rule(0.0);
135 std::cout <<"Using Chain Rule = " << n_dz_dt_chain_rule << cpg::endl;
136
137 auto dz_dt_definition = [&](auto t) // dz/dt - mathematical definition
138 {
139 auto z_of_t = [&](auto tt)
140 {
141 return z(x(tt), y(tt));
142 };
143
144 return cna::five_point_stencil<1>(z_of_t, t);
145 };
146
147 auto n_dz_dt_definition = dz_dt_definition(0.0);
148 std::cout <<"Using Math Definition = " << n_dz_dt_definition << cpg::endl;
149
150 auto n_dz_dt_definition_2 = cna::directional_derivative(z, std::tuple{x, y}, 0.0);
151 std::cout <<"Using Math Definition = " << n_dz_dt_definition_2 << cpg::endl;
152
153 auto cmd = cna::create_command(d_0_1);
154
155 auto result = cna::parametric_derivative(cmd, z, std::tuple{x, y}, 0.0);
156 std::cout <<"Using Math Definition = " << result << cpg::endl;
157}
158
160{
161 using namespace cpg::numerical_analysis::commands;
162
163 auto z =[](auto x, auto y)
164 {
165 return std::exp(x) * std::sin(y);
166 };
167
168 auto x = [](auto s, auto t)
169 {
170 return s * t * t;
171 };
172
173 auto y = [](auto s, auto t)
174 {
175 return s * s * t;
176 };
177
178 auto cmd = cna::create_command(d_0_1, d_1_1);
179 // s t
180 auto numerical = cna::parametric_derivative(cmd, z, std::tuple{x, y}, std::array{1.0, 1.0} );
181
182 auto dz_ds = [&](auto s, auto t)
183 {
184 return std::exp(x(s, t))*std::sin(y(s, t))*t*t +
185 2 * std::exp(x(s, t))*std::cos(y(s, t))*s*t;
186 };
187
188 auto dz_dt = [&](auto s, auto t)
189 {
190 return 2 * std::exp(x(s, t))*std::sin(y(s, t))*s*t +
191 std::exp(x(s, t))*std::cos(y(s, t))*s*s;
192 };
193
194 auto algebraic = std::array{ dz_ds(1.0, 1.0), dz_dt(1.0, 1.0) };
195
196 std::cout <<"numerical = " << numerical << std::endl;
197 std::cout <<"algebraic = " << algebraic << std::endl;
198}
199
200int main()
201{
203
205}
auto & cout
auto & endl
void test_parameterized_derivatives()
void test_parametric_derivatives()
auto parametric_derivative(cpt::type_container< CmdTypes... > cmd, FuncType &&f, std::tuple< VariableTypes... > const &vars, ParamTypes... ps)
constexpr auto create_command(SeqType, SeqTypes...) noexcept
auto partial_derivative(cpt::sequence< VarIndex, Order >, FuncType &&func, ArgTypes... args) noexcept
auto directional_derivative(FuncType &&f, std::tuple< VariableTypes... > const &vars, ParamType param)
constexpr auto endl
Definition: cpg_types.hpp:213