Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
cmake_minimum_required(VERSION 3.31)

project(
"Polyfit"
LANGUAGES CXX
VERSION 7.1.0
DESCRIPTION "C++ implementation of polyfit, with optional weighting like in numpy. Two implementations, one with boost/ublas lib, and one with the Eigen lib."
HOMEPAGE_URL "https://github.com/dand-oss/Polyfit"
)

set(libname "Polyfit")

# Option to build demos
option(POLYFIT_BUILD_DEMOS "Build demonstration programs" OFF)

include(GNUInstallDirs)

# -----------------------
# Public header list
# -----------------------
set(public_headers
PolyfitBoost.hpp
PolyfitEigen.hpp
)

# -----------------------
# Main interface library
# -----------------------
add_library(${libname} INTERFACE)
# add alias so the project can be used with add_subdirectory
add_library(${libname}::${libname} ALIAS ${libname})

# Use file sets for public headers
target_sources(${libname} INTERFACE
FILE_SET HEADERS
BASE_DIRS .
FILES ${public_headers}
)

target_include_directories(
${libname}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_compile_features(${libname} INTERFACE cxx_std_11)

# -----------------------
# Demo executables (optional)
# -----------------------
if(POLYFIT_BUILD_DEMOS)
# Find dependencies for demos
find_package(Eigen3 CONFIG REQUIRED)

# Find Boost - use CONFIG mode if available, fallback to MODULE mode
find_package(Boost CONFIG QUIET COMPONENTS headers)
if(NOT Boost_FOUND)
find_package(Boost REQUIRED COMPONENTS headers)
endif()

add_subdirectory(demo-boost)
add_subdirectory(demo-eigen)
endif()

# -----------------------
# Install library and headers
# -----------------------
install(TARGETS ${libname}
EXPORT ${libname}_Targets
FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(EXPORT ${libname}_Targets
FILE ${libname}Targets.cmake
NAMESPACE ${libname}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${libname}
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${libname}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${libname}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${libname}Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${libname}
)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${libname}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${libname}ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${libname}
)

# -----------------------
# CPack configuration
# -----------------------
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_DESCRIPTION}")
set(CPACK_PACKAGE_VENDOR "dand-oss")
set(CPACK_PACKAGE_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
set(CPACK_GENERATOR "ZIP;TGZ")
include(CPack)
2 changes: 1 addition & 1 deletion PolyfitEigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Use the Eigen library for fitting: http://eigen.tuxfamily.org
// See https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html for different methods

#include "Eigen/Dense"
#include <Eigen/Dense>

template<typename T>
std::vector<T> polyfit_Eigen(const std::vector<T> &xValues, const std::vector<T> &yValues, const int degree, const std::vector<T>& weights = std::vector<T>(), bool useJacobi = true)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ https://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html
Credits:

http://www.vilipetek.com/2013/10/07/polynomial-fitting-in-c-using-boost/

TODO
- option for building demos as subdirectory
5 changes: 5 additions & 0 deletions cmake/PolyfitConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")
21 changes: 21 additions & 0 deletions demo-boost/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

set(exename "Polyfit-Boost-demo")

if(NOT TARGET Boost::headers)
message(FATAL_ERROR "Boost not found - should be found by parent project")
endif()

add_executable(${exename})

target_link_libraries(${exename} PUBLIC
Polyfit::Polyfit
Boost::headers
)

target_sources(${exename} PRIVATE
main.cpp
)

install(TARGETS ${exename}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
51 changes: 51 additions & 0 deletions demo-boost/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// main.cpp
// Polyfit
//
// Created by Patrick Löber on 23.11.18.
// Copyright © 2018 Patrick Loeber. All rights reserved.
//

#include <iostream>
#include "PolyfitBoost.hpp"

int main(int argc, const char * argv[]) {

std::vector<double> x;
std::vector<double> y;
std::vector<double> weights;

for (int i = 0; i <= 5; ++i)
{
x.push_back(double(i));
weights.push_back(i*i);
}

y.push_back(0.0);
y.push_back(0.8);
y.push_back(0.9);
y.push_back(0.1);
y.push_back(-0.8);
y.push_back(-0.1);

std::cout << "Testing Polyfit With Boost...\n";
std::vector<double> coeff = polyfit_boost(x, y, 3);
std::vector<double> coeffWeighted = polyfit_boost(x, y, 3, weights);

std::cout << "Coefficients:\n";
for (auto it = coeff.begin(); it != coeff.end(); ++it)
std::cout << *it << " ";
std::cout << "\nCoefficients weighted:\n";
for (auto it = coeffWeighted.begin(); it != coeffWeighted.end(); ++it)
std::cout << *it << " ";

std::vector<double> testValuesX(x);

std::cout << "\nFitted y Values:\n";
std::vector<double> testValuesY = polyval(coeff, testValuesX);
for (auto it = testValuesY.begin(); it != testValuesY.end(); ++it)
std::cout << *it << " ";
std::cout << "\n\n";

return 0;
}
24 changes: 24 additions & 0 deletions demo-eigen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(exename "Polyfit-Eigen-demo")

if(NOT TARGET Eigen3::Eigen)
message(FATAL_ERROR "Eigen3 not found - should be found by parent project")
endif()
if(NOT TARGET Boost::headers)
message(FATAL_ERROR "Boost not found - should be found by parent project")
endif()

add_executable(${exename})

target_link_libraries(${exename} PUBLIC
Polyfit::Polyfit
Eigen3::Eigen
Boost::headers
)

target_sources(${exename} PRIVATE
main.cpp
)

install(TARGETS ${exename}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
File renamed without changes.