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// Copyright © 2018–2026 Dario Izzo (dario.izzo@gmail.com),
 2// Francesco Biscani (bluescarni@gmail.com),
 3// Sean Cowan (lambertarc@icloud.com)
 4//
 5// This file is part of the audi library.
 6//
 7// The audi library is free software: you can redistribute it and/or modify
 8// it under the terms of either:
 9//   - the GNU General Public License as published by the Free Software
10//     Foundation, either version 3 of the License, or (at your option)
11//     any later version, or
12//   - the GNU Lesser General Public License as published by the Free
13//     Software Foundation, either version 3 of the License, or (at your
14//     option) any later version.
15//
16// The audi library is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19// GNU General Public License and the GNU Lesser General Public License
20// for more details.
21//
22// You should have received a copy of the GNU General Public License
23// and the GNU Lesser General Public License along with the audi library.
24// If not, see <https://www.gnu.org/licenses/>.
25
26#include <audi/gdual.hpp>
27#include <audi/functions.hpp>
28#include <iostream>
29
30using namespace audi;
31
32int main()
33{
34    // We want to compute the Taylor expansion of a function f (and thus all derivatives) at x=2, y=3
35    using gdual = gdual<double>;
36    // 1 - Define the generalized dual numbers (over doubles, 7 is the truncation order, i.e. the maximum
37    // order of derivation we will need)
38    gdual x(2, "x", 7);
39    gdual y(3, "y", 7);
40
41    // 2 - Compute your function as usual
42    auto f = exp(x * x + cbrt(y) / log(x * y));
43
44    // 3 - Inspect the results (this has a constant complexity now as all computations have been made already)
45    std::cout << "Taylor polynomial: " << f
46              << std::endl; // This is the Taylor expansion of f (truncated at the 7th order)
47    std::cout << "Derivative value: " << f.get_derivative({1, 0})
48              << std::endl; // This is the value of the derivative (d / dx)
49    std::cout << "Derivative value: " << f.get_derivative({4, 3})
50              << std::endl; // This is the value of the mixed derivative (d^7 / dx^4dy^3)
51
52    // 4 - Using the dictionary interface (note the presence of the "d" before all variables)
53    std::cout << "Derivative value: " << f.get_derivative({{"dx", 1}})
54              << std::endl; // This is the value of the derivative (d / dx)
55    std::cout << "Derivative value: " << f.get_derivative({{"dx", 4}, {"dy", 3}})
56              << std::endl; // This is the value of the mixed derivative (d^7 / dx^4dy^3)
57}

Place it into a getting_started.cpp text file. Compiling such an example file with C++ manually means making sure that all dependencies (i.e. headers and libraries) are on the search path of the compiler you are using (e.g. g++). On possible example of compiling the program could be:

g++ -std=c++11 getting_started.cpp -lmpfr -lgmp -pthread

another example with manual specification (and clang++) could be:

clang++ audi_test.cpp \
  -std=c++20 \
  -I$CONDA_PREFIX/include \
  -L$CONDA_PREFIX/lib \
  -lmpfr -lgmp \
  -lboost_system \
  -lobake \
  -ltbb \
  -lmp++ \
  -lfmt \
  -pthread \
  -o audi_test