|
3 | 3 | [](https://github.com/zfergus/finite-diff/actions/workflows/continuous.yml)
|
4 | 4 | [](https://opensource.org/licenses/MIT)
|
5 | 5 |
|
6 |
| -A simple finite difference library. |
| 6 | +**A simple finite difference library.** |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +### Add it to CMake |
| 11 | + |
| 12 | +The easiest way to add the library to an existing CMake project is to download it through CMake. |
| 13 | +CMake provides functionality for doing this called [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) (requires CMake ≥ 3.14). |
| 14 | +We use this same process to download all external dependencies. |
| 15 | +For example, |
| 16 | + |
| 17 | +```CMake |
| 18 | +include(FetchContent) |
| 19 | +FetchContent_Declare( |
| 20 | + finite-diff |
| 21 | + GIT_REPOSITORY https://github.com/zfergus/finite-diff.git |
| 22 | + GIT_TAG ${FINITE_DIFF_GIT_TAG} |
| 23 | + GIT_SHALLOW TRUE |
| 24 | +) |
| 25 | +FetchContent_MakeAvailable(finite-diff) |
| 26 | +``` |
| 27 | + |
| 28 | +where `FINITE_DIFF_GIT_TAG` is set to the version you want to use. This will download and add the library to CMake. The library can then be linked against using |
| 29 | + |
| 30 | +```CMake |
| 31 | +target_link_libraries(${TARGET_NAME} PUBLIC finitediff::finitediff) |
| 32 | +``` |
| 33 | + |
| 34 | +where `TARGET_NAME` is the name of your library/executable. |
| 35 | + |
| 36 | +### API |
| 37 | + |
| 38 | +All functiononality can be included with `#include <finitediff.hpp>`. |
| 39 | + |
| 40 | +The library provides three main function `finite_gradient`, `finite_jacobian`, and `finite_hessian`. |
| 41 | + |
| 42 | +#### `finite_gradient`: |
| 43 | + |
| 44 | +```c++ |
| 45 | +void finite_gradient( |
| 46 | + const Eigen::VectorXd& x, |
| 47 | + const std::function<double(const Eigen::VectorXd&)>& f, |
| 48 | + Eigen::VectorXd& grad, |
| 49 | + const AccuracyOrder accuracy = SECOND, |
| 50 | + const double eps = 1.0e-8); |
| 51 | +``` |
| 52 | +
|
| 53 | +The `finite_gradient` function computes the [gradient](https://en.wikipedia.org/wiki/Gradient) (first derivative) `grad` of a function `f: ℝⁿ ↦ ℝ` at a point `x`. This will result in a vector of size `n`. |
| 54 | +
|
| 55 | +#### `finite_jacobian`: |
| 56 | +
|
| 57 | +```c++ |
| 58 | +void finite_jacobian( |
| 59 | + const Eigen::VectorXd& x, |
| 60 | + const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>& f, |
| 61 | + Eigen::MatrixXd& jac, |
| 62 | + const AccuracyOrder accuracy = SECOND, |
| 63 | + const double eps = 1.0e-8); |
| 64 | +``` |
| 65 | + |
| 66 | +The `finite_jacobian` function computes the [Jacobian](https://en.wikipedia.org/wiki/Jacobian_matrix_and_determinant) (first derivative) `jac` of a function `f: ℝⁿ ↦ ℝᵐ` at a point `x`. This will result in a matrix of size `m × n`. |
| 67 | + |
| 68 | + |
| 69 | +#### `finite_hessian`: |
| 70 | + |
| 71 | +```c++ |
| 72 | +void finite_hessian( |
| 73 | + const Eigen::VectorXd& x, |
| 74 | + const std::function<double(const Eigen::VectorXd&)>& f, |
| 75 | + Eigen::MatrixXd& hess, |
| 76 | + const AccuracyOrder accuracy = SECOND, |
| 77 | + const double eps = 1.0e-5); |
| 78 | +``` |
| 79 | +
|
| 80 | +The `finite_hessian` function computes the [Hessian](https://en.wikipedia.org/wiki/Hessian_matrix) (second derivative) `hess` of a function `f: ℝⁿ ↦ ℝ` at a point `x`. This will result in a matrix of size `n × n`. |
| 81 | +
|
| 82 | +#### `AccuracyOrder`: |
| 83 | +
|
| 84 | +Each finite difference function takes as input the accuracy order for the method. Possible options are: |
| 85 | +```c++ |
| 86 | +enum AccuracyOrder { |
| 87 | + SECOND, // Second order accuracy. |
| 88 | + FOURTH, // Fourth order accuracy. |
| 89 | + SIXTH, // Sixth order accuracy. |
| 90 | + EIGHTH // Eighth order accuracy. |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +#### `eps`: |
| 95 | + |
| 96 | +The parameter `eps` is the finite difference step size. Smaller values result in a more accuracate approximation, but too small of a value can result in large numerical error because the difference will be divided by a small number. |
| 97 | + |
| 98 | +## Dependencies |
| 99 | + |
| 100 | +**All dependancies are downloaded through CMake** depending on the build options. |
| 101 | +The following libraries are used in this project: |
| 102 | + |
| 103 | +* [Eigen](https://eigen.tuxfamily.org/): linear algebra |
| 104 | +* [spdlog](https://github.com/gabime/spdlog): logging information |
| 105 | + |
| 106 | +### Optional |
| 107 | + |
| 108 | +* [Catch2](https://github.com/catchorg/Catch2.git): testing (see [Unit Tests](#unit_tests)) |
| 109 | + |
| 110 | +## <a name="unit_tests"></a>Unit Tests |
| 111 | + |
| 112 | +We provide unit tests for ensuring the correctness of our functions. |
| 113 | +To enable the unit tests use the flag `-DFINITE_DIFF_BUILD_UNIT_TESTS=ON` with CMake. |
| 114 | + |
| 115 | +## Contributing |
| 116 | + |
| 117 | +This project is open for contributors! Contibutions can come in the form of feature requests, bug fixes, documentation, tutorials and the like. We highly recommend to file an Issue first before submitting a Pull Request. |
| 118 | + |
| 119 | +Simply fork this repository and make a Pull Request! We'd definitely appreciate: |
| 120 | + |
| 121 | +* Implementation of new features |
| 122 | +* Bug Reports |
| 123 | +* Documentation |
| 124 | +* Testing |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +MIT License © 2019, Zachary Ferguson (See [`LICENSE.txt`](https://github.com/zfergus/finite-diff/blob/main/LICENSE) for details). |
| 129 | + |
| 130 | +### Acknowledgements |
7 | 131 |
|
8 | 132 | Based on the functions in [CppNumericalSolvers](https://github.com/PatWie/CppNumericalSolvers)
|
9 | 133 | by Patrick Wieschollek and rewritten to use Eigen.
|
0 commit comments