Skip to content
Snippets Groups Projects
Commit 7a5d7f6c authored by Ned Coltman's avatar Ned Coltman Committed by Timo Koch
Browse files

[math][linspace] move linspace to math.hh

parent 2fe262e9
No related branches found
No related tags found
1 merge request!1624Feature/linspace
......@@ -562,6 +562,26 @@ struct LinearTable
} // end namespace InterpolationPolicy
/*!
* \ingroup Common
* \brief Generates linearly spaced vectors
*
* \param begin The first value in the vector
* \param end The last value in the vector
* \param samples The size of the vector
*/
template <class Scalar>
std::vector<Scalar> linspace(const Scalar begin, const Scalar end, std::size_t samples)
{
using std::max;
samples = max(std::size_t{2}, samples); // only makes sense for 2 or more samples
const Scalar delta = (end-begin)/static_cast<Scalar>(samples-1);
std::vector<Scalar> vec(samples);
for (std::size_t i = 0; i < samples; ++i)
vec[i] = begin + i*delta;
return vec;
}
/*!
* \ingroup Common
......
......@@ -29,18 +29,10 @@
#include <dune/common/exceptions.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dumux/common/math.hh>
#include <dumux/common/cubicspline.hh>
#include <dumux/io/gnuplotinterface.hh>
std::vector<double> linspace(const double begin, const double end, const double samples)
{
const double delta = (end-begin)/static_cast<double>(samples-1);
std::vector<double> vec(samples);
for (int i = 0; i < samples; ++i)
vec[i] = begin + i*delta;
return vec;
}
template<class Function>
std::vector<double> eval(const Function& f, const std::vector<double>& x)
{
......@@ -58,12 +50,12 @@ int main(int argc, char** argv)
const auto df = [](double x){ return -2.0*x/(( 1.0 + x*x )*( 1.0 + x*x )); };
// create some test samples
const auto testPoints = linspace(-4.0, 4.0, 1000);
const auto testPoints = Dumux::linspace(-4.0, 4.0, 1000);
const auto ref = eval(f, testPoints);
const auto refDeriv = eval(df, testPoints);
// create the spline sample points
const auto samplePoints = linspace(-4.0, 4.0, 15);
const auto samplePoints = Dumux::linspace(-4.0, 4.0, 15);
const auto y = eval(f, samplePoints);
// create the spline
......@@ -86,7 +78,7 @@ int main(int argc, char** argv)
DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!");
// plot with Gnuplot (plot a bit more so we can see the linear extension)
const auto plotPoints = linspace(-8.0, 8.0, 1000);
const auto plotPoints = Dumux::linspace(-8.0, 8.0, 1000);
const auto refPlot = eval(f, plotPoints);
const auto refDerivPlot = eval(df, plotPoints);
const auto resultPlot = eval([&](const double x) { return spline.eval(x); }, plotPoints);
......
......@@ -29,18 +29,10 @@
#include <dune/common/exceptions.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dumux/common/math.hh>
#include <dumux/common/monotonecubicspline.hh>
#include <dumux/io/gnuplotinterface.hh>
std::vector<double> linspace(const double begin, const double end, const double samples)
{
const double delta = (end-begin)/static_cast<double>(samples-1);
std::vector<double> vec(samples);
for (int i = 0; i < samples; ++i)
vec[i] = begin + i*delta;
return vec;
}
template<class Function>
std::vector<double> eval(const Function& f, const std::vector<double>& x)
{
......@@ -58,12 +50,12 @@ int main(int argc, char** argv)
const auto df = [](double x){ return 3*x*x; };
// create some test samples
const auto testPoints = linspace(0.0, 4.0, 1000);
const auto testPoints = Dumux::linspace(0.0, 4.0, 1000);
const auto ref = eval(f, testPoints);
const auto refDeriv = eval(df, testPoints);
// create the spline sample points
const auto samplePoints = linspace(0.0, 5.0, 10);
const auto samplePoints = Dumux::linspace(0.0, 5.0, 10);
const auto y = eval(f, samplePoints);
// create the spline
......@@ -88,7 +80,7 @@ int main(int argc, char** argv)
DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!");
// plot with Gnuplot (plot a bit more so we can see the linear extension)
const auto plotPoints = linspace(-1.0, 5.0, 1000);
const auto plotPoints = Dumux::linspace(-1.0, 5.0, 1000);
const auto refPlot = eval(f, plotPoints);
const auto refDerivPlot = eval(df, plotPoints);
const auto resultPlot = eval([&](const double x) { return spline.eval(x); }, plotPoints);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment