Skip to content

Commit 5d2114d

Browse files
committed
Added examples for complex number and updated documentation.
1 parent d390b5c commit 5d2114d

6 files changed

Lines changed: 79 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ There is a newsletter available at [codi-info@uni-kl.de](https://lists.uni-kl.de
2121

2222
CoDiPack is a header only library.
2323
The only file the user needs to include is `codi.hpp`.
24-
The only other requirement is a C++11 compliant compiler
25-
where one usually needs to specify '-std=c++11' in compiler arguments.
24+
The only other requirement is a C++17 compliant compiler
25+
where one usually needs to specify '-std=c++17' in compiler arguments. For a C++11 compilant version of CoDiPack use th 2.* release.
2626
CoDiPack is tested with gcc, clang, and the Intel compiler.
2727

2828
The file `codi.hpp` defines several datatypes. The most important ones are:
@@ -83,6 +83,12 @@ Therefore we recomend to force inlining of CoDiPack with the option
8383
-DCODI_UseForcedInlines
8484
~~~~
8585

86+
### Complex numbers
87+
88+
If your compiler has problems with the specialization of `std::complex` for CoDiPack types, try `-DCODI_SpecializeStdComplex=0` to disable this behaviour.
89+
Without the specialization, complex types can be defined by using `codi::ActiveComplex<CoDiType>`, e.g. `codi::ActiveComplex<codi::RealReverse>`.
90+
91+
8692
## Hello World Example
8793

8894
A very small and simple example for the usage of the RealForward type is the following code:
@@ -106,11 +112,11 @@ A very small and simple example for the usage of the RealForward type is the fol
106112

107113
It is compiled with
108114
~~~~{.txt}
109-
g++ -I<path to codi>/include -std=c++11 -g -o forward forward.cpp
115+
g++ -I<path to codi>/include -std=c++17 -g -o forward forward.cpp
110116
~~~~
111117
for the gcc compiler or with
112118
~~~~{.txt}
113-
icpc -I<path to codi>/include -std=c++11 -g -o forward forward.cpp
119+
icpc -I<path to codi>/include -std=c++17 -g -o forward forward.cpp
114120
~~~~
115121
for the Intel compiler.
116122

documentation/CoDiLayout.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<tab type="user" title="Example 25 - Tape writers" url="@ref Example_25_Tape_Writers"/>
4646
<tab type="user" title="Example 26 - Jacobian tape readers" url="@ref Example_26_Jacobian_Tape_Readers"/>
4747
<tab type="user" title="Example 27 - Primal tape readers" url="@ref Example_27_Primal_Tape_Readers"/>
48+
<tab type="user" title="Example 28 - Complex numbers" url="@ref Example_28_Complex_numbers"/>
4849
</tab>
4950
<tab type="user" title="Papers" url="@ref Papers"/>
5051
<tab type="user" title="Taping strategies" url="@ref TapingStrategy"/>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! [Example 28 - Complex numbers]
2+
#include <iostream>
3+
#include <codi.hpp>
4+
5+
//! [Function implementations]
6+
template<typename Type>
7+
Type func(Type const& v) {
8+
Type t = v + v.real();
9+
return 2 * v * t;
10+
}
11+
//! [Function implementations]
12+
13+
int main(int nargs, char** args) {
14+
using Real = codi::RealReverse;
15+
using Tape = typename Real::Tape;
16+
17+
Tape& tape = Real::getTape();
18+
19+
std::complex<Real> x = 10.0;
20+
Real w = 5.0;
21+
22+
tape.setActive();
23+
24+
codi::RealTraits::registerInput(w);
25+
codi::RealTraits::registerInput(x); // Use general registration function for complex numbers.
26+
27+
// Use complex numbers as usual.
28+
std::complex<Real> y = func(x);
29+
y *= w;
30+
31+
codi::RealTraits::registerOutput(y); // Use general registration function for complex numbers.
32+
33+
tape.setPassive();
34+
35+
// Cast to Real* is still possible.
36+
Real* x_p = reinterpret_cast<Real*>(&x);
37+
Real* y_p = reinterpret_cast<Real*>(&y);
38+
for(int i = 0; i < 2; i += 1) {
39+
tape.clearAdjoints();
40+
y_p[i].setGradient(1.0);
41+
tape.evaluate();
42+
std::cout << "Gradient of dy[" << i << "]/dw: " << w.getGradient() << std::endl;
43+
std::cout << "Gradient of dy[" << i << "]/dx[0]: " << x_p[0].getGradient() << std::endl;
44+
std::cout << "Gradient of dy[" << i << "]/dx[1]: " << x_p[1].getGradient() << std::endl;
45+
}
46+
}
47+
//! [Example 28 - Complex numbers]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Example 28 - Complex numbers {#Example_28_Complex_numbers}
2+
=======
3+
4+
**Goal:** Demonstrate the use of std::complex with CoDiPack.
5+
6+
**Prerequisite:** \ref Tutorial_02_Reverse_mode_AD
7+
8+
**Function:**
9+
\snippet examples/Example_28_Complex_numbers.cpp Function implementations
10+
11+
**Full code:**
12+
\snippet examples/Example_28_Complex_numbers.cpp Example 28 - Complex numbers
13+
14+
**Additional information:**
15+
std::complex is automatically specialized for CoDiPack types. With some compilers this can lead to problems.
16+
Use the option `-DCODI_SpecializeStdComplex=0` to disable this behaviour.
17+
Without the specialization, complex types can be defined by using `codi::ActiveComplex<CoDiType>`, e.g. `codi::ActiveComplex<codi::RealReverse>`.

documentation/user/Tutorials.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ or pointers to other features.
4747
| \subpage Example_25_Tape_Writers "" | Writing tapes to disk and other tape writers.|
4848
| \subpage Example_26_Jacobian_Tape_Readers "" | Reading Jacobian tapes from disk. |
4949
| \subpage Example_27_Primal_Tape_Readers "" | Rading primal value tapes from disk. |
50+
| \subpage Example_28_Complex_numbers "" | How to use complex numbers in CoDiPack. |
5051

5152
The graph shows how the tutorials and examples are connected. Usually it is better to understand first the prerequisites
5253
of a tutorial/example before reading the actual example.

documentation/user/TutorialsGraph.dot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ digraph Tutorials {
7474

7575
E27 [label="E27 - Primal tape readers"];
7676

77+
E28 [label="E28 - Complex numbers"];
78+
7779
// Edges (sorted)
7880
E02:e -> E08:w;
7981
E02:e -> E09:w;
@@ -99,6 +101,7 @@ digraph Tutorials {
99101
T02:e -> E22:w;
100102
T02:e -> E23:w;
101103
T02:e -> E25:w;
104+
T02:e -> E28:w;
102105
T02:e -> T03:w;
103106
T02:e -> T04:w;
104107
T02:e -> T05:w;

0 commit comments

Comments
 (0)