/* C translation of fdcoeff.m, from R. J. Leveque, downloaded from faculty.washington.edu/rjl/fdmbook/matlab/fdcoeff.m on 2015.08.22 by WWS. to compile main program below to test, define LOCALTEST and something like g++ -I../../base/include -I../../../rvl/rvl/include fdcoeff.cc */ #include "utils.h" #include "std_cpp_includes.hh" //#define LOCALTEST //function c = fdcoeffF(k,xbar,x) //double * fdcoeff(int k, double xbar, std::vector x) { std::vector fdcoeff(int k, double xbar, std::vector x) { /* % Compute coefficients for finite difference approximation for the % derivative of order k at xbar based on grid values at points in x. % % This function returns a row vector c of dimension 1 by n, where n=length(x), % containing coefficients to approximate u^{(k)}(xbar), % the k'th derivative of u evaluated at xbar, based on n values % of u at x(1), x(2), ... x(n). % % If U is a column vector containing u(x) at these n points, then % c*U will give the approximation to u^{(k)}(xbar). % % Note for k=0 this can be used to evaluate the interpolating polynomial % itself. % % Requires length(x) > k. % Usually the elements x(i) are monotonically increasing % and x(1) <= xbar <= x(n), but neither condition is required. % The x values need not be equally spaced but must be distinct. % % This program should give the same results as fdcoeffV.m, but for large % values of n is much more stable numerically. % % Based on the program "weights" in % B. Fornberg, "Calculation of weights in finite difference formulas", % SIAM Review 40 (1998), pp. 685-691. % % Note: Forberg's algorithm can be used to simultaneously compute the % coefficients for derivatives of order 0, 1, ..., m where m <= n-1. % This gives a coefficient matrix C(1:n,1:m) whose k'th column gives % the coefficients for the k'th derivative. % % In this version we set m=k and only compute the coefficients for % derivatives of order up to order k, and then return only the k'th column % of the resulting C matrix (converted to a row vector). % This routine is then compatible with fdcoeffV. % It can be easily modified to return the whole array if desired. % % From http://www.amath.washington.edu/~rjl/fdmbook/ (2007) */ // n = length(x); // assumes that size of x can be represented in 4 bytes int n = x.size(); if (k >= n) { // error('*** length(x) must be larger than k') /* RVLException e; e<<"ERROR: fdcoeff\n"; e<<" order k="< CapC(nn); for (int i=0; i< nn; i++) CapC[i]=0.0; //C = zeros(n-1,m+1); //C(1,1) = 1; CapC[0]=1.0; // for i=1:n-1 for (int i=1; i x(2*ord); for (int i=0;i<2*ord;i++) x[i]=double(i-ord)+0.5f; std::vector c(2*ord); c = fdcoeff(k,0.0f,x); //double * c = fdcoeff(k,0.0f,x); cout<<" "; for (int i=0;i<2*ord;i++) cout< x(2*ord+1); for (int i=0;i<2*ord+1;i++) x[i]=double(i-ord); std::vector c(2*ord+1); c = fdcoeff(k,0.0f,x); // double * c = fdcoeff(k,0.0f,x); cout<<" "; for (int i=0;i<2*ord+1;i++) cout<