Skip to content

Commit

Permalink
Merge pull request #11 from bjodah/generate-weights
Browse files Browse the repository at this point in the history
Added new C++11 function: generate_weights
  • Loading branch information
bjodah authored Aug 17, 2016
2 parents 48a2c52 + adef62f commit 6ef0aab
Show file tree
Hide file tree
Showing 8 changed files with 10,649 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ dist/
doc/
finitediff/_finitediff_templated.cpp
*.egg-info
tests/test_finitediff_templated
40 changes: 40 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ Autogenerated API documentation for latest stable release is found here:
(and development docs for the current master branch are found here:
`<http://hera.physchem.kth.se/~finitediff/branches/master/html>`_).

Examples
--------
Generating finite difference weights is simple using C++11:

.. code:: C++

#include "finitediff_templated.hpp"
#include <vector>
#include <iostream>

int main(){
std::vector<double> x {-1, 0, 1};
auto coeffs = finitediff::generate_weights(0.0, x3, 2);
std::cout << "Zeroth order: " << coeffs[0] << " " << coeffs[1] << " " << coeffs[2] << std::endl;
std::cout << "First order: " << coeffs[3] << " " << coeffs[4] << " " << coeffs[5] << std::endl;
std::cout << "Second order: " << coeffs[6] << " " << coeffs[7] << " " << coeffs[8] << std::endl;
}

::

$ cd examples/
$ g++ -std=c++11 demo.cpp -I../include
$ ./a.out
Zeroth order: 0 1 -0
First order: -0.5 0 0.5
Second order: 1 -2 1

and of course using the python bindings:

.. code:: python
>>> from finitediff import get_weights
>>> import numpy as np
>>> c = get_weights(np.array([-1., 0, 1]), 0, maxorder=1)
>>> np.allclose(c[:, 1], [-.5, 0, .5])
True
see the ``examples/`` directory for more examples.

Installation
------------
Simplest way to install finitediff is to use the
Expand Down
11 changes: 11 additions & 0 deletions examples/demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "finitediff_templated.hpp"
#include <vector>
#include <iostream>

int main(){
std::vector<double> x {-1, 0, 1};
auto coeffs = finitediff::generate_weights(0.0, x, 2);
std::cout << "Zeroth order: " << coeffs[0] << " " << coeffs[1] << " " << coeffs[2] << std::endl;
std::cout << "First order: " << coeffs[3] << " " << coeffs[4] << " " << coeffs[5] << std::endl;
std::cout << "Second order: " << coeffs[6] << " " << coeffs[7] << " " << coeffs[8] << std::endl;
}
24 changes: 20 additions & 4 deletions include/finitediff_templated.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef FINITEDIFF_TEMPLATED_HPP_IWIDN4CP6RGFHFVKSWG4HICEZE
#define FINITEDIFF_TEMPLATED_HPP_IWIDN4CP6RGFHFVKSWG4HICEZE

// Pre-processor macro __cplusplus == 201103L in ISO C++11 compliant compilers. (e.g. GCC >= 4.7.0)
#if __cplusplus > 199711L
#include <stdexcept>
#endif

namespace finitediff {

template <typename Real_t>
Expand All @@ -19,10 +24,7 @@ namespace finitediff {
// Generation of Finite Difference Formulas on Arbitrarily
// Spaced Grids, Bengt Fornberg,
// Mathematics of compuation, 51, 184, 1988, 699-706
//
// Notes
// -----
// If c is to be used repeatedly, consider transposing.

Real_t c1, c2, c3, c4, c5;
c1 = 1;
c4 = x[0] - z;
Expand Down Expand Up @@ -71,6 +73,20 @@ namespace finitediff {
}
delete []c;
}


// Pre-processor macro __cplusplus == 201103L in ISO C++11 compliant compilers. (e.g. GCC >= 4.7.0)
#if __cplusplus > 199711L
template<typename Real_t, template<typename, typename...> class Cont, typename... Args>
Cont<Real_t, Args...> generate_weights(const Real_t z, const Cont<Real_t, Args...>& x, const unsigned maxorder){
Cont<Real_t, Args...> coeffs(x.size()*(maxorder+1));
if (x.size() < maxorder + 1){
throw std::logic_error("size of x insufficient");
}
populate_weights<Real_t>(z, &x[0], x.size() - 1, maxorder, &coeffs[0]);
return coeffs;
}
#endif
}


Expand Down
1 change: 1 addition & 0 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash -xeu
(cd tests/; make)
PKG_NAME=${1:-${CI_REPO##*/}}
if [[ "$CI_BRANCH" =~ ^v[0-9]+.[0-9]?* ]]; then
eval export ${PKG_NAME^^}_RELEASE_VERSION=\$CI_BRANCH
Expand Down
11 changes: 11 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CXX ?= g++
CXXFLAGS ?= -std=c++11 -Wall -Wextra -Werror -Wpadded -pedantic -I../include -fno-omit-frame-pointer

.PHONY: test debug clean

test: test_finitediff_templated
./test_finitediff_templated

test_finitediff_templated: test_finitediff_templated.cpp ../include/finitediff_templated.hpp
$(CXX) $(CXXFLAGS) -o $@ $<

Loading

0 comments on commit 6ef0aab

Please sign in to comment.