(C++) AlglibExample2

August 30, 2019 · View on GitHub

STLQt Creator

ALGLIB example 2: linear fit is an ALGLIB example.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppAlglibExample2/CppAlglibExample2.pro

 


include(../../ConsoleApplication.pri) #Or use the code below # QT += core # QT += gui # greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # CONFIG   += console # CONFIG   -= app_bundle # TEMPLATE = app # CONFIG(release, debug|release) { #   DEFINES += NDEBUG NTRACE_BILDERBIKKEL # } # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ # unix { #   QMAKE_CXXFLAGS += -Werror # } include(../../Libraries/Boost.pri) #Or use the code below # win32 { #   INCLUDEPATH += \ #     ../../Libraries/boost_1_54_0 # } SOURCES += main.cpp # # # alglib # # unix {   message(Lubuntu: alglib not tested) } win32 {   message(Windows: alglib: include path)   INCLUDEPATH += ../../Libraries/alglib-3.8.0/src SOURCES += \     ../../Libraries/alglib-3.8.0/src/statistics.cpp \     ../../Libraries/alglib-3.8.0/src/specialfunctions.cpp \     ../../Libraries/alglib-3.8.0/src/solvers.cpp \     ../../Libraries/alglib-3.8.0/src/optimization.cpp \     ../../Libraries/alglib-3.8.0/src/linalg.cpp \     ../../Libraries/alglib-3.8.0/src/interpolation.cpp \     ../../Libraries/alglib-3.8.0/src/integration.cpp \     ../../Libraries/alglib-3.8.0/src/fasttransforms.cpp \     ../../Libraries/alglib-3.8.0/src/diffequations.cpp \     ../../Libraries/alglib-3.8.0/src/dataanalysis.cpp \     ../../Libraries/alglib-3.8.0/src/ap.cpp \     ../../Libraries/alglib-3.8.0/src/alglibmisc.cpp \     ../../Libraries/alglib-3.8.0/src/alglibinternal.cpp HEADERS += \     ../../Libraries/alglib-3.8.0/src/statistics.h \     ../../Libraries/alglib-3.8.0/src/specialfunctions.h \     ../../Libraries/alglib-3.8.0/src/solvers.h \     ../../Libraries/alglib-3.8.0/src/optimization.h \     ../../Libraries/alglib-3.8.0/src/linalg.h \     ../../Libraries/alglib-3.8.0/src/interpolation.h \     ../../Libraries/alglib-3.8.0/src/integration.h \     ../../Libraries/alglib-3.8.0/src/fasttransforms.h \     ../../Libraries/alglib-3.8.0/src/diffequations.h \     ../../Libraries/alglib-3.8.0/src/dataanalysis.h \     ../../Libraries/alglib-3.8.0/src/ap.h \     ../../Libraries/alglib-3.8.0/src/alglibmisc.h \     ../../Libraries/alglib-3.8.0/src/alglibinternal.h }

 

 

 

 

 

./CppAlglibExample2/main.cpp

 


///Adapted from http://www.alglib.net/translator/man/manual.cpp.html#lsfit_d_lin%20example #include <cassert> #include <iostream> #include "interpolation.h" ///Result should be {a,b}, as a linear trendline has the equation ///y = a.x + b, but I could not find out how to obtain b std::pair<double,double> FitLinear(   const std::vector<double>& xs,   const std::vector<double>& ys) {   alglib::real_1d_array y;   y.setcontent(ys.size(), &ys[0]);   alglib::real_2d_array fmatrix;   fmatrix.setcontent(xs.size(),1,&xs[0]);   alglib::ae_int_t info;   alglib::real_1d_array c;   alglib::lsfitreport rep;   alglib::lsfitlinear(y, fmatrix, info, c, rep);   assert(info == 1 && "task is solved successfully");   ///Result should be {a,b}, as a linear trendline has the equation   ///y = a.x + b, but I could not find out how to obtain b   return std::make_pair(c[0],c[0]); } int main() {   const std::vector<double> x { 0.606531,0.670320,0.740818,0.818731,0.904837,1.000000,1.105171,1.221403,1.349859,1.491825,1.648721 };   const std::vector<double> y { 1.133719, 1.306522, 1.504604, 1.554663, 1.884638, 2.072436, 2.257285, 2.534068, 2.622017, 2.897713, 3.219371 };   const std::pair<double,double> p = FitLinear(x,y);   assert(p.first > 1.98650 && p.first < 1.98651);   assert(p.second > 1.98650 && p.second < 1.98651); }