Quick start
Python
My first program
If you have successfully compiled and installed pyaudi following the Python bindings you will be able to test its use typing the following script.
1from pyaudi import gdual_double as gdual
2from pyaudi import exp, log, cbrt
3
4# We want to compute the Taylor expansion of a function f (and thus all derivatives) at x=2, y=3
5# 1 - Define the generalized dual numbers (7 is the truncation order, i.e.
6# the maximum order of derivation we will need)
7
8x = gdual(2, "x", 7)
9y = gdual(3, "y", 7)
10
11# 2 - Compute your function as usual
12f = exp(x * x + cbrt(y) / log(x * y))
13
14# 3 - Inspect the results (this does not require any more computations)
15# This is the Taylor expansion of f (truncated at the 7th order)
16print("Taylor polynomial: " + str(f))
17# This is the value of the derivative (d / dx)
18print("Derivative value [1,0]: " + str(f.get_derivative([1, 0])))
19# This is the value of the mixed derivative (d^7 / dx^4dy^3)
20print("Derivative value [4,3]: " + str(f.get_derivative([4, 3])))
21
22# 4 - Using the dictionary interface (note the presence of the "d" before
23# all variables)
24# This is the value of the derivative (d / dx)
25print("Derivative value [1,0]: " + str(f.get_derivative({"dx": 1})))
26# This is the value of the mixed derivative (d^7 / dx^4dy^3)
27print("Derivative value [4,3]: " + str(f.get_derivative({"dx": 4, "dy": 3})))
Place it into a getting_started.py text file and run it with
python getting_started.py
We recommend the use of Jupyter or ipython do enjoy pyaudi the most.
C++
My first program
After following the Packages you will be able to compile and run your first C++ AuDi program:
1#include <audi/gdual.hpp>
2#include <audi/functions.hpp>
3#include <iostream>
4
5using namespace audi;
6
7int main()
8{
9 // We want to compute the Taylor expansion of a function f (and thus all derivatives) at x=2, y=3
10 using gdual = gdual<double>;
11 // 1 - Define the generalized dual numbers (over doubles, 7 is the truncation order, i.e. the maximum
12 // order of derivation we will need)
13 gdual x(2, "x", 7);
14 gdual y(3, "y", 7);
15
16 // 2 - Compute your function as usual
17 auto f = exp(x * x + cbrt(y) / log(x * y));
18
19 // 3 - Inspect the results (this has a constant complexity now as all computations have been made already)
20 std::cout << "Taylor polynomial: " << f
21 << std::endl; // This is the Taylor expansion of f (truncated at the 7th order)
22 std::cout << "Derivative value: " << f.get_derivative({1, 0})
23 << std::endl; // This is the value of the derivative (d / dx)
24 std::cout << "Derivative value: " << f.get_derivative({4, 3})
25 << std::endl; // This is the value of the mixed derivative (d^7 / dx^4dy^3)
26
27 // 4 - Using the dictionary interface (note the presence of the "d" before all variables)
28 std::cout << "Derivative value: " << f.get_derivative({{"dx", 1}})
29 << std::endl; // This is the value of the derivative (d / dx)
30 std::cout << "Derivative value: " << f.get_derivative({{"dx", 4}, {"dy", 3}})
31 << std::endl; // This is the value of the mixed derivative (d^7 / dx^4dy^3)
32}
Place it into a getting_started.cpp text file and compile it with:
g++ -std=c++11 getting_started.cpp -lmpfr -lgmp -pthread