任意间距网格上任意导数阶的有限差分权重。
finitediff的Python项目详细描述
finitediff包含begnt fornberg的三个实现 有限差分权的精确生成公式 间隔一维网格:
有限差分权重可以是 用于优化内部/外推数据系列 任意导数顺序。Python还提供了绑定(到c版本)。
能力
finitediff当前提供用于估计衍生工具的回调 或在单个点或阵列上进行插值(可用 从python绑定)。
用户还可以手动生成相应的权重。(见 calculate_weights)
finitediff可以有条件地编译成finitediff_interpolate_by_finite_diff 多线程。然后通过环境变量设置使用的线程数 FINITEDIFF_NUM_THREADS(或OMP_NUM_THREADS)。
文档
最新稳定版本的自动生成API文档可在以下位置找到: https://bjodah.github.io/finitediff/latest (当前主分支的开发版本如下: http://hera.physchem.kth.se/~finitediff/branches/master/html)。
示例
利用C++ 11:生成有限差分权值#include"finitediff_templated.hpp" #include<vector> #include<string> #include<iostream>intmain(){constunsignedmax_deriv=2;std::vector<std::string>labels{"0th derivative","1st derivative","2nd derivative"};std::vector<double>x{0,1,-1,2,-2};// Fourth order of accuracy autocoeffs=finitediff::generate_weights(x,max_deriv);for(unsignedderiv_i=0;deriv_i<=max_deriv;deriv_i++){std::cout<<labels[deriv_i]<<": ";for(unsignedidx=0;idx<x.size();idx++){std::cout<<coeffs[deriv_i*x.size()+idx]<<" ";}std::cout<<std::endl;}}
$ cd examples/ $ g++ -std=c++11 demo.cpp -I../include $ ./a.out Zeroth derivative (interpolation): 1 -0 0 0 -0 First derivative: -0 0.666667 -0.666667 -0.0833333 0.0833333 Second derivative: -2.5 1.33333 1.33333 -0.0833333 -0.0833333
当然,还可以使用python绑定:
>>>fromfinitediffimportget_weights>>>importnumpyasnp>>>c=get_weights(np.array([0,-1.,1]),0,maxorder=1)>>>np.allclose(c[:,1],[0,-.5,.5])True
在python中,还可以使用有限差分来插值 数值(或其导数):
>>>fromfinitediffimportinterpolate_by_finite_diffasifd>>>x=np.array([0,1,2])>>>y=np.array([[2,3,5],[3,4,7],[7,8,9],[3,4,6]])>>>xout=np.linspace(0.5,1.5,5)>>>r=ifd(x,y,xout,maxorder=2)>>>r.shape(5,4,3)
有关更多示例,请参见examples/目录。
安装
最简单的安装方法是使用conda package manager:
$ conda install -c conda-forge finitediff pytest $ python -m pytest --pyargs finitediff
测试应该通过。
手动安装
您可以使用pip:
安装finitediff。$ python -m pip install --user finitediff
(如果您具有根权限,则可以跳过--user标志), 要运行测试,您还需要pytest:
$ python -m pip install --user --upgrade pytest $ python -m pytest --pyargs finitediff
依赖关系
需要C、C++或FORTRAN 90编译器。在基于debian的linux系统上,您可以通过发出以下命令来安装(全部):
$ sudo apt-get install gfortran g++ gcc
有关可选(python)依赖项,请参见setup.py。
引用
算法是对以下内容的重写:
http://dx.doi.org/10.1137/S0036144596322507
@article{fornberg_classroom_1998, title={Classroom note: Calculation of weights in finite difference formulas}, author={Fornberg, Bengt}, journal={SIAM review}, volume={40}, number={3}, pages={685--691}, year={1998}, publisher={SIAM} doi={10.1137/S0036144596322507} }
引用同一作者的文章:
http://dx.doi.org/10.1090/S0025-5718-1988-0935077-0
@article{fornberg_generation_1988, title={Generation of finite difference formulas on arbitrarily spaced grids}, author={Fornberg, Bengt}, journal={Mathematics of computation}, volume={51}, number={184}, pages={699--706}, year={1988} doi={10.1090/S0025-5718-1988-0935077-0} }
除了论文之外,您还可以引用finitediff(例如,再现性)。 您可以从Zenodo存档中获得每个版本的DOI:
许可
源代码是开放源码的,在 “simplified (2-clause) BSD license”。 有关详细信息,请参见LICENSE。