This is the repo for the implementation of my Capstone project in the Udacity C++ Nanodegree Program.
Mathematically speaking, in this program we want to solve the differential equation:
for f(x), where
i.e., the second derivative of a Gaussian function. Without shift (for now, for simplicity we impose zero shift, i.e. we set
where the
where the
Of course, in principle we could numerically obtain the coefficients and solve this example in O(n log(n)) by using FFT libraries (such as FFTW) (or possibly even purely analytical), but to have some fun and get a smooth animation, we use a stochastic solver with adaptive learning rate lr to incrementally find our solution.
In other words, we train our coefficients
which occurs if and only if the L2 distance
would decrease with the new coefficients.
Within the code, this training process is done using a D2Fourier
object which derives from the abstract base class Function
(same for Fourier
, Gauss
and D2Gauss
). The L2 distance functional is implemented by creating a std::function<double(double)>
object from the function arguments via lambda function syntax. This object then is passed to the function Mathutil::simpson
which implements the composite Simpson`s rule integrator.
Lastly, we display the solving process using gnuplot
, which runs in a second std::thread
using member function syntax. Since the GnuplotFunctionViewer
class also overloads the operator()
, we could have also passed the viewer instance itself to the thread constructor. To avoid data leaks in inter-thread communication, we use modern memory management techniques (in particular, std::shared_ptr<T>
objects).
Due to nested namespaces this project requires C++17 (hence, gcc/g++ >= 6.0).
- cmake >= 3.7
- All OS: click here for installation instructions
- make >= 4.1 (Linux, Mac), 3.81 (Windows)
- gcc/g++ >= 6.0
- gnuplot
- Primary download site on SourceForge
- Debian/Ubuntu:
sudo apt install gnuplot
- Linux, Mac binaries
- Windows binaries
- Clone this repo.
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run:
./StochasticFourierSolver