Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/2dfunction #73

Merged
merged 13 commits into from
Sep 6, 2024
6 changes: 5 additions & 1 deletion cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ add_subdirectory( src/scion/interpolation/LinearLinear/test )
add_subdirectory( src/scion/interpolation/LinearLogarithmic/test )
add_subdirectory( src/scion/interpolation/LogarithmicLinear/test )
add_subdirectory( src/scion/interpolation/LogarithmicLogarithmic/test )
add_subdirectory( src/scion/interpolation/Table/test )

add_subdirectory( src/scion/linearisation/ToleranceConvergence/test )
add_subdirectory( src/scion/linearisation/MidpointSplit/test )
Expand All @@ -76,10 +75,15 @@ add_subdirectory( src/scion/math/newton/test )
add_subdirectory( src/scion/math/IntervalDomain/test )
add_subdirectory( src/scion/math/OpenDomain/test )
add_subdirectory( src/scion/math/LinearLinearTable/test )
add_subdirectory( src/scion/math/LinearLinearTableFunction/test )
add_subdirectory( src/scion/math/HistogramTable/test )
add_subdirectory( src/scion/math/HistogramTableFunction/test )
add_subdirectory( src/scion/math/LogLinearTable/test )
add_subdirectory( src/scion/math/LogLinearTableFunction/test )
add_subdirectory( src/scion/math/LinearLogTable/test )
add_subdirectory( src/scion/math/LinearLogTableFunction/test )
add_subdirectory( src/scion/math/LogLogTable/test )
add_subdirectory( src/scion/math/LogLogTableFunction/test )
add_subdirectory( src/scion/math/InterpolationTable/test )
add_subdirectory( src/scion/math/ChebyshevSeries/test )
add_subdirectory( src/scion/math/ChebyshevApproximation/test )
Expand Down
3 changes: 1 addition & 2 deletions python/src/definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

// other includes
#include "scion/linearisation/ToleranceConvergence.hpp"
#include "scion/math/FunctionBase.hpp"

// namespace aliases
namespace python = pybind11;
Expand Down Expand Up @@ -73,7 +72,7 @@ template < typename Component, typename X, typename Y, typename PythonClass >
void addStandardFunctionDefinitions( PythonClass& component ) {

using ToleranceConvergence = njoy::scion::linearisation::ToleranceConvergence< X, Y >;
using DomainVariant = typename njoy::scion::math::FunctionBase< X, Y >::DomainVariant;
using DomainVariant = typename Component::DomainVariant;

// note: for is_same_domain to bind properly, all possible variant members
// must have a default constructor
Expand Down
2 changes: 1 addition & 1 deletion src/scion/interpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#include "scion/interpolation/LinearLogarithmic.hpp"
#include "scion/interpolation/LogarithmicLinear.hpp"
#include "scion/interpolation/LogarithmicLogarithmic.hpp"
#include "scion/interpolation/Table.hpp"

46 changes: 45 additions & 1 deletion src/scion/interpolation/Histogram/test/Histogram.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SCENARIO( "Histogram" ) {

GIVEN( "Histogram interpolation object" ) {

WHEN( "interpolating an interval" ) {
WHEN( "interpolating an x,y interval" ) {

interpolation::Histogram interpolator{};

Expand All @@ -31,6 +31,29 @@ SCENARIO( "Histogram" ) {
CHECK_THAT( 1., WithinRel( interpolator( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,f(y) interval" ) {

interpolation::Histogram interpolator{};

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return - y; };

CHECK_THAT( 0.0, WithinRel( interpolator( 1.0, 0.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 0.0, WithinRel( interpolator( 1.5, 0.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 0.0, WithinRel( interpolator( 2.0, 0.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.0, WithinRel( interpolator( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.0, WithinRel( interpolator( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.0, WithinRel( interpolator( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( -1.0, WithinRel( interpolator( 1.0, -1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( -1.0, WithinRel( interpolator( 1.5, -1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( -1.0, WithinRel( interpolator( 2.0, -1.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
joewpeterson marked this conversation as resolved.
Show resolved Hide resolved
} // GIVEN

GIVEN( "histogram interpolation function" ) {
Expand All @@ -49,5 +72,26 @@ SCENARIO( "Histogram" ) {
CHECK_THAT( 1., WithinRel( interpolation::histogram( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,y interval" ) {

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return - y; };

CHECK_THAT( 0.0, WithinRel( interpolation::histogram( 1.0, 0.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 0.0, WithinRel( interpolation::histogram( 1.5, 0.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 0.0, WithinRel( interpolation::histogram( 2.0, 0.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.0, WithinRel( interpolation::histogram( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.0, WithinRel( interpolation::histogram( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.0, WithinRel( interpolation::histogram( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( -1.0, WithinRel( interpolation::histogram( 1.0, -1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( -1.0, WithinRel( interpolation::histogram( 1.5, -1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( -1.0, WithinRel( interpolation::histogram( 2.0, -1.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO
39 changes: 38 additions & 1 deletion src/scion/interpolation/InterpolatorBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace interpolation {
/* methods */

/**
* @brief Perform interpolation
* @brief Perform interpolation on x,y tabulated data
*
* @param[in] x the value of x
* @param[in] xLeft the left value on the x interval
Expand All @@ -38,6 +38,43 @@ namespace interpolation {

return static_cast< const Derived* >( this )->interpolate( x, xLeft, xRight, yLeft, yRight );
}

/**
* @brief Perform interpolation on x,f(y) tabulated data
*
* This interpolation works by first evaluating the functions z = f(y) for the value
* of y, and then performing the normal interpolation on the interval defined by
* xl,zl and xr,zr as illustrated here:
*
* fl(y)
* xl - - - # - - - - > left function of y
* |
* x - - - *
* |
* xr - - - # - - - - > right function of y
* |
* fr(y)
*
* As such, this does not do unit-base interpolation.
*
* Since we can reuse the operator() on this base class so the derived class
joewpeterson marked this conversation as resolved.
Show resolved Hide resolved
* does not need to implement this particular interpolation operation.
*
* @param[in] x the value of x
* @param[in] y the value of y
* @param[in] xLeft the left value on the x interval
* @param[in] xRight the right value on the x interval
* @param[in] fLeft the left function on the y axis
* @param[in] fRight the right function on the y axis
*/
template < typename X, typename Y, typename FLeft, typename FRight = FLeft,
typename Z = decltype( std::declval< FLeft >()( std::declval< Y >() ) ) >
Z operator()( const X& x, const Y& y,
const X& xLeft, const X& xRight,
const FLeft& fLeft, const FRight& fRight ) const noexcept {

return this->operator()( x, xLeft, xRight, fLeft( y ), fRight( y ) );
}
};

} // interpolation namespace
Expand Down
48 changes: 46 additions & 2 deletions src/scion/interpolation/LinearLinear/test/LinearLinear.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SCENARIO( "LinearLinear" ) {

GIVEN( "LinearLinear interpolation object" ) {

WHEN( "interpolating an interval" ) {
WHEN( "interpolating an x,y interval" ) {

interpolation::LinearLinear interpolator{};

Expand All @@ -31,11 +31,34 @@ SCENARIO( "LinearLinear" ) {
CHECK_THAT( 4.0, WithinRel( interpolator( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,f(y) interval" ) {

interpolation::LinearLinear interpolator{};

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return 2 * y; };

CHECK_THAT( 1.0 , WithinRel( interpolator( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.5 , WithinRel( interpolator( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.0 , WithinRel( interpolator( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.5 , WithinRel( interpolator( 1.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 3.75, WithinRel( interpolator( 1.5, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.0 , WithinRel( interpolator( 2.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 4.0 , WithinRel( interpolator( 1.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 6.0 , WithinRel( interpolator( 1.5, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 8.0 , WithinRel( interpolator( 2.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN

GIVEN( "linlin interpolation function" ) {

WHEN( "interpolating an interval" ) {
WHEN( "interpolating an x,y interval" ) {

THEN( "the interpolation is performed correctly" ) {

Expand All @@ -49,5 +72,26 @@ SCENARIO( "LinearLinear" ) {
CHECK_THAT( 4.0, WithinRel( interpolation::linlin( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,y interval" ) {

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return 2 * y; };

CHECK_THAT( 1.0 , WithinRel( interpolation::linlin( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.5 , WithinRel( interpolation::linlin( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.0 , WithinRel( interpolation::linlin( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.5 , WithinRel( interpolation::linlin( 1.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 3.75, WithinRel( interpolation::linlin( 1.5, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.0 , WithinRel( interpolation::linlin( 2.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 4.0 , WithinRel( interpolation::linlin( 1.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 6.0 , WithinRel( interpolation::linlin( 1.5, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 8.0 , WithinRel( interpolation::linlin( 2.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ using Catch::Matchers::WithinRel;
#include "scion/interpolation/LinearLogarithmic.hpp"

// other includes

#include <iostream>
#include <iomanip>
// convenience typedefs
using namespace njoy::scion;

SCENARIO( "LinearLogarithmic" ) {

GIVEN( "LinearLogarithmic interpolation object" ) {

WHEN( "interpolating an interval" ) {
WHEN( "interpolating an x,y interval" ) {

interpolation::LinearLogarithmic interpolator{};

Expand All @@ -31,6 +32,29 @@ SCENARIO( "LinearLogarithmic" ) {
CHECK_THAT( 4.0 , WithinRel( interpolator( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,f(y) interval" ) {

interpolation::LinearLogarithmic interpolator{};

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return 2 * y; };

CHECK_THAT( 1.0 , WithinRel( interpolator( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.58496250072116, WithinRel( interpolator( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.0 , WithinRel( interpolator( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.5 , WithinRel( interpolator( 1.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 3.96240625180289, WithinRel( interpolator( 1.5, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.0 , WithinRel( interpolator( 2.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 4.0 , WithinRel( interpolator( 1.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 6.33985000288462, WithinRel( interpolator( 1.5, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 8.0 , WithinRel( interpolator( 2.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN

GIVEN( "linlog interpolation function" ) {
Expand All @@ -49,5 +73,26 @@ SCENARIO( "LinearLogarithmic" ) {
CHECK_THAT( 4.0 , WithinRel( interpolation::linlog( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,y interval" ) {

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return 2 * y; };

CHECK_THAT( 1.0 , WithinRel( interpolation::linlog( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.58496250072116, WithinRel( interpolation::linlog( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.0 , WithinRel( interpolation::linlog( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.5 , WithinRel( interpolation::linlog( 1.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 3.96240625180289, WithinRel( interpolation::linlog( 1.5, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.0 , WithinRel( interpolation::linlog( 2.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 4.0 , WithinRel( interpolation::linlog( 1.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 6.33985000288462, WithinRel( interpolation::linlog( 1.5, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 8.0 , WithinRel( interpolation::linlog( 2.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SCENARIO( "LogarithmicLinear" ) {

GIVEN( "LogarithmicLinear interpolation object" ) {

WHEN( "interpolating an interval" ) {
WHEN( "interpolating an x,y interval" ) {

interpolation::LogarithmicLinear interpolator{};

Expand All @@ -31,6 +31,29 @@ SCENARIO( "LogarithmicLinear" ) {
CHECK_THAT( 4., WithinRel( interpolator( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,f(y) interval" ) {

interpolation::LogarithmicLinear interpolator{};

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return 2 * y; };

CHECK_THAT( 1.0 , WithinRel( interpolator( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.41421356237309, WithinRel( interpolator( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.0 , WithinRel( interpolator( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.5 , WithinRel( interpolator( 1.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 3.53553390593274, WithinRel( interpolator( 1.5, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.0 , WithinRel( interpolator( 2.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 4.0 , WithinRel( interpolator( 1.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.65685424949238, WithinRel( interpolator( 1.5, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 8.0 , WithinRel( interpolator( 2.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN

GIVEN( "loglin interpolation function" ) {
Expand All @@ -49,5 +72,26 @@ SCENARIO( "LogarithmicLinear" ) {
CHECK_THAT( 4., WithinRel( interpolation::loglin( 2.0, xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN

WHEN( "interpolating an x,y interval" ) {

THEN( "the interpolation is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
auto fLeft = [] ( double y ) { return y; };
auto fRight = [] ( double y ) { return 2 * y; };

CHECK_THAT( 1.0 , WithinRel( interpolation::loglin( 1.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 1.41421356237309, WithinRel( interpolation::loglin( 1.5, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.0 , WithinRel( interpolation::loglin( 2.0, 1.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 2.5 , WithinRel( interpolation::loglin( 1.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 3.53553390593274, WithinRel( interpolation::loglin( 1.5, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.0 , WithinRel( interpolation::loglin( 2.0, 2.5, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 4.0 , WithinRel( interpolation::loglin( 1.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 5.65685424949238, WithinRel( interpolation::loglin( 1.5, 4.0, xLeft, xRight, fLeft, fRight ) ) );
CHECK_THAT( 8.0 , WithinRel( interpolation::loglin( 2.0, 4.0, xLeft, xRight, fLeft, fRight ) ) );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO
Loading